Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: missing items that match after update/patch #71

Merged
merged 3 commits into from
May 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions src/useFind.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,12 @@ function loadServiceEventHandlers<
return;
}

data.value = data.value.map((item) => {
if (getId(item) === getId(changedItem)) {
return changedItem;
}

return item;
});
const itemIndex = data.value.findIndex((item) => getId(item) === getId(changedItem));
if (itemIndex === -1) {
data.value = [...data.value, changedItem];
} else {
data.value = [...data.value.slice(0, itemIndex), changedItem, ...data.value.slice(itemIndex + 1)];
}
};

// eslint-disable-next-line @typescript-eslint/no-unsafe-call
Expand Down
63 changes: 63 additions & 0 deletions test/useFind.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,36 @@ describe('Find composition', () => {
expect(findComposition && findComposition.data.value).toContainEqual(changedTestModel);
});

it('should keep order of items when handling "update" events', async () => {
expect.assertions(3);

// given
const emitter = eventHelper();
const feathersMock = {
service: () => ({
find: jest.fn(() => [additionalTestModel2, testModel, additionalTestModel]),
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<TestModel> | null;
mountComposition(() => {
findComposition = useFind('testModels');
});
await nextTick();

// when
emitter.emit('updated', changedTestModel);

// then
expect(findComposition).toBeTruthy();
expect(findComposition && findComposition.data.value).toHaveLength(3);
expect(findComposition && findComposition.data.value[1]).toStrictEqual(changedTestModel);
});

it('should listen to "patch" & "update" events when query is matching', async () => {
expect.assertions(2);

Expand Down Expand Up @@ -606,6 +636,39 @@ 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 () => {
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<TestModel> | null;
mountComposition(() => {
findComposition = useFind('testModels', ref({ query: { category: changedTestModel.category } }));
});

// 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);

Expand Down