refactor: Change routing constructors to work with Components.js

This commit is contained in:
Joachim Van Herwegen 2020-11-02 15:25:09 +01:00
parent b6c8e2cb8f
commit 50dfea1a27
3 changed files with 7 additions and 24 deletions

View File

@ -24,9 +24,9 @@ export class ConvertingRouterRule extends RouterRule {
private readonly typedStores: ConvertingStoreEntry[]; private readonly typedStores: ConvertingStoreEntry[];
private readonly defaultStore: ResourceStore; private readonly defaultStore: ResourceStore;
public constructor(typedStores: ConvertingStoreEntry[], defaultStore: ResourceStore) { public constructor(typedStore: ConvertingStoreEntry, defaultStore: ResourceStore) {
super(); super();
this.typedStores = typedStores; this.typedStores = [ typedStore ];
this.defaultStore = defaultStore; this.defaultStore = defaultStore;
} }

View File

@ -20,7 +20,8 @@ export class RegexRouterRule extends RouterRule {
/** /**
* The keys of the `storeMap` will be converted into actual RegExp objects that will be used for testing. * The keys of the `storeMap` will be converted into actual RegExp objects that will be used for testing.
*/ */
public constructor(base: string, storeMap: Record<string, ResourceStore>) { // eslint-disable-next-line @typescript-eslint/consistent-indexed-object-style
public constructor(base: string, storeMap: { [ regex: string ]: ResourceStore }) {
super(); super();
this.base = trimTrailingSlashes(base); this.base = trimTrailingSlashes(base);
this.regexes = new Map(Object.keys(storeMap).map((regex): [ RegExp, ResourceStore ] => this.regexes = new Map(Object.keys(storeMap).map((regex): [ RegExp, ResourceStore ] =>

View File

@ -9,17 +9,14 @@ import { NotFoundHttpError } from '../../../../src/util/errors/NotFoundHttpError
describe('A ConvertingRouterRule', (): void => { describe('A ConvertingRouterRule', (): void => {
let store1: ResourceStore; let store1: ResourceStore;
let store2: ResourceStore;
let defaultStore: ResourceStore; let defaultStore: ResourceStore;
let checker1: PreferenceSupport; let checker1: PreferenceSupport;
let checker2: PreferenceSupport;
let rule: ConvertingRouterRule; let rule: ConvertingRouterRule;
let representation: Representation; let representation: Representation;
let metadata: RepresentationMetadata; let metadata: RepresentationMetadata;
beforeEach(async(): Promise<void> => { beforeEach(async(): Promise<void> => {
store1 = { name: 'turtleStore' } as any; store1 = { name: 'turtleStore' } as any;
store2 = { name: 'textStore' } as any;
defaultStore = { name: 'defaultStore' } as any; defaultStore = { name: 'defaultStore' } as any;
checker1 = { checker1 = {
@ -27,14 +24,8 @@ describe('A ConvertingRouterRule', (): void => {
return input.representation.metadata.contentType === 'text/turtle'; return input.representation.metadata.contentType === 'text/turtle';
}, },
} as any; } as any;
checker2 = {
async supports(input: { representation: Representation }): Promise<boolean> {
return input.representation.metadata.contentType === 'application/ld+json';
},
} as any;
rule = new ConvertingRouterRule([{ store: store1, supportChecker: checker1 }, rule = new ConvertingRouterRule({ store: store1, supportChecker: checker1 }, defaultStore);
{ store: store2, supportChecker: checker2 }], defaultStore);
metadata = new RepresentationMetadata(); metadata = new RepresentationMetadata();
representation = { binary: true, data: 'data!' as any, metadata }; representation = { binary: true, data: 'data!' as any, metadata };
@ -43,9 +34,6 @@ describe('A ConvertingRouterRule', (): void => {
it('returns the corresponding store if it supports the input.', async(): Promise<void> => { it('returns the corresponding store if it supports the input.', async(): Promise<void> => {
metadata.contentType = 'text/turtle'; metadata.contentType = 'text/turtle';
await expect(rule.handle({ identifier: { path: 'identifier' }, representation })).resolves.toBe(store1); await expect(rule.handle({ identifier: { path: 'identifier' }, representation })).resolves.toBe(store1);
metadata.contentType = 'application/ld+json';
await expect(rule.handle({ identifier: { path: 'identifier' }, representation })).resolves.toBe(store2);
}); });
it('returns the defaultStore if the converter does not support the input.', async(): Promise<void> => { it('returns the defaultStore if the converter does not support the input.', async(): Promise<void> => {
@ -54,11 +42,8 @@ describe('A ConvertingRouterRule', (): void => {
it('checks if the stores contain the identifier if there is no data.', async(): Promise<void> => { it('checks if the stores contain the identifier if there is no data.', async(): Promise<void> => {
const data: Readable = { destroy: jest.fn() } as any; const data: Readable = { destroy: jest.fn() } as any;
store1.getRepresentation = (): any => { store1.getRepresentation = async(): Promise<Representation> => ({ data } as any);
throw new NotFoundHttpError(); await expect(rule.handle({ identifier: { path: 'identifier' }})).resolves.toBe(store1);
};
store2.getRepresentation = async(): Promise<Representation> => ({ data } as any);
await expect(rule.handle({ identifier: { path: 'identifier' }})).resolves.toBe(store2);
expect(data.destroy).toHaveBeenCalledTimes(1); expect(data.destroy).toHaveBeenCalledTimes(1);
}); });
@ -66,9 +51,6 @@ describe('A ConvertingRouterRule', (): void => {
store1.getRepresentation = (): any => { store1.getRepresentation = (): any => {
throw new NotFoundHttpError(); throw new NotFoundHttpError();
}; };
store2.getRepresentation = (): any => {
throw new NotFoundHttpError();
};
await expect(rule.handle({ identifier: { path: 'identifier' }})).resolves.toBe(defaultStore); await expect(rule.handle({ identifier: { path: 'identifier' }})).resolves.toBe(defaultStore);
}); });