CommunitySolidServer/test/unit/storage/PatchingStore.test.ts
Joachim Van Herwegen 8491300f42 feat: Pass ResourceStore as param to PatchHandler
This way the chain of ResourceStores is a bit easier to configure.
This commit also updates the SparqlUpdatePatchHandler to keep the metadata
and content-type of the resource that is being modified.
2021-05-19 09:40:40 +02:00

41 lines
1.8 KiB
TypeScript

import type { Patch } from '../../../src/ldp/http/Patch';
import type { PatchHandler } from '../../../src/storage/patch/PatchHandler';
import { PatchingStore } from '../../../src/storage/PatchingStore';
import type { ResourceStore } from '../../../src/storage/ResourceStore';
describe('A PatchingStore', (): void => {
let store: PatchingStore;
let source: ResourceStore;
let patcher: PatchHandler;
let handleSafeFn: jest.Mock<Promise<void>, []>;
beforeEach(async(): Promise<void> => {
source = {
modifyResource: jest.fn(async(): Promise<any> => 'modify'),
} as unknown as ResourceStore;
handleSafeFn = jest.fn(async(): Promise<any> => 'patcher');
patcher = { handleSafe: handleSafeFn } as unknown as PatchHandler;
store = new PatchingStore(source, patcher);
});
it('calls modifyResource directly from the source if available.', async(): Promise<void> => {
await expect(store.modifyResource({ path: 'modifyPath' }, {} as Patch)).resolves.toBe('modify');
expect(source.modifyResource).toHaveBeenCalledTimes(1);
expect(source.modifyResource).toHaveBeenLastCalledWith({ path: 'modifyPath' }, {}, undefined);
});
it('calls its patcher if modifyResource failed.', async(): Promise<void> => {
source.modifyResource = jest.fn(async(): Promise<any> => {
throw new Error('dummy');
});
await expect(store.modifyResource({ path: 'modifyPath' }, {} as Patch)).resolves.toBe('patcher');
expect(source.modifyResource).toHaveBeenCalledTimes(1);
expect(source.modifyResource).toHaveBeenLastCalledWith({ path: 'modifyPath' }, {}, undefined);
await expect((source.modifyResource as jest.Mock).mock.results[0].value).rejects.toThrow('dummy');
expect(handleSafeFn).toHaveBeenCalledTimes(1);
expect(handleSafeFn).toHaveBeenLastCalledWith({ source, identifier: { path: 'modifyPath' }, patch: {}});
});
});