Skip to content

Commit c39becb

Browse files
authored
fix: handle partial patch payload (#80)
* add handling of partial patch payload * added gitpod config
1 parent d43c221 commit c39becb

File tree

5 files changed

+84
-6
lines changed

5 files changed

+84
-6
lines changed

.gitpod.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# This configuration file was automatically generated by Gitpod.
2+
# Please adjust to your needs (see https://www.gitpod.io/docs/introduction/learn-gitpod/gitpod-yaml)
3+
# and commit this file to your remote git repository to share the goodness with others.
4+
5+
# Learn more from ready-to-use templates: https://www.gitpod.io/docs/introduction/getting-started/quickstart
6+
7+
tasks:
8+
- before: npm install -g pnpm@7
9+
init: pnpm install
10+
command: pnpm run start

src/useFind.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,20 @@ function loadServiceEventHandlers<
3434
};
3535

3636
const onItemChanged = (changedItem: M): void => {
37+
const existingItem = data.value.find((item) => getId(item) === getId(changedItem));
38+
const newItem = { ...existingItem, ...changedItem };
3739
// ignore items not matching the query or when no params are set
38-
if (!params.value || (params.value.query !== undefined && !sift(params.value.query)(changedItem))) {
40+
if (!params.value || (params.value.query !== undefined && !sift(params.value.query)(newItem))) {
3941
// remove item from the list if they have been on it before
40-
data.value = data.value.filter((item) => getId(item) !== getId(changedItem));
42+
data.value = data.value.filter((item) => getId(item) !== getId(newItem));
4143
return;
4244
}
4345

44-
const itemIndex = data.value.findIndex((item) => getId(item) === getId(changedItem));
46+
const itemIndex = data.value.findIndex((item) => getId(item) === getId(newItem));
4547
if (itemIndex === -1) {
46-
data.value = [...data.value, changedItem];
48+
data.value = [...data.value, newItem];
4749
} else {
48-
data.value = [...data.value.slice(0, itemIndex), changedItem, ...data.value.slice(itemIndex + 1)];
50+
data.value = [...data.value.slice(0, itemIndex), newItem, ...data.value.slice(itemIndex + 1)];
4951
}
5052
};
5153

src/useGet.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ function loadServiceEventHandlers<
2727

2828
const onItemChanged = (item: M): void => {
2929
if (_id.value === getId(item)) {
30-
data.value = item;
30+
data.value = { ...data.value, ...item };
3131
}
3232
};
3333

test/useFind.test.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const testModel: TestModel = { _id: '111', mood: '😀', action: '🧘', categor
1313
const additionalTestModel: TestModel = { _id: 'aaa', mood: '🤩', action: '🏄', category: 'sport' };
1414
const additionalTestModel2: TestModel = { _id: 'bbb', mood: '', action: '', category: 'sport' };
1515
const changedTestModel: TestModel = { ...testModel, mood: '😅', action: '🏋️', category: 'sport' };
16+
const partialChangedTestModel: Partial<TestModel> = { _id: '111', action: '🏋️', category: 'sport' };
1617
const testModels: TestModel[] = [testModel, additionalTestModel2];
1718

1819
describe('Find composition', () => {
@@ -843,6 +844,38 @@ describe('Find composition', () => {
843844
expect(findComposition && findComposition.data.value).toStrictEqual([changedTestModel]);
844845
});
845846

847+
it('should handle "patch" events with partial responses properly', async () => {
848+
expect.assertions(2);
849+
850+
// given
851+
const emitter = eventHelper();
852+
const feathersMock = {
853+
service: () => ({
854+
find: vi.fn(() => testModels),
855+
on: emitter.on,
856+
off: vi.fn(),
857+
}),
858+
on: vi.fn(),
859+
off: vi.fn(),
860+
} as unknown as Application;
861+
const useFind = useFindOriginal(feathersMock);
862+
let findComposition = null as UseFind<TestModel> | null;
863+
mountComposition(() => {
864+
findComposition = useFind('testModels');
865+
});
866+
await nextTick();
867+
868+
// when
869+
emitter.emit('patched', partialChangedTestModel);
870+
871+
// then
872+
expect(findComposition).toBeTruthy();
873+
expect(findComposition && findComposition.data.value).toContainEqual({
874+
...testModel,
875+
...partialChangedTestModel,
876+
});
877+
});
878+
846879
it('should listen to "remove" events', async () => {
847880
expect.assertions(2);
848881

test/useGet.test.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import TestModel from '$/__helpers__/TestModel';
1212
const testModel: TestModel = { _id: '111', mood: '😀', action: '🧘', category: 'enjoy' };
1313
const additionalTestModel: TestModel = { _id: 'aaa', mood: '🤩', action: '🏄', category: 'sport' };
1414
const changedTestModel: TestModel = { ...testModel, mood: '😅', action: '🏋️', category: 'sport' };
15+
const partialChangedTestModel: Partial<TestModel> = { _id: '111', action: '🏋️', category: 'sport' };
1516

1617
describe('Get composition', () => {
1718
beforeEach(() => {
@@ -504,6 +505,38 @@ describe('Get composition', () => {
504505
expect(getComposition && getComposition.data.value).toStrictEqual(testModel);
505506
});
506507

508+
it('should handle "patch" events with partial responses properly', async () => {
509+
expect.assertions(3);
510+
511+
// given
512+
const emitter = eventHelper();
513+
const feathersMock = {
514+
service: () => ({
515+
get: vi.fn(() => testModel),
516+
on: emitter.on,
517+
off: vi.fn(),
518+
}),
519+
on: vi.fn(),
520+
off: vi.fn(),
521+
} as unknown as Application;
522+
const useGet = useGetOriginal(feathersMock);
523+
let getComposition = null as UseGet<TestModel> | null;
524+
mountComposition(() => {
525+
getComposition = useGet('testModels', ref(testModel._id));
526+
});
527+
528+
// before then to ensure previous state
529+
await nextTick();
530+
expect(getComposition && getComposition.data.value).toStrictEqual(testModel);
531+
532+
// when
533+
emitter.emit('patched', partialChangedTestModel);
534+
535+
// then
536+
expect(getComposition).toBeTruthy();
537+
expect(getComposition && getComposition.data.value).toStrictEqual({ ...testModel, ...partialChangedTestModel });
538+
});
539+
507540
it('should listen to "remove" events', async () => {
508541
expect.assertions(3);
509542

0 commit comments

Comments
 (0)