Skip to content

Instantly share code, notes, and snippets.

@Pyrolistical
Created October 9, 2021 21:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Pyrolistical/78b5355eb5eb5bdb50343f21d4f06430 to your computer and use it in GitHub Desktop.
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
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