|
| 1 | +import { BadRequest } from '@feathersjs/errors' |
| 2 | +import { throwIfIsMulti } from './throw-if-is-multi.js' |
| 3 | +import type { HookContext } from '@feathersjs/feathers' |
| 4 | + |
| 5 | +describe('throwIfIsMulti', () => { |
| 6 | + describe('general', () => { |
| 7 | + it('does not throw on find', async () => { |
| 8 | + const context = { |
| 9 | + method: 'find', |
| 10 | + params: { query: { name: 'item1' } }, |
| 11 | + } as HookContext |
| 12 | + |
| 13 | + await throwIfIsMulti()(context) |
| 14 | + }) |
| 15 | + |
| 16 | + it('does not throw on get', async () => { |
| 17 | + const context = { |
| 18 | + method: 'get', |
| 19 | + id: 'item1', |
| 20 | + } as HookContext |
| 21 | + await throwIfIsMulti()(context) |
| 22 | + }) |
| 23 | + |
| 24 | + it('does not throw on single create', async () => { |
| 25 | + const context = { |
| 26 | + method: 'create', |
| 27 | + data: { name: 'item1' }, |
| 28 | + } as HookContext |
| 29 | + |
| 30 | + await throwIfIsMulti()(context) |
| 31 | + }) |
| 32 | + |
| 33 | + it('does not throw on single update', async () => { |
| 34 | + const context = { |
| 35 | + method: 'update', |
| 36 | + id: 'item1', |
| 37 | + data: { name: 'item1' }, |
| 38 | + } as HookContext |
| 39 | + |
| 40 | + await throwIfIsMulti()(context) |
| 41 | + }) |
| 42 | + |
| 43 | + it('does not throw on single patch', async () => { |
| 44 | + const context = { |
| 45 | + method: 'patch', |
| 46 | + id: 'item1', |
| 47 | + data: { name: 'item1' }, |
| 48 | + } as HookContext |
| 49 | + |
| 50 | + await throwIfIsMulti()(context) |
| 51 | + }) |
| 52 | + |
| 53 | + it('does not throw on single remove', async () => { |
| 54 | + const context = { |
| 55 | + method: 'remove', |
| 56 | + id: 'item1', |
| 57 | + } as HookContext |
| 58 | + |
| 59 | + await throwIfIsMulti()(context) |
| 60 | + }) |
| 61 | + }) |
| 62 | + |
| 63 | + it('throws on multi create by default', async () => { |
| 64 | + const context = { |
| 65 | + method: 'create', |
| 66 | + data: [{ name: 'item1' }, { name: 'item2' }], |
| 67 | + } as HookContext |
| 68 | + |
| 69 | + await expect(() => throwIfIsMulti()(context)).rejects.toThrow(BadRequest) |
| 70 | + }) |
| 71 | + |
| 72 | + it('throws on multi patch by default', async () => { |
| 73 | + const context = { |
| 74 | + method: 'patch', |
| 75 | + id: null, |
| 76 | + data: { name: 'item1' }, |
| 77 | + } as any as HookContext |
| 78 | + |
| 79 | + await expect(() => throwIfIsMulti()(context)).rejects.toThrow(BadRequest) |
| 80 | + }) |
| 81 | + |
| 82 | + it('throws on multi remove by default', async () => { |
| 83 | + const context = { |
| 84 | + method: 'remove', |
| 85 | + id: null, |
| 86 | + } as any as HookContext |
| 87 | + |
| 88 | + await expect(() => throwIfIsMulti()(context)).rejects.toThrow(BadRequest) |
| 89 | + }) |
| 90 | + |
| 91 | + describe('with filter', () => { |
| 92 | + it('filter function has context as argument', async () => { |
| 93 | + const filterFn = vi.fn(() => true) |
| 94 | + const context = { |
| 95 | + method: 'create', |
| 96 | + data: [{ name: 'item1' }, { name: 'item2' }], |
| 97 | + } as HookContext |
| 98 | + await expect(() => throwIfIsMulti({ filter: filterFn })(context)).rejects.toThrow(BadRequest) |
| 99 | + expect(filterFn).toHaveBeenCalledWith(context) |
| 100 | + }) |
| 101 | + |
| 102 | + it('does not throw on multi create if filter returns false', async () => { |
| 103 | + const context = { |
| 104 | + method: 'create', |
| 105 | + data: [{ name: 'item1' }, { name: 'item2' }], |
| 106 | + } as HookContext |
| 107 | + |
| 108 | + await expect(() => throwIfIsMulti()(context)).rejects.toThrow(BadRequest) |
| 109 | + |
| 110 | + // sync |
| 111 | + await expect( |
| 112 | + throwIfIsMulti({ |
| 113 | + filter: () => false, |
| 114 | + })(context), |
| 115 | + ).resolves.not.toThrow() |
| 116 | + |
| 117 | + // async |
| 118 | + await expect( |
| 119 | + throwIfIsMulti({ |
| 120 | + filter: async () => false, |
| 121 | + })(context), |
| 122 | + ).resolves.not.toThrow() |
| 123 | + }) |
| 124 | + |
| 125 | + it('throws on multi create if filter returns true', async () => { |
| 126 | + const context = { |
| 127 | + method: 'create', |
| 128 | + data: [{ name: 'item1' }, { name: 'item2' }], |
| 129 | + } as HookContext |
| 130 | + |
| 131 | + // sync |
| 132 | + await expect(() => |
| 133 | + throwIfIsMulti({ |
| 134 | + filter: () => true, |
| 135 | + })(context), |
| 136 | + ).rejects.toThrow() |
| 137 | + |
| 138 | + // async |
| 139 | + await expect(() => |
| 140 | + throwIfIsMulti({ |
| 141 | + filter: async () => true, |
| 142 | + })(context), |
| 143 | + ).rejects.toThrow() |
| 144 | + }) |
| 145 | + }) |
| 146 | +}) |
0 commit comments