Skip to content

Instantly share code, notes, and snippets.

@loujaybee
Last active December 11, 2019 07:36
Show Gist options
  • Save loujaybee/de2b6e766648cc6dd9eb2a5f02675e4e to your computer and use it in GitHub Desktop.
Save loujaybee/de2b6e766648cc6dd9eb2a5f02675e4e to your computer and use it in GitHub Desktop.
An example set of component tests
/* ----- Your Microservice ----- */
// An imaginary database
const DATABASE = [{ id: 2, name: "Lou" }]
// library.js
function databaseLibrary() {
return {
findByID: (searched_id) => DATABASE.find(id => searched_id)
}
}
// models/user.js
function getUser(searched_id) {
return databaseLibrary().findByID(searched_id)
}
// index.js
function microservice({ searched_id: id }) {
if (!id) throw new Error("User ID must be passed");
return getUser(id);
}
/* ----- Your Component Tests ----- */
let mockDatabase = {
findByID: jest.fn()
}
beforeEach(() => {
databaseLibrary = jest.fn().mockReturnValue(mockDatabase);
})
test('Throws error user ID is not set', () => {
expect(() => microservice({ searched_id: null })).toThrow("User ID must be passed");
});
test('Calls the database with passed user ID', () => {
microservice({ searched_id: 1 })
expect(mockDatabase.findByID).toHaveBeenCalledWith(1);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment