From 6e9e4d43d9b83edbf8394796f7ba2452e87c1142 Mon Sep 17 00:00:00 2001 From: Javad Mnjd Date: Mon, 10 Jun 2024 13:07:40 +0330 Subject: [PATCH] chore: update tests for replaceItems with actOnDispatch --- test/hooks/get-replace-items.test.ts | 57 +++++++++++++++++++++++++++- 1 file changed, 56 insertions(+), 1 deletion(-) 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 }]); + }); + }); });