CommunitySolidServer/test/unit/storage/MonitoringStore.test.ts
Joachim Van Herwegen 4404fa07d9 refactor: Rename resourceExists to hasResource
The function was also moved to the smaller interface ResourceSet.
2022-03-18 14:15:12 +01:00

115 lines
5.8 KiB
TypeScript

import type { Patch } from '../../../src/http/representation/Patch';
import type { Representation } from '../../../src/http/representation/Representation';
import { MonitoringStore } from '../../../src/storage/MonitoringStore';
import type { ResourceStore } from '../../../src/storage/ResourceStore';
describe('A MonitoringStore', (): void => {
let store: MonitoringStore;
let source: ResourceStore;
let changedCallback: () => void;
const modified = [
{ path: 'http://example.org/modified/1' },
{ path: 'http://example.org/modified/2' },
];
beforeEach(async(): Promise<void> => {
source = {
getRepresentation: jest.fn(async(): Promise<any> => ({ success: true })),
addResource: jest.fn(async(): Promise<any> => ({ path: 'http://example.org/foo/bar/new' })),
setRepresentation: jest.fn(async(): Promise<any> => modified),
deleteResource: jest.fn(async(): Promise<any> => modified),
modifyResource: jest.fn(async(): Promise<any> => modified),
hasResource: jest.fn(async(): Promise<any> => undefined),
};
store = new MonitoringStore(source);
changedCallback = jest.fn();
store.on('changed', changedCallback);
});
afterEach(async(): Promise<void> => {
store.removeListener('changed', changedCallback);
});
it('calls getRepresentation directly from the source.', async(): Promise<void> => {
await expect(store.getRepresentation({ path: 'getPath' }, {})).resolves.toEqual({ success: true });
expect(source.getRepresentation).toHaveBeenCalledTimes(1);
expect(source.getRepresentation).toHaveBeenLastCalledWith({ path: 'getPath' }, {}, undefined);
});
it('does not fire a change event after getRepresentation.', async(): Promise<void> => {
expect(changedCallback).toHaveBeenCalledTimes(0);
await store.getRepresentation({ path: 'http://example.org/foo/bar' }, {});
expect(changedCallback).toHaveBeenCalledTimes(0);
});
it('calls addResource directly from the source.', async(): Promise<void> => {
await expect(store.addResource({ path: 'http://example.org/foo/bar' }, {} as Representation)).resolves
.toStrictEqual({ path: 'http://example.org/foo/bar/new' });
expect(source.addResource).toHaveBeenCalledTimes(1);
expect(source.addResource).toHaveBeenLastCalledWith({ path: 'http://example.org/foo/bar' }, {}, undefined);
});
it('fires container and resource change events after addResource.', async(): Promise<void> => {
const result = store.addResource({ path: 'http://example.org/foo/bar/' }, {} as Representation);
expect(changedCallback).toHaveBeenCalledTimes(0);
await result;
expect(changedCallback).toHaveBeenCalledTimes(2);
expect(changedCallback).toHaveBeenCalledWith({ path: 'http://example.org/foo/bar/' });
expect(changedCallback).toHaveBeenCalledWith({ path: 'http://example.org/foo/bar/new' });
});
it('calls setRepresentation directly from the source.', async(): Promise<void> => {
await expect(store.setRepresentation({ path: 'http://example.org/foo/bar' }, {} as Representation))
.resolves.toEqual(modified);
expect(source.setRepresentation).toHaveBeenCalledTimes(1);
expect(source.setRepresentation).toHaveBeenLastCalledWith({ path: 'http://example.org/foo/bar' }, {}, undefined);
});
it('fires all modified change events after setRepresentation.', async(): Promise<void> => {
const result = store.setRepresentation({ path: 'http://example.org/foo/bar' }, {} as Representation);
expect(changedCallback).toHaveBeenCalledTimes(0);
await result;
expect(changedCallback).toHaveBeenCalledTimes(2);
expect(changedCallback).toHaveBeenCalledWith({ path: 'http://example.org/modified/1' });
expect(changedCallback).toHaveBeenCalledWith({ path: 'http://example.org/modified/2' });
});
it('calls deleteResource directly from the source.', async(): Promise<void> => {
await expect(store.deleteResource({ path: 'http://example.org/foo/bar' }))
.resolves.toEqual(modified);
expect(source.deleteResource).toHaveBeenCalledTimes(1);
expect(source.deleteResource).toHaveBeenLastCalledWith({ path: 'http://example.org/foo/bar' }, undefined);
});
it('fires all modified change events after deleteResource.', async(): Promise<void> => {
const result = store.deleteResource({ path: 'http://example.org/foo/bar' });
expect(changedCallback).toHaveBeenCalledTimes(0);
await result;
expect(changedCallback).toHaveBeenCalledTimes(2);
expect(changedCallback).toHaveBeenCalledWith({ path: 'http://example.org/modified/1' });
expect(changedCallback).toHaveBeenCalledWith({ path: 'http://example.org/modified/2' });
});
it('calls modifyResource directly from the source.', async(): Promise<void> => {
await expect(store.modifyResource({ path: 'http://example.org/foo/bar' }, {} as Patch))
.resolves.toEqual(modified);
expect(source.modifyResource).toHaveBeenCalledTimes(1);
expect(source.modifyResource).toHaveBeenLastCalledWith({ path: 'http://example.org/foo/bar' }, {}, undefined);
});
it('fires all modified change events after modifyResource.', async(): Promise<void> => {
const result = store.modifyResource({ path: 'http://example.org/foo/bar' }, {} as Patch);
expect(changedCallback).toHaveBeenCalledTimes(0);
await result;
expect(changedCallback).toHaveBeenCalledTimes(2);
expect(changedCallback).toHaveBeenCalledWith({ path: 'http://example.org/modified/1' });
expect(changedCallback).toHaveBeenCalledWith({ path: 'http://example.org/modified/2' });
});
it('calls hasResource directly from the source.', async(): Promise<void> => {
await expect(store.hasResource({ path: 'http://example.org/foo/bar' })).resolves.toBeUndefined();
expect(source.hasResource).toHaveBeenCalledTimes(1);
expect(source.hasResource).toHaveBeenLastCalledWith({ path: 'http://example.org/foo/bar' });
});
});