|
| 1 | +import { render, screen, waitFor } from "@solidjs/testing-library"; |
| 2 | +import { createResource, Resource, Show } from "solid-js"; |
| 3 | +import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 4 | + |
| 5 | +import AsyncContent from "../../../src/ts/components/common/AsyncContent"; |
| 6 | +import * as Notifications from "../../../src/ts/elements/notifications"; |
| 7 | + |
| 8 | +describe("AsyncContent", () => { |
| 9 | + const addNotificationMock = vi.spyOn(Notifications, "add"); |
| 10 | + |
| 11 | + beforeEach(() => { |
| 12 | + addNotificationMock.mockClear(); |
| 13 | + }); |
| 14 | + describe("with resource", () => { |
| 15 | + it("renders loading state while resource is pending", () => { |
| 16 | + const [resource] = createResource(async () => { |
| 17 | + await new Promise((resolve) => setTimeout(resolve, 100)); |
| 18 | + return "data"; |
| 19 | + }); |
| 20 | + |
| 21 | + const { container } = renderWithResource(resource); |
| 22 | + |
| 23 | + const preloader = container.querySelector(".preloader"); |
| 24 | + expect(preloader).toBeInTheDocument(); |
| 25 | + expect(preloader).toHaveClass("preloader"); |
| 26 | + expect(preloader?.querySelector("i")).toHaveClass( |
| 27 | + "fas", |
| 28 | + "fa-fw", |
| 29 | + "fa-spin", |
| 30 | + "fa-circle-notch", |
| 31 | + ); |
| 32 | + }); |
| 33 | + |
| 34 | + it("renders data when resource resolves", async () => { |
| 35 | + const [resource] = createResource(async () => { |
| 36 | + return "Test Data"; |
| 37 | + }); |
| 38 | + |
| 39 | + renderWithResource(resource); |
| 40 | + |
| 41 | + await waitFor(() => { |
| 42 | + expect(screen.getByTestId("content")).toHaveTextContent("Test Data"); |
| 43 | + }); |
| 44 | + }); |
| 45 | + |
| 46 | + it("renders error message when resource fails", async () => { |
| 47 | + const [resource] = createResource(async () => { |
| 48 | + throw new Error("Test error"); |
| 49 | + }); |
| 50 | + |
| 51 | + renderWithResource(resource, "Custom error message"); |
| 52 | + |
| 53 | + await waitFor(() => { |
| 54 | + expect(screen.getByText(/Custom error message/)).toBeInTheDocument(); |
| 55 | + }); |
| 56 | + expect(addNotificationMock).toHaveBeenCalledWith( |
| 57 | + "Custom error message: Test error", |
| 58 | + -1, |
| 59 | + ); |
| 60 | + }); |
| 61 | + |
| 62 | + it("renders default error message when no custom message provided", async () => { |
| 63 | + const [resource] = createResource(async () => { |
| 64 | + throw new Error("Test error"); |
| 65 | + }); |
| 66 | + |
| 67 | + renderWithResource(resource); |
| 68 | + |
| 69 | + await waitFor(() => { |
| 70 | + expect(screen.getByText(/An error occurred/)).toBeInTheDocument(); |
| 71 | + }); |
| 72 | + expect(addNotificationMock).toHaveBeenCalledWith( |
| 73 | + "An error occurred: Test error", |
| 74 | + -1, |
| 75 | + ); |
| 76 | + }); |
| 77 | + |
| 78 | + it("renders content while resource is pending if alwaysShowContent", () => { |
| 79 | + const [resource] = createResource(async () => { |
| 80 | + await new Promise((resolve) => setTimeout(resolve, 100)); |
| 81 | + return "data"; |
| 82 | + }); |
| 83 | + |
| 84 | + const { container } = renderWithResource(resource, undefined, true); |
| 85 | + |
| 86 | + const preloader = container.querySelector(".preloader"); |
| 87 | + expect(preloader).not.toBeInTheDocument(); |
| 88 | + expect(container.querySelector("div")).toHaveTextContent("no data"); |
| 89 | + }); |
| 90 | + |
| 91 | + it("renders data when resource resolves if alwaysShowContent", async () => { |
| 92 | + const [resource] = createResource(async () => { |
| 93 | + return "Test Data"; |
| 94 | + }); |
| 95 | + |
| 96 | + const { container } = renderWithResource(resource, undefined, true); |
| 97 | + |
| 98 | + await waitFor(() => { |
| 99 | + expect(screen.getByTestId("content")).toHaveTextContent("Test Data"); |
| 100 | + }); |
| 101 | + const preloader = container.querySelector(".preloader"); |
| 102 | + expect(preloader).not.toBeInTheDocument(); |
| 103 | + }); |
| 104 | + |
| 105 | + it("renders error message when resource fails if alwaysShowContent", async () => { |
| 106 | + const [resource] = createResource(async () => { |
| 107 | + throw new Error("Test error"); |
| 108 | + }); |
| 109 | + |
| 110 | + const { container } = renderWithResource( |
| 111 | + resource, |
| 112 | + "Custom error message", |
| 113 | + true, |
| 114 | + ); |
| 115 | + |
| 116 | + await waitFor(() => { |
| 117 | + expect(screen.getByText(/Custom error message/)).toBeInTheDocument(); |
| 118 | + }); |
| 119 | + expect(addNotificationMock).toHaveBeenCalledWith( |
| 120 | + "Custom error message: Test error", |
| 121 | + -1, |
| 122 | + ); |
| 123 | + console.log(container.innerHTML); |
| 124 | + }); |
| 125 | + function renderWithResource<T>( |
| 126 | + resource: Resource<T>, |
| 127 | + errorMessage?: string, |
| 128 | + alwaysShowContent?: true, |
| 129 | + ): { |
| 130 | + container: HTMLElement; |
| 131 | + } { |
| 132 | + const { container } = render(() => ( |
| 133 | + <AsyncContent |
| 134 | + resource={resource} |
| 135 | + errorMessage={errorMessage} |
| 136 | + alwaysShowContent={alwaysShowContent} |
| 137 | + > |
| 138 | + {(data: T | undefined) => ( |
| 139 | + <> |
| 140 | + foo |
| 141 | + <Show when={data !== undefined} fallback={<div>no data</div>}> |
| 142 | + <div data-testid="content">{String(data)}</div> |
| 143 | + </Show> |
| 144 | + </> |
| 145 | + )} |
| 146 | + </AsyncContent> |
| 147 | + )); |
| 148 | + |
| 149 | + return { |
| 150 | + container, |
| 151 | + }; |
| 152 | + } |
| 153 | + }); |
| 154 | +}); |
0 commit comments