|
| 1 | +var $ = require("jquery"); |
| 2 | +var assert = require("chai/chai").assert; |
| 3 | +var TOCContainer = require("../toc-container-control"); |
| 4 | + |
| 5 | +require("steal-mocha"); |
| 6 | + |
| 7 | +describe("TOCContainer", function() { |
| 8 | + var $el, $testArea; |
| 9 | + |
| 10 | + beforeEach(function() { |
| 11 | + $("body").append('<div id="toc-container"></div>'); |
| 12 | + |
| 13 | + $el = $("#toc-container"); |
| 14 | + $testArea = $("#test-area"); |
| 15 | + }); |
| 16 | + |
| 17 | + afterEach(function() { |
| 18 | + $el.remove(); |
| 19 | + $testArea.empty(); |
| 20 | + }); |
| 21 | + |
| 22 | + it("by default gets headings from 'article' children", function() { |
| 23 | + var headings = [ |
| 24 | + '<article>', |
| 25 | + "<h2>Usage</h2>", |
| 26 | + "<h2>Install</h2>", |
| 27 | + "<h2>Configure</h2>", |
| 28 | + "</article>" |
| 29 | + ]; |
| 30 | + |
| 31 | + // append the headings to the DOM and then instantiate the control |
| 32 | + $("body").append(headings.join("")); |
| 33 | + new TOCContainer($el.get(0)); |
| 34 | + |
| 35 | + var $items = $("#toc-container ul"); |
| 36 | + assert.equal( |
| 37 | + $items.html(), |
| 38 | + [ |
| 39 | + '<li><a href="#usage">Usage</a></li>', |
| 40 | + '<li><a href="#install">Install</a></li>', |
| 41 | + '<li><a href="#configure">Configure</a></li>', |
| 42 | + ].join(""), |
| 43 | + "should create table of contents from the headings inside container" |
| 44 | + ); |
| 45 | + |
| 46 | + // remove headings container from the DOM |
| 47 | + $("article").remove(); |
| 48 | + }); |
| 49 | + |
| 50 | + it("reads the headings container selector from the element", function() { |
| 51 | + // set the container selector as a data-* attribute |
| 52 | + $el.attr( |
| 53 | + "data-headings-container-selector", |
| 54 | + ".my-custom-container" |
| 55 | + ); |
| 56 | + |
| 57 | + var headings = [ |
| 58 | + '<div class="my-custom-container">', |
| 59 | + "<h2>Usage</h2>", |
| 60 | + "<h2>Install</h2>", |
| 61 | + "<h2>Configure</h2>", |
| 62 | + "<h2>Configure</h2>", |
| 63 | + "</div>" |
| 64 | + ]; |
| 65 | + |
| 66 | + // append the headings to the DOM and then instantiate the control |
| 67 | + $("body").append(headings.join("")); |
| 68 | + new TOCContainer($el.get(0)); |
| 69 | + |
| 70 | + var $items = $("#toc-container ul"); |
| 71 | + assert.equal( |
| 72 | + $items.html(), |
| 73 | + [ |
| 74 | + '<li><a href="#usage">Usage</a></li>', |
| 75 | + '<li><a href="#install">Install</a></li>', |
| 76 | + '<li><a href="#configure">Configure</a></li>', |
| 77 | + '<li><a href="#configure-1">Configure</a></li>' |
| 78 | + ].join(""), |
| 79 | + "should create table of contents from the headings inside container" |
| 80 | + ); |
| 81 | + |
| 82 | + // remove headings container from the DOM |
| 83 | + $(".my-custom-container").remove(); |
| 84 | + }); |
| 85 | +}); |
0 commit comments