|
| 1 | +import React from "react"; |
| 2 | +import { render } from "@testing-library/react"; |
| 3 | +import { StoreProvider, createStore, thunk } from "easy-peasy"; |
| 4 | +import userEvent from "@testing-library/user-event" |
| 5 | +import { store } from "../../store"; |
| 6 | +import Shortener from "../Shortener"; |
| 7 | + |
| 8 | +describe("<Shortener /> component test", () => { |
| 9 | + let app; |
| 10 | + |
| 11 | + beforeEach(() => { |
| 12 | + store.links = { |
| 13 | + ...store.links, |
| 14 | + submit: thunk(async (actions, payload) => { |
| 15 | + return { |
| 16 | + id: "0", |
| 17 | + address: "localhost:3000/foobar", |
| 18 | + banned: false, |
| 19 | + created_at: "now", |
| 20 | + link: "localhost:3000/foobar", |
| 21 | + target: "", |
| 22 | + updated_at: "now", |
| 23 | + visit_count: 0 |
| 24 | + }; |
| 25 | + }) |
| 26 | + }; |
| 27 | + const testStore = createStore(store); |
| 28 | + app = ( |
| 29 | + <StoreProvider store={testStore}> |
| 30 | + <Shortener /> |
| 31 | + </StoreProvider> |
| 32 | + ); |
| 33 | + }); |
| 34 | + |
| 35 | + it("Should show the short URL", async () => { |
| 36 | + const screen = render(app); |
| 37 | + const urlInput = screen.getByRole("textbox", { name: "target" }); |
| 38 | + userEvent.type(urlInput, "https://easy-peasy.now.sh/docs/api/thunk.html"); |
| 39 | + const submitButton = screen.getByRole("button", { name: "submit" }); |
| 40 | + userEvent.click(submitButton); |
| 41 | + const msg = await screen.findByText(/localhost:3000\/foobar/i); |
| 42 | + expect(msg).toBeInTheDocument(); |
| 43 | + }); |
| 44 | + |
| 45 | + it("Should empty target input", async () => { |
| 46 | + const screen = render(app); |
| 47 | + let urlInput: HTMLInputElement = screen.getByRole("textbox", { |
| 48 | + name: "target" |
| 49 | + }) as HTMLInputElement; |
| 50 | + userEvent.type(urlInput, "https://easy-peasy.now.sh/docs/api/thunk.html"); |
| 51 | + const submitButton = screen.getByRole("button", { name: "submit" }); |
| 52 | + userEvent.click(submitButton); |
| 53 | + await screen.findByText(/localhost:3000\/foobar/i); |
| 54 | + urlInput = screen.getByRole("textbox", { |
| 55 | + name: "target" |
| 56 | + }) as HTMLInputElement; |
| 57 | + expect(urlInput.value).toEqual(""); |
| 58 | + }); |
| 59 | +}); |
0 commit comments