Skip to content
This repository was archived by the owner on Feb 12, 2024. It is now read-only.

Commit 066f200

Browse files
committed
Add tests for some of the example components
1 parent b3af469 commit 066f200

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
"devDependencies": {
3434
"chai": "^3.5.0",
3535
"jshint": "^2.9.2",
36+
"linkifyjs": "^2.0.0",
3637
"mocha": "^2.4.5",
3738
"np": "pimterry/np",
3839
"watch": "^0.18.0"

test/example-components.js

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
var expect = require('chai').expect;
2+
var components = require("../src/index.js");
3+
4+
var linkify = require("linkifyjs/element");
5+
6+
describe("An example component:", () => {
7+
describe("using static rendering", () => {
8+
before(() => {
9+
var StaticElement = components.newElement();
10+
StaticElement.createdCallback = function () {
11+
this.innerHTML = "Hi there";
12+
};
13+
14+
components.registerElement("my-greeting", { prototype: StaticElement });
15+
});
16+
17+
it("replaces its content with the given text", () => {
18+
return components.renderFragment("<my-greeting></my-greeting>").then((output) => {
19+
expect(output).to.equal("<my-greeting>Hi there</my-greeting>");
20+
});
21+
});
22+
});
23+
24+
describe("using dynamic logic for rendering", () => {
25+
before(() => {
26+
var CounterElement = components.newElement();
27+
var currentCount = 0;
28+
29+
CounterElement.createdCallback = function () {
30+
currentCount += 1;
31+
this.innerHTML = "There have been " + currentCount + " visitors.";
32+
};
33+
34+
components.registerElement("visitor-counter", { prototype: CounterElement });
35+
});
36+
37+
it("dynamically changes its content", () => {
38+
components.renderFragment("<visitor-counter></visitor-counter>");
39+
components.renderFragment("<visitor-counter></visitor-counter>");
40+
components.renderFragment("<visitor-counter></visitor-counter>");
41+
42+
return components.renderFragment("<visitor-counter></visitor-counter>").then((output) => {
43+
expect(output).to.equal(
44+
"<visitor-counter>There have been 4 visitors.</visitor-counter>"
45+
);
46+
});
47+
});
48+
});
49+
50+
describe("parameterised by HTML content", () => {
51+
before(() => {
52+
var LinkifyElement = components.newElement();
53+
54+
LinkifyElement.createdCallback = function (document) {
55+
// Delegate the whole thing to a real normal front-end library!
56+
linkify(this, { target: () => null, linkClass: "autolinked" }, document);
57+
};
58+
59+
components.registerElement("linkify-urls", { prototype: LinkifyElement });
60+
});
61+
62+
it("should be able to parse and manipulate it's content", () => {
63+
return components.renderFragment(
64+
"<linkify-urls>Have you heard of www.facebook.com?</linkify-urls>"
65+
).then((output) => expect(output).to.equal(
66+
'<linkify-urls>Have you heard of <a href="http://www.facebook.com" class="autolinked">www.facebook.com</a>?</linkify-urls>'
67+
));
68+
});
69+
});
70+
});

0 commit comments

Comments
 (0)