Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GN-137 Add tests #158

Merged
merged 9 commits into from
Dec 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 7 additions & 67 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion src/Header/Account/Account.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ describe("Account", () => {
);

wrapper.find("#my-account-button").simulate("click", { pageX: 99 });
// wrapper.instance().forceUpdate();
wrapper.update();

expect(wrapper.find("#my-account-button").props()["aria-expanded"]).toBe(
Expand Down
2 changes: 1 addition & 1 deletion src/Header/Nav/Dropdown/Components/Guidance.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ export function Guidance() {
</li>
<li>
<h4 className="h5 mb--c">
<a href={`${baseUrl}/proposed`}>Proposed</a>
<a href={`${baseUrl}/proposed`}>Awaiting development</a>
</h4>
<p className="mt--0">
Guidance and quality standards that have been proposed for
Expand Down
70 changes: 70 additions & 0 deletions src/Header/Nav/Dropdown/Dropdown.test.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import React from "react";
import { shallow } from "enzyme";
import { Dropdown } from "./Dropdown";
import { AboutUs } from "./Components/AboutUs";

describe("Dropdown", () => {
const defaultProps = {
component: "AboutUs", // Needs to be present in the dropdown key values in the component
text: "Our Dropdown",
id: "our-dropdown",
};

it("Renders without crashing", () => {
const wrapper = shallow(<Dropdown {...defaultProps} />);
expect(wrapper).toHaveLength(1);
});

it("Should have a data-tracking attribute on the wrapper", () => {
const wrapper = shallow(<Dropdown {...defaultProps} />);
const trackingDiv = wrapper.find("div[data-tracking]");
expect(trackingDiv.length).toEqual(1);
expect(trackingDiv.props()["data-tracking"]).toEqual(
"Our Dropdown dropdown"
);
});

it("should render a component by the supplied component string", () => {
const wrapper = shallow(<Dropdown {...defaultProps} />);
expect(wrapper.find(AboutUs).length).toEqual(1);
});

it("Should render a skip link when there is a next nav item to skip to", () => {
const closingFunction = jest.fn();
const preventer = jest.fn();
const wrapper = shallow(
<Dropdown
{...defaultProps}
nextNavSlug="next-thing"
closeDropdown={closingFunction}
/>
);
const skipButton = wrapper.find("a[href='#next-thing']");
expect(skipButton.length).toEqual(1);
expect(skipButton.text()).toBe("Skip Our Dropdown submenu");
skipButton
.props()
.onClick({ preventDefault: preventer, target: { hash: "#myButton" } });
expect(closingFunction).toHaveBeenCalled();
expect(preventer).toHaveBeenCalled();
});

it("Should not render a skip link when there is no next nav item to skip to", () => {
const wrapper = shallow(<Dropdown {...defaultProps} />);
expect(wrapper.find("a[href='#next-thing']").length).toEqual(0);
});

it("should fire onClosing when the close dropdown button is clicked", () => {
const closingFunction = jest.fn();

const wrapper = shallow(
<Dropdown {...defaultProps} closeDropdown={closingFunction} />
);

const closeButton = wrapper.find("button");
expect(closeButton.props()["data-tracking"]).toEqual("Close menu");

closeButton.props().onClick();
expect(closingFunction).toHaveBeenCalled();
});
});
71 changes: 0 additions & 71 deletions src/Header/Nav/Nav.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,34 +53,6 @@ describe("Nav", () => {
expect(toJson(wrapper.find("nav").at(1))).toMatchSnapshot();
});

it("Matches snapshot with sub links for selected external service", () => {
const wrapper = mount(
<Nav {...defaultProps} service={externalServices[1].id} />
);
expect(toJson(wrapper)).toMatchSnapshot();
});

// it("Adds aria-current=true attribute for selected service", () => {
// const wrapper = shallow(
// <Nav {...defaultProps} service={externalServices[1].id} />
// );
// expect(wrapper.find("a").at(1).props()["aria-current"]).toEqual(true);
// });

// it("Adds aria-current=page attribute for selected service when matches current URL", () => {
// const oldLocation = global.window.location;
// delete global.window.location;
// global.window.location = new URL(externalServices[1].href);

// const wrapper = shallow(
// <Nav {...defaultProps} service={externalServices[1].id} />
// );
// expect(wrapper.find("a").at(1).props()["aria-current"]).toEqual("page");

// // Tidy up
// global.window.location = oldLocation;
// });

it("Add expanded class to root element when passed isExpanded prop", () => {
const wrapper = shallow(<Nav {...defaultProps} isExpanded={false} />);
expect(wrapper.props().className).not.toContain("wrapperExpanded");
Expand All @@ -98,49 +70,6 @@ describe("Nav", () => {
delete window.dataLayer;
});

// it("should push dataLayer event for nav item click", () => {
// const wrapper = shallow(<Nav {...defaultProps} isExpanded={false} />);

// wrapper.find("a[href='https://url1/']").simulate("click", {
// preventDefault: () => {},
// currentTarget: {
// // Mock e.currentTarget.getAttribute("href")
// getAttribute: () => "",
// textContent: "First link",
// },
// });

// expect(window.dataLayer).toEqual([
// {
// event: eventName,
// eventCategory: defaultEventCategory,
// eventAction: headerClickEventAction,
// eventLabel: "First link",
// eventCallback: expect.any(Function),
// eventTimeout: eventTimeout,
// },
// ]);
// });

// it("should prevent default and navigate in event callback on nav item click", () => {
// const wrapper = shallow(<Nav {...defaultProps} isExpanded={false} />);

// const preventDefault = jest.fn();

// wrapper.find("a[href='https://url1/']").simulate("click", {
// preventDefault: preventDefault,
// currentTarget: {
// // Mock e.currentTarget.getAttribute("href")
// getAttribute: () => "https://url1/",
// },
// });

// expect(preventDefault).toHaveBeenCalled();

// window.dataLayer[0].eventCallback();
// expect(window.location.href).toEqual("https://url1/");
// });

describe("Accounts links", () => {
const accountsLinks = {
"Joe Bloggs": "https://accounts.nice.org.uk/users/12345/editprofile",
Expand Down
7 changes: 4 additions & 3 deletions src/Header/Nav/NavLinks/NavLinks.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState, useEffect, useContext } from "react";
import React, { useEffect } from "react";
import classnames from "classnames";
import FocusTrap from "focus-trap-react";
import PropTypes from "prop-types";
Expand All @@ -23,8 +23,9 @@ export function NavLinks({
onNavigating,
skipLinkId,
}) {
const [canUseDOM, setCanUseDOM] = useState(false);
const { idOfOpenDropdown, setidOfOpenDropdown } = useContext(HeaderContext);
const [canUseDOM, setCanUseDOM] = React.useState(false);
const { idOfOpenDropdown, setidOfOpenDropdown } =
React.useContext(HeaderContext);

useEffect(() => {
setCanUseDOM(true);
Expand Down
Loading