Skip to content

Commit

Permalink
Use toHaveBeenCalled() Jest API 🃏👏🏼
Browse files Browse the repository at this point in the history
  • Loading branch information
kutyel committed Aug 4, 2017
1 parent 2c9c109 commit 8e26ac9
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 23 deletions.
42 changes: 21 additions & 21 deletions index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,43 +4,43 @@ const Emitter = require('.')

describe('Emitter', () => {
let sub
const callback = jest.fn()
const callback1 = jest.fn()
const callback2 = jest.fn()
const emitter = new Emitter()

test('should trigger nothing', () => {
emitter.emit('sum', 1)
expect(callback.mock.calls.length).toBe(0)
emitter.emit('event', 'foo')
expect(callback1).not.toHaveBeenCalled()
})

test('should trigger 1 callback', () => {
sub = emitter.subscribe('sum', callback)
emitter.emit('sum', 1)
expect(callback.mock.calls.length).toBe(1)
expect(callback.mock.calls[0][0]).toBe(1)
sub = emitter.subscribe('event', callback1)
emitter.emit('event', 'foo')
expect(callback1).toHaveBeenCalledTimes(1)
expect(callback1).toHaveBeenCalledWith('foo')
})

test('should trigger 2 callbacks', () => {
emitter.subscribe('sum', callback)
emitter.emit('sum', 2)
expect(callback.mock.calls.length).toBe(3)
expect(callback.mock.calls[0][0]).toBe(1)
expect(callback.mock.calls[1][0]).toBe(2)
expect(callback.mock.calls[2][0]).toBe(2)
emitter.subscribe('event', callback2)
emitter.emit('event', 'bar', 'baz')
expect(callback1).toHaveBeenCalledTimes(2)
expect(callback1).toHaveBeenCalledWith('bar', 'baz')
expect(callback2).toHaveBeenCalledTimes(1)
expect(callback2).toHaveBeenCalledWith('bar', 'baz')
})

test('should release first callback, and call the second', () => {
sub()
emitter.emit('sum', 3)
expect(callback.mock.calls.length).toBe(3)
expect(callback.mock.calls[0][0]).toBe(1)
expect(callback.mock.calls[1][0]).toBe(2)
expect(callback.mock.calls[2][0]).toBe(2)
emitter.emit('event', 'meow')
expect(callback1).toHaveBeenCalledTimes(2) // same number as before
expect(callback2).toHaveBeenCalledTimes(2)
expect(callback2).toHaveBeenCalledWith('meow')
})

test('should return the return values of all the events in the callstack', () => {
emitter.subscribe('myEvent', jest.fn().mockReturnValue(1))
emitter.subscribe('myEvent', jest.fn().mockReturnValue(2))
emitter.subscribe('myEvent', jest.fn().mockReturnValue(true))
emitter.subscribe('myEvent', () => 1)
emitter.subscribe('myEvent', () => 2)
emitter.subscribe('myEvent', () => true)
const result = emitter.emit('myEvent')
expect(result).toEqual([1, 2, true])
})
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@
"prelint": "npm run pretty",
"pretest": "npm run lint",
"pretty": "prettier-standard index.js src/**/*.js test/**/*.js bin/**/*.js",
"test": "jest --coverage",
"test:w": "jest --watch"
"test": "jest --coverage"
},
"license": "MIT",
"jest": {
Expand Down

0 comments on commit 8e26ac9

Please sign in to comment.