Skip to content

Commit

Permalink
improve the whats-a- stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
Kent C. Dodds committed Apr 16, 2018
1 parent 5d7050b commit b46117c
Show file tree
Hide file tree
Showing 29 changed files with 581 additions and 714 deletions.
5 changes: 5 additions & 0 deletions other/whats-a-mock/__tests__/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"rules": {
"import/namespace": "off"
}
}
14 changes: 0 additions & 14 deletions other/whats-a-mock/__tests__/thumb-war.0.solution.js

This file was deleted.

134 changes: 0 additions & 134 deletions other/whats-a-mock/__tests__/thumb-war.1.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,144 +3,10 @@ import * as utils from '../utils'

test('returns winner', () => {
const originalGetWinner = utils.getWinner
// change the getWinner implementation to a function
// that keeps track of how often it's called and
// the arguments it's called with (Hint #1)
// eslint-disable-next-line import/namespace
utils.getWinner = (p1, p2) => p2

const winner = thumbWar('Ken Wheeler', 'Kent C. Dodds')
expect(winner).toBe('Kent C. Dodds')
// add an assertion for how many times the getWinner function
// was supposed to be called (2 times) (Hint #2)
//
// add another assertion that every time it was called
// it was called with the right arguments: 'Ken Wheeler', 'Kent C. Dodds'
// (Hint #3)

// eslint-disable-next-line import/namespace
utils.getWinner = originalGetWinner
})

/*
Hints below:
Hint #1:
There's nothing too magical going on here, you just need some place to store the state for every time the function is
called. Something like this should do just fine (Sorry, this is the solution I have. I couldn't think of a way to hint
at it without totally giving it away or leading you astray):
utils.getWinner = (...args) => {
utils.getWinner.mock.calls.push(args)
return args[1]
}
utils.getWinner.mock = {calls: []}
Hint #2:
Depending on how you store the state of how many times it's been called, you might either do this:
expect(timesCalled).toBe(2)
Or you might do this:
expect(utils.getWinner.mock.calls).toHaveLength(2)
Either way is fine.
Hint #3:
You can have assertions within a forEach and that's not entirely uncommon. Depending on how you're storing the state
of the arguments its called with you might do this (#spoileralert this is the solution... sorry):
utils.getWinner.mock.calls.forEach(args => {
expect(args).toEqual(['Ken Wheeler', 'Kent C. Dodds'])
})
*/
22 changes: 0 additions & 22 deletions other/whats-a-mock/__tests__/thumb-war.1.solution.js

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// monkey-patching
import thumbWar from '../thumb-war'
// import the utils module (see hint #1 at the bottom of the file)

Expand Down
80 changes: 0 additions & 80 deletions other/whats-a-mock/__tests__/thumb-war.2.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import * as utils from '../utils'

test('returns winner', () => {
const originalGetWinner = utils.getWinner
// change this to a jest mock function (Hint #1)
// eslint-disable-next-line import/namespace
utils.getWinner = (...args) => {
utils.getWinner.mock.calls.push(args)
return args[1]
Expand All @@ -13,88 +11,10 @@ test('returns winner', () => {

const winner = thumbWar('Ken Wheeler', 'Kent C. Dodds')
expect(winner).toBe('Kent C. Dodds')
// change this to the built-in jest assertion for how many times a mock
// function was called (use toHaveBeenCalledTimes) (Hint #2)
expect(utils.getWinner.mock.calls).toHaveLength(2)
utils.getWinner.mock.calls.forEach(args => {
expect(args).toEqual(['Ken Wheeler', 'Kent C. Dodds'])
})

// eslint-disable-next-line import/namespace
utils.getWinner = originalGetWinner
})

/*
Hints below
Hint #1:
You can create a jest mock function with `jest.fn()`:
const myMockFunction = jest.fn((greeting, name) => `${greeting} ${name}`)
myMockFunction('Hey', 'Joe') // returns 'Hey Joe'
Jest keeps track of mock function metadata the same way we did (via myMockFunction.mock.calls)
Hint #2:
You can assert the number of times called with:
expect(myMockFunction).toHaveBeenCalledTimes(1)
*/
18 changes: 0 additions & 18 deletions other/whats-a-mock/__tests__/thumb-war.2.solution.js

This file was deleted.

Loading

0 comments on commit b46117c

Please sign in to comment.