|
| 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