Skip to content

Commit df1dcb0

Browse files
authored
fix: undefined query should match all items (#73)
* fix: undefined query should match all items * make condition more explicit
1 parent a2aff9c commit df1dcb0

File tree

2 files changed

+65
-3
lines changed

2 files changed

+65
-3
lines changed

src/useFind.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ function loadServiceEventHandlers<
1616
): () => void {
1717
const onCreated = (createdItem: M): void => {
1818
// ignore items not matching the query or when no params are set
19-
if (!params.value || !sift(params.value.query)(createdItem)) {
19+
if (!params.value || (params.value.query !== undefined && !sift(params.value.query)(createdItem))) {
2020
return;
2121
}
2222

@@ -34,7 +34,7 @@ function loadServiceEventHandlers<
3434

3535
const onItemChanged = (changedItem: M): void => {
3636
// ignore items not matching the query or when no params are set
37-
if (!params.value || !sift(params.value.query)(changedItem)) {
37+
if (!params.value || (params.value.query !== undefined && !sift(params.value.query)(changedItem))) {
3838
// remove item from the list if they have been on it before
3939
data.value = data.value.filter((item) => getId(item) !== getId(changedItem));
4040
return;

test/useFind.test.ts

+63-1
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,35 @@ describe('Find composition', () => {
479479
expect(findComposition && findComposition.data.value).toContainEqual(additionalTestModel);
480480
});
481481

482+
it('should listen to "create" events when query is undefined', async () => {
483+
expect.assertions(2);
484+
485+
// given
486+
const emitter = eventHelper();
487+
const feathersMock = {
488+
service: () => ({
489+
find: jest.fn(() => []),
490+
on: emitter.on,
491+
off: jest.fn(),
492+
}),
493+
on: jest.fn(),
494+
off: jest.fn(),
495+
} as unknown as Application;
496+
const useFind = useFindOriginal(feathersMock);
497+
let findComposition = null as UseFind<TestModel> | null;
498+
mountComposition(() => {
499+
findComposition = useFind('testModels', ref({ query: undefined }));
500+
});
501+
await nextTick();
502+
503+
// when
504+
emitter.emit('created', additionalTestModel);
505+
506+
// then
507+
expect(findComposition).toBeTruthy();
508+
expect(findComposition && findComposition.data.value).toContainEqual(additionalTestModel);
509+
});
510+
482511
it('should ignore "create" events when query is not matching', () => {
483512
expect.assertions(2);
484513

@@ -716,7 +745,7 @@ describe('Find composition', () => {
716745
expect(findComposition && findComposition.data.value.length).toBe(0);
717746
});
718747

719-
it('should listen to "patch" & "update" events and add item from list when query is matching now', async () => {
748+
it('should listen to "patch" & "update" events and add item to list when query is matching now', async () => {
720749
expect.assertions(4);
721750

722751
// given
@@ -749,6 +778,39 @@ describe('Find composition', () => {
749778
expect(findComposition && findComposition.data.value).toStrictEqual([changedTestModel]);
750779
});
751780

781+
it('should listen to "patch" & "update" events and add item to list when query is undefined', async () => {
782+
expect.assertions(4);
783+
784+
// given
785+
const emitter = eventHelper();
786+
const feathersMock = {
787+
service: () => ({
788+
find: jest.fn(() => []),
789+
on: emitter.on,
790+
off: jest.fn(),
791+
}),
792+
on: jest.fn(),
793+
off: jest.fn(),
794+
} as unknown as Application;
795+
const useFind = useFindOriginal(feathersMock);
796+
let findComposition = null as UseFind<TestModel> | null;
797+
mountComposition(() => {
798+
findComposition = useFind('testModels', ref({ query: undefined }));
799+
});
800+
801+
// before then to ensure that the previous loading procedure is completed
802+
await nextTick();
803+
expect(findComposition && findComposition.isLoading.value).toBeFalsy();
804+
expect(findComposition && findComposition.data.value.length).toBe(0);
805+
806+
// when
807+
emitter.emit('updated', changedTestModel);
808+
809+
// then
810+
expect(findComposition).toBeTruthy();
811+
expect(findComposition && findComposition.data.value).toStrictEqual([changedTestModel]);
812+
});
813+
752814
it('should listen to "remove" events', async () => {
753815
expect.assertions(2);
754816

0 commit comments

Comments
 (0)