return token;
}
+let tokenContextPluginRegistered = false;
+
export function tokenContextPlugin(builder: LunrBuilder) {
- (lunr as any).Pipeline.registerFunction(tokenContextPluginCallback, 'tokenContext');
+ if (!tokenContextPluginRegistered) {
+ (lunr as any).Pipeline.registerFunction(tokenContextPluginCallback, 'tokenContext');
+ tokenContextPluginRegistered = true;
+ }
+
builder.pipeline.before((lunr as any).stemmer, tokenContextPluginCallback);
builder.metadataWhitelist.push('tokenContext');
}
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
import { shallow } from 'enzyme';
-import lunr, { LunrToken } from 'lunr';
+import lunr, { LunrBuilder, LunrToken } from 'lunr';
import * as React from 'react';
import { mockDocumentationEntry } from '../../../../helpers/testMocks';
import { getUrlsList } from '../../navTreeUtils';
getUrlsList: jest.fn().mockReturnValue([])
}));
-jest.mock('lunr', () =>
- jest.fn(() => ({
+jest.mock('lunr', () => {
+ const lunr = jest.fn(() => ({
search: jest.fn(() => [
{
ref: 'lorem/origin',
}
}
])
- }))
-);
+ }));
+
+ (lunr as any).Pipeline = {
+ registerFunction: jest.fn()
+ };
+
+ return lunr;
+});
it('should render correctly', () => {
expect(shallowRender()).toMatchSnapshot('default');
}
}
+ class LunrBuilderMock {
+ pipeline: { before: (stemmer: any, cb: Function) => void };
+ metadataWhitelist: string[];
+
+ constructor() {
+ this.pipeline = {
+ before: () => {
+ /* noop */
+ }
+ };
+ this.metadataWhitelist = [];
+ }
+ }
+
function mockLunrToken(str: string): LunrToken {
return new LunrTokenMock(str);
}
+ function mockLunrBuilder(): LunrBuilder {
+ return new LunrBuilderMock();
+ }
+
it('should correctly provide token context for text', () => {
const tokens = [
mockLunrToken('this'),
expect.objectContaining({ tokenContext: 'some text' })
);
});
+
+ it('should only register the plugin once', () => {
+ tokenContextPlugin(mockLunrBuilder());
+ tokenContextPlugin(mockLunrBuilder());
+ expect((lunr as any).Pipeline.registerFunction).toBeCalledTimes(1);
+ });
});
function shallowRender(props: Partial<SearchResults['props']> = {}) {