|
| 1 | +import React from "react"; |
| 2 | +import { render, fireEvent, cleanup } from "@testing-library/react"; |
| 3 | +import { expect, sinon } from "../../test/test-helpers"; |
| 4 | +import Link from "./Link"; |
| 5 | + |
| 6 | +describe("<Link />", () => { |
| 7 | + const ariaLabel = "Read more about the interesting article"; |
| 8 | + const text = "Read More"; |
| 9 | + const href = "https://www.monday.com"; |
| 10 | + let onClickStub; |
| 11 | + beforeEach(() => { |
| 12 | + onClickStub = sinon.stub(); |
| 13 | + }); |
| 14 | + |
| 15 | + afterEach(() => { |
| 16 | + cleanup(); |
| 17 | + onClickStub.reset(); |
| 18 | + }); |
| 19 | + |
| 20 | + describe("default props", () => { |
| 21 | + it("should open link in a new window", () => { |
| 22 | + const { getByText } = render(<Link text={text} />); |
| 23 | + const element = getByText(text).closest("a"); |
| 24 | + expect(element.target).to.equal(Link.target.NEW_WINDOW); |
| 25 | + }); |
| 26 | + it("should open link to have noreferrer attribute", () => { |
| 27 | + const { getByText } = render(<Link text={text} />); |
| 28 | + const element = getByText(text).closest("a"); |
| 29 | + expect(element.rel).to.equal("noreferrer"); |
| 30 | + }); |
| 31 | + }); |
| 32 | + |
| 33 | + it("should call onClick", () => { |
| 34 | + const { getByText } = render(<Link text={text} onClick={onClickStub} />); |
| 35 | + const element = getByText(text).closest("a"); |
| 36 | + fireEvent.click(element); |
| 37 | + expect(onClickStub).to.be.called; |
| 38 | + }); |
| 39 | + |
| 40 | + it("should have the correct href", () => { |
| 41 | + const { getByText } = render(<Link text={text} href={href} />); |
| 42 | + const element = getByText(text).closest("a"); |
| 43 | + expect(element.href).to.equal(`${href}/`); |
| 44 | + }); |
| 45 | + |
| 46 | + it("should have the correct target", () => { |
| 47 | + const target = Link.target.SELF; |
| 48 | + const { getByText } = render( |
| 49 | + <Link text={text} href={href} target={target} /> |
| 50 | + ); |
| 51 | + const element = getByText(text).closest("a"); |
| 52 | + expect(element.target).to.equal(target); |
| 53 | + }); |
| 54 | + |
| 55 | + it("should apply aria label correctly", () => { |
| 56 | + const { getByLabelText } = render( |
| 57 | + <Link text={text} ariaLabelDescription={ariaLabel} /> |
| 58 | + ); |
| 59 | + const element = getByLabelText(ariaLabel); |
| 60 | + expect(element).to.be.ok; |
| 61 | + }); |
| 62 | + |
| 63 | + describe("Icons", () => { |
| 64 | + it("Should present the icon", () => { |
| 65 | + const { container } = render( |
| 66 | + <Link ariaLabelDescription={ariaLabel} iconName="icon-name" /> |
| 67 | + ); |
| 68 | + const element = container.querySelector(".icon_component"); |
| 69 | + expect(element).to.be.ok; |
| 70 | + }); |
| 71 | + |
| 72 | + it("Icon should be before of the text", () => { |
| 73 | + const { container } = render( |
| 74 | + <Link |
| 75 | + text={text} |
| 76 | + ariaLabelDescription={ariaLabel} |
| 77 | + iconName="icon-name" |
| 78 | + /> |
| 79 | + ); |
| 80 | + const textElement = container.querySelector(".monday-style-link--text"); |
| 81 | + const iconElement = container.querySelector(".icon_component"); |
| 82 | + const textBox = textElement.getBoundingClientRect(); |
| 83 | + const iconBox = iconElement.getBoundingClientRect(); |
| 84 | + expect(textBox.left > iconBox.left).to.equal(true); |
| 85 | + }); |
| 86 | + |
| 87 | + it("Icon should be after of the text", () => { |
| 88 | + const { container } = render( |
| 89 | + <Link |
| 90 | + text={text} |
| 91 | + ariaLabelDescription={ariaLabel} |
| 92 | + iconName="icon-name" |
| 93 | + iconPosition={Link.position.END} |
| 94 | + /> |
| 95 | + ); |
| 96 | + const textElement = container.querySelector(".monday-style-link--text"); |
| 97 | + const iconElement = container.querySelector(".icon_component"); |
| 98 | + const textBox = textElement.getBoundingClientRect(); |
| 99 | + const iconBox = iconElement.getBoundingClientRect(); |
| 100 | + expect(textBox.left < iconBox.left).to.equal(true); |
| 101 | + }); |
| 102 | + }); |
| 103 | +}); |
0 commit comments