getting-started-app/spec/routes/addItem.spec.js
Craig Osterhout 9bbd95c7dd first commit
2023-07-17 10:17:25 -07:00

31 lines
884 B
JavaScript

const db = require('../../src/persistence');
const addItem = require('../../src/routes/addItem');
const ITEM = { id: 12345 };
const {v4 : uuid} = require('uuid');
jest.mock('uuid', () => ({ v4: jest.fn() }));
jest.mock('../../src/persistence', () => ({
removeItem: jest.fn(),
storeItem: jest.fn(),
getItem: jest.fn(),
}));
test('it stores item correctly', async () => {
const id = 'something-not-a-uuid';
const name = 'A sample item';
const req = { body: { name } };
const res = { send: jest.fn() };
uuid.mockReturnValue(id);
await addItem(req, res);
const expectedItem = { id, name, completed: false };
expect(db.storeItem.mock.calls.length).toBe(1);
expect(db.storeItem.mock.calls[0][0]).toEqual(expectedItem);
expect(res.send.mock.calls[0].length).toBe(1);
expect(res.send.mock.calls[0][0]).toEqual(expectedItem);
});