Skip to content

Commit b2bef6c

Browse files
authored
fix: missing items that match after update/patch (#71)
1 parent 0294a7e commit b2bef6c

File tree

2 files changed

+69
-7
lines changed

2 files changed

+69
-7
lines changed

src/useFind.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,12 @@ function loadServiceEventHandlers<
3939
return;
4040
}
4141

42-
data.value = data.value.map((item) => {
43-
if (getId(item) === getId(changedItem)) {
44-
return changedItem;
45-
}
46-
47-
return item;
48-
});
42+
const itemIndex = data.value.findIndex((item) => getId(item) === getId(changedItem));
43+
if (itemIndex === -1) {
44+
data.value = [...data.value, changedItem];
45+
} else {
46+
data.value = [...data.value.slice(0, itemIndex), changedItem, ...data.value.slice(itemIndex + 1)];
47+
}
4948
};
5049

5150
// eslint-disable-next-line @typescript-eslint/no-unsafe-call

test/useFind.test.ts

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,36 @@ describe('Find composition', () => {
514514
expect(findComposition && findComposition.data.value).toContainEqual(changedTestModel);
515515
});
516516

517+
it('should keep order of items when handling "update" events', async () => {
518+
expect.assertions(3);
519+
520+
// given
521+
const emitter = eventHelper();
522+
const feathersMock = {
523+
service: () => ({
524+
find: jest.fn(() => [additionalTestModel2, testModel, additionalTestModel]),
525+
on: emitter.on,
526+
off: jest.fn(),
527+
}),
528+
on: jest.fn(),
529+
off: jest.fn(),
530+
} as unknown as Application;
531+
const useFind = useFindOriginal(feathersMock);
532+
let findComposition = null as UseFind<TestModel> | null;
533+
mountComposition(() => {
534+
findComposition = useFind('testModels');
535+
});
536+
await nextTick();
537+
538+
// when
539+
emitter.emit('updated', changedTestModel);
540+
541+
// then
542+
expect(findComposition).toBeTruthy();
543+
expect(findComposition && findComposition.data.value).toHaveLength(3);
544+
expect(findComposition && findComposition.data.value[1]).toStrictEqual(changedTestModel);
545+
});
546+
517547
it('should listen to "patch" & "update" events when query is matching', async () => {
518548
expect.assertions(2);
519549

@@ -606,6 +636,39 @@ describe('Find composition', () => {
606636
expect(findComposition && findComposition.data.value.length).toBe(0);
607637
});
608638

639+
it('should listen to "patch" & "update" events and add item from list when query is matching now', async () => {
640+
expect.assertions(4);
641+
642+
// given
643+
const emitter = eventHelper();
644+
const feathersMock = {
645+
service: () => ({
646+
find: jest.fn(() => []),
647+
on: emitter.on,
648+
off: jest.fn(),
649+
}),
650+
on: jest.fn(),
651+
off: jest.fn(),
652+
} as unknown as Application;
653+
const useFind = useFindOriginal(feathersMock);
654+
let findComposition = null as UseFind<TestModel> | null;
655+
mountComposition(() => {
656+
findComposition = useFind('testModels', ref({ query: { category: changedTestModel.category } }));
657+
});
658+
659+
// before then to ensure that the previous loading procedure is completed
660+
await nextTick();
661+
expect(findComposition && findComposition.isLoading.value).toBeFalsy();
662+
expect(findComposition && findComposition.data.value.length).toBe(0);
663+
664+
// when
665+
emitter.emit('updated', changedTestModel);
666+
667+
// then
668+
expect(findComposition).toBeTruthy();
669+
expect(findComposition && findComposition.data.value).toStrictEqual([changedTestModel]);
670+
});
671+
609672
it('should listen to "remove" events', async () => {
610673
expect.assertions(2);
611674

0 commit comments

Comments
 (0)