From a175e699cf41c4eb93b46a924a0eb6008cabeb73 Mon Sep 17 00:00:00 2001 From: Lukas Hass Date: Wed, 15 Jun 2022 20:55:07 +0200 Subject: [PATCH 1/2] fix: undefined query should match all items --- src/useFind.ts | 4 +-- test/useFind.test.ts | 64 +++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 65 insertions(+), 3 deletions(-) diff --git a/src/useFind.ts b/src/useFind.ts index b3c277f..785890e 100644 --- a/src/useFind.ts +++ b/src/useFind.ts @@ -16,7 +16,7 @@ function loadServiceEventHandlers< ): () => void { const onCreated = (createdItem: M): void => { // ignore items not matching the query or when no params are set - if (!params.value || !sift(params.value.query)(createdItem)) { + if (!params.value || !sift(params.value.query || {})(createdItem)) { return; } @@ -34,7 +34,7 @@ function loadServiceEventHandlers< const onItemChanged = (changedItem: M): void => { // ignore items not matching the query or when no params are set - if (!params.value || !sift(params.value.query)(changedItem)) { + if (!params.value || !sift(params.value.query || {})(changedItem)) { // remove item from the list if they have been on it before data.value = data.value.filter((item) => getId(item) !== getId(changedItem)); return; diff --git a/test/useFind.test.ts b/test/useFind.test.ts index 40739ba..57e265d 100644 --- a/test/useFind.test.ts +++ b/test/useFind.test.ts @@ -479,6 +479,35 @@ describe('Find composition', () => { expect(findComposition && findComposition.data.value).toContainEqual(additionalTestModel); }); + it('should listen to "create" events when query is undefined', async () => { + expect.assertions(2); + + // given + const emitter = eventHelper(); + const feathersMock = { + service: () => ({ + find: jest.fn(() => []), + on: emitter.on, + off: jest.fn(), + }), + on: jest.fn(), + off: jest.fn(), + } as unknown as Application; + const useFind = useFindOriginal(feathersMock); + let findComposition = null as UseFind | null; + mountComposition(() => { + findComposition = useFind('testModels', ref({ query: undefined })); + }); + await nextTick(); + + // when + emitter.emit('created', additionalTestModel); + + // then + expect(findComposition).toBeTruthy(); + expect(findComposition && findComposition.data.value).toContainEqual(additionalTestModel); + }); + it('should ignore "create" events when query is not matching', () => { expect.assertions(2); @@ -716,7 +745,7 @@ describe('Find composition', () => { expect(findComposition && findComposition.data.value.length).toBe(0); }); - it('should listen to "patch" & "update" events and add item from list when query is matching now', async () => { + it('should listen to "patch" & "update" events and add item to list when query is matching now', async () => { expect.assertions(4); // given @@ -749,6 +778,39 @@ describe('Find composition', () => { expect(findComposition && findComposition.data.value).toStrictEqual([changedTestModel]); }); + it('should listen to "patch" & "update" events and add item to list when query is undefined', async () => { + expect.assertions(4); + + // given + const emitter = eventHelper(); + const feathersMock = { + service: () => ({ + find: jest.fn(() => []), + on: emitter.on, + off: jest.fn(), + }), + on: jest.fn(), + off: jest.fn(), + } as unknown as Application; + const useFind = useFindOriginal(feathersMock); + let findComposition = null as UseFind | null; + mountComposition(() => { + findComposition = useFind('testModels', ref({ query: undefined })); + }); + + // before then to ensure that the previous loading procedure is completed + await nextTick(); + expect(findComposition && findComposition.isLoading.value).toBeFalsy(); + expect(findComposition && findComposition.data.value.length).toBe(0); + + // when + emitter.emit('updated', changedTestModel); + + // then + expect(findComposition).toBeTruthy(); + expect(findComposition && findComposition.data.value).toStrictEqual([changedTestModel]); + }); + it('should listen to "remove" events', async () => { expect.assertions(2); From 05e98760ceb8e93572a4cda0b327ef63cee89099 Mon Sep 17 00:00:00 2001 From: Lukas Hass Date: Wed, 15 Jun 2022 21:01:11 +0200 Subject: [PATCH 2/2] make condition more explicit --- src/useFind.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/useFind.ts b/src/useFind.ts index 785890e..728b6f1 100644 --- a/src/useFind.ts +++ b/src/useFind.ts @@ -16,7 +16,7 @@ function loadServiceEventHandlers< ): () => void { const onCreated = (createdItem: M): void => { // ignore items not matching the query or when no params are set - if (!params.value || !sift(params.value.query || {})(createdItem)) { + if (!params.value || (params.value.query !== undefined && !sift(params.value.query)(createdItem))) { return; } @@ -34,7 +34,7 @@ function loadServiceEventHandlers< const onItemChanged = (changedItem: M): void => { // ignore items not matching the query or when no params are set - if (!params.value || !sift(params.value.query || {})(changedItem)) { + if (!params.value || (params.value.query !== undefined && !sift(params.value.query)(changedItem))) { // remove item from the list if they have been on it before data.value = data.value.filter((item) => getId(item) !== getId(changedItem)); return;