UNPKG

1.87 kBJavaScriptView Raw
1'use strict';
2
3const _ = require('lodash'),
4 lib = require('./deep-reduce'),
5 refProp = '_ref',
6 ref = 'domain.com/_components/foo',
7 component = {
8 [refProp]: ref,
9 a: 'b'
10 },
11 compose = (obj) => (str, data) => obj[str] = data;
12
13describe('deep reduce', () => {
14 it('calls fn when it finds a component', () => {
15 let obj = {};
16
17 _.reduce([component], (result, val) => lib(result, val, compose(obj)), {});
18 expect(obj).toEqual({ [ref]: component });
19 });
20
21 it('does not call fn on non-component refs', () => {
22 let obj = {};
23
24 _.reduce([{ [refProp]: 'domain.com/_pages/foo' }], (result, val) => lib(result, val, compose(obj)), {});
25 expect(obj).toEqual({});
26 });
27
28 it('does not call fn on ignored keys', () => {
29 let obj = {};
30
31 _.reduce([{ locals: component }], (result, val) => lib(result, val, compose(obj)), {});
32 expect(obj).toEqual({});
33 });
34
35 // note: keys beginning with underscores are metadata, e.g. _layoutRef, _components
36 it('does not call fn on keys beginning with underscores', () => {
37 let obj = {};
38
39 _.reduce([{ _components: component }], (result, val) => lib(result, val, compose(obj)), {});
40 expect(obj).toEqual({});
41 });
42
43 it('calls fn on keys containing underscores (not beginning)', () => {
44 let obj = {};
45
46 _.reduce([{ cool_components: component }], (result, val) => lib(result, val, compose(obj)), {});
47 expect(obj).toEqual({ [ref]: component });
48 });
49
50 it('recursively calls itself on objects', () => {
51 let obj = {};
52
53 _.reduce([{ a: component }], (result, val) => lib(result, val, compose(obj)), {});
54 expect(obj).toEqual({ [ref]: component });
55 });
56
57 it('recursively calls itself on arrays', () => {
58 let obj = {};
59
60 _.reduce([[component]], (result, val) => lib(result, val, compose(obj)), {});
61 expect(obj).toEqual({ [ref]: component });
62 });
63});