0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-01-06 22:40:14 -05:00

Added tests for bookmark card

no issue
This commit is contained in:
Kevin Ansfield 2019-10-02 17:47:21 +01:00
parent 7abeaf6f18
commit 839cf0289f
3 changed files with 253 additions and 50 deletions

View file

@ -37,11 +37,12 @@ module.exports = createCard({
name: 'bookmark',
type: 'dom',
render(opts) {
if (!opts.payload.metadata) {
let {payload, env: {dom}} = opts;
if (!payload.metadata || !payload.metadata.url || !payload.metadata.title || !payload.metadata.description) {
return '';
}
let {payload, env: {dom}} = opts;
let figure = createElement(dom, 'figure', 'kg-card kg-bookmark-card');
let linkTag = createElement(dom, 'a', 'kg-bookmark-container', [{
key: 'href',

View file

@ -0,0 +1,204 @@
const should = require('should');
const card = require('../../../../../server/lib/mobiledoc/cards/bookmark');
const SimpleDom = require('simple-dom');
const serializer = new SimpleDom.HTMLSerializer(SimpleDom.voidMap);
describe('Bookmark card', function () {
it('renders', function () {
let opts = {
env: {dom: new SimpleDom.Document()},
payload: {
metadata: {
url: 'http://example.com',
title: 'Title',
description: 'Description',
icon: 'http://example.com/icon.png',
thumbnail: 'http://exampple.com/thumbnail.png',
author: 'Author',
publisher: 'Publisher'
},
caption: 'Caption'
}
};
serializer.serialize(card.render(opts))
.should.equal('<!--kg-card-begin: bookmark--><figure class="kg-card kg-bookmark-card kg-card-hascaption"><a class="kg-bookmark-container" href="http://example.com"><div class="kg-bookmark-content"><div class="kg-bookmark-title">Title</div><div class="kg-bookmark-description">Description</div><div class="kg-bookmark-metadata"><img class="kg-bookmark-icon" src="http://example.com/icon.png"><span class="kg-bookmark-author">Author</span><span class="kg-bookmark-publisher">Publisher</span></div></div><div class="kg-bookmark-thumbnail"><img src="http://exampple.com/thumbnail.png"></div></a><figcaption>Caption</figcaption></figure><!--kg-card-end: bookmark-->');
});
it('skips icon when missing', function () {
let opts = {
env: {dom: new SimpleDom.Document()},
payload: {
metadata: {
url: 'http://example.com',
title: 'Title',
description: 'Description',
icon: null,
thumbnail: 'http://exampple.com/thumbnail.png',
author: 'Author',
publisher: 'Publisher'
},
caption: 'Caption'
}
};
serializer.serialize(card.render(opts))
.should.equal('<!--kg-card-begin: bookmark--><figure class="kg-card kg-bookmark-card kg-card-hascaption"><a class="kg-bookmark-container" href="http://example.com"><div class="kg-bookmark-content"><div class="kg-bookmark-title">Title</div><div class="kg-bookmark-description">Description</div><div class="kg-bookmark-metadata"><span class="kg-bookmark-author">Author</span><span class="kg-bookmark-publisher">Publisher</span></div></div><div class="kg-bookmark-thumbnail"><img src="http://exampple.com/thumbnail.png"></div></a><figcaption>Caption</figcaption></figure><!--kg-card-end: bookmark-->');
});
it('skips thumbnail when missing', function () {
let opts = {
env: {dom: new SimpleDom.Document()},
payload: {
metadata: {
url: 'http://example.com',
title: 'Title',
description: 'Description',
icon: 'http://example.com/icon.png',
thumbnail: null,
author: 'Author',
publisher: 'Publisher'
},
caption: 'Caption'
}
};
serializer.serialize(card.render(opts))
.should.equal('<!--kg-card-begin: bookmark--><figure class="kg-card kg-bookmark-card kg-card-hascaption"><a class="kg-bookmark-container" href="http://example.com"><div class="kg-bookmark-content"><div class="kg-bookmark-title">Title</div><div class="kg-bookmark-description">Description</div><div class="kg-bookmark-metadata"><img class="kg-bookmark-icon" src="http://example.com/icon.png"><span class="kg-bookmark-author">Author</span><span class="kg-bookmark-publisher">Publisher</span></div></div></a><figcaption>Caption</figcaption></figure><!--kg-card-end: bookmark-->');
});
it('skips author when missing', function () {
let opts = {
env: {dom: new SimpleDom.Document()},
payload: {
metadata: {
url: 'http://example.com',
title: 'Title',
description: 'Description',
icon: 'http://example.com/icon.png',
thumbnail: 'http://exampple.com/thumbnail.png',
author: null,
publisher: 'Publisher'
},
caption: 'Caption'
}
};
serializer.serialize(card.render(opts))
.should.equal('<!--kg-card-begin: bookmark--><figure class="kg-card kg-bookmark-card kg-card-hascaption"><a class="kg-bookmark-container" href="http://example.com"><div class="kg-bookmark-content"><div class="kg-bookmark-title">Title</div><div class="kg-bookmark-description">Description</div><div class="kg-bookmark-metadata"><img class="kg-bookmark-icon" src="http://example.com/icon.png"><span class="kg-bookmark-publisher">Publisher</span></div></div><div class="kg-bookmark-thumbnail"><img src="http://exampple.com/thumbnail.png"></div></a><figcaption>Caption</figcaption></figure><!--kg-card-end: bookmark-->');
});
it('skips publisher when missing', function () {
let opts = {
env: {dom: new SimpleDom.Document()},
payload: {
metadata: {
url: 'http://example.com',
title: 'Title',
description: 'Description',
icon: 'http://example.com/icon.png',
thumbnail: 'http://exampple.com/thumbnail.png',
author: 'Author',
publisher: null
},
caption: 'Caption'
}
};
serializer.serialize(card.render(opts))
.should.equal('<!--kg-card-begin: bookmark--><figure class="kg-card kg-bookmark-card kg-card-hascaption"><a class="kg-bookmark-container" href="http://example.com"><div class="kg-bookmark-content"><div class="kg-bookmark-title">Title</div><div class="kg-bookmark-description">Description</div><div class="kg-bookmark-metadata"><img class="kg-bookmark-icon" src="http://example.com/icon.png"><span class="kg-bookmark-author">Author</span></div></div><div class="kg-bookmark-thumbnail"><img src="http://exampple.com/thumbnail.png"></div></a><figcaption>Caption</figcaption></figure><!--kg-card-end: bookmark-->');
});
it('skips caption when missing', function () {
let opts = {
env: {dom: new SimpleDom.Document()},
payload: {
metadata: {
url: 'http://example.com',
title: 'Title',
description: 'Description',
icon: 'http://example.com/icon.png',
thumbnail: 'http://exampple.com/thumbnail.png',
author: 'Author',
publisher: 'Publisher'
},
caption: ''
}
};
serializer.serialize(card.render(opts))
.should.equal('<!--kg-card-begin: bookmark--><figure class="kg-card kg-bookmark-card"><a class="kg-bookmark-container" href="http://example.com"><div class="kg-bookmark-content"><div class="kg-bookmark-title">Title</div><div class="kg-bookmark-description">Description</div><div class="kg-bookmark-metadata"><img class="kg-bookmark-icon" src="http://example.com/icon.png"><span class="kg-bookmark-author">Author</span><span class="kg-bookmark-publisher">Publisher</span></div></div><div class="kg-bookmark-thumbnail"><img src="http://exampple.com/thumbnail.png"></div></a></figure><!--kg-card-end: bookmark-->');
});
it('renders nothing when payload is undefined', function () {
let opts = {
env: {dom: new SimpleDom.Document()},
payload: {
metadata: undefined
}
};
serializer.serialize(card.render(opts))
.should.equal('');
});
it('renders nothing when payload metadata is empty', function () {
let opts = {
env: {dom: new SimpleDom.Document()},
payload: {
metadata: {}
}
};
serializer.serialize(card.render(opts))
.should.equal('');
});
it('renders nothing when url is missing', function () {
let opts = {
env: {dom: new SimpleDom.Document()},
payload: {
metadata: {
url: null,
title: 'Test bookmark',
description: 'This is just a test'
}
}
};
serializer.serialize(card.render(opts))
.should.equal('');
});
it('renders nothing when title is missing', function () {
let opts = {
env: {dom: new SimpleDom.Document()},
payload: {
metadata: {
url: 'http://example.com',
title: null,
description: 'This is just a test'
}
}
};
serializer.serialize(card.render(opts))
.should.equal('');
});
it('renders nothing when description is missing', function () {
let opts = {
env: {dom: new SimpleDom.Document()},
payload: {
metadata: {
url: 'http://example.com',
title: 'Test bookmark',
description: null
}
}
};
serializer.serialize(card.render(opts))
.should.equal('');
});
});

View file

@ -4,60 +4,58 @@ const SimpleDom = require('simple-dom');
const serializer = new SimpleDom.HTMLSerializer(SimpleDom.voidMap);
describe('Markdown card', function () {
describe('default', function () {
it('Markdown Card renders', function () {
let opts = {
env: {
dom: new SimpleDom.Document()
},
payload: {
markdown: '#HEADING\r\n- list\r\n- items'
}
};
it('renders', function () {
let opts = {
env: {
dom: new SimpleDom.Document()
},
payload: {
markdown: '#HEADING\r\n- list\r\n- items'
}
};
serializer.serialize(card.render(opts)).should.eql('<!--kg-card-begin: markdown--><h1 id="heading">HEADING</h1>\n<ul>\n<li>list</li>\n<li>items</li>\n</ul>\n<!--kg-card-end: markdown-->');
});
serializer.serialize(card.render(opts)).should.eql('<!--kg-card-begin: markdown--><h1 id="heading">HEADING</h1>\n<ul>\n<li>list</li>\n<li>items</li>\n</ul>\n<!--kg-card-end: markdown-->');
});
it('Accepts invalid HTML in markdown', function () {
let opts = {
env: {
dom: new SimpleDom.Document()
},
payload: {
markdown: '#HEADING\r\n<h2>Heading 2>'
}
};
it('Accepts invalid HTML in markdown', function () {
let opts = {
env: {
dom: new SimpleDom.Document()
},
payload: {
markdown: '#HEADING\r\n<h2>Heading 2>'
}
};
serializer.serialize(card.render(opts)).should.eql('<!--kg-card-begin: markdown--><h1 id="heading">HEADING</h1>\n<h2>Heading 2><!--kg-card-end: markdown-->');
});
serializer.serialize(card.render(opts)).should.eql('<!--kg-card-begin: markdown--><h1 id="heading">HEADING</h1>\n<h2>Heading 2><!--kg-card-end: markdown-->');
});
it('Renders nothing when payload is undefined', function () {
let opts = {
env: {
dom: new SimpleDom.Document()
},
payload: {
markdown: undefined
}
};
it('Renders nothing when payload is undefined', function () {
let opts = {
env: {
dom: new SimpleDom.Document()
},
payload: {
markdown: undefined
}
};
serializer.serialize(card.render(opts)).should.eql('');
});
serializer.serialize(card.render(opts)).should.eql('');
});
it('[deprecated] version 1', function () {
let opts = {
env: {
dom: new SimpleDom.Document()
},
payload: {
markdown: '#HEADING\r\n- list\r\n- items'
},
options: {
version: 1
}
};
it('[deprecated] version 1', function () {
let opts = {
env: {
dom: new SimpleDom.Document()
},
payload: {
markdown: '#HEADING\r\n- list\r\n- items'
},
options: {
version: 1
}
};
serializer.serialize(card.render(opts)).should.eql('<!--kg-card-begin: markdown--><div class="kg-card-markdown"><h1 id="heading">HEADING</h1>\n<ul>\n<li>list</li>\n<li>items</li>\n</ul>\n</div><!--kg-card-end: markdown-->');
});
serializer.serialize(card.render(opts)).should.eql('<!--kg-card-begin: markdown--><div class="kg-card-markdown"><h1 id="heading">HEADING</h1>\n<ul>\n<li>list</li>\n<li>items</li>\n</ul>\n</div><!--kg-card-end: markdown-->');
});
});