From 98cd0442263cb77deddecc2d0e2d5d326b6fcba4 Mon Sep 17 00:00:00 2001 From: Eric Peterson Date: Tue, 16 Feb 2021 15:14:11 +0100 Subject: [PATCH] Add tests exercising the API of the search-indexer-backend plugin. Signed-off-by: Eric Peterson --- .../search-indexer-backend/src/index.test.ts | 121 ++++++++++++++++++ .../search-indexer-backend/src/registry.ts | 32 +++-- 2 files changed, 143 insertions(+), 10 deletions(-) create mode 100644 plugins/search-indexer-backend/src/index.test.ts diff --git a/plugins/search-indexer-backend/src/index.test.ts b/plugins/search-indexer-backend/src/index.test.ts new file mode 100644 index 0000000000..21db1657b3 --- /dev/null +++ b/plugins/search-indexer-backend/src/index.test.ts @@ -0,0 +1,121 @@ +/* + * Copyright 2021 Spotify AB + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { registerCollator, registerDecorator } from './'; +import { Registry } from './registry'; + +describe('external api', () => { + afterEach(() => { + Registry.getInstance()._reset(); + }); + + describe('registerCollator', () => { + it('registers a collator', async () => { + const collatorSpy = jest.fn(async () => []); + + // Register a collator. + registerCollator({ + type: 'anything', + defaultRefreshIntervalSeconds: 600, + collator: collatorSpy, + }); + + // Execute the registry and ensure the collator was invoked. + await Registry.getInstance().execute(); + expect(collatorSpy).toHaveBeenCalled(); + }); + }); + + describe('registerDecorator', () => { + it('registers a decorator', async () => { + const mockCollator = jest.fn(async () => []); + const decoratorSpy = jest.fn(async docs => docs); + + // Register a collator. + registerCollator({ + type: 'anything', + defaultRefreshIntervalSeconds: 600, + collator: mockCollator, + }); + + // Register a decorator. + registerDecorator({ + decorator: decoratorSpy, + }); + + // Execute the registry and ensure the decorator was invoked. + await Registry.getInstance().execute(); + expect(decoratorSpy).toHaveBeenCalled(); + }); + + it('registers a type-specific decorator', async () => { + const expectedType = 'an-expected-type'; + const docFixture = { + title: 'Test', + text: 'Test text.', + location: '/test/location', + }; + const mockCollator = jest.fn(async () => [docFixture]); + const decoratorSpy = jest.fn(async docs => docs); + + // Register a collator. + registerCollator({ + type: expectedType, + defaultRefreshIntervalSeconds: 600, + collator: mockCollator, + }); + + // Register a decorator for the same type. + registerDecorator({ + types: [expectedType], + decorator: decoratorSpy, + }); + + // Execute the registry and ensure the decorator was invoked. + await Registry.getInstance().execute(); + expect(decoratorSpy).toHaveBeenCalled(); + expect(decoratorSpy).toHaveBeenCalledWith([docFixture]); + }); + + it('registers a type-specific decorator that should not be called', async () => { + const expectedType = 'an-expected-type'; + const docFixture = { + title: 'Test', + text: 'Test text.', + location: '/test/location', + }; + const mockCollator = jest.fn(async () => [docFixture]); + const decoratorSpy = jest.fn(async docs => docs); + + // Register a collator. + registerCollator({ + type: expectedType, + defaultRefreshIntervalSeconds: 600, + collator: mockCollator, + }); + + // Register a decorator for a different type. + registerDecorator({ + types: ['not-the-expected-type'], + decorator: decoratorSpy, + }); + + // Execute the registry and ensure the decorator was not invoked. + await Registry.getInstance().execute(); + expect(decoratorSpy).not.toHaveBeenCalled(); + }); + }); +}); diff --git a/plugins/search-indexer-backend/src/registry.ts b/plugins/search-indexer-backend/src/registry.ts index 23d39b12ff..8b71a87e57 100644 --- a/plugins/search-indexer-backend/src/registry.ts +++ b/plugins/search-indexer-backend/src/registry.ts @@ -69,18 +69,30 @@ export class Registry { } // @todo But like with coordination, timing, error handling, and what have you. - execute() { - Object.keys(this.collators).forEach(async type => { - const decorators: IndexableDocumentDecorator[] = ( - this.decorators['*'] || [] - ).concat(this.decorators[type] || []); - let documents = await this.collators[type].collate(); + async execute(): Promise { + return new Promise(resolve => { + Object.keys(this.collators).forEach(async type => { + const decorators: IndexableDocumentDecorator[] = ( + this.decorators['*'] || [] + ).concat(this.decorators[type] || []); + let documents = await this.collators[type].collate(); - for (let i = 0; i < decorators.length; i++) { - documents = await decorators[i](documents); - } + for (let i = 0; i < decorators.length; i++) { + documents = await decorators[i](documents); + } - // @todo: push documents to a configured search engine. + // @todo: push documents to a configured search engine. + resolve(undefined); + }); }); } + + /** + * Utility method for tests. Do not use otherwise. + * @private + */ + _reset() { + this.collators = {}; + this.decorators = {}; + } }