Spaces:
Configuration error
Configuration error
Download frontend/__tests__/components/user-avatar.test.tsx from flyingdarkdev/README: direct link, hf CLI and curl.
- Browser
- Download file 2.23 kB
-
https://huggingface.co/spaces/flyingdarkdev/README/resolve/main/frontend/__tests__/components/user-avatar.test.tsx
- Command line
-
hf download hf://spaces/flyingdarkdev/README/frontend/__tests__/components/user-avatar.test.tsx
-
curl -L -o user-avatar.test.tsx https://huggingface.co/spaces/flyingdarkdev/README/resolve/main/frontend/__tests__/components/user-avatar.test.tsx
2.23 kB
| import { render, screen } from "@testing-library/react"; | |
| import userEvent from "@testing-library/user-event"; | |
| import { afterEach, describe, expect, it, vi } from "vitest"; | |
| import { UserAvatar } from "#/components/features/sidebar/user-avatar"; | |
| describe("UserAvatar", () => { | |
| const onClickMock = vi.fn(); | |
| afterEach(() => { | |
| onClickMock.mockClear(); | |
| }); | |
| it("(default) should render the placeholder avatar when the user is logged out", () => { | |
| render(<UserAvatar onClick={onClickMock} />); | |
| expect(screen.getByTestId("user-avatar")).toBeInTheDocument(); | |
| expect( | |
| screen.getByLabelText("USER$AVATAR_PLACEHOLDER"), | |
| ).toBeInTheDocument(); | |
| }); | |
| it("should call onClick when clicked", async () => { | |
| const user = userEvent.setup(); | |
| render(<UserAvatar onClick={onClickMock} />); | |
| const userAvatarContainer = screen.getByTestId("user-avatar"); | |
| await user.click(userAvatarContainer); | |
| expect(onClickMock).toHaveBeenCalledOnce(); | |
| }); | |
| it("should display the user's avatar when available", () => { | |
| render( | |
| <UserAvatar | |
| onClick={onClickMock} | |
| avatarUrl="https://example.com/avatar.png" | |
| />, | |
| ); | |
| expect(screen.getByAltText("user avatar")).toBeInTheDocument(); | |
| expect( | |
| screen.queryByLabelText("USER$AVATAR_PLACEHOLDER"), | |
| ).not.toBeInTheDocument(); | |
| }); | |
| it("should display a loading spinner instead of an avatar when isLoading is true", () => { | |
| const { rerender } = render(<UserAvatar onClick={onClickMock} />); | |
| expect(screen.queryByTestId("loading-spinner")).not.toBeInTheDocument(); | |
| expect( | |
| screen.getByLabelText("USER$AVATAR_PLACEHOLDER"), | |
| ).toBeInTheDocument(); | |
| rerender(<UserAvatar onClick={onClickMock} isLoading />); | |
| expect(screen.getByTestId("loading-spinner")).toBeInTheDocument(); | |
| expect( | |
| screen.queryByLabelText("USER$AVATAR_PLACEHOLDER"), | |
| ).not.toBeInTheDocument(); | |
| rerender( | |
| <UserAvatar | |
| onClick={onClickMock} | |
| avatarUrl="https://example.com/avatar.png" | |
| isLoading | |
| />, | |
| ); | |
| expect(screen.getByTestId("loading-spinner")).toBeInTheDocument(); | |
| expect(screen.queryByAltText("user avatar")).not.toBeInTheDocument(); | |
| }); | |
| }); | |