hexo-component-inferno 3.1.2 → 4.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (144) hide show
  1. package/asset/js/algolia.js +51 -10
  2. package/asset/js/google_cse.js +50 -7
  3. package/asset/js/insight.js +48 -7
  4. package/lib/hexo/filter/locals.js +2 -2
  5. package/lib/schema/comment/valine.json +2 -2
  6. package/lib/schema/plugin/matomo.json +25 -0
  7. package/lib/schema/plugin/umami.json +25 -0
  8. package/lib/schema/search/algolia.json +4 -0
  9. package/lib/util/script.js +65 -0
  10. package/lib/view/comment/changyan.js +5 -5
  11. package/lib/view/comment/disqus.js +1 -1
  12. package/lib/view/comment/disqusjs.js +6 -5
  13. package/lib/view/comment/facebook.js +1 -1
  14. package/lib/view/comment/giscus.js +1 -1
  15. package/lib/view/comment/gitalk.js +6 -5
  16. package/lib/view/comment/gitment.js +6 -5
  17. package/lib/view/comment/isso.js +7 -3
  18. package/lib/view/comment/livere.js +1 -1
  19. package/lib/view/comment/twikoo.js +3 -4
  20. package/lib/view/comment/utterances.js +1 -1
  21. package/lib/view/comment/valine.js +3 -6
  22. package/lib/view/comment/waline.js +8 -7
  23. package/lib/view/common/article_media.js +2 -2
  24. package/lib/view/donate/afdian.js +1 -1
  25. package/lib/view/donate/alipay.js +1 -1
  26. package/lib/view/donate/buymeacoffee.js +1 -1
  27. package/lib/view/donate/patreon.js +1 -1
  28. package/lib/view/donate/paypal.js +1 -1
  29. package/lib/view/donate/wechat.js +1 -1
  30. package/lib/view/misc/article_licensing.js +1 -1
  31. package/lib/view/misc/meta.js +1 -1
  32. package/lib/view/misc/open_graph.js +1 -1
  33. package/lib/view/misc/paginator.js +9 -5
  34. package/lib/view/misc/structured_data.js +1 -1
  35. package/lib/view/misc/web_app.js +2 -2
  36. package/lib/view/plugin/baidu_analytics.js +1 -1
  37. package/lib/view/plugin/bing_webmaster.js +1 -1
  38. package/lib/view/plugin/busuanzi.js +1 -1
  39. package/lib/view/plugin/cnzz.js +1 -1
  40. package/lib/view/plugin/cookie_consent.js +1 -1
  41. package/lib/view/plugin/gallery.js +2 -2
  42. package/lib/view/plugin/google_analytics.js +1 -1
  43. package/lib/view/plugin/hotjar.js +1 -1
  44. package/lib/view/plugin/katex.js +1 -1
  45. package/lib/view/plugin/mathjax.js +1 -1
  46. package/lib/view/plugin/matomo.js +89 -0
  47. package/lib/view/plugin/outdated_browser.js +1 -1
  48. package/lib/view/plugin/progressbar.js +1 -1
  49. package/lib/view/plugin/statcounter.js +1 -1
  50. package/lib/view/plugin/twitter_conversion_tracking.js +1 -1
  51. package/lib/view/plugin/umami.js +88 -0
  52. package/lib/view/search/algolia.js +11 -5
  53. package/lib/view/search/baidu.js +5 -3
  54. package/lib/view/search/google_cse.js +4 -2
  55. package/lib/view/search/insight.js +1 -1
  56. package/lib/view/share/addthis.js +1 -1
  57. package/lib/view/share/addtoany.js +1 -1
  58. package/lib/view/share/bdshare.js +1 -1
  59. package/lib/view/share/sharejs.js +1 -1
  60. package/lib/view/share/sharethis.js +1 -1
  61. package/lib/view/widget/adsense.js +1 -1
  62. package/lib/view/widget/archives.js +23 -5
  63. package/lib/view/widget/categories.js +1 -1
  64. package/lib/view/widget/followit.js +1 -1
  65. package/lib/view/widget/links.js +1 -1
  66. package/lib/view/widget/recent_posts.js +1 -1
  67. package/lib/view/widget/subscribe_email.js +1 -1
  68. package/lib/view/widget/tags.js +1 -1
  69. package/lib/view/widget/toc.js +1 -1
  70. package/package.json +10 -5
  71. package/lib/core/migrate.test.js +0 -48
  72. package/lib/core/schema.test.js +0 -520
  73. package/lib/core/view.test.js +0 -41
  74. package/lib/hexo/filter/locals.test.js +0 -118
  75. package/lib/hexo/generator/assets.test.js +0 -32
  76. package/lib/hexo/generator/categories.test.js +0 -41
  77. package/lib/hexo/generator/category.test.js +0 -84
  78. package/lib/hexo/generator/insight.test.js +0 -95
  79. package/lib/hexo/generator/manifest.test.js +0 -52
  80. package/lib/hexo/generator/tags.test.js +0 -41
  81. package/lib/hexo/helper/cdn.test.js +0 -128
  82. package/lib/hexo/helper/page.test.js +0 -62
  83. package/lib/hexo/tag/message.test.js +0 -72
  84. package/lib/hexo/tag/tabs.test.js +0 -98
  85. package/lib/schema/schema/comment/changyan.json +0 -21
  86. package/lib/schema/schema/comment/disqus.json +0 -17
  87. package/lib/schema/schema/comment/disqusjs.json +0 -53
  88. package/lib/schema/schema/comment/facebook.json +0 -13
  89. package/lib/schema/schema/comment/giscus.json +0 -105
  90. package/lib/schema/schema/comment/gitalk.json +0 -81
  91. package/lib/schema/schema/comment/gitment.json +0 -47
  92. package/lib/schema/schema/comment/isso.json +0 -17
  93. package/lib/schema/schema/comment/livere.json +0 -17
  94. package/lib/schema/schema/comment/twikoo.json +0 -28
  95. package/lib/schema/schema/comment/utterances.json +0 -59
  96. package/lib/schema/schema/comment/valine.json +0 -105
  97. package/lib/schema/schema/comment/waline.json +0 -176
  98. package/lib/schema/schema/donate/afdian.json +0 -17
  99. package/lib/schema/schema/donate/alipay.json +0 -17
  100. package/lib/schema/schema/donate/buymeacoffee.json +0 -17
  101. package/lib/schema/schema/donate/patreon.json +0 -17
  102. package/lib/schema/schema/donate/paypal.json +0 -22
  103. package/lib/schema/schema/donate/wechat.json +0 -17
  104. package/lib/schema/schema/misc/manifest.json +0 -66
  105. package/lib/schema/schema/misc/meta.json +0 -11
  106. package/lib/schema/schema/misc/open_graph.json +0 -78
  107. package/lib/schema/schema/misc/poly_links.json +0 -35
  108. package/lib/schema/schema/misc/structured_data.json +0 -47
  109. package/lib/schema/schema/plugin/baidu_analytics.json +0 -14
  110. package/lib/schema/schema/plugin/bing_webmaster.json +0 -14
  111. package/lib/schema/schema/plugin/busuanzi.json +0 -7
  112. package/lib/schema/schema/plugin/cnzz.json +0 -19
  113. package/lib/schema/schema/plugin/cookie_consent.json +0 -38
  114. package/lib/schema/schema/plugin/gallery.json +0 -7
  115. package/lib/schema/schema/plugin/google_analytics.json +0 -14
  116. package/lib/schema/schema/plugin/hotjar.json +0 -14
  117. package/lib/schema/schema/plugin/katex.json +0 -7
  118. package/lib/schema/schema/plugin/mathjax.json +0 -7
  119. package/lib/schema/schema/plugin/outdated_browser.json +0 -7
  120. package/lib/schema/schema/plugin/progressbar.json +0 -7
  121. package/lib/schema/schema/plugin/statcounter.json +0 -19
  122. package/lib/schema/schema/plugin/twitter_conversion_tracking.json +0 -14
  123. package/lib/schema/schema/search/algolia.json +0 -13
  124. package/lib/schema/schema/search/baidu.json +0 -13
  125. package/lib/schema/schema/search/google_cse.json +0 -17
  126. package/lib/schema/schema/search/insight.json +0 -19
  127. package/lib/schema/schema/share/addthis.json +0 -17
  128. package/lib/schema/schema/share/addtoany.json +0 -13
  129. package/lib/schema/schema/share/bdshare.json +0 -13
  130. package/lib/schema/schema/share/sharejs.json +0 -13
  131. package/lib/schema/schema/share/sharethis.json +0 -17
  132. package/lib/schema/schema/widget/adsense.json +0 -21
  133. package/lib/schema/schema/widget/archives.json +0 -13
  134. package/lib/schema/schema/widget/categories.json +0 -13
  135. package/lib/schema/schema/widget/followit.json +0 -26
  136. package/lib/schema/schema/widget/links.json +0 -47
  137. package/lib/schema/schema/widget/recent_posts.json +0 -13
  138. package/lib/schema/schema/widget/subscribe_email.json +0 -22
  139. package/lib/schema/schema/widget/tags.json +0 -30
  140. package/lib/schema/schema/widget/toc.json +0 -31
  141. package/lib/util/cache.test.js +0 -24
  142. package/lib/util/classname.test.js +0 -13
  143. package/lib/util/yaml.test.js +0 -18
  144. package/lib/view/widget/toc.test.js +0 -30
@@ -1,520 +0,0 @@
1
- const {
2
- MAGIC,
3
- DefaultValue,
4
- Schema,
5
- SchemaLoader
6
- } = require('./schema');
7
- const PRIMARY_INPUTS = [null, false, true, 0, 1, '', '1'];
8
- describe('DefaultValue class tests', () => {
9
- describe('#clone()', () => {
10
- test('Clone DefaultValue', () => {
11
- PRIMARY_INPUTS.forEach(val => {
12
- const result = new DefaultValue(val, 'source').clone();
13
- expect(result.description).toBe('source');
14
- expect(result.value).toBe(val);
15
- });
16
- let source = new DefaultValue([1, [2]], 'source');
17
- let result = source.clone();
18
- expect(result.value).not.toBe(source.value);
19
- expect(result.value).toEqual(source.value);
20
- expect(result.description).toBe(source.description);
21
- source = new DefaultValue({
22
- a: 1,
23
- b: {
24
- c: 2
25
- }
26
- }, 'source');
27
- result = source.clone();
28
- expect(result.value).not.toBe(source.value);
29
- expect(result.value).toEqual(source.value);
30
- expect(result.description).toBe(source.description);
31
- });
32
- test('Clone nested DefaultValue', () => {
33
- const source = new DefaultValue(new DefaultValue({
34
- a: 1
35
- }, 'child'), 'parent');
36
- const result = source.clone();
37
- expect(result.value instanceof DefaultValue).toBe(true);
38
- expect(result.value.value).not.toBe(source.value.value);
39
- expect(result.value.value).toEqual(source.value.value);
40
- expect(result.description).toBe(source.description);
41
- expect(result.value.description).toBe(source.value.description);
42
- });
43
- });
44
- describe('#flatten()', () => {
45
- test('Flatten nested DefaultValue', () => {
46
- const source = new DefaultValue(new DefaultValue({
47
- a: 1
48
- }, 'child'), 'parent');
49
- source.flatten();
50
- expect(source.description).toBe('child');
51
- expect(source.value).toEqual({
52
- a: 1
53
- });
54
- });
55
- test("Don't flatten DefaultValue is its value is not a DefaultValue", () => {
56
- const source = new DefaultValue({
57
- value: new DefaultValue({
58
- a: 1
59
- }, 'child')
60
- }, 'parent');
61
- source.flatten();
62
- expect(source.description).toBe('parent');
63
- expect(source.value.value.description).toBe('child');
64
- expect(source.value.value.value).toEqual({
65
- a: 1
66
- });
67
- });
68
- });
69
- describe('#merge()', () => {
70
- test('Merge descriptions', () => {
71
- const target = new DefaultValue(true, 'val1');
72
- const source = new DefaultValue(true, 'val2');
73
- target.merge(source);
74
- expect(target.description).toBe('val2');
75
- source.description = '';
76
- target.merge(source);
77
- expect(target.description).toBe('val2');
78
- delete source.description;
79
- target.merge(source);
80
- expect(target.description).toBe('val2');
81
- });
82
- test('Merge another primary default value', () => {
83
- PRIMARY_INPUTS.forEach(val1 => {
84
- PRIMARY_INPUTS.forEach(val2 => {
85
- const target = new DefaultValue(val1);
86
- const source = new DefaultValue(val2);
87
- target.merge(source);
88
- if (typeof val1 === typeof val2 || val1 === null) {
89
- expect(target.value).toBe(val2);
90
- } else {
91
- expect(target.value).toBe(val1);
92
- }
93
- });
94
- });
95
- });
96
- test('Merge arrays', () => {
97
- const target = new DefaultValue([1, ['2']]);
98
- const source = new DefaultValue(['3', [4]]);
99
- target.merge(source);
100
- expect(target.value).toEqual([1, ['2'], '3', [4]]);
101
- });
102
- test('Merge objects', () => {
103
- const target = new DefaultValue({
104
- a: 1,
105
- b: 2
106
- });
107
- const source = new DefaultValue({
108
- c: 3
109
- });
110
- target.merge(source);
111
- expect(target.value).toEqual({
112
- a: 1,
113
- b: 2,
114
- c: 3
115
- });
116
- source.value = {
117
- a: 4,
118
- b: {
119
- d: 5
120
- }
121
- };
122
- target.merge(source);
123
- expect(target.value).toEqual({
124
- a: 4,
125
- b: {
126
- d: 5
127
- },
128
- c: 3
129
- });
130
- });
131
- test('Recursive merge if .value is a DefaultValue', () => {
132
- const target = new DefaultValue(new DefaultValue({
133
- a: 1
134
- }));
135
- const source = new DefaultValue({
136
- b: 2
137
- });
138
- target.merge(source);
139
- expect(target.value).toEqual({
140
- a: 1,
141
- b: 2
142
- });
143
- source.value = new DefaultValue({
144
- c: 3
145
- }, 'child');
146
- target.merge(source);
147
- expect(target.description).toEqual('child');
148
- expect(target.value).toEqual({
149
- a: 1,
150
- b: 2,
151
- c: 3
152
- });
153
- target.value = {
154
- a: 1
155
- };
156
- target.merge(source);
157
- expect(target.value).toEqual({
158
- a: 1,
159
- c: 3
160
- });
161
- });
162
- });
163
- describe('#toCommented()', () => {
164
- test('Primary DefaultValue', () => {
165
- PRIMARY_INPUTS.forEach(val => {
166
- const source = new DefaultValue(val, 'source');
167
- expect(source.toCommented()).toBe(val);
168
- });
169
- });
170
- test('Nested DefaultValue', () => {
171
- const source = new DefaultValue(new DefaultValue(1, 'child'), 'parent');
172
- expect(source.toCommented()).toBe(1);
173
- });
174
- test('#toCommentedArray()', () => {
175
- const source = new DefaultValue([0, new DefaultValue(1, 'child-1'), 2, new DefaultValue(3, 'child-3\nchild-4'), new DefaultValue(4)], 'parent');
176
- expect(source.toCommented()).toEqual(source.toCommentedArray());
177
- expect(source.toCommented()).toEqual([0, MAGIC + '0: child-1', 1, 2, MAGIC + '0: child-3', MAGIC + '1: child-4', 3, 4]);
178
- });
179
- test('#toCommentedObject()', () => {
180
- const source = new DefaultValue({
181
- a: 0,
182
- b: new DefaultValue(1, 'child-1'),
183
- c: 2,
184
- d: new DefaultValue(3, 'child-3\nchild-4')
185
- }, 'parent');
186
- expect(source.toCommented()).toEqual(source.toCommentedObject());
187
- expect(source.toCommented()).toEqual({
188
- a: 0,
189
- [MAGIC + 'b0']: 'child-1',
190
- b: 1,
191
- c: 2,
192
- [MAGIC + 'd0']: 'child-3',
193
- [MAGIC + 'd1']: 'child-4',
194
- d: 3
195
- });
196
- });
197
- test('Complex cases', () => {
198
- let source = new DefaultValue({
199
- a: [0, new DefaultValue(1, 'child-1')]
200
- }, 'parent');
201
- expect(source.toCommented()).toEqual({
202
- a: [0, MAGIC + '0: child-1', 1]
203
- });
204
- source = new DefaultValue([0, {
205
- a: new DefaultValue(1, 'child-1')
206
- }], 'parent');
207
- expect(source.toCommented()).toEqual([0, {
208
- [MAGIC + 'a0']: 'child-1',
209
- a: 1
210
- }]);
211
- });
212
- });
213
- describe('#toYaml()', () => {
214
- const source = new DefaultValue({
215
- a: new DefaultValue(1, 'child-1'),
216
- b: new DefaultValue(2, 'child-2\nchild-3'),
217
- c: 0,
218
- d: [0, new DefaultValue(3, 'child-4')],
219
- e: {
220
- f: 4,
221
- g: new DefaultValue(5, 'child-5')
222
- },
223
- h: new DefaultValue(1)
224
- }, 'parent');
225
- expect(source.toYaml()).toEqual('# child-1\na: 1\n# child-2\n# child-3\nb: 2\nc: 0\nd:\n - 0' + '\n # child-4\n - 3\ne:\n f: 4\n # child-5\n g: 5\nh: 1\n');
226
- });
227
- });
228
- describe('SchemaLoader class tests', () => {
229
- const loader = new SchemaLoader();
230
- const definitions = [require('../../fixture/schema/root'), require('../../fixture/schema/parent'), require('../../fixture/schema/child1'), require('../../fixture/schema/child2')];
231
- test('#addSchema()', () => {
232
- expect(Object.prototype.hasOwnProperty.call(loader.schemas, '')).toBe(false);
233
- definitions.forEach(loader.addSchema.bind(loader));
234
- definitions.forEach(def => {
235
- expect(Object.prototype.hasOwnProperty.call(loader.schemas, def.$id)).toBe(true);
236
- });
237
- expect(Object.keys(loader.schemas).length).toBe(definitions.length);
238
- expect(() => {
239
- loader.addSchema(definitions[0]);
240
- }).toThrow();
241
- expect(() => {
242
- loader.addSchema({});
243
- }).toThrow();
244
- });
245
- test('#removeSchema()', () => {
246
- loader.removeSchema(definitions[0].$id);
247
- expect(Object.keys(loader.schemas).length).toBe(definitions.length - 1);
248
- expect(Object.prototype.hasOwnProperty.call(loader, definitions[0].$id)).toBe(false);
249
- loader.removeSchema(definitions[definitions.length - 1].$id);
250
- expect(Object.keys(loader.schemas).length).toBe(definitions.length - 2);
251
- expect(Object.prototype.hasOwnProperty.call(loader, definitions[definitions.length - 1].$id)).toBe(false);
252
- loader.addSchema(definitions[0]);
253
- loader.addSchema(definitions[definitions.length - 1]);
254
- });
255
- test('#getSchema()', () => {
256
- expect(loader.getSchema(definitions[0].$id) instanceof Schema).toBe(true);
257
- expect(loader.getSchema(definitions[0].$id).def).toBe(definitions[0]);
258
- expect(loader.getSchema('')).toBeUndefined();
259
- });
260
- test('#compileValidator()', () => {
261
- const validate = loader.compileValidator(definitions[0].$id);
262
- expect(typeof validate).toBe('function');
263
- expect(validate({
264
- parent: {
265
- prop1: ''
266
- }
267
- })).toBe(false);
268
- expect(validate.errors).toBeDefined();
269
- expect(validate.errors).not.toBeNull();
270
- expect(validate({
271
- parent: []
272
- })).toBe(true);
273
- expect(validate.errors).toBeNull();
274
- });
275
- test('SchemaLoader.load()', () => {
276
- let loader = SchemaLoader.load(definitions[0], '../../fixture/schema');
277
- definitions.forEach(def => expect(loader.getSchema(def.$id) instanceof Schema).toBe(true));
278
- definitions.forEach(def => expect(loader.getSchema(def.$id).def).toBe(def));
279
- loader = SchemaLoader.load({
280
- $id: '/root.json',
281
- type: 'object',
282
- properties: {
283
- ref: {
284
- $ref: '/comment/disqus.json'
285
- }
286
- }
287
- });
288
- expect(Object.keys(loader.schemas).length).toBe(2);
289
- expect(loader.getSchema('/comment/disqus.json').def).toEqual(require('../schema/comment/disqus.json'));
290
- expect(() => {
291
- return SchemaLoader.load({
292
- $id: '/root.json',
293
- type: 'object',
294
- properties: {
295
- ref: {
296
- $ref: '/unknown.json'
297
- }
298
- }
299
- });
300
- }).toThrow();
301
- });
302
- });
303
- describe('Schema class tests', () => {
304
- const loader = new SchemaLoader();
305
- const definitions = [require('../../fixture/schema/root'), require('../../fixture/schema/parent'), require('../../fixture/schema/child1'), require('../../fixture/schema/child2')];
306
- definitions.forEach(loader.addSchema.bind(loader));
307
- test('.constructor()', () => {
308
- expect(() => {
309
- return new Schema();
310
- }).toThrow();
311
- expect(() => {
312
- return new Schema(loader);
313
- }).toThrow();
314
- });
315
- test('#validate()', () => {
316
- const schema = new Schema(loader, definitions[2]);
317
- expect(schema.validate({})).not.toBe(true);
318
- expect(schema.validate({
319
- prop1: 'prop1',
320
- prop2: 'prop2'
321
- })).toBe(true);
322
- });
323
- test('#getArrayDefaultValue()', () => {
324
- const schema = new Schema(loader, {
325
- $id: '/example.json',
326
- type: 'array',
327
- items: {
328
- type: 'string',
329
- description: 'array element'
330
- }
331
- });
332
- let defaultValue = schema.getArrayDefaultValue(schema.def);
333
- expect(Array.isArray(defaultValue.value)).toBe(true);
334
- expect(defaultValue.value.length).toBe(1);
335
- expect(defaultValue.value[0] instanceof DefaultValue).toBe(true);
336
- expect(defaultValue.value[0].value).toBe('');
337
- expect(defaultValue.value[0].description).toBe('array element');
338
- schema.def.items.oneOf = [{
339
- type: 'string',
340
- const: 'hello'
341
- }, {
342
- type: 'string',
343
- default: 'world',
344
- description: 'world element'
345
- }];
346
- defaultValue = schema.getArrayDefaultValue(schema.def);
347
- expect(defaultValue.value.length).toBe(2);
348
- expect(defaultValue.value[0].value).toBe('hello');
349
- expect(defaultValue.value[1].value).toBe('world');
350
- expect(defaultValue.value[0].description).toBe('array element');
351
- expect(defaultValue.value[1].description).toBe('world element');
352
- });
353
- test('#getObjectDefaultValue()', () => {
354
- const schema = new Schema(loader, {
355
- $id: '/example.json',
356
- type: 'object',
357
- properties: {
358
- prop1: {
359
- type: 'string',
360
- description: 'prop1 description'
361
- },
362
- prop2: {
363
- type: 'number',
364
- description: 'prop2 description',
365
- default: 1
366
- }
367
- }
368
- });
369
- let defaultValue = schema.getObjectDefaultValue(schema.def);
370
- expect(defaultValue.value.prop1 instanceof DefaultValue).toBe(true);
371
- expect(defaultValue.value.prop2 instanceof DefaultValue).toBe(true);
372
- expect(defaultValue.value.prop1.value).toBe('');
373
- expect(defaultValue.value.prop2.value).toBe(1);
374
- expect(defaultValue.value.prop1.description).toBe('prop1 description');
375
- expect(defaultValue.value.prop2.description).toBe('prop2 description');
376
- schema.def.oneOf = [{
377
- type: 'object',
378
- properties: {
379
- prop3: {
380
- type: 'boolean',
381
- description: 'prop3 description'
382
- }
383
- }
384
- }, {
385
- type: 'object',
386
- properties: {
387
- prop4: {
388
- type: 'string'
389
- }
390
- }
391
- }];
392
- defaultValue = schema.getObjectDefaultValue(schema.def);
393
- expect(defaultValue.value.prop3 instanceof DefaultValue).toBe(true);
394
- expect(defaultValue.value.prop3.value).toBe(false);
395
- expect(defaultValue.value.prop3.description).toBe('prop3 description');
396
- expect(Object.prototype.hasOwnProperty.call(defaultValue.value, 'prop4')).toBe(false);
397
- });
398
- test('#getReferredDefaultValue', () => {
399
- const schema = new Schema(loader, {
400
- $id: '/example.json',
401
- description: 'example schema',
402
- $ref: '/child1.json'
403
- });
404
- const defaultValue = schema.getReferredDefaultValue(schema.def);
405
- expect(defaultValue.description).toBe('example schema');
406
- expect(defaultValue.value.prop1 instanceof DefaultValue).toBe(true);
407
- expect(defaultValue.value.prop2 instanceof DefaultValue).toBe(true);
408
- expect(defaultValue.value.prop1.value).toBe('prop1');
409
- expect(defaultValue.value.prop2.value).toBe('');
410
- schema.def.$ref = '/unknown.json';
411
- expect(() => {
412
- return schema.getReferredDefaultValue({});
413
- }).toThrow();
414
- });
415
- describe('#getTypedDefaultValue()', () => {
416
- test('Primitives types', () => {
417
- const types = ['null', 'boolean', 'number', 'integer', 'string'];
418
- const defaultValues = [null, false, 0, 0, ''];
419
- types.forEach((type, i) => {
420
- [true, false].forEach(nullable => {
421
- const schema = new Schema(loader, {
422
- $id: '/example.json',
423
- type: type,
424
- nullable: nullable
425
- });
426
- let defaultValue = schema.getTypedDefaultValue(schema.def);
427
- if (nullable) {
428
- expect(defaultValue.value).toBe(null);
429
- } else {
430
- expect(defaultValue.value).toBe(defaultValues[i]);
431
- }
432
-
433
- // ref schema does not override default value of primitive types
434
- schema.$ref = '/child1.json';
435
- defaultValue = schema.getTypedDefaultValue(schema.def);
436
- if (nullable) {
437
- expect(defaultValue.value).toBe(null);
438
- } else {
439
- expect(defaultValue.value).toBe(defaultValues[i]);
440
- }
441
- });
442
- });
443
- const schema = new Schema(loader, {
444
- $id: '/example.json',
445
- type: 'unknown'
446
- });
447
- expect(() => {
448
- return schema.getTypedDefaultValue(schema.def);
449
- }).toThrow();
450
- });
451
- test('With $ref', () => {
452
- let schema = new Schema(loader, {
453
- $id: '/example.json',
454
- type: 'object',
455
- description: 'example schema',
456
- properties: {
457
- prop2: {
458
- type: 'string',
459
- const: 'hello'
460
- },
461
- prop3: {
462
- type: 'string',
463
- const: 'world'
464
- }
465
- },
466
- $ref: '/child1.json'
467
- });
468
- let defaultValue = schema.getTypedDefaultValue(schema.def);
469
- expect(defaultValue.description).toBe('example schema');
470
- expect(defaultValue.value.prop1 instanceof DefaultValue).toBe(true);
471
- expect(defaultValue.value.prop2 instanceof DefaultValue).toBe(true);
472
- expect(defaultValue.value.prop3 instanceof DefaultValue).toBe(true);
473
- expect(defaultValue.value.prop1.value).toBe('prop1');
474
- expect(defaultValue.value.prop2.value).toBe('hello');
475
- expect(defaultValue.value.prop3.value).toBe('world');
476
- schema = new Schema(loader, {
477
- $id: '/example.json',
478
- type: 'array',
479
- description: 'example schema',
480
- items: {
481
- type: 'number',
482
- const: 1
483
- },
484
- $ref: '/child2.json'
485
- });
486
- defaultValue = schema.getTypedDefaultValue(schema.def);
487
- expect(defaultValue.description).toBe('example schema');
488
- expect(Array.isArray(defaultValue.value)).toBe(true);
489
- expect(defaultValue.value.length).toBe(2);
490
- expect(defaultValue.value[0] instanceof DefaultValue).toBe(true);
491
- expect(defaultValue.value[1] instanceof DefaultValue).toBe(true);
492
- expect(defaultValue.value[0].value).toBe('');
493
- expect(defaultValue.value[1].value).toBe(1);
494
- });
495
- });
496
- test('#getDefaultValue()', () => {
497
- const schema = new Schema(loader, {
498
- $id: '/example.json',
499
- type: 'null'
500
- });
501
- schema.def.const = 'hello world';
502
- expect(schema.getDefaultValue().value).toBe('hello world');
503
- delete schema.def.const;
504
- schema.def.default = 'world hello';
505
- expect(schema.getDefaultValue(schema.def).value).toBe('world hello');
506
- delete schema.def.default;
507
- schema.def.examples = ['hello', 'world'];
508
- expect(schema.getDefaultValue(schema.def).value).toBe('hello');
509
- delete schema.def.examples;
510
- schema.def.type = 'string';
511
- expect(schema.getDefaultValue(schema.def).value).toBe('');
512
- delete schema.def.type;
513
- schema.def.$ref = '/child1.json';
514
- expect(schema.getDefaultValue(schema.def).value.prop1.value).toBe('prop1');
515
- delete schema.def.$ref;
516
- expect(() => {
517
- return schema.getDefaultValue(schema.def);
518
- }).toThrow();
519
- });
520
- });
@@ -1,41 +0,0 @@
1
- const path = require('path');
2
- const view = require('./view');
3
- const DisqusLibraryView = require('../view/comment/disqus');
4
- const OpenGraphLibraryView = require('../view/misc/open_graph');
5
- const themeDir = path.join(__dirname, '../../fixture');
6
- const ExampleFixtureView = require(themeDir + '/layout/example');
7
- const DisqusFixtureView = require(themeDir + '/layout/comment/disqus');
8
- beforeAll(() => {
9
- view.init({
10
- theme_dir: themeDir
11
- });
12
- });
13
- describe('Correctly resolve path of the view', () => {
14
- test('when theme and this library both have the view file', () => {
15
- expect(view.require.resolve('comment/disqus')).toBe(path.join(themeDir, 'layout/comment/disqus.jsx'));
16
- });
17
- test('when only this library has the view file', () => {
18
- expect(view.require.resolve('misc/open_graph')).toBe(path.join(__dirname, '../view/misc/open_graph.js'));
19
- });
20
- test('when only the theme has the view file', () => {
21
- expect(view.require.resolve('example')).toBe(path.join(themeDir, 'layout/example.jsx'));
22
- });
23
- test("don' resolve absolute path", () => {
24
- const absPath = path.join(__dirname, '../view/comment/disqus.js');
25
- expect(view.require.resolve(absPath)).toBe(absPath);
26
- });
27
- });
28
- describe('Correctly require() the view', () => {
29
- test('when theme and this library both have the view file', () => {
30
- expect(view.require('comment/disqus')).toBe(DisqusFixtureView);
31
- });
32
- test('when only this library has the view file', () => {
33
- expect(view.require('misc/open_graph')).toBe(OpenGraphLibraryView);
34
- });
35
- test('when only the theme has the view file', () => {
36
- expect(view.require('example')).toBe(ExampleFixtureView);
37
- });
38
- test("don' resolve absolute path", () => {
39
- expect(view.require(path.join(__dirname, '../view/comment/disqus'))).toBe(DisqusLibraryView);
40
- });
41
- });
@@ -1,118 +0,0 @@
1
- const fs = require('fs');
2
- const util = require('util');
3
- const path = require('path');
4
- const yaml = require('js-yaml');
5
- const Hexo = require('hexo');
6
- const register = require('./locals');
7
- const rootDir = path.join(__dirname, '../../../fixture/site');
8
- const hexo = new Hexo(rootDir, {
9
- silent: true
10
- });
11
- const regex = new RegExp(/\[Function: bound (.*)\]/gi);
12
- let siteConfig, themeConfig, pageConfigInSiteDir, postConfigInSiteDir, pageConfigInThemeDir, postConfigInThemeDir;
13
- const basePageConfig = {
14
- title: 'about',
15
- comments: false,
16
- int: 10000,
17
- float: 0.0001,
18
- bool: true,
19
- str: '10000',
20
- empty: null,
21
- arr: [10000, 20000, 30000],
22
- obj: {
23
- field: 10000
24
- },
25
- int_ex: 10000,
26
- float_ex: 0.0001,
27
- bool_ex: true,
28
- str_ex: '10000',
29
- empty_ex: null,
30
- arr_ex: [10000, 20000, 30000],
31
- obj_ex: {
32
- field: 10000
33
- }
34
- };
35
- beforeAll(async () => {
36
- await hexo.init();
37
- await hexo.load();
38
- siteConfig = yaml.load(fs.readFileSync(path.join(hexo.base_dir, '_config.yml')));
39
- themeConfig = yaml.load(fs.readFileSync(path.join(hexo.theme_dir, '_config.yml')));
40
- pageConfigInSiteDir = yaml.load(fs.readFileSync(path.join(hexo.base_dir, '_config.page.yml')));
41
- postConfigInSiteDir = yaml.load(fs.readFileSync(path.join(hexo.base_dir, '_config.post.yml')));
42
- pageConfigInThemeDir = yaml.load(fs.readFileSync(path.join(hexo.theme_dir, '_config.page.yml')));
43
- postConfigInThemeDir = yaml.load(fs.readFileSync(path.join(hexo.theme_dir, '_config.post.yml')));
44
- register(hexo);
45
- });
46
- test('Register all helper functions to "locals.helper"', async () => {
47
- const Locals = hexo._generateLocals();
48
- const helpers = hexo.extend.helper.list();
49
- const locals = await hexo.extend.filter.exec('template_locals', new Locals('about/index.html'), {
50
- context: hexo
51
- });
52
- expect(Object.keys(locals.helper).sort()).toEqual(Object.keys(helpers).concat('__', '_p').sort());
53
- for (const name in locals.helper) {
54
- expect(typeof locals.helper[name]).toBe('function');
55
- const match = regex.exec(util.inspect(locals.helper[name]));
56
- if (match && match.length > 1) {
57
- expect(match[1]).toBe(helpers[name].name);
58
- }
59
- }
60
- });
61
- test('Correctly merge theme config with site config', async () => {
62
- const Locals = hexo._generateLocals();
63
- const locals = await hexo.extend.filter.exec('template_locals', new Locals('about/index.html'), {
64
- context: hexo
65
- });
66
- expect(locals.config.title).toBe(themeConfig.title);
67
- expect(locals.config.int).toBe(themeConfig.int);
68
- expect(locals.config.float).toBe(themeConfig.float);
69
- expect(locals.config.bool).toBe(themeConfig.bool);
70
- expect(locals.config.str).toBe(themeConfig.str);
71
- expect(locals.config.empty).toBe(themeConfig.empty);
72
- expect(locals.config.arr).toEqual(themeConfig.arr);
73
- expect(locals.config.obj).toEqual(themeConfig.obj);
74
- expect(locals.config.theme).toEqual(siteConfig.theme);
75
- });
76
- test('Page/post layout configs override theme and site configs', async () => {
77
- const Locals = hexo._generateLocals();
78
- const configs = [Object.assign({}, basePageConfig, {
79
- layout: 'page'
80
- }), Object.assign({}, basePageConfig, {
81
- layout: 'post'
82
- })];
83
- for (const page of configs) {
84
- const locals = await hexo.extend.filter.exec('template_locals', new Locals('about/index.html', page), {
85
- context: hexo
86
- });
87
- expect(locals.config.comments).toBeUndefined();
88
- // theme page/post layout configs override ones under site root
89
- const keys = ['title', 'int', 'float', 'bool', 'str', 'empty', 'arr', 'obj'];
90
- for (const key of keys) {
91
- expect(locals.config[key]).toEqual(page.layout === 'page' ? pageConfigInThemeDir[key] : postConfigInThemeDir[key]);
92
- }
93
- const keysEx = ['title_ex', 'int_ex', 'float_ex', 'bool_ex', 'str_ex', 'empty_ex', 'arr_ex', 'obj_ex'];
94
- for (const key of keysEx) {
95
- expect(locals.config[key]).toEqual(page.layout === 'page' ? pageConfigInSiteDir[key] : postConfigInSiteDir[key]);
96
- }
97
- }
98
- });
99
- test('Configs in page/post front-matter override all other config sources', async () => {
100
- const Locals = hexo._generateLocals();
101
- const configs = [Object.assign({}, basePageConfig, {
102
- __page: true,
103
- layout: 'page'
104
- }), Object.assign({}, basePageConfig, {
105
- __post: true,
106
- layout: 'post'
107
- })];
108
- for (const page of configs) {
109
- const locals = await hexo.extend.filter.exec('template_locals', new Locals('about/index.html', page), {
110
- context: hexo
111
- });
112
- expect(locals.config.title).toBe(page.layout === 'page' ? pageConfigInThemeDir.title : postConfigInThemeDir.title);
113
- expect(locals.config.comments).toBeUndefined();
114
- ['int', 'float', 'bool', 'str', 'empty', 'arr', 'obj', 'int_ex', 'float_ex', 'bool_ex', 'str_ex', 'empty_ex', 'arr_ex', 'obj_ex'].forEach(key => {
115
- expect(locals.config[key]).toEqual(page[key]);
116
- });
117
- }
118
- });