UNPKG

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