|
| 1 | +// tslint:disable:no-any |
| 2 | +import * as sourceMapSupport from 'source-map-support'; |
| 3 | +import assertOnDeleteItem from '../utils/assertOnDeleteItem'; |
| 4 | +import initTests from '../utils/initTests'; |
| 5 | +sourceMapSupport.install(); |
| 6 | +import dotenv from 'dotenv'; |
| 7 | +dotenv.config(); |
| 8 | +import { ItemNotFoundError } from '@js-items/foundation'; |
| 9 | +import { Options as DeleteItemOptions } from '@js-items/foundation/dist/functions/DeleteItem'; |
| 10 | +import testItem, { |
| 11 | + TestItem, |
| 12 | +} from '@js-items/foundation/dist/functions/utils/testItem'; |
| 13 | +import { NOT_FOUND, OK } from 'http-status-codes'; |
| 14 | +import { TEST_URL } from '../../constants'; |
| 15 | +import createInMemoryService from '../utils/createInMemoryService'; |
| 16 | + |
| 17 | +jest.mock('uuid', () => ({ |
| 18 | + v4: jest.fn(() => '1'), |
| 19 | +})); |
| 20 | + |
| 21 | +const notExistingId = 'not-existing-id'; |
| 22 | + |
| 23 | +const deleteItem = jest.fn(({ filter, id }: DeleteItemOptions<TestItem>) => { |
| 24 | + if ( |
| 25 | + id !== testItem.id || |
| 26 | + (filter !== undefined && |
| 27 | + (filter as any).booleanProperty !== testItem.booleanProperty) |
| 28 | + ) { |
| 29 | + return Promise.reject(new ItemNotFoundError('item', testItem.id)); |
| 30 | + } |
| 31 | + |
| 32 | + return Promise.resolve(); |
| 33 | +}); |
| 34 | + |
| 35 | +describe('@deleteItem', () => { |
| 36 | + beforeEach(async () => { |
| 37 | + jest.clearAllMocks(); |
| 38 | + }); |
| 39 | + |
| 40 | + const service = createInMemoryService({ |
| 41 | + deleteItem, |
| 42 | + }); |
| 43 | + |
| 44 | + const { request } = initTests<TestItem>({ service }); |
| 45 | + |
| 46 | + const defaultParams = { |
| 47 | + filter: JSON.stringify({ booleanProperty: testItem.booleanProperty }), |
| 48 | + }; |
| 49 | + |
| 50 | + const defaultOptions = { |
| 51 | + client: request, |
| 52 | + deleteItem, |
| 53 | + params: defaultParams, |
| 54 | + url: `${TEST_URL}/${testItem.id}`, |
| 55 | + }; |
| 56 | + |
| 57 | + it('delete item', async () => { |
| 58 | + await assertOnDeleteItem(defaultOptions); |
| 59 | + }); |
| 60 | + |
| 61 | + it('throws not found error when item does not exist', async () => { |
| 62 | + await assertOnDeleteItem({ |
| 63 | + ...defaultOptions, |
| 64 | + status: NOT_FOUND, |
| 65 | + url: `${TEST_URL}/${notExistingId}`, |
| 66 | + }); |
| 67 | + }); |
| 68 | + |
| 69 | + it('throws not found error when item does not exist because filter does not match', async () => { |
| 70 | + await assertOnDeleteItem({ |
| 71 | + ...defaultOptions, |
| 72 | + params: { |
| 73 | + ...defaultOptions.params, |
| 74 | + filter: JSON.stringify({ |
| 75 | + booleanProperty: !testItem.booleanProperty, |
| 76 | + }), |
| 77 | + }, |
| 78 | + status: NOT_FOUND, |
| 79 | + url: `${TEST_URL}/${testItem.id}`, |
| 80 | + }); |
| 81 | + }); |
| 82 | + |
| 83 | + it('throws not found error when item does not exist and envelope is enabled and pretty is disabled', async () => { |
| 84 | + await assertOnDeleteItem({ |
| 85 | + ...defaultOptions, |
| 86 | + params: { |
| 87 | + ...defaultOptions.params, |
| 88 | + envelope: true, |
| 89 | + pretty: false, |
| 90 | + }, |
| 91 | + status: OK, |
| 92 | + url: `${TEST_URL}/${notExistingId}`, |
| 93 | + }); |
| 94 | + }); |
| 95 | + |
| 96 | + it('delete item when envelope is enabled and pretty response is enabled', async () => { |
| 97 | + await assertOnDeleteItem({ |
| 98 | + ...defaultOptions, |
| 99 | + params: { |
| 100 | + ...defaultParams, |
| 101 | + envelope: true, |
| 102 | + }, |
| 103 | + status: OK, |
| 104 | + }); |
| 105 | + }); |
| 106 | + |
| 107 | + it('delete item when envelope is enabled and pretty response is disabled', async () => { |
| 108 | + await assertOnDeleteItem({ |
| 109 | + ...defaultOptions, |
| 110 | + params: { |
| 111 | + ...defaultParams, |
| 112 | + envelope: true, |
| 113 | + pretty: false, |
| 114 | + }, |
| 115 | + status: OK, |
| 116 | + }); |
| 117 | + }); |
| 118 | +// tslint:disable-next-line:max-file-line-count |
| 119 | +}); |
0 commit comments