UNPKG

4.53 kBJavaScriptView Raw
1'use strict';
2
3const lib = require('./composer');
4
5describe('normalize component data', () => {
6 describe('normalize', () => {
7 const fn = lib.normalize;
8
9 it('cleans component lists', () => {
10 expect(fn({ a: [{
11 _ref: 'foo',
12 b: 'c'
13 }] })).toEqual({ a: [{ _ref: 'foo' }]});
14 });
15
16 it('passes through other arrays', () => {
17 const data = { a: ['b', 'c'] };
18
19 expect(fn(data)).toEqual(data);
20 });
21
22 it('cleans component props', () => {
23 expect(fn({ a: {
24 _ref: 'foo',
25 b: 'c'
26 } })).toEqual({ a: { _ref: 'foo' }});
27 });
28
29 it('passes through other objects', () => {
30 const data = { a: { b: true, c: true } };
31
32 expect(fn(data)).toEqual(data);
33 });
34
35 it('passes through other data', () => {
36 const data = {
37 a: 'some string',
38 b: false,
39 c: 0,
40 d: null,
41 e: '',
42 f: [],
43 g: {}
44 };
45
46 expect(fn(data)).toEqual(data);
47 });
48
49 it('removes root-level _ref', () => {
50 expect(fn({ _ref: 'foo', a: 'b' })).toEqual({ a: 'b' });
51 });
52 });
53
54 describe('denormalize', () => {
55 const fn = lib.denormalize,
56 bootstrap = {
57 _components: {
58 a: {
59 child: [{
60 _ref: '/_components/b'
61 }],
62 instances: {
63 1: {
64 child: [{
65 _ref: '/_components/b/instances/1'
66 }]
67 }
68 }
69 },
70 b: {
71 c: 'd',
72 instances: {
73 1: {
74 c: 'd'
75 }
76 }
77 }
78 }
79 },
80 bootstrapDeep = {
81 _components: {
82 a: {
83 instances: {
84 1: {
85 child: [{
86 _ref: '/_components/b/instances/1'
87 }]
88 }
89 }
90 },
91 b: {
92 instances: {
93 1: {
94 child: {
95 _ref: '/_components/c/instances/1'
96 }
97 }
98 }
99 },
100 c: {
101 instances: {
102 1: {
103 d: 'e'
104 }
105 }
106 }
107 }
108 };
109
110 it('skips empty refs if not in the data', () => {
111 expect(fn({ _ref: '/_components/a', child: [{ _ref: '/_components/b' }]}, {}, {})).toEqual({
112 _ref: '/_components/a',
113 child: [{
114 _ref: '/_components/b'
115 }]
116 });
117 });
118
119 it('adds component lists', () => {
120 expect(fn({ _ref: '/_components/a', child: [{ _ref: '/_components/b' }]}, bootstrap, {})).toEqual({
121 _ref: '/_components/a',
122 child: [{
123 _ref: '/_components/b',
124 c: 'd'
125 }]
126 });
127 });
128
129 it('adds component lists with instances', () => {
130 expect(fn({ _ref: '/_components/a/instances/1', child: [{ _ref: '/_components/b/instances/1' }]}, bootstrap, {})).toEqual({
131 _ref: '/_components/a/instances/1',
132 child: [{
133 _ref: '/_components/b/instances/1',
134 c: 'd'
135 }]
136 });
137 });
138
139 it('passes through other arrays', () => {
140 const data = { a: ['b', 'c'] };
141
142 expect(fn(data, bootstrap, {})).toEqual(data);
143 });
144
145 it('adds component props', () => {
146 expect(fn({ _ref: '/_components/a', child: { _ref: '/_components/b' }}, bootstrap, {})).toEqual({
147 _ref: '/_components/a',
148 child: {
149 _ref: '/_components/b',
150 c: 'd'
151 }
152 });
153 });
154
155 it('passes through other objects', () => {
156 const data = { a: { b: true, c: true } };
157
158 expect(fn(data, bootstrap, {})).toEqual(data);
159 });
160
161 it('passes through other data', () => {
162 const data = {
163 _ref: '/_components/a',
164 a: 'some string',
165 b: false,
166 c: 0,
167 d: null,
168 e: '',
169 f: [],
170 g: {}
171 };
172
173 expect(fn(data, bootstrap, {})).toEqual(data);
174 });
175
176 it('retains root-level _ref', () => {
177 expect(fn({ _ref: 'foo', a: 'b' })).toEqual({ _ref: 'foo', a: 'b' });
178 });
179
180 it('composes deep objects', () => {
181 expect(fn({ _ref: '/_components/a/instances/1', child: [{ _ref: '/_components/b/instances/1' }]}, bootstrapDeep, {})).toEqual({
182 _ref: '/_components/a/instances/1',
183 child: [{
184 _ref: '/_components/b/instances/1',
185 child: {
186 _ref: '/_components/c/instances/1',
187 d: 'e'
188 }
189 }]
190 });
191 });
192 });
193});