shortener.test.tsx 1.9 KB

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