const test = require("ava"); const EleventyNavigation = require("../eleventy-navigation"); test("Empty navigation", t => { t.deepEqual(EleventyNavigation.findNavigationEntries(), []); }); test("One root page navigation", t => { let obj = EleventyNavigation.findNavigationEntries([ { data: { eleventyNavigation: { key: "root1" }, page: { url: "root1.html" } } } ]); t.is(obj[0].key, "root1"); t.is(obj[0].pluginType, "eleventy-navigation"); // Warning, title must be preserved per the public API t.is(obj[0].title, "root1"); // Warning, url must be preserved per the public API t.is(obj[0].url, "root1.html"); t.is(obj[0].children.length, 0); }); test("One root page navigation with separate title", t => { let obj = EleventyNavigation.findNavigationEntries([ { data: { eleventyNavigation: { key: "root1", title: "Another title" }, page: { url: "root1.html" } } } ]); t.is(obj[0].key, "root1"); t.is(obj[0].pluginType, "eleventy-navigation"); t.is(obj[0].title, "Another title"); t.is(obj[0].children.length, 0); }); test("One root, one child page navigation", t => { let obj = EleventyNavigation.findNavigationEntries([ { data: { eleventyNavigation: { key: "root1" }, page: { url: "root1.html" } } }, { data: { eleventyNavigation: { parent: "root1", key: "child1" }, page: { url: "child1.html" } } } ]); t.is(obj[0].key, "root1"); t.is(obj[0].children.length, 1); t.is(obj[0].children[0].parent, "root1"); t.is(obj[0].children[0].key, "child1"); }); test("Three layers deep navigation", t => { let obj = EleventyNavigation.findNavigationEntries([ { data: { eleventyNavigation: { key: "root1" }, page: { url: "root1.html" } } }, { data: { eleventyNavigation: { parent: "root1", key: "child1" }, page: { url: "child1.html" } } }, { data: { eleventyNavigation: { parent: "child1", key: "grandchild1" }, page: { url: "grandchild1.html" } } } ]); t.is(obj[0].key, "root1"); t.is(obj[0].children.length, 1); t.is(obj[0].children[0].parent, "root1"); t.is(obj[0].children[0].key, "child1"); t.is(obj[0].children[0].children[0].parent, "child1"); t.is(obj[0].children[0].children[0].key, "grandchild1"); }); test("One root, three child navigation (order)", t => { let obj = EleventyNavigation.findNavigationEntries([ { data: { eleventyNavigation: { key: "root1" }, page: { url: "root1.html" } } }, { data: { eleventyNavigation: { parent: "root1", key: "child1", order: 3 }, page: { url: "child1.html" } } }, { data: { eleventyNavigation: { parent: "root1", key: "child2", order: 1 }, page: { url: "child2.html" } } }, { data: { eleventyNavigation: { parent: "root1", key: "child3", order: 2 }, page: { url: "child3.html" } } }, { data: { eleventyNavigation: { parent: "root1", key: "child4" }, page: { url: "child4.html" } } } ]); t.is(obj[0].key, "root1"); t.is(obj[0].children.length, 4); t.is(obj[0].children.map(e => e.key).join(","), "child2,child3,child1,child4"); }); test("One root, three child navigation, one with 0 (order)", t => { let obj = EleventyNavigation.findNavigationEntries([ { data: { eleventyNavigation: { key: "root1" }, page: { url: "root1.html" } } }, { data: { eleventyNavigation: { parent: "root1", key: "child1", order: 3 }, page: { url: "child1.html" } } }, { data: { eleventyNavigation: { parent: "root1", key: "child2", order: 0 }, page: { url: "child2.html" } } }, { data: { eleventyNavigation: { parent: "root1", key: "child3", order: 2 }, page: { url: "child3.html" } } }, { data: { eleventyNavigation: { parent: "root1", key: "child4" }, page: { url: "child4.html" } } } ]); t.is(obj[0].key, "root1"); t.is(obj[0].children.length, 4); t.is(obj[0].children.map(e => e.key).join(","), "child2,child3,child1,child4"); }); test("One root, three child navigation (implied order)", t => { let obj = EleventyNavigation.findNavigationEntries([ { data: { eleventyNavigation: { key: "root1" }, page: { url: "root1.html" } } }, { data: { eleventyNavigation: { parent: "root1", key: "child1", order: 3 }, page: { url: "child1.html" } } }, { data: { eleventyNavigation: { parent: "root1", key: "child2" }, page: { url: "child2.html" } } }, { data: { eleventyNavigation: { parent: "root1", key: "child3", order: -1 }, page: { url: "child3.html" } } } ]); t.is(obj[0].key, "root1"); t.is(obj[0].children.length, 3); t.is(obj[0].children.map(e => e.key).join(","), "child3,child1,child2"); }); test("Show throw an error without a config", t => { let obj = EleventyNavigation.findNavigationEntries([ { data: { eleventyNavigation: { key: "root1" }, page: { url: "root1.html" } } } ]); t.throws(() => { EleventyNavigation.toHtml(obj); }); }); let fakeConfig = { nunjucksFilters: { url: url => url } }; let fakeNavigationEntries = [ { data: { eleventyNavigation: { key: "root1" }, page: { url: "root1.html" } } }, { data: { eleventyNavigation: { parent: "root1", key: "child1" }, page: { url: "child1.html" } } } ]; let fakeNavigationEntriesEmptyUrl = [ { data: { eleventyNavigation: { key: "root1" }, page: { url: "root1.html" } } }, { data: { eleventyNavigation: { parent: "root1", key: "child1" }, page: { url: false } } } ]; test("Checking active class on output HTML", t => { let obj = EleventyNavigation.findNavigationEntries(fakeNavigationEntries); let html = EleventyNavigation.toHtml.call(fakeConfig, obj); t.is(html, `
`); let activeHtmlItem = EleventyNavigation.toHtml.call(fakeConfig, obj, { activeKey: "child1", activeListItemClass: "this-is-the-active-item" }); t.is(activeHtmlItem, ``); let activeHtmlAnchor = EleventyNavigation.toHtml.call(fakeConfig, obj, { activeKey: "child1", activeAnchorClass: "this-is-the-active-anchor" }); t.is(activeHtmlAnchor, ``); let activeHtmlItemAndAnchor = EleventyNavigation.toHtml.call(fakeConfig, obj, { activeKey: "child1", activeListItemClass: "this-is-the-active-item", activeAnchorClass: "this-is-the-active-anchor" }); t.is(activeHtmlItemAndAnchor, ``); }); test("Checking aria-current option on output HTML", t => { let obj = EleventyNavigation.findNavigationEntries(fakeNavigationEntries); let html = EleventyNavigation.toHtml.call(fakeConfig, obj); t.true(html.indexOf(`