diff --git a/test/hooks/get-replace-items.test.ts b/test/hooks/get-replace-items.test.ts index f9d10d6..e110596 100755 --- a/test/hooks/get-replace-items.test.ts +++ b/test/hooks/get-replace-items.test.ts @@ -1,5 +1,5 @@ import { assert } from 'vitest'; -import { getItems, replaceItems } from '../../src'; +import { actOnDispatch, getItems, replaceItems } from '../../src'; // Tests when context.params._actOn === 'dispatch' are in act-on.test.ts @@ -183,4 +183,59 @@ describe('services getItems & replaceItems', () => { assert.deepEqual(hookFindPaginate.result.data, [{ a: 1 }, { b: 2 }]); }); }); + + describe('replaceItems actOnDispatch', () => { + beforeEach(() => { + hookBefore = { type: 'before', method: 'create', params: { provider: 'rest' } }; + hookAfter = { type: 'after', method: 'create', params: { provider: 'rest' } }; + hookFindPaginate = { + type: 'after', + method: 'find', + params: { provider: 'rest' }, + dispatch: { + total: 200, + data: [], + }, + }; + hookFind = { + type: 'after', + method: 'find', + params: { provider: 'rest' }, + }; + }); + + it('updates hook after::create item', async () => { + await actOnDispatch(() => replaceItems(hookAfter, { a: 1 }))(hookAfter); + assert.deepEqual(hookAfter.dispatch, { a: 1 }); + }); + + it('updates hook after::create items', async () => { + await actOnDispatch(() => replaceItems(hookAfter, [{ a: 1 }, { b: 2 }]))(hookAfter); + assert.deepEqual(hookAfter.dispatch, [{ a: 1 }, { b: 2 }]); + }); + + it('updates hook after::find item', async () => { + await actOnDispatch(() => replaceItems(hookFind, { a: 1 }))(hookFind); + assert.deepEqual(hookFind.dispatch, { a: 1 }); + }); + + it('updates hook after::find items', async () => { + await actOnDispatch(() => replaceItems(hookFind, [{ a: 1 }, { b: 2 }]))(hookFind); + assert.deepEqual(hookFind.dispatch, [{ a: 1 }, { b: 2 }]); + }); + + it('updates hook after::find item paginated NOTE THIS TEST NOTE THIS TEST', async () => { + await actOnDispatch(() => replaceItems(hookFindPaginate, { a: 1 }))(hookFindPaginate); + assert.equal(hookFindPaginate.dispatch.total, 200); + assert.deepEqual(hookFindPaginate.dispatch.data, [{ a: 1 }]); + }); + + it('updates hook after::find items paginated', async () => { + await actOnDispatch(() => replaceItems(hookFindPaginate, [{ a: 1 }, { b: 2 }]))( + hookFindPaginate, + ); + assert.equal(hookFindPaginate.dispatch.total, 200); + assert.deepEqual(hookFindPaginate.dispatch.data, [{ a: 1 }, { b: 2 }]); + }); + }); });