1 define(function(require) {
5 /* jshint multistr:true */
6 /* globals describe, it */
8 var chai = require('libs/chai'),
9 wlxml = require('wlxml/wlxml'),
11 $ = require('libs/jquery');
14 var getDocumentFromXML = function(xml, options) {
15 return wlxml.WLXMLDocumentFromXML(xml, options || {});
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);
24 it('allows to set metadata on an element node', function() {
25 var doc = getDocumentFromXML('<section></section>'),
26 metadata = doc.root.getMetadata();
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');
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();
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');
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');
51 it('serializes node\'s metadata to its metadata child node', function() {
52 var doc = getDocumentFromXML('<section></section>');
54 doc.root.getMetadata().add({key: 'key', value: 'value'});
56 var metadataNodes = $(doc.toXML()).children('metadata'),
57 keyNodes = metadataNodes.children();
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');
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);
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');
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'});
90 expect(metadata.length).to.equal(0);
92 expect(metadata.length).to.equal(1);
93 expect(metadata.at(0).getValue()).to.equal('v');
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(),
102 expect(row.getKey()).to.equal('key');
104 expect(row.getKey()).to.equal('key2');
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);
111 row.setValue('value2');
113 expect(row.getValue()).to.equal('value');
115 expect(row.getValue()).to.equal('value2');
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);
124 expect(metadata.length).to.equal(1, 'undo brought back metadata');
126 expect(metadata.length).to.equal(0, 'redo removed metadata');
129 describe('Regression tests', function() {
130 it('passes undoing & redoing scenario', function() {
131 var doc = getDocumentFromXML('<section></section>');
133 doc.startTransaction();
134 var comment = doc.root.append({tagName: 'aside', attrs: {klass: 'comment'}});
135 comment.append({text: ''});
136 var meta = comment.getMetadata();
137 meta.add({key: 'k', value: '1'});
138 doc.endTransaction();
143 comment = doc.root.contents()[0];
144 expect(comment.getMetadata().length).to.equal(1);
146 expect(doc.root.contents().length).to.equal(0);