|
| 1 | + |
| 2 | +import { QuotePage as Component } from './quote.page'; |
| 3 | +import { createMemoryHistory } from '@tanstack/react-router'; |
| 4 | +import { router } from '@/provider/tanstack-router.provider'; |
| 5 | +import { renderWithFileRoutes } from '@/test/file-route-utils'; |
| 6 | +import { beforeEach, describe, expect, it, test, vi } from 'vitest'; |
| 7 | +import { createRouter, RouterProvider } from "@tanstack/react-router"; |
| 8 | +import { act, logDOM, render, screen, waitFor, waitForElementToBeRemoved } from '@testing-library/react'; |
| 9 | +import { userEvent } from '@testing-library/user-event'; |
| 10 | + |
| 11 | +/// mock hook |
| 12 | +import { useGetQuoteDetails } from '@/api-hook/use-get-quote-details.hook'; |
| 13 | +vi.mock("@/api-hook/use-get-quote-details.hook") |
| 14 | +const mockUseGetQuoteDetails = vi.mocked(useGetQuoteDetails) |
| 15 | + |
| 16 | +beforeEach(() => { |
| 17 | + |
| 18 | + ///reset mock value |
| 19 | + mockUseGetQuoteDetails.mockReturnValue({ |
| 20 | + data: undefined, |
| 21 | + isLoading: false, |
| 22 | + error: undefined, |
| 23 | + }) |
| 24 | +}) |
| 25 | + |
| 26 | +describe(Component.name, () => { |
| 27 | + describe("rendering", () => { |
| 28 | + |
| 29 | + it("renders quote page: add mode", async () => { |
| 30 | + await router.navigate({ to: '/new' }) |
| 31 | + await renderWithFileRoutes(<Component mode="add" />) |
| 32 | + const loader = screen.queryByLabelText("loading-editor") |
| 33 | + if (loader) { |
| 34 | + expect(await loader).toBeInTheDocument() |
| 35 | + await waitForElementToBeRemoved(loader, { timeout: 8000 }) |
| 36 | + } |
| 37 | + const editor = await screen.findByLabelText("editor") |
| 38 | + expect(await editor).toBeInTheDocument() |
| 39 | + }) |
| 40 | + |
| 41 | + it("renders quote page: edit mode", async () => { |
| 42 | + const editorContent = "editor-content" |
| 43 | + const tagContent = "tag-content" |
| 44 | + mockUseGetQuoteDetails.mockReturnValue({ |
| 45 | + data: { |
| 46 | + text: editorContent, |
| 47 | + id: 1, |
| 48 | + tags: [{ id: 1, name: tagContent }] |
| 49 | + }, |
| 50 | + isLoading: false, |
| 51 | + error: undefined, |
| 52 | + }) |
| 53 | + await router.navigate({ to: '/$quoteId', params: { quoteId: "1" } }) |
| 54 | + await renderWithFileRoutes(<Component mode="edit" />) |
| 55 | + |
| 56 | + const loader = screen.queryByLabelText("loading") |
| 57 | + if (loader) { |
| 58 | + expect(await loader).toBeInTheDocument() |
| 59 | + await waitForElementToBeRemoved(loader, { timeout: 8000 }) |
| 60 | + } |
| 61 | + |
| 62 | + const editor = await screen.findByLabelText("editor") |
| 63 | + const tagList = await screen.findByLabelText("tag-list") |
| 64 | + await expect(await editor).toBeInTheDocument() |
| 65 | + await expect(await tagList).toBeInTheDocument() |
| 66 | + // logDOM(tagList) |
| 67 | + await expect(await screen.findByText(editorContent)).toBeInTheDocument() |
| 68 | + await expect(await screen.findByLabelText(tagContent)).toBeInTheDocument() |
| 69 | + |
| 70 | + }) |
| 71 | + |
| 72 | + it("shows loading indicator when fetching quote", async () => { |
| 73 | + mockUseGetQuoteDetails.mockReturnValue({ |
| 74 | + data: undefined, |
| 75 | + isLoading: true, |
| 76 | + error: undefined, |
| 77 | + }) |
| 78 | + |
| 79 | + await router.navigate({ to: '/$quoteId', params: { quoteId: "1" } }) |
| 80 | + await renderWithFileRoutes(<Component mode="edit" />) |
| 81 | + |
| 82 | + expect(await screen.findByLabelText("loading")).toBeInTheDocument() |
| 83 | + }) |
| 84 | + |
| 85 | + it("should show error", async () => { |
| 86 | + const somethingBad = "bugs took over the entire tech world" |
| 87 | + mockUseGetQuoteDetails.mockReturnValue({ |
| 88 | + data: undefined, |
| 89 | + isLoading: false, |
| 90 | + error: somethingBad |
| 91 | + }) |
| 92 | + |
| 93 | + router.navigate({ to: "/$quoteId", params: { quoteId: "1" } }) |
| 94 | + renderWithFileRoutes(<Component mode='edit' />) |
| 95 | + |
| 96 | + const errorElement = await screen.findByLabelText("error") |
| 97 | + expect(errorElement).toBeInTheDocument() |
| 98 | + |
| 99 | + const badElement = await screen.findByText(somethingBad, { exact: false }) |
| 100 | + await expect(badElement).toBeInTheDocument() |
| 101 | + }) |
| 102 | + |
| 103 | + |
| 104 | + |
| 105 | + }) |
| 106 | + |
| 107 | + describe("interaction", () => { |
| 108 | + |
| 109 | + it("adds tag when chosen", async () => { |
| 110 | + await router.navigate({ to: '/new' }) |
| 111 | + await renderWithFileRoutes(<Component mode="add" />) |
| 112 | + |
| 113 | + const loader = screen.queryByLabelText("loading-editor") |
| 114 | + if (loader) { |
| 115 | + expect(await loader).toBeInTheDocument() |
| 116 | + await waitForElementToBeRemoved(loader, { timeout: 8000 }) |
| 117 | + } |
| 118 | + |
| 119 | + const editor = await screen.findByLabelText("editor") |
| 120 | + expect(await editor).toBeInTheDocument() |
| 121 | + |
| 122 | + const addTag = screen.queryByLabelText("add-tag") |
| 123 | + expect(await addTag).toBeInTheDocument() |
| 124 | + await userEvent.click(addTag!) |
| 125 | + |
| 126 | + const tags = screen.queryByLabelText("tags") |
| 127 | + expect(await tags).toBeInTheDocument() |
| 128 | + |
| 129 | + const tagGroup = screen.getByText("relationship", { exact: false }) |
| 130 | + expect(await tagGroup).toBeInTheDocument() |
| 131 | + await userEvent.click(tagGroup!) |
| 132 | + // logDOM(tagGroup!) |
| 133 | + |
| 134 | + const tag = screen.getByText("kindness", { exact: false }) |
| 135 | + expect(await tag).toBeInTheDocument() |
| 136 | + // logDOM(tag!) |
| 137 | + }) |
| 138 | + |
| 139 | + }) |
| 140 | + |
| 141 | +}); |
| 142 | + |
0 commit comments