/
validateThemeConfig.test.js
135 lines (122 loc) · 3.15 KB
/
validateThemeConfig.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
const {validateThemeConfig, DEFAULT_CONFIG} = require('../validateThemeConfig');
function testValidateThemeConfig(themeConfig) {
function validate(schema, cfg) {
const {value, error} = schema.validate(cfg, {
convert: false,
});
if (error) {
throw error;
}
return value;
}
return validateThemeConfig({themeConfig, validate});
}
describe('validateThemeConfig', () => {
test('minimal config', () => {
const algolia = {
indexName: 'index',
apiKey: 'apiKey',
};
expect(testValidateThemeConfig({algolia})).toEqual({
algolia: {
...DEFAULT_CONFIG,
...algolia,
},
});
});
test('unknown attributes', () => {
const algolia = {
indexName: 'index',
apiKey: 'apiKey',
unknownKey: 'unknownKey',
};
expect(testValidateThemeConfig({algolia})).toEqual({
algolia: {
...DEFAULT_CONFIG,
...algolia,
},
});
});
test('undefined config', () => {
const algolia = undefined;
expect(() =>
testValidateThemeConfig({algolia}),
).toThrowErrorMatchingInlineSnapshot(
`"\\"themeConfig.algolia\\" is required"`,
);
});
test('undefined config 2', () => {
expect(() =>
testValidateThemeConfig({}),
).toThrowErrorMatchingInlineSnapshot(
`"\\"themeConfig.algolia\\" is required"`,
);
});
test('empty config', () => {
const algolia = {};
expect(() =>
testValidateThemeConfig({algolia}),
).toThrowErrorMatchingInlineSnapshot(`"\\"algolia.apiKey\\" is required"`);
});
test('missing indexName config', () => {
const algolia = {apiKey: 'apiKey'};
expect(() =>
testValidateThemeConfig({algolia}),
).toThrowErrorMatchingInlineSnapshot(
`"\\"algolia.indexName\\" is required"`,
);
});
test('missing apiKey config', () => {
const algolia = {indexName: 'indexName'};
expect(() =>
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('externalUrlRegex config', () => {
const algolia = {
indexName: 'index',
apiKey: 'apiKey',
externalUrlRegex: 'http://external-domain.com',
};
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,
},
});
});
});