422a3713e415b3b2078ee98a758223bfbe6f1934
[fnpeditor.git] / src / wlxml / extensions / metadata / metadata.test.js
1 define(function(require) {
2     
3 'use strict';
4
5 /* jshint multistr:true */
6 /* globals describe, it */
7
8 var chai = require('libs/chai'),
9     wlxml = require('wlxml/wlxml'),
10     expect = chai.expect,
11     $ = require('libs/jquery');
12
13
14 var getDocumentFromXML = function(xml, options) {
15     return wlxml.WLXMLDocumentFromXML(xml, options || {});
16 };
17
18
19 describe('Metadata API', function() {
20     it('returns empty metadata for node without metadata', function() {
21         var doc = getDocumentFromXML('<section></section>');
22         expect(doc.root.getMetadata().length).to.equal(0);
23     });
24     it('allows to set metadata on an element node', function() {
25         var doc = getDocumentFromXML('<section></section>'),
26             metadata = doc.root.getMetadata();
27         
28         var row = metadata.add({key: 'key', value: 'value'});
29         expect(metadata.length).to.equal(1);
30         expect(metadata.at(0)).to.equal(row);
31         expect(row.getKey()).to.equal('key');
32         expect(row.getValue()).to.equal('value');
33     });
34     it('allows to remove specific metadata row', function() {
35         var doc = getDocumentFromXML('<section><metadata><dc:key>value</dc:key><dc:key>value</dc:key></metadata></section>'),
36             metadata = doc.root.getMetadata();
37         
38         expect(metadata.length).to.equal(2);
39         metadata.at(0).remove();
40         expect(metadata.length).to.equal(1);
41         expect(metadata.at(0).getValue()).to.equal('value');
42     });
43     it('reads node\'s metadata from source of its metadata child node', function() {
44         var doc = getDocumentFromXML('<section><metadata><dc:key>value</dc:key></metadata></section>'),
45             metadata = doc.root.getMetadata();
46         expect(metadata.length).to.equal(1);
47         expect(metadata.at(0).getKey()).to.equal('key');
48         expect(metadata.at(0).getValue()).to.equal('value');
49     });
50
51     it('serializes node\'s metadata to its metadata child node', function() {
52         var doc = getDocumentFromXML('<section></section>');
53
54         doc.root.getMetadata().add({key: 'key', value: 'value'});
55
56         var metadataNodes = $(doc.toXML()).children('metadata'),
57             keyNodes = metadataNodes.children();
58         
59         expect(metadataNodes).to.have.length(1);
60         expect(keyNodes).to.have.length(1);
61         expect(keyNodes[0].tagName.toLowerCase()).to.equal('dc:key');
62         expect($(keyNodes[0]).text()).to.equal('value');
63     });
64
65     describe('Hiding metadata nodes from document api', function() {
66         it('hides metadata nodes from document api', function() {
67             var doc = getDocumentFromXML('<section><div></div><metadata><dc:key>value</dc:key></metadata><div></div></section>'),
68                 rootContents = doc.root.contents();
69             expect(rootContents).to.have.length(2);
70             expect(rootContents[0].getTagName()).to.equal('div');
71             expect(rootContents[1].getTagName()).to.equal('div');
72             expect(rootContents[0].next().sameNode(rootContents[1])).to.equal(true);
73             expect(rootContents[1].prev().sameNode(rootContents[0])).to.equal(true);
74         });
75
76         it('merges adjacent text nodes', function() {
77             var doc = getDocumentFromXML('<section>Alice<metadata></metadata> has a cat</section>'),
78                 contents = doc.root.contents();
79             expect(contents.length).to.equal(1);
80             expect(contents[0].getText()).to.equal('Alice has a cat');
81         });
82     });
83
84     describe('undo', function() {
85         it('undoes adding metadata', function() {
86             var doc = getDocumentFromXML('<section></section>'),
87                 metadata = doc.root.getMetadata();
88             metadata.add({key: 'k', value: 'v'});
89             doc.undo();
90             expect(metadata.length).to.equal(0);
91             doc.redo();
92             expect(metadata.length).to.equal(1);
93             expect(metadata.at(0).getValue()).to.equal('v');
94         });
95         it('undoes changing metadata key', function() {
96             var doc = getDocumentFromXML('<section><metadata><dc:key>value</dc:key></metadata></section>'),
97                 metadata = doc.root.getMetadata(),
98                 row = metadata.at(0);
99
100             row.setKey('key2');
101             doc.undo();
102             expect(row.getKey()).to.equal('key');
103             doc.redo();
104             expect(row.getKey()).to.equal('key2');
105         });
106         it('undoes changing metadata value', function() {
107             var doc = getDocumentFromXML('<section><metadata><dc:key>value</dc:key></metadata></section>'),
108                 metadata = doc.root.getMetadata(),
109                 row = metadata.at(0);
110
111             row.setValue('value2');
112             doc.undo();
113             expect(row.getValue()).to.equal('value');
114             doc.redo();
115             expect(row.getValue()).to.equal('value2');
116         });
117         it('undoes removing metadata', function() {
118             var doc = getDocumentFromXML('<section><metadata><dc:key>value</dc:key></metadata></section>'),
119                 metadata = doc.root.getMetadata(),
120                 row = metadata.at(0);
121             
122             row.remove();
123             doc.undo();
124             expect(metadata.length).to.equal(1, 'undo brought back metadata');
125             doc.redo();
126             expect(metadata.length).to.equal(0, 'redo removed metadata');
127         });
128     });
129
130
131
132 });
133
134
135 });