cleanup
[fnpeditor.git] / modules / documentCanvas / tests / canvas.test.js
1 define([\r
2 'libs/jquery-1.9.1.min',\r
3 'libs/chai', \r
4 './utils.js',\r
5 'modules/documentCanvas/canvas'\r
6 ], function($, chai, utils, canvas) {\r
7 \r
8     'use strict';\r
9 \r
10     var assert = chai.assert;\r
11     \r
12     assert.xmlEqual = function(lhsText, rhsText) {\r
13         var cleanLhs = utils.cleanUp(lhsText);\r
14         var cleanRhs = utils.cleanUp(rhsText);\r
15         \r
16         var lhs = $(cleanLhs);\r
17         var rhs = $(cleanRhs);\r
18         \r
19         this.equal(lhs.length, 1);\r
20         this.equal(rhs.length, 1);\r
21         \r
22         lhs = lhs.get(0);\r
23         rhs = rhs.get(0);\r
24         \r
25         var test = lhs.isEqualNode(rhs);\r
26         if(!test) {\r
27             console.log(cleanLhs);\r
28             console.log(cleanRhs);    \r
29         }\r
30         return this.ok(test, 'xmls are equal');\r
31     };\r
32     \r
33     var retrievingTest = function(title, xml) {\r
34         test(title, function() {\r
35             var c = new canvas.Canvas(xml);\r
36             assert.xmlEqual(c.toXML(), xml);    \r
37         });\r
38     };\r
39     \r
40     suite('Basic document retrieving', function() {\r
41         test('empty document', function() {\r
42             var c = new canvas.Canvas('');\r
43             assert.equal(c.toXML(), '');\r
44         });\r
45         retrievingTest('empty tag', '<section></section>');\r
46         retrievingTest('tag with content', '<section>Some text</section>');\r
47         retrievingTest('tag with class', '<section class="some.class"></section>');\r
48     });\r
49     \r
50     suite('Nodes', function() {\r
51         test('getting nodes via selector', function() {\r
52             var c = new canvas.Canvas('<section><header class="some.class">Header 1</header></section>');\r
53             var header = c.getNode({tag: 'header'})[0];\r
54             assert.equal(header.tag, 'header');\r
55             assert.equal(header.klass, 'some-class');\r
56         });\r
57         \r
58         test('getting previous node', function() {\r
59             var c = new canvas.Canvas('<section><div class="some.class">Div 1</div><div class="some.other.class">Div 2</div></section>');\r
60             var secondDiv = c.getNode({tag: 'div'})[1];\r
61             var firstDiv = c.getPreviousNode({node: secondDiv});\r
62             assert.equal(firstDiv.klass, 'some-class');\r
63         })\r
64         \r
65         test('pervious node of node without "previous siblings" is its parent', function() {\r
66             var c = new canvas.Canvas('<section><div class="some.class">Div 1</div></section>');\r
67             var div = c.getNode({tag: 'div'})[0];\r
68             var section = c.getPreviousNode({node: div});\r
69             assert.equal(section.tag, 'section');\r
70         })\r
71     \r
72         test('inserting after', function() {\r
73             var c = new canvas.Canvas('<section><header>Header 1</header></section>');\r
74             var header = c.getNode({tag: 'header'})[0];\r
75             c.insertNode({place: 'after', context: header, tag: 'div', klass: 'some.class'});\r
76             assert.xmlEqual(c.toXML(), '<section><header>Header 1</header><div class="some.class"></div></section>');\r
77         });\r
78         \r
79         test('wrap text in node', function() {\r
80             var c = new canvas.Canvas('<section><header>Header 1</header></section>');\r
81             var header = c.getNode({tag: 'header'})[0];\r
82             c.insertNode({place: 'wrapText', context: header, tag: 'span', klass: 'url', offsetStart: 1, offsetEnd: 6});\r
83             assert.xmlEqual(c.toXML(), '<section><header>H<span class="url">eader</span> 1</header></section>');\r
84         });\r
85         \r
86         test('split node', function() {\r
87             var c = new canvas.Canvas('<section><header class="some.class">Header 1</header></section>');\r
88             var header = c.getNode({tag: 'header'})[0];\r
89             c.splitNode({node: header, offset: 4});\r
90             assert.xmlEqual(c.toXML(), '\\r
91                 <section> \\r
92                     <header class="some.class">Head</header>\\r
93                     <header class="some.class">er 1</header>\\r
94                 </section>'\r
95             );\r
96         });\r
97         \r
98         test('split node with subnodes', function() {\r
99             var c = new canvas.Canvas('<section><header class="some.class">Fancy and nice <span>header</span> 1</header></section>');\r
100             var header = c.getNode({tag: 'header'})[0];\r
101             c.splitNode({node: header, textNodeIdx: 0, offset: 5});\r
102             assert.xmlEqual(c.toXML(), '\\r
103                 <section> \\r
104                     <header class="some.class">Fancy</header>\\r
105                     <header class="some.class"> and nice <span>header</span> 1</header>\\r
106                 </section>'\r
107             );\r
108         });\r
109         \r
110         test('remove node', function() {\r
111             var c = new canvas.Canvas('<section><header class="some.class">Fancy and nice <span>header</span> 1</header></section>');\r
112             var span = c.getNode({tag: 'span'})[0];\r
113             var siblings = c.removeNode({node:span});\r
114             assert.xmlEqual(c.toXML(), '\\r
115                 <section>\\r
116                     <header class="some.class">Fancy and nice  1</header>\\r
117                 </section>'\r
118             );\r
119         });\r
120         \r
121         test('create list from existing nodes', function() {\r
122             var c = new canvas.Canvas('<section><div>Alice</div>has<div>a cat</div><div>some text</div></section>');\r
123             var div1 = c.getNode({tag:'div'})[0];\r
124             var div2 = c.getNode({tag:'div'})[1];\r
125             \r
126             c.createList({start: div1, end: div2});\r
127             \r
128             assert.xmlEqual(c.toXML(), '\\r
129                 <section>\\r
130                     <div class="list.items">\\r
131                         <div class="item">Alice</div>\\r
132                         <div class="item">has</div>\\r
133                         <div class="item">a cat</div>\\r
134                     </div>\\r
135                     <div>some text</div>\\r
136                 </section>');\r
137 \r
138         });\r
139         \r
140         test('remove list', function() {\r
141             var xml = '\\r
142                 <section>\\r
143                     <div class="list.items">\\r
144                         <div class="item">Alice</div>\\r
145                         <div class="item">has</div>\\r
146                         <div class="item">a cat</div>\\r
147                     </div>\\r
148                     <div>some text</div>\\r
149                 </section>';\r
150            var c = new canvas.Canvas(xml);\r
151            var item = c.getNode({klass: 'item'})[1];\r
152            c.removeList({pointer: item});\r
153            assert.xmlEqual(c.toXML(), '\\r
154                 <section>\\r
155                     <div>Alice</div>\\r
156                     <div>has</div>\\r
157                     <div>a cat</div>\\r
158                     <div>some text</div>\\r
159                 </section>');\r
160         });\r
161     });\r
162 \r
163 });