🗃️(service-worker) cache successfull update in doc-item
When we updated the doc successfully then directly pass offline, the cache was not updated. It is because a successful update didn't update the cache of the doc-item.
This commit is contained in:
@@ -44,16 +44,36 @@ export class ApiPlugin implements WorkboxPlugin {
|
|||||||
request,
|
request,
|
||||||
response,
|
response,
|
||||||
}) => {
|
}) => {
|
||||||
if (this.options.type === 'list' || this.options.type === 'item') {
|
if (response.status !== 200) {
|
||||||
if (response.status !== 200) {
|
return response;
|
||||||
return response;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
if (this.options.type === 'list' || this.options.type === 'item') {
|
||||||
const tableName = this.options.tableName;
|
const tableName = this.options.tableName;
|
||||||
const body = (await response.clone().json()) as DocsResponse | Doc;
|
const body = (await response.clone().json()) as DocsResponse | Doc;
|
||||||
await DocsDB.cacheResponse(request.url, body, tableName);
|
await DocsDB.cacheResponse(request.url, body, tableName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (this.options.type === 'update') {
|
||||||
|
const db = await DocsDB.open();
|
||||||
|
const storedResponse = await db.get('doc-item', request.url);
|
||||||
|
|
||||||
|
if (!storedResponse || !this.initialRequest) {
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
|
||||||
|
const bodyMutate = (await this.initialRequest
|
||||||
|
.clone()
|
||||||
|
.json()) as Partial<Doc>;
|
||||||
|
|
||||||
|
const newResponse = {
|
||||||
|
...storedResponse,
|
||||||
|
...bodyMutate,
|
||||||
|
};
|
||||||
|
|
||||||
|
await DocsDB.cacheResponse(request.url, newResponse, 'doc-item');
|
||||||
|
}
|
||||||
|
|
||||||
return response;
|
return response;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -30,17 +30,34 @@ jest.mock('idb', () => ({
|
|||||||
describe('ApiPlugin', () => {
|
describe('ApiPlugin', () => {
|
||||||
afterEach(() => jest.clearAllMocks());
|
afterEach(() => jest.clearAllMocks());
|
||||||
|
|
||||||
['doc-item', 'doc-list'].forEach((type) => {
|
[
|
||||||
|
{ type: 'item', table: 'doc-item' },
|
||||||
|
{ type: 'list', table: 'doc-list' },
|
||||||
|
{ type: 'update', table: 'doc-item' },
|
||||||
|
].forEach(({ type, table }) => {
|
||||||
it(`calls fetchDidSucceed with type ${type} and status 200`, async () => {
|
it(`calls fetchDidSucceed with type ${type} and status 200`, async () => {
|
||||||
|
const mockedSync = jest.fn().mockResolvedValue({});
|
||||||
const apiPlugin = new ApiPlugin({
|
const apiPlugin = new ApiPlugin({
|
||||||
tableName: type as any,
|
tableName: table as any,
|
||||||
type: 'list',
|
type: type as any,
|
||||||
syncManager: jest.fn() as any,
|
syncManager: {
|
||||||
|
sync: () => mockedSync(),
|
||||||
|
} as any,
|
||||||
});
|
});
|
||||||
|
|
||||||
const body = { lastName: 'Doe' };
|
const body = { lastName: 'Doe' };
|
||||||
const bodyBuffer = RequestSerializer.objectToArrayBuffer(body);
|
const bodyBuffer = RequestSerializer.objectToArrayBuffer(body);
|
||||||
|
|
||||||
|
const requestInit = {
|
||||||
|
request: {
|
||||||
|
url: 'test-url',
|
||||||
|
clone: () => mockedClone(),
|
||||||
|
json: () => body,
|
||||||
|
} as unknown as Request,
|
||||||
|
} as any;
|
||||||
|
const mockedClone = jest.fn().mockReturnValue(requestInit.request);
|
||||||
|
await apiPlugin.requestWillFetch?.(requestInit);
|
||||||
|
|
||||||
const response = await apiPlugin.fetchDidSucceed?.({
|
const response = await apiPlugin.fetchDidSucceed?.({
|
||||||
request: {
|
request: {
|
||||||
url: 'test-url',
|
url: 'test-url',
|
||||||
@@ -55,15 +72,15 @@ describe('ApiPlugin', () => {
|
|||||||
}),
|
}),
|
||||||
} as any);
|
} as any);
|
||||||
|
|
||||||
expect(mockedPut).toHaveBeenCalledWith(type, body, 'test-url');
|
expect(mockedPut).toHaveBeenCalledWith(table, body, 'test-url');
|
||||||
expect(mockedClose).toHaveBeenCalled();
|
expect(mockedClose).toHaveBeenCalled();
|
||||||
expect(response?.status).toBe(200);
|
expect(response?.status).toBe(200);
|
||||||
});
|
});
|
||||||
|
|
||||||
it(`calls fetchDidSucceed with type ${type} and status other that 200`, async () => {
|
it(`calls fetchDidSucceed with type ${type} and status other that 200`, async () => {
|
||||||
const apiPlugin = new ApiPlugin({
|
const apiPlugin = new ApiPlugin({
|
||||||
tableName: type as any,
|
tableName: table as any,
|
||||||
type: 'list',
|
type: type as any,
|
||||||
syncManager: jest.fn() as any,
|
syncManager: jest.fn() as any,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user