Created
October 9, 2021 21:36
-
-
Save Pyrolistical/78b5355eb5eb5bdb50343f21d4f06430 to your computer and use it in GitHub Desktop.
separate business layer from infrastructure layer before service.spec.js with jest mocks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {jest} from '@jest/globals' | |
import Service from './service'; | |
test('teams with disqualified players cannot check-in', async () => { | |
const playerDisqualificationsFindToArray = jest.fn() | |
.mockResolvedValueOnce([ | |
{ | |
teamID: 'some team id', | |
reason: 'was a big meanie' | |
} | |
]); | |
const playerDisqualificationsFind = jest.fn() | |
.mockReturnValueOnce({ | |
toArray: playerDisqualificationsFindToArray | |
}); | |
const checkInsUpdateOne = jest.fn(); | |
const db = { | |
collection(collection) { | |
switch (collection) { | |
case 'playerDisqualifications': { | |
return { | |
find: playerDisqualificationsFind | |
}; | |
} | |
case 'checkIns': { | |
return { | |
updateOne: checkInsUpdateOne | |
}; | |
} | |
} | |
} | |
}; | |
const service = Service(db); | |
await expect(service.checkIn('some team id')).rejects.toThrow(); | |
expect(playerDisqualificationsFind).toHaveBeenNthCalledWith(1, {teamID: 'some team id'}); | |
expect(playerDisqualificationsFindToArray).toHaveBeenCalled(); | |
expect(checkInsUpdateOne).not.toHaveBeenCalled(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment