Skip to content

Commit

Permalink
tests: add more tests for saving pending in crud
Browse files Browse the repository at this point in the history
  • Loading branch information
jmeistrich committed Nov 3, 2024
1 parent 21b0af5 commit 9f98ed1
Showing 1 changed file with 219 additions and 2 deletions.
221 changes: 219 additions & 2 deletions tests/keel.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,8 @@ describe('keel', () => {
other: null,
});
});
});
describe('error handling', () => {
test('setting error retries', async () => {
let numUpdates = 0;
const obs = observable(
Expand Down Expand Up @@ -517,7 +519,7 @@ describe('keel', () => {
},
]);
});
test('Error handling in crud ', async () => {
test('Error handling in crud sets pending', async () => {
const persistName = getPersistName();
let errorAtOnError: Error | undefined = undefined;
let numErrors = 0;
Expand Down Expand Up @@ -556,6 +558,16 @@ describe('keel', () => {

await promiseTimeout(1);

expect(localStorage.getItem(persistName + '__m')!).toEqual(
JSON.stringify({
lastSync: 1,
pending: {
id2: { p: null, t: ['object'], v: { id: 'id2', test: 'hi', other: 3, another: 4 } },
'id1/test': { p: 'hi', t: ['object', 'object'], v: 'hello' },
},
}),
);

expect(errorAtOnError).toEqual(new Error('test'));
expect(numErrors).toEqual(1);

Expand All @@ -570,7 +582,7 @@ describe('keel', () => {
},
id2: { id: 'id2', test: 'hi', other: 3, another: 4 },
});
await promiseTimeout(10);
await promiseTimeout(1);

expect(localStorage.getItem(persistName + '__m')!).toEqual(
JSON.stringify({
Expand All @@ -579,6 +591,80 @@ describe('keel', () => {
}),
);
});
test('Error handling in crud sets pending 2', async () => {
const persistName = getPersistName();
let errorAtOnError: Error | undefined = undefined;
let numErrors = 0;
const obs$ = observable(
syncedKeel({
list: async () => fakeKeelList([{ ...ItemBasicValue(), other: 2, another: 3 }]),
create: async (input): Promise<any> => {
return { data: { ...input, updatedAt: 2 } } as any;
},
update: async (): Promise<any> => {
return { error: { message: 'test' } };
},
onError: (error) => {
numErrors++;
errorAtOnError = error;
},
changesSince: 'last-sync',
persist: {
name: persistName,
plugin: ObservablePersistLocalStorage,
retrySync: true,
},
}),
);

expect(obs$.get()).toEqual(undefined);

await promiseTimeout(1);

expect(obs$.get()).toEqual({
id1: { id: 'id1', test: 'hi', other: 2, another: 3, createdAt: 1, updatedAt: 1 },
});

obs$.id1.test.set('hello');
obs$.id2.set({ id: 'id2', test: 'hi', other: 3, another: 4 });

await promiseTimeout(1);

expect(localStorage.getItem(persistName + '__m')!).toEqual(
JSON.stringify({
lastSync: 1,
pending: {
id2: { p: null, t: ['object'], v: { id: 'id2', test: 'hi', other: 3, another: 4 } },
'id1/test': { p: 'hi', t: ['object', 'object'], v: 'hello' },
},
}),
);

expect(errorAtOnError).toEqual(new Error('test'));
expect(numErrors).toEqual(1);

expect(obs$.get()).toEqual({
id1: {
id: 'id1',
test: 'hello',
other: 2,
another: 3,
createdAt: 1,
updatedAt: 1,
},
id2: { id: 'id2', test: 'hi', other: 3, another: 4, updatedAt: 2 },
});
await promiseTimeout(1);

expect(localStorage.getItem(persistName + '__m')!).toEqual(
JSON.stringify({
lastSync: 1,
pending: {
'id1/test': { p: 'hi', t: ['object', 'object'], v: 'hello' },
},
}),
);
});
test('onError reverts only one change if multiple fails', async () => {
let errorAtOnError: Error | undefined = undefined;
let numErrors = 0;
Expand Down Expand Up @@ -627,4 +713,135 @@ describe('keel', () => {
id2: undefined,
});
});
test('Adding an item sets pending ', async () => {
const persistName = getPersistName();
let errorAtOnError: Error | undefined = undefined;
let numErrors = 0;
const obs$ = observable(
syncedKeel({
list: async () => fakeKeelList([{ ...ItemBasicValue(), other: 2, another: 3 }]),
create: async (): Promise<any> => {
return { error: { message: 'test' } };
},
onError: (error) => {
numErrors++;
errorAtOnError = error;
},
changesSince: 'last-sync',
persist: {
name: persistName,
plugin: ObservablePersistLocalStorage,
retrySync: true,
},
}),
);

expect(obs$.get()).toEqual(undefined);

await promiseTimeout(1);

expect(obs$.get()).toEqual({
id1: { id: 'id1', test: 'hi', other: 2, another: 3, createdAt: 1, updatedAt: 1 },
});

obs$.id2.set({ id: 'id2', test: 'hi', other: 3, another: 4 });

await promiseTimeout(1);

expect(errorAtOnError).toEqual(new Error('test'));
expect(numErrors).toEqual(1);

expect(obs$.get()).toEqual({
id1: {
id: 'id1',
test: 'hi',
other: 2,
another: 3,
createdAt: 1,
updatedAt: 1,
},
id2: { id: 'id2', test: 'hi', other: 3, another: 4 },
});

expect(localStorage.getItem(persistName + '__m')!).toEqual(
JSON.stringify({
lastSync: 1,
pending: { id2: { p: null, t: ['object'], v: { id: 'id2', test: 'hi', other: 3, another: 4 } } },
}),
);
});
test('Updating an item sets pending ', async () => {
const persistName = getPersistName();
let errorAtOnError: Error | undefined = undefined;
let numErrors = 0;
const obs$ = observable(
syncedKeel({
list: async () => fakeKeelList([{ ...ItemBasicValue(), other: 2, another: 3 }]),
create: async (input): Promise<any> => {
return { data: { ...input, updatedAt: 2 } } as any;
},
update: async (): Promise<any> => {
return { error: { message: 'test' } };
},
onError: (error) => {
numErrors++;
errorAtOnError = error;
},
changesSince: 'last-sync',
persist: {
name: persistName,
plugin: ObservablePersistLocalStorage,
retrySync: true,
},
}),
);

expect(obs$.get()).toEqual(undefined);

await promiseTimeout(1);

expect(obs$.get()).toEqual({
id1: { id: 'id1', test: 'hi', other: 2, another: 3, createdAt: 1, updatedAt: 1 },
});

obs$.id1.test.set('hello');
obs$.id2.set({ id: 'id2', test: 'hi', other: 3, another: 4 });

await promiseTimeout(1);

expect(localStorage.getItem(persistName + '__m')!).toEqual(
JSON.stringify({
lastSync: 1,
pending: {
id2: { p: null, t: ['object'], v: { id: 'id2', test: 'hi', other: 3, another: 4 } },
'id1/test': { p: 'hi', t: ['object', 'object'], v: 'hello' },
},
}),
);

expect(errorAtOnError).toEqual(new Error('test'));
expect(numErrors).toEqual(1);

expect(obs$.get()).toEqual({
id1: {
id: 'id1',
test: 'hello',
other: 2,
another: 3,
createdAt: 1,
updatedAt: 1,
},
id2: { id: 'id2', test: 'hi', other: 3, another: 4, updatedAt: 2 },
});
await promiseTimeout(10);

expect(localStorage.getItem(persistName + '__m')!).toEqual(
JSON.stringify({
lastSync: 1,
pending: {
'id1/test': { p: 'hi', t: ['object', 'object'], v: 'hello' },
},
}),
);
});
});

0 comments on commit 9f98ed1

Please sign in to comment.