Skip to content

Commit

Permalink
add Algolia validateThemeConfig tests
Browse files Browse the repository at this point in the history
  • Loading branch information
slorber committed Oct 15, 2020
1 parent 303d208 commit c4abd5d
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,50 @@ describe('validateThemeConfig', () => {
testValidateThemeConfig({algolia}),
).toThrowErrorMatchingInlineSnapshot(`"\\"algolia.apiKey\\" is required"`);
});

test('contextualSearch config', () => {
const algolia = {
indexName: 'index',
apiKey: 'apiKey',
contextualSearch: true,
};
expect(testValidateThemeConfig({algolia})).toEqual({
algolia: {
...DEFAULT_CONFIG,
...algolia,
},
});
});

test('searchParameters.facetFilters search config', () => {
const algolia = {
indexName: 'index',
apiKey: 'apiKey',
searchParameters: {
facetFilters: ['version:1.0'],
},
};
expect(testValidateThemeConfig({algolia})).toEqual({
algolia: {
...DEFAULT_CONFIG,
...algolia,
},
});
});

test('contextualSearch + searchParameters.facetFilters config', () => {
const algolia = {
indexName: 'index',
apiKey: 'apiKey',
contextualSearch: true,
searchParameters: {
facetFilters: ['version:1.0'],
},
};
expect(() =>
testValidateThemeConfig({algolia}),
).toThrowErrorMatchingInlineSnapshot(
`"If you are using algolia.contextualSearch: true, you should not provide algolia.searchParameters.facetFilters, as it is computed for you dynamically"`,
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,28 @@
const Joi = require('@hapi/joi');

const DEFAULT_CONFIG = {
contextualSearch: false, // future: maybe we want to enable this by default

// By default, all Docusaurus sites are using the same AppId
// This has been designed on purpose with Algolia.
appId: 'BH4D9OD16A',

searchParameters: {},
};
exports.DEFAULT_CONFIG = DEFAULT_CONFIG;

const Schema = Joi.object({
algolia: Joi.object({
// Docusaurus attributes
contextualSearch: Joi.boolean().default(false),
contextualSearch: Joi.boolean().default(DEFAULT_CONFIG.contextualSearch),

// Algolia attributes
appId: Joi.string().default(DEFAULT_CONFIG.appId),
apiKey: Joi.string().required(),
indexName: Joi.string().required(),
searchParameters: Joi.object().default({}).unknown(),
searchParameters: Joi.object()
.default(DEFAULT_CONFIG.searchParameters)
.unknown(),
})
.label('themeConfig.algolia')
.required()
Expand Down

0 comments on commit c4abd5d

Please sign in to comment.