1 |
|
2 |
|
3 |
|
4 | const helper = require('../lib/helper');
|
5 | const should = require('chai').should();
|
6 |
|
7 | describe('helper.simplifyNode()', () => {
|
8 | it('should return the already simplified', () => {
|
9 | should.not.exist(helper.simplifyNode(null));
|
10 | helper.simplifyNode('simple').should.equal('simple');
|
11 | });
|
12 |
|
13 | it('should return only text when really simple', () => {
|
14 | const input = {
|
15 | $name: 'title',
|
16 | $attrs: {},
|
17 | $text: 'this is a title'
|
18 | };
|
19 | helper.simplifyNode(input, true).should.equal('this is a title');
|
20 | helper.simplifyNode(input).should.not.equal('this is a title');
|
21 | });
|
22 |
|
23 | it('should return only attributes when really simple', () => {
|
24 | const input = {
|
25 | $name: 'div',
|
26 | $attrs: { id: '34', type: 'thing' }
|
27 | };
|
28 | helper.simplifyNode(input).should.deep.equal({ $name: 'div', id: '34', type: 'thing' });
|
29 | helper.simplifyNode(input, true).should.deep.equal({ id: '34', type: 'thing' });
|
30 | });
|
31 |
|
32 | it('should drop unecessary properties', () => {
|
33 | const input = {
|
34 | $name: 'title',
|
35 | $attrs: { id: '34' },
|
36 | $text: null
|
37 | };
|
38 |
|
39 | const output = {
|
40 | $name: 'title',
|
41 | id: '34'
|
42 | };
|
43 | helper.simplifyNode(input).should.deep.equal(output);
|
44 | helper.simplifyNode(input, true).should.deep.equal(input.$attrs);
|
45 | });
|
46 |
|
47 | it('should not oversimplify empty nodes', () => {
|
48 | const input = {
|
49 | $name: 'title',
|
50 | $text: null
|
51 | };
|
52 |
|
53 | const output = {
|
54 | $name: 'title'
|
55 | };
|
56 | helper.simplifyNode(input).should.deep.equal(output);
|
57 | });
|
58 |
|
59 | it('should simplify $markup', () => {
|
60 | const input = {
|
61 | $name: 'title',
|
62 | $attrs: {},
|
63 | $markup: [{ $name: 'p', $attrs: {}, $markup: [ 'stuff' ]}]
|
64 | };
|
65 |
|
66 | const output = {
|
67 | $name: 'title',
|
68 | $markup: [{ $name: 'p', $markup: [ 'stuff' ]}]
|
69 | };
|
70 | helper.simplifyNode(input).should.deep.equal(output);
|
71 | });
|
72 |
|
73 | it('should strip single-element arrays', () => {
|
74 | helper.simplifyNode({
|
75 | $name: 'tag',
|
76 | stuff: [ 'test' ]
|
77 | }).should.deep.equal({
|
78 | $name: 'tag',
|
79 | stuff: 'test'
|
80 | });
|
81 |
|
82 | helper.simplifyNode([ 'test' ]).should.equal('test');
|
83 | });
|
84 |
|
85 | it('should preserve arrays when asked', () => {
|
86 | helper.simplifyNode({
|
87 | $name: 'tag',
|
88 | stuff: [ 'test' ]
|
89 | }, false, true).should.deep.equal({
|
90 | $name: 'tag',
|
91 | stuff: [ 'test' ]
|
92 | });
|
93 |
|
94 | helper.simplifyNode([ 'test' ], false, true).should.deep.equal([ 'test' ]);
|
95 | });
|
96 |
|
97 | it('should simplify arrays as arrays', () => {
|
98 | const input = {
|
99 | items: [{ id: 1 }, { id: 2 }]
|
100 | };
|
101 |
|
102 | const output = [
|
103 | { id: 1 }, { id: 2 }
|
104 | ];
|
105 |
|
106 | helper.simplifyNode(input).should.deep.equal(output);
|
107 | });
|
108 |
|
109 | it('should not simplify when things get interesting', () => {
|
110 | const input = {
|
111 | $name: 'header',
|
112 | $attrs: { id: '3' },
|
113 | $markup: [ 'some text' ]
|
114 | };
|
115 |
|
116 | const output = {
|
117 | $name: 'header',
|
118 | $attrs: { id: '3' },
|
119 | $markup: [ 'some text' ]
|
120 | };
|
121 |
|
122 | helper.simplifyNode(input).should.deep.equal(output);
|
123 | delete output.$name;
|
124 |
|
125 | helper.simplifyNode(input, true).should.deep.equal(output);
|
126 | });
|
127 | });
|