c94d0ddaa79d70bf55ae7d23c4781197ca96baf4
[fnpeditor.git] / modules / documentCanvas / canvas / canvas.test3.js
1 define([
2 'libs/chai',
3 'modules/documentCanvas/canvas/canvas',
4 'modules/documentCanvas/canvas/documentElement'
5 ], function(chai, canvas, documentElement) {
6     
7 'use strict';
8
9 var expect = chai.expect;
10
11 describe('Canvas', function() {
12     describe('basic properties', function() {
13         it('renders empty document when canvas created from empty XML', function() {
14             var c = canvas.fromXML('');
15             expect(c.doc()).to.equal(null);
16         });
17
18         it('gives access to its document root node', function() {
19             var c = canvas.fromXML('<section></section>');
20             expect(c.doc().wlxmlTag).to.equal('section');
21         });
22     });
23
24     describe('document representation api', function() {
25         describe('document root element', function() {
26             var c = canvas.fromXML('<section></section>');
27             it('exists', function() {
28                 expect(c.doc()).to.be.instanceOf(documentElement.DocumentElement);
29             });
30             it('is of type DocumentNodeElement', function() {
31                 expect(c.doc()).to.be.instanceOf(documentElement.DocumentNodeElement);
32             });
33         });
34
35         describe('DocumentElements comparison', function() {
36             it('reports dwo DocumentElements to be the same when they represent the same wlxml document element', function() {
37                 var c = canvas.fromXML('<section><div></div><div></div></section>'),
38                     first_div1 = c.doc().children()[0],
39                     first_div2 = c.doc().children()[0],
40                     second_div = c.doc().children()[1];
41                 expect(first_div1.sameNode(first_div1)).to.be.true;
42                 expect(first_div1.sameNode(first_div2)).to.be.true;
43                 expect(first_div1.sameNode(second_div)).to.be.false;
44             });
45         });
46
47         describe('traversing', function() {
48             it('reports element nodes', function() {
49                 var c = canvas.fromXML('<section><div></div></section>'),
50                     children = c.doc().children();
51                 expect(children.length).to.equal(1);
52                 expect(children[0]).to.be.instanceOf(documentElement.DocumentNodeElement);
53
54                 c = canvas.fromXML('<section><div></div><div></div></section>'),
55                     children = c.doc().children();
56                 expect(children.length).to.equal(2);
57                 expect(children[0]).to.be.instanceOf(documentElement.DocumentNodeElement);
58                 expect(children[1]).to.be.instanceOf(documentElement.DocumentNodeElement);
59             });
60             
61             it('reports text nodes', function() {
62                 var c = canvas.fromXML('<section>Alice</section>'),
63                     children = c.doc().children();
64                 expect(children.length).to.equal(1);
65                 expect(children[0]).to.be.instanceOf(documentElement.DocumentTextElement);
66             });
67
68             describe('accessing parents', function() {
69                 it('returns DocumentNodeElement representing parent in wlxml document as DocumentNodeElement parent', function() {
70                     var c = canvas.fromXML('<section><div></div></section>'),
71                         div = c.doc().children()[0];
72                     expect(div.parent().sameNode(c.doc())).to.be.true;
73                 });
74                 it('returns DocumentNodeElement representing parent in wlxml document as DocumentTextElement parent', function() {
75                     var c = canvas.fromXML('<section>Alice</section>'),
76                         text = c.doc().children()[0];
77                     expect(text.parent().sameNode(c.doc())).to.be.true;
78                 });
79             });
80
81
82             describe('free text handling', function() {
83                     it('sees free text', function() {
84                         var c = canvas.fromXML('<section>Alice <span>has</span> a cat</section>'),
85                             children = c.doc().children();
86                         expect(children.length).to.equal(3);
87                         expect(children[0]).to.be.instanceOf(documentElement.DocumentTextElement);
88                         expect(children[1]).to.be.instanceOf(documentElement.DocumentNodeElement);
89                         expect(children[2]).to.be.instanceOf(documentElement.DocumentTextElement);
90                     });
91             });
92             describe('white characters handling', function() {
93                 it('says empty element node has no children', function() {
94                     var c = canvas.fromXML('<section></section>');
95                     expect(c.doc().children().length).to.equal(0);
96                 });
97                 it('says element node with one space has one DocumentTextElement', function() {
98                     var c = canvas.fromXML('<section> </section>');
99                     expect(c.doc().children().length).to.equal(1);
100                     expect(c.doc().children()[0]).to.be.instanceOf(documentElement.DocumentTextElement);
101                 });
102                 it('ignores white space surrounding block elements', function() {
103                     var c = canvas.fromXML('<section> <div></div> </section>');
104                         var children = c.doc().children();
105                         expect(children.length).to.equal(1);
106                         expect(children[0]).to.be.instanceOf(documentElement.DocumentNodeElement);
107                 });
108                 it('ignores white space between block elements', function() {
109                     var c = canvas.fromXML('<section><div></div> <div></div></section>');
110                         var children = c.doc().children();
111                         expect(children.length === 2);
112                         [0,1].forEach(function(idx) {
113                             expect(children[idx]).to.be.instanceOf(documentElement.DocumentNodeElement);
114                         });
115                 });
116             });
117         })
118
119         describe('manipulation api', function() {
120             
121
122
123             describe('Basic Element inserting', function() {
124                 it('can put new NodeElement at the end', function() {
125                     var c = canvas.fromXML('<section></section>'),
126                         appended = c.doc().append({tag: 'header', klass: 'some.class'}),
127                         children = c.doc().children();
128
129                     expect(children.length).to.equal(1);
130                     expect(children[0].sameNode(appended));
131                 });
132
133                 it('can put new NodeElement after another NodeElement', function() {
134                     var c = canvas.fromXML('<section><div></div></section>'),
135                         div = c.doc().children()[0],
136                         added = div.after({tag: 'header', klass: 'some.class'}),
137                         children = c.doc().children();
138                     expect(children.length).to.equal(2);
139                     expect(children[1].sameNode(added));
140                 });
141             });
142
143             describe('wrapping', function() {
144                 it('wraps DocumentNodeElement', function() {
145                     var c = canvas.fromXML('<section><div></div></section>'),
146                         div = c.doc().children()[0];
147                     
148                     var returned = div.wrapWithNodeElement({tag: 'header', klass: 'some.class'}),
149                         parent = div.parent(),
150                         parent2 = c.doc().children()[0];
151
152                     expect(returned.sameNode(parent)).to.be.true;
153                     expect(returned.sameNode(parent2)).to.be.true;
154                 });
155                 it('wraps DocumentTextElement', function() {
156                     var c = canvas.fromXML('<section>Alice</section>'),
157                         text = c.doc().children()[0];
158                     
159                     var returned = text.wrapWithNodeElement({tag: 'header', klass: 'some.class'}),
160                         parent = text.parent(),
161                         parent2 = c.doc().children()[0];
162
163                     expect(returned.sameNode(parent)).to.be.true;
164                     expect(returned.sameNode(parent2)).to.be.true;
165                 });
166             })
167         });
168
169     });
170 });
171
172 /*describe('Canvas', function() {
173     it('can wrap selected document nodes in a list', function() {
174         var c = canvas.fromXML('\
175             <section>\
176                 <div>Alice</div>\
177                 <div>has</div>\
178                 <div>a cat</div>\
179             </section>
180         ');
181         var div_alice   = c.doc().children({tag: 'div'})[0];
182         var div_cat     = c.doc().children({tag: 'div'})[2];
183         c.doc.wrapInList({start: div_alice, end: div_cat});
184
185         expect(c.doc().children().length === 3)
186
187
188     })
189 });*/
190
191
192
193
194 });