wlxml: metadata wip - part of a new api approach
[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 var getDocumentFromXML = function(xml, options) {
14     return wlxml.WLXMLDocumentFromXML(xml, options || {});
15 };
16
17
18 describe.only('Metadata API', function() {
19     it('returns empty metadata for node without metadata', function() {
20         var doc = getDocumentFromXML('<section></section>');
21         expect(doc.root.getMetadata().length).to.equal(0);
22     });
23     it('allows to set metadata on an element node', function() {
24         var doc = getDocumentFromXML('<section></section>');
25
26         var row = doc.root.addMetadata({key: 'key', value: 'value'}),
27             metadata = doc.root.getMetadata();
28
29         expect(metadata.length).to.equal(1);
30         expect(metadata[0]).to.equal(row, 'aaa');
31
32         expect(row.getKey()).to.equal('key');
33         expect(row.getValue()).to.equal('value');
34     });
35     // it('allows to remove specific metadata row', function() {
36     //     var doc = getDocumentFromXML('<section><metadata><dc:key>value</dc:key><dc:key>value</dc:key></metadata></section>'),
37     //         metadata = doc.root.getMetadata();
38     //     expect(metadata.length).to.equal(2);
39     //     row.remove();
40     //     expect(metadata.length)
41     //     expect(metadata[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[0].getKey()).to.equal('key');
48         expect(metadata[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.addMetadata({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     it('doesn\'t show metadata node on nodes contents', function() {
65         var doc = getDocumentFromXML('<section><metadata><dc:key>value</dc:key></metadata></section>');
66         expect(doc.root.contents()).to.have.length(0);
67     });
68
69 });
70
71
72 });