778c4bfb187f914d1f35977c51c735247a9c35fe
[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
12 describe('Canvas', function() {
13     describe('basic properties', function() {
14         it('renders empty document when canvas created from empty XML', function() {
15             var c = canvas.fromXML('');
16             expect(c.doc()).to.equal(null);
17         });
18
19         it('gives access to its document root node', function() {
20             var c = canvas.fromXML('<section></section>');
21             expect(c.doc().wlxmlTag).to.equal('section');
22         });
23     });
24
25     describe('document representation api', function() {
26         describe('document root element', function() {
27             var c = canvas.fromXML('<section></section>');
28             it('exists', function() {
29                 expect(c.doc()).to.be.instanceOf(documentElement.DocumentElement);
30             });
31             it('is of type DocumentNodeElement', function() {
32                 expect(c.doc()).to.be.instanceOf(documentElement.DocumentNodeElement);
33             });
34         });
35
36         describe('DocumentElements comparison', function() {
37             it('reports dwo DocumentElements to be the same when they represent the same wlxml document element', function() {
38                 var c = canvas.fromXML('<section><div></div><div></div></section>'),
39                     first_div1 = c.doc().children()[0],
40                     first_div2 = c.doc().children()[0],
41                     second_div = c.doc().children()[1];
42                 expect(first_div1.sameNode(first_div1)).to.be.true;
43                 expect(first_div1.sameNode(first_div2)).to.be.true;
44                 expect(first_div1.sameNode(second_div)).to.be.false;
45             });
46         });
47
48         describe('traversing', function() {
49             it('reports element nodes', function() {
50                 var c = canvas.fromXML('<section><div></div></section>'),
51                     children = c.doc().children();
52                 expect(children.length).to.equal(1);
53                 expect(children[0]).to.be.instanceOf(documentElement.DocumentNodeElement);
54
55                 c = canvas.fromXML('<section><div></div><div></div></section>'),
56                     children = c.doc().children();
57                 expect(children.length).to.equal(2);
58                 expect(children[0]).to.be.instanceOf(documentElement.DocumentNodeElement);
59                 expect(children[1]).to.be.instanceOf(documentElement.DocumentNodeElement);
60             });
61             
62             it('reports text nodes', function() {
63                 var c = canvas.fromXML('<section>Alice</section>'),
64                     children = c.doc().children();
65                 expect(children.length).to.equal(1);
66                 expect(children[0]).to.be.instanceOf(documentElement.DocumentTextElement);
67             });
68
69             describe('accessing parents', function() {
70                 it('returns DocumentNodeElement representing parent in wlxml document as DocumentNodeElement parent', function() {
71                     var c = canvas.fromXML('<section><div></div></section>'),
72                         div = c.doc().children()[0];
73                     expect(div.parent().sameNode(c.doc())).to.be.true;
74                 });
75                 it('returns DocumentNodeElement representing parent in wlxml document as DocumentTextElement parent', function() {
76                     var c = canvas.fromXML('<section>Alice</section>'),
77                         text = c.doc().children()[0];
78                     expect(text.parent().sameNode(c.doc())).to.be.true;
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             
93             describe('white characters handling', function() {
94                 it('says empty element node has no children', function() {
95                     var c = canvas.fromXML('<section></section>');
96                     expect(c.doc().children().length).to.equal(0);
97                 });
98                 it('says element node with one space has one DocumentTextElement', function() {
99                     var c = canvas.fromXML('<section> </section>');
100                     expect(c.doc().children().length).to.equal(1);
101                     expect(c.doc().children()[0]).to.be.instanceOf(documentElement.DocumentTextElement);
102                 });
103                 it('ignores white space surrounding block elements', function() {
104                     var c = canvas.fromXML('<section> <div></div> </section>');
105                     var children = c.doc().children();
106                     expect(children.length).to.equal(1);
107                     expect(children[0]).to.be.instanceOf(documentElement.DocumentNodeElement);
108                 });
109                 it('ignores white space between block elements', function() {
110                     var c = canvas.fromXML('<section><div></div> <div></div></section>');
111                     var children = c.doc().children();
112                     expect(children.length === 2);
113                     [0,1].forEach(function(idx) {
114                         expect(children[idx]).to.be.instanceOf(documentElement.DocumentNodeElement);
115                     });
116                 });
117             });
118         });
119
120         describe('manipulation api', function() {
121
122             describe('Basic Element inserting', function() {
123                 it('can put new NodeElement at the end', function() {
124                     var c = canvas.fromXML('<section></section>'),
125                         appended = c.doc().append({tag: 'header', klass: 'some.class'}),
126                         children = c.doc().children();
127
128                     expect(children.length).to.equal(1);
129                     expect(children[0].sameNode(appended));
130                 });
131
132                 it('can put new NodeElement after another NodeElement', function() {
133                     var c = canvas.fromXML('<section><div></div></section>'),
134                         div = c.doc().children()[0],
135                         added = div.after({tag: 'header', klass: 'some.class'}),
136                         children = c.doc().children();
137                     expect(children.length).to.equal(2);
138                     expect(children[1].sameNode(added));
139                 });
140             });
141
142             describe('wrapping', function() {
143                 it('wraps DocumentNodeElement', function() {
144                     var c = canvas.fromXML('<section><div></div></section>'),
145                         div = c.doc().children()[0];
146                     
147                     var returned = div.wrapWithNodeElement({tag: 'header', klass: 'some.class'}),
148                         parent = div.parent(),
149                         parent2 = c.doc().children()[0];
150
151                     expect(returned.sameNode(parent)).to.be.true;
152                     expect(returned.sameNode(parent2)).to.be.true;
153                 });
154                 it('wraps DocumentTextElement', function() {
155                     var c = canvas.fromXML('<section>Alice</section>'),
156                         text = c.doc().children()[0];
157                     
158                     var returned = text.wrapWithNodeElement({tag: 'header', klass: 'some.class'}),
159                         parent = text.parent(),
160                         parent2 = c.doc().children()[0];
161
162                     expect(returned.sameNode(parent)).to.be.true;
163                     expect(returned.sameNode(parent2)).to.be.true;
164                 });
165             });
166         });
167
168     });
169 });
170
171
172 });