From b32cdd5a2ad67f29e5fd287eab86dfdedf9dd9c0 Mon Sep 17 00:00:00 2001 From: Mcat12 Date: Sat, 7 Jul 2018 18:50:29 -0400 Subject: [PATCH] Add Sidebar tests Signed-off-by: Mcat12 --- src/components/Sidebar.test.js | 102 +++++++++++++++++++++++++++++---- 1 file changed, 92 insertions(+), 10 deletions(-) diff --git a/src/components/Sidebar.test.js b/src/components/Sidebar.test.js index d8031f5..c34602d 100644 --- a/src/components/Sidebar.test.js +++ b/src/components/Sidebar.test.js @@ -8,29 +8,111 @@ * This file is copyright under the latest version of the EUPL. * Please see LICENSE file for your rights under this license. */ -import React, { Fragment } from "react"; +import React from "react"; import { shallow } from "enzyme"; -import { dropDownClassList, navItem } from "./Sidebar"; +import { dropDownClassList, navDropdown, navItem, navList } from "./Sidebar"; +import { api } from "../utils"; it("expands active drop down items", () => { - const classList = dropDownClassList("testRoute", { location: { pathname: "/someRoute/testRoute/page" } }); + const classList = dropDownClassList("/testRoute", { location: { pathname: "/testRoute/page" } }); expect(classList).toContain("open"); }); it("does not expand inactive drop down items", () => { - const classList = dropDownClassList("testRoute", { location: { pathname: "/someRoute/page" } }); + const classList = dropDownClassList("/testRoute", { location: { pathname: "/page" } }); expect(classList).not.toContain("open"); }); it("creates nav items with correct data", () => { - const itemData = { url: "/testUrl", icon: "test-icon", name: "testName" }; + const item = { url: "/testUrl", icon: "test-icon", name: "testName" }; const key = "testKey"; - const item = shallow(React.createElement(() => navItem(itemData, key, { t: key => key }))); + const wrapper = shallow(React.createElement(() => navItem(item, key, { t: key => key }))); - expect(item.key()).toEqual(key); - expect(item.childAt(0)).toHaveProp("to", itemData.url); - expect(item.childAt(0).childAt(0)).toHaveClassName(itemData.icon); - expect(item.childAt(0).childAt(1)).toHaveText(itemData.name); + expect(wrapper.key()).toEqual(key); + expect(wrapper.childAt(0)).toHaveProp("to", item.url); + expect(wrapper.childAt(0).childAt(0)).toHaveClassName(item.icon); + expect(wrapper.childAt(0).childAt(1)).toHaveText(item.name); +}); + +it("creates a nav dropdown with correct data", () => { + const item = { + name: "Blacklist", + url: "/blacklist", + icon: "fa fa-ban", + auth: false, + children: [] + }; + const key = "testKey"; + const props = { + t: key => key, + location: { + pathname: "/blacklist/exact" + } + }; + const wrapper = shallow(React.createElement(() => navDropdown(item, key, props))); + + expect(wrapper.key()).toEqual(key); + expect(wrapper.childAt(0).childAt(0)).toHaveClassName(item.icon); + expect(wrapper.childAt(0).childAt(1)).toHaveText(item.name); + expect(wrapper.childAt(1).children()).toHaveLength(0); +}); + +it("shows auth routes when logged in", () => { + api.loggedIn = true; + + const item = { + name: "Query Log", + url: "/query-log", + icon: "fa fa-database", + auth: true + }; + const wrapper = shallow(React.createElement(() => )); + + expect(wrapper.children()).toHaveLength(1); +}); + +it("hides auth routes when not logged in", () => { + api.loggedIn = false; + + const item = { + name: "Query Log", + url: "/query-log", + icon: "fa fa-database", + auth: true + }; + const wrapper = shallow(React.createElement(() => )); + + expect(wrapper.children()).toHaveLength(0); +}); + +it("hides strict non-auth routes when logged in", () => { + api.loggedIn = true; + + const item = { + name: "Login", + url: "/login", + icon: "fa fa-user", + auth: false, + authStrict: true + }; + const wrapper = shallow(React.createElement(() => )); + + expect(wrapper.children()).toHaveLength(0); +}); + +it("hides strict auth routes when not logged in", () => { + api.loggedIn = false; + + const item = { + name: "Logout", + url: "/logout", + icon: "fa fa-user-times", + auth: true, + authStrict: true + }; + const wrapper = shallow(React.createElement(() => )); + + expect(wrapper.children()).toHaveLength(0); });