mirror of
https://github.com/CommunitySolidServer/CommunitySolidServer.git
synced 2024-10-03 14:55:10 +00:00
feat: Include parent containers in POST and DELETE changes.
This commit is contained in:
parent
0099d1d5dc
commit
d8799368fd
@ -3,6 +3,7 @@ import type { Patch } from '../ldp/http/Patch';
|
|||||||
import type { Representation } from '../ldp/representation/Representation';
|
import type { Representation } from '../ldp/representation/Representation';
|
||||||
import type { RepresentationPreferences } from '../ldp/representation/RepresentationPreferences';
|
import type { RepresentationPreferences } from '../ldp/representation/RepresentationPreferences';
|
||||||
import type { ResourceIdentifier } from '../ldp/representation/ResourceIdentifier';
|
import type { ResourceIdentifier } from '../ldp/representation/ResourceIdentifier';
|
||||||
|
import { getParentContainer } from '../util/PathUtil';
|
||||||
import type { Conditions } from './Conditions';
|
import type { Conditions } from './Conditions';
|
||||||
import type { ResourceStore } from './ResourceStore';
|
import type { ResourceStore } from './ResourceStore';
|
||||||
|
|
||||||
@ -22,12 +23,24 @@ export class MonitoringStore<T extends ResourceStore = ResourceStore>
|
|||||||
public async addResource(container: ResourceIdentifier, representation: Representation,
|
public async addResource(container: ResourceIdentifier, representation: Representation,
|
||||||
conditions?: Conditions): Promise<ResourceIdentifier> {
|
conditions?: Conditions): Promise<ResourceIdentifier> {
|
||||||
const identifier = await this.source.addResource(container, representation, conditions);
|
const identifier = await this.source.addResource(container, representation, conditions);
|
||||||
|
|
||||||
|
// Both the container contents and the resource itself have changed
|
||||||
|
this.emit('changed', container);
|
||||||
this.emit('changed', identifier);
|
this.emit('changed', identifier);
|
||||||
|
|
||||||
return identifier;
|
return identifier;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async deleteResource(identifier: ResourceIdentifier, conditions?: Conditions): Promise<void> {
|
public async deleteResource(identifier: ResourceIdentifier, conditions?: Conditions): Promise<void> {
|
||||||
await this.source.deleteResource(identifier, conditions);
|
await this.source.deleteResource(identifier, conditions);
|
||||||
|
|
||||||
|
// Both the container contents and the resource itself have changed
|
||||||
|
try {
|
||||||
|
const container = getParentContainer(identifier);
|
||||||
|
this.emit('changed', container);
|
||||||
|
} catch {
|
||||||
|
// Parent container not found
|
||||||
|
}
|
||||||
this.emit('changed', identifier);
|
this.emit('changed', identifier);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -10,8 +10,8 @@ describe('A MonitoringStore', (): void => {
|
|||||||
|
|
||||||
beforeEach(async(): Promise<void> => {
|
beforeEach(async(): Promise<void> => {
|
||||||
source = {
|
source = {
|
||||||
getRepresentation: jest.fn(async(): Promise<any> => 'get'),
|
getRepresentation: jest.fn(async(): Promise<any> => ({ success: true })),
|
||||||
addResource: jest.fn(async(): Promise<any> => ({ path: 'newResource' })),
|
addResource: jest.fn(async(): Promise<any> => ({ path: 'http://example.org/foo/bar/new' })),
|
||||||
setRepresentation: jest.fn(async(): Promise<any> => undefined),
|
setRepresentation: jest.fn(async(): Promise<any> => undefined),
|
||||||
deleteResource: jest.fn(async(): Promise<any> => undefined),
|
deleteResource: jest.fn(async(): Promise<any> => undefined),
|
||||||
modifyResource: jest.fn(async(): Promise<any> => undefined),
|
modifyResource: jest.fn(async(): Promise<any> => undefined),
|
||||||
@ -26,69 +26,83 @@ describe('A MonitoringStore', (): void => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('calls getRepresentation directly from the source.', async(): Promise<void> => {
|
it('calls getRepresentation directly from the source.', async(): Promise<void> => {
|
||||||
await expect(store.getRepresentation({ path: 'getPath' }, {})).resolves.toBe('get');
|
await expect(store.getRepresentation({ path: 'getPath' }, {})).resolves.toEqual({ success: true });
|
||||||
expect(source.getRepresentation).toHaveBeenCalledTimes(1);
|
expect(source.getRepresentation).toHaveBeenCalledTimes(1);
|
||||||
expect(source.getRepresentation).toHaveBeenLastCalledWith({ path: 'getPath' }, {}, undefined);
|
expect(source.getRepresentation).toHaveBeenLastCalledWith({ path: 'getPath' }, {}, undefined);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('does not fire a change event after completing getRepresentation.', async(): Promise<void> => {
|
it('does not fire a change event after completing getRepresentation.', async(): Promise<void> => {
|
||||||
expect(changedCallback).toHaveBeenCalledTimes(0);
|
expect(changedCallback).toHaveBeenCalledTimes(0);
|
||||||
await store.getRepresentation({ path: 'getPath' }, {});
|
await store.getRepresentation({ path: 'http://example.org/foo/bar' }, {});
|
||||||
expect(changedCallback).toHaveBeenCalledTimes(0);
|
expect(changedCallback).toHaveBeenCalledTimes(0);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('calls addResource directly from the source.', async(): Promise<void> => {
|
it('calls addResource directly from the source.', async(): Promise<void> => {
|
||||||
await expect(store.addResource({ path: 'addPath' }, {} as Representation)).resolves
|
await expect(store.addResource({ path: 'http://example.org/foo/bar' }, {} as Representation)).resolves
|
||||||
.toStrictEqual({ path: 'newResource' });
|
.toStrictEqual({ path: 'http://example.org/foo/bar/new' });
|
||||||
expect(source.addResource).toHaveBeenCalledTimes(1);
|
expect(source.addResource).toHaveBeenCalledTimes(1);
|
||||||
expect(source.addResource).toHaveBeenLastCalledWith({ path: 'addPath' }, {}, undefined);
|
expect(source.addResource).toHaveBeenLastCalledWith({ path: 'http://example.org/foo/bar' }, {}, undefined);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('fires a change event after completing addResource.', async(): Promise<void> => {
|
it('fires resource and container change events after completing addResource.', async(): Promise<void> => {
|
||||||
const result = store.addResource({ path: 'addPath' }, {} as Representation);
|
const result = store.addResource({ path: 'http://example.org/foo/bar' }, {} as Representation);
|
||||||
expect(changedCallback).toHaveBeenCalledTimes(0);
|
expect(changedCallback).toHaveBeenCalledTimes(0);
|
||||||
await result;
|
await result;
|
||||||
expect(changedCallback).toHaveBeenCalledTimes(1);
|
expect(changedCallback).toHaveBeenCalledTimes(2);
|
||||||
expect(changedCallback).toHaveBeenCalledWith({ path: 'newResource' });
|
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> => {
|
it('calls setRepresentation directly from the source.', async(): Promise<void> => {
|
||||||
await expect(store.setRepresentation({ path: 'setPath' }, {} as Representation)).resolves.toBeUndefined();
|
await expect(store.setRepresentation({ path: 'http://example.org/foo/bar' }, {} as Representation))
|
||||||
|
.resolves.toBeUndefined();
|
||||||
expect(source.setRepresentation).toHaveBeenCalledTimes(1);
|
expect(source.setRepresentation).toHaveBeenCalledTimes(1);
|
||||||
expect(source.setRepresentation).toHaveBeenLastCalledWith({ path: 'setPath' }, {}, undefined);
|
expect(source.setRepresentation).toHaveBeenLastCalledWith({ path: 'http://example.org/foo/bar' }, {}, undefined);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('fires a change event after completing setRepresentation.', async(): Promise<void> => {
|
it('fires a resource change event after completing setRepresentation.', async(): Promise<void> => {
|
||||||
const result = store.setRepresentation({ path: 'setPath' }, {} as Representation);
|
const result = store.setRepresentation({ path: 'http://example.org/foo/bar' }, {} as Representation);
|
||||||
expect(changedCallback).toHaveBeenCalledTimes(0);
|
expect(changedCallback).toHaveBeenCalledTimes(0);
|
||||||
await result;
|
await result;
|
||||||
expect(changedCallback).toHaveBeenCalledTimes(1);
|
expect(changedCallback).toHaveBeenCalledTimes(1);
|
||||||
expect(changedCallback).toHaveBeenCalledWith({ path: 'setPath' });
|
expect(changedCallback).toHaveBeenCalledWith({ path: 'http://example.org/foo/bar' });
|
||||||
});
|
});
|
||||||
|
|
||||||
it('calls deleteResource directly from the source.', async(): Promise<void> => {
|
it('calls deleteResource directly from the source.', async(): Promise<void> => {
|
||||||
await expect(store.deleteResource({ path: 'deletePath' })).resolves.toBeUndefined();
|
await expect(store.deleteResource({ path: 'http://example.org/foo/bar' })).resolves.toBeUndefined();
|
||||||
expect(source.deleteResource).toHaveBeenCalledTimes(1);
|
expect(source.deleteResource).toHaveBeenCalledTimes(1);
|
||||||
expect(source.deleteResource).toHaveBeenLastCalledWith({ path: 'deletePath' }, undefined);
|
expect(source.deleteResource).toHaveBeenLastCalledWith({ path: 'http://example.org/foo/bar' }, undefined);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('fires a change event after completing deleteResource.', async(): Promise<void> => {
|
it('fires resource and container change events after completing deleteResource.', async(): Promise<void> => {
|
||||||
const result = store.deleteResource({ path: 'deletePath' });
|
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/foo/' });
|
||||||
|
expect(changedCallback).toHaveBeenCalledWith({ path: 'http://example.org/foo/bar' });
|
||||||
|
});
|
||||||
|
|
||||||
|
it('fires a resource change event after completing deleteResource on the root.', async(): Promise<void> => {
|
||||||
|
const result = store.deleteResource({ path: 'http://example.org/' });
|
||||||
expect(changedCallback).toHaveBeenCalledTimes(0);
|
expect(changedCallback).toHaveBeenCalledTimes(0);
|
||||||
await result;
|
await result;
|
||||||
expect(changedCallback).toHaveBeenCalledTimes(1);
|
expect(changedCallback).toHaveBeenCalledTimes(1);
|
||||||
|
expect(changedCallback).toHaveBeenCalledWith({ path: 'http://example.org/' });
|
||||||
});
|
});
|
||||||
|
|
||||||
it('calls modifyResource directly from the source.', async(): Promise<void> => {
|
it('calls modifyResource directly from the source.', async(): Promise<void> => {
|
||||||
await expect(store.modifyResource({ path: 'modifyPath' }, {} as Patch)).resolves.toBeUndefined();
|
await expect(store.modifyResource({ path: 'http://example.org/foo/bar' }, {} as Patch))
|
||||||
|
.resolves.toBeUndefined();
|
||||||
expect(source.modifyResource).toHaveBeenCalledTimes(1);
|
expect(source.modifyResource).toHaveBeenCalledTimes(1);
|
||||||
expect(source.modifyResource).toHaveBeenLastCalledWith({ path: 'modifyPath' }, {}, undefined);
|
expect(source.modifyResource).toHaveBeenLastCalledWith({ path: 'http://example.org/foo/bar' }, {}, undefined);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('fires a change event after completing modifyResource.', async(): Promise<void> => {
|
it('fires a resource change event after completing modifyResource.', async(): Promise<void> => {
|
||||||
const result = store.modifyResource({ path: 'modifyPath' }, {} as Patch);
|
const result = store.modifyResource({ path: 'http://example.org/foo/bar' }, {} as Patch);
|
||||||
expect(changedCallback).toHaveBeenCalledTimes(0);
|
expect(changedCallback).toHaveBeenCalledTimes(0);
|
||||||
await result;
|
await result;
|
||||||
expect(changedCallback).toHaveBeenCalledTimes(1);
|
expect(changedCallback).toHaveBeenCalledTimes(1);
|
||||||
|
expect(changedCallback).toHaveBeenCalledWith({ path: 'http://example.org/foo/bar' });
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user