getting-started/app/spec/routes/addItem.spec.js
Michael Irwin 5bb26cc53a
Initial commit
Signed-off-by: mikesir87 <mikesir87@gmail.com>

Imported from dockersamples/101-tutorial, removed other languages
for now, and replaced PWD references with Docker Desktop.
2020-02-05 22:04:43 -05:00

31 lines
873 B
JavaScript

const db = require('../../src/persistence');
const addItem = require('../../src/routes/addItem');
const ITEM = { id: 12345 };
const uuid = require('uuid/v4');
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);
});