|
| 1 | +import React from "react"; |
| 2 | +import { act, render, waitFor, fireEvent } from "@testing-library/react"; |
| 3 | +import { Anchor } from "./anchor"; |
| 4 | +import faker from "faker"; |
| 5 | +import { MemoryRouter } from "react-router-dom"; |
| 6 | + |
| 7 | +describe("anchor", () => { |
| 8 | + it("when default props, renders link with correct url", async () => { |
| 9 | + // Arrange |
| 10 | + const expected = `/some/random/path/${faker.random.word()}`; |
| 11 | + |
| 12 | + // Act |
| 13 | + const { container } = render( |
| 14 | + <MemoryRouter> |
| 15 | + <Anchor to={expected} /> |
| 16 | + </MemoryRouter> |
| 17 | + ); |
| 18 | + |
| 19 | + // Assert |
| 20 | + await waitFor(() => { |
| 21 | + expect( |
| 22 | + container.querySelector(`[href="${expected}"]`) |
| 23 | + ).not.toBeNull(); |
| 24 | + }); |
| 25 | + }); |
| 26 | + |
| 27 | + it("when cssClassName provided, assigned class property", async () => { |
| 28 | + // Arrange |
| 29 | + const expected = faker.random.words(); |
| 30 | + const url = faker.internet.url(); |
| 31 | + |
| 32 | + // Act |
| 33 | + const { container } = render( |
| 34 | + <MemoryRouter> |
| 35 | + <Anchor to={url} cssClassName={expected} /> |
| 36 | + </MemoryRouter> |
| 37 | + ); |
| 38 | + |
| 39 | + // Assert |
| 40 | + await waitFor(() => { |
| 41 | + expect( |
| 42 | + container.querySelector(`[class="${expected}"]`) |
| 43 | + ).not.toBeNull(); |
| 44 | + }); |
| 45 | + }); |
| 46 | + |
| 47 | + it("when external is 'true', renders link with correct url", async () => { |
| 48 | + // Arrange |
| 49 | + const expected = faker.internet.url(); |
| 50 | + |
| 51 | + // Act |
| 52 | + const { container } = render( |
| 53 | + <MemoryRouter> |
| 54 | + <Anchor to={expected} external={true} /> |
| 55 | + </MemoryRouter> |
| 56 | + ); |
| 57 | + |
| 58 | + // Assert |
| 59 | + await waitFor(() => { |
| 60 | + expect( |
| 61 | + container.querySelector(`[href="${expected}"]`) |
| 62 | + ).not.toBeNull(); |
| 63 | + }); |
| 64 | + }); |
| 65 | + |
| 66 | + it("when onClick set, calls handler upon click", async () => { |
| 67 | + // Arrange |
| 68 | + let calledTimes = 0; |
| 69 | + const handleClick = () => calledTimes++; |
| 70 | + const url = faker.internet.url(); |
| 71 | + const buttonText = "test button"; |
| 72 | + |
| 73 | + // Act |
| 74 | + const { getByText } = render( |
| 75 | + <MemoryRouter> |
| 76 | + <Anchor to={url} onClick={handleClick}> |
| 77 | + {buttonText} |
| 78 | + </Anchor> |
| 79 | + </MemoryRouter> |
| 80 | + ); |
| 81 | + |
| 82 | + fireEvent.click(getByText(buttonText)); |
| 83 | + |
| 84 | + // Assert |
| 85 | + await waitFor(() => { |
| 86 | + expect(calledTimes).toEqual(1); |
| 87 | + }); |
| 88 | + }); |
| 89 | + |
| 90 | + it("when target is set, rel attribute is set to avoid security risk", async () => { |
| 91 | + // Arrange |
| 92 | + const url = faker.internet.url(); |
| 93 | + |
| 94 | + // Act |
| 95 | + const { container } = render( |
| 96 | + <MemoryRouter> |
| 97 | + <Anchor to={url} target="_blank" /> |
| 98 | + </MemoryRouter> |
| 99 | + ); |
| 100 | + |
| 101 | + // Assert |
| 102 | + await waitFor(() => { |
| 103 | + expect( |
| 104 | + container.querySelector(`[rel="noopener noreferrer"]`) |
| 105 | + ).not.toBeNull(); |
| 106 | + }); |
| 107 | + }); |
| 108 | +}); |
0 commit comments