X-Git-Url: https://git.mdrn.pl/fnpeditor.git/blobdiff_plain/85331f59b2aa174291878f90633f7189fd221628..4f9d834d93e434d9b8375bb320b0555c98ef5d07:/src/wlxml/extensions/metadata/metadata.test.js
diff --git a/src/wlxml/extensions/metadata/metadata.test.js b/src/wlxml/extensions/metadata/metadata.test.js
index 5de573b..ca6c309 100644
--- a/src/wlxml/extensions/metadata/metadata.test.js
+++ b/src/wlxml/extensions/metadata/metadata.test.js
@@ -10,48 +10,48 @@ var chai = require('libs/chai'),
expect = chai.expect,
$ = require('libs/jquery');
+
var getDocumentFromXML = function(xml, options) {
return wlxml.WLXMLDocumentFromXML(xml, options || {});
};
-describe.only('Metadata API', function() {
+describe('Metadata API', function() {
it('returns empty metadata for node without metadata', function() {
var doc = getDocumentFromXML('');
expect(doc.root.getMetadata().length).to.equal(0);
});
it('allows to set metadata on an element node', function() {
- var doc = getDocumentFromXML('');
-
- var row = doc.root.addMetadata({key: 'key', value: 'value'}),
+ var doc = getDocumentFromXML(''),
metadata = doc.root.getMetadata();
-
+
+ var row = metadata.add({key: 'key', value: 'value'});
expect(metadata.length).to.equal(1);
- expect(metadata[0]).to.equal(row, 'aaa');
-
+ expect(metadata.at(0)).to.equal(row);
expect(row.getKey()).to.equal('key');
expect(row.getValue()).to.equal('value');
});
- // it('allows to remove specific metadata row', function() {
- // var doc = getDocumentFromXML(''),
- // metadata = doc.root.getMetadata();
- // expect(metadata.length).to.equal(2);
- // row.remove();
- // expect(metadata.length)
- // expect(metadata[0].getValue()).to.equal('value');
- // });
+ it('allows to remove specific metadata row', function() {
+ var doc = getDocumentFromXML(''),
+ metadata = doc.root.getMetadata();
+
+ expect(metadata.length).to.equal(2);
+ metadata.at(0).remove();
+ expect(metadata.length).to.equal(1);
+ expect(metadata.at(0).getValue()).to.equal('value');
+ });
it('reads node\'s metadata from source of its metadata child node', function() {
var doc = getDocumentFromXML(''),
metadata = doc.root.getMetadata();
expect(metadata.length).to.equal(1);
- expect(metadata[0].getKey()).to.equal('key');
- expect(metadata[0].getValue()).to.equal('value');
+ expect(metadata.at(0).getKey()).to.equal('key');
+ expect(metadata.at(0).getValue()).to.equal('value');
});
it('serializes node\'s metadata to its metadata child node', function() {
var doc = getDocumentFromXML('');
- doc.root.addMetadata({key: 'key', value: 'value'});
+ doc.root.getMetadata().add({key: 'key', value: 'value'});
var metadataNodes = $(doc.toXML()).children('metadata'),
keyNodes = metadataNodes.children();
@@ -61,11 +61,95 @@ describe.only('Metadata API', function() {
expect(keyNodes[0].tagName.toLowerCase()).to.equal('dc:key');
expect($(keyNodes[0]).text()).to.equal('value');
});
- it('doesn\'t show metadata node on nodes contents', function() {
- var doc = getDocumentFromXML('');
- expect(doc.root.contents()).to.have.length(0);
+
+ describe('Hiding metadata nodes from document api', function() {
+ it('hides metadata nodes from document api', function() {
+ var doc = getDocumentFromXML(''),
+ rootContents = doc.root.contents();
+ expect(rootContents).to.have.length(2);
+ expect(rootContents[0].getTagName()).to.equal('div');
+ expect(rootContents[1].getTagName()).to.equal('div');
+ expect(rootContents[0].next().sameNode(rootContents[1])).to.equal(true);
+ expect(rootContents[1].prev().sameNode(rootContents[0])).to.equal(true);
+ });
+
+ it('merges adjacent text nodes', function() {
+ var doc = getDocumentFromXML(''),
+ contents = doc.root.contents();
+ expect(contents.length).to.equal(1);
+ expect(contents[0].getText()).to.equal('Alice has a cat');
+ });
+ });
+
+ describe('undo', function() {
+ it('undoes adding metadata', function() {
+ var doc = getDocumentFromXML(''),
+ metadata = doc.root.getMetadata();
+ metadata.add({key: 'k', value: 'v'});
+ doc.undo();
+ expect(metadata.length).to.equal(0);
+ doc.redo();
+ expect(metadata.length).to.equal(1);
+ expect(metadata.at(0).getValue()).to.equal('v');
+ });
+ it('undoes changing metadata key', function() {
+ var doc = getDocumentFromXML(''),
+ metadata = doc.root.getMetadata(),
+ row = metadata.at(0);
+
+ row.setKey('key2');
+ doc.undo();
+ expect(row.getKey()).to.equal('key');
+ doc.redo();
+ expect(row.getKey()).to.equal('key2');
+ });
+ it('undoes changing metadata value', function() {
+ var doc = getDocumentFromXML(''),
+ metadata = doc.root.getMetadata(),
+ row = metadata.at(0);
+
+ row.setValue('value2');
+ doc.undo();
+ expect(row.getValue()).to.equal('value');
+ doc.redo();
+ expect(row.getValue()).to.equal('value2');
+ });
+ it('undoes removing metadata', function() {
+ var doc = getDocumentFromXML(''),
+ metadata = doc.root.getMetadata(),
+ row = metadata.at(0);
+
+ row.remove();
+ doc.undo();
+ expect(metadata.length).to.equal(1, 'undo brought back metadata');
+ doc.redo();
+ expect(metadata.length).to.equal(0, 'redo removed metadata');
+ });
+
+ describe('Regression tests', function() {
+ it('passes undoing & redoing scenario', function() {
+ var doc = getDocumentFromXML('');
+
+ doc.startTransaction();
+ var comment = doc.root.append({tagName: 'aside', attrs: {klass: 'comment'}});
+ comment.append({text: ''});
+ var meta = comment.getMetadata();
+ meta.add({key: 'k', value: '1'});
+ doc.endTransaction();
+
+ doc.undo();
+ doc.redo();
+
+ comment = doc.root.contents()[0];
+ expect(comment.getMetadata().length).to.equal(1);
+ doc.undo();
+ expect(doc.root.contents().length).to.equal(0);
+ });
+ });
});
+
+
});