|
| 1 | +// tslint:disable:no-any |
| 2 | +import { ConflictingItemError } from '@js-items/foundation'; |
| 3 | +import testItem from '@js-items/foundation/dist/functions/utils/testItem'; |
| 4 | +import { config } from '../../utils/testConfig'; |
| 5 | +import createItem from './index'; |
| 6 | + |
| 7 | +beforeEach(() => jest.clearAllMocks()); |
| 8 | + |
1 | 9 | describe('@createItem', () => {
|
2 |
| - it('tests', () => { |
3 |
| - // TODO: implements tests |
4 |
| - expect(true).toBe(true); |
| 10 | + it('creates item', async () => { |
| 11 | + const kyMock = jest.fn(() => ({ |
| 12 | + json: () => Promise.resolve({ item: testItem }), |
| 13 | + })); |
| 14 | + |
| 15 | + const createItemOptionsMock = jest.fn(() => ({})); |
| 16 | + |
| 17 | + const { item } = await createItem({ |
| 18 | + ...config, |
| 19 | + createItemOptions: createItemOptionsMock, |
| 20 | + ky: () => Promise.resolve(kyMock) as any, |
| 21 | + })({ |
| 22 | + id: testItem.id, |
| 23 | + item: testItem, |
| 24 | + }); |
| 25 | + |
| 26 | + expect(createItemOptionsMock).toBeCalledWith(testItem); |
| 27 | + |
| 28 | + expect(config.convertDocumentIntoItem).toBeCalledWith(testItem); |
| 29 | + |
| 30 | + expect(item).toEqual(testItem); |
| 31 | + |
| 32 | + expect(kyMock).toBeCalledWith('', { |
| 33 | + json: { item: testItem }, |
| 34 | + method: 'post', |
| 35 | + }); |
| 36 | + }); |
| 37 | + |
| 38 | + it('creates item with custom json object', async () => { |
| 39 | + const kyMock = jest.fn(() => ({ |
| 40 | + json: () => Promise.resolve({ item: testItem, otherProp: 'test' }), |
| 41 | + })); |
| 42 | + |
| 43 | + const createItemOptionsMock = jest.fn(() => ({ |
| 44 | + json: { otherProp: 'test' }, |
| 45 | + })); |
| 46 | + |
| 47 | + await createItem({ |
| 48 | + ...config, |
| 49 | + createItemOptions: createItemOptionsMock, |
| 50 | + ky: () => Promise.resolve(kyMock) as any, |
| 51 | + })({ |
| 52 | + item: testItem, |
| 53 | + }); |
| 54 | + |
| 55 | + expect(kyMock).toBeCalledWith('', { |
| 56 | + json: { item: testItem, otherProp: 'test' }, |
| 57 | + method: 'post', |
| 58 | + }); |
| 59 | + }); |
| 60 | + |
| 61 | + it('does not create item', async () => { |
| 62 | + const error = new ConflictingItemError('TestItem'); |
| 63 | + |
| 64 | + const facadeConfig = { |
| 65 | + ...config, |
| 66 | + createItemOptions: jest.fn(() => ({ json: { item: testItem } })), |
| 67 | + ky: jest.fn(() => Promise.reject(error)), |
| 68 | + }; |
| 69 | + |
| 70 | + try { |
| 71 | + await createItem(facadeConfig)({ |
| 72 | + id: testItem.id, |
| 73 | + item: testItem, |
| 74 | + }); |
| 75 | + } catch (e) { |
| 76 | + expect(e).toEqual(error); |
| 77 | + } |
5 | 78 | });
|
6 | 79 | });
|
0 commit comments