From bf8df1705c9e16c0814b3732536cb8f0d6729ff3 Mon Sep 17 00:00:00 2001 From: Cohan Carpentier Date: Tue, 7 May 2024 09:35:53 -0400 Subject: [PATCH] tests: add tests --- .husky/pre-commit | 1 + .npmignore | 3 ++- loader.test.tsx | 23 +++++++++++++++++++++++ package.json | 4 +++- required.test.tsx | 22 ++++++++++++++++++++++ separator.test.tsx | 22 ++++++++++++++++++++++ separator.tsx | 1 + setupTests.ts | 1 + tsconfig.json | 3 ++- vitest.config.ts | 1 + 10 files changed, 78 insertions(+), 3 deletions(-) create mode 100644 loader.test.tsx create mode 100644 required.test.tsx create mode 100644 separator.test.tsx create mode 100644 setupTests.ts diff --git a/.husky/pre-commit b/.husky/pre-commit index 72c34ef..bc4b96b 100644 --- a/.husky/pre-commit +++ b/.husky/pre-commit @@ -1,5 +1,6 @@ #!/bin/bash bun run check +bun run test bun run bump:version git add . \ No newline at end of file diff --git a/.npmignore b/.npmignore index 777ebe2..62d1cd3 100644 --- a/.npmignore +++ b/.npmignore @@ -7,4 +7,5 @@ components.json tsconfig.tsbuildinfo *.test.* *.spec.* -vitest.config.ts \ No newline at end of file +vitest.config.ts +setupTests.ts \ No newline at end of file diff --git a/loader.test.tsx b/loader.test.tsx new file mode 100644 index 0000000..64c7fd7 --- /dev/null +++ b/loader.test.tsx @@ -0,0 +1,23 @@ +import { Loader } from './loader'; +import { render, screen } from '@testing-library/react'; + +describe('Loader', () => { + test('renders image without crashing', () => { + render(); + const loaderImage = screen.getByAltText('Loading'); + expect(loaderImage).toBeInTheDocument(); + }); + + test('displays default loading text when no loadingText prop is provided', () => { + render(); + const loadingText = screen.getByText('Loading…'); + expect(loadingText).toBeInTheDocument(); + }); + + test('displays custom loading text when loadingText prop is provided', () => { + const customText = 'Custom loading text'; + render(); + const loadingText = screen.getByText(customText); + expect(loadingText).toBeInTheDocument(); + }); +}); \ No newline at end of file diff --git a/package.json b/package.json index 6961bd9..d6a3432 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@risc0/ui", - "version": "0.0.65", + "version": "0.0.66", "sideEffects": false, "type": "module", "scripts": { @@ -44,6 +44,8 @@ }, "devDependencies": { "@biomejs/biome": "1.7.3", + "@testing-library/jest-dom": "6.4.5", + "@testing-library/react": "15.0.7", "@testing-library/react-hooks": "8.0.1", "@types/jest": "29.5.12", "@types/lodash-es": "4.17.12", diff --git a/required.test.tsx b/required.test.tsx new file mode 100644 index 0000000..357e888 --- /dev/null +++ b/required.test.tsx @@ -0,0 +1,22 @@ +import { Required } from './required'; +import { render, screen } from '@testing-library/react'; + +describe('Required', () => { + test('renders without crashing', () => { + render(); + const requiredElement = screen.getByText('*'); + expect(requiredElement).toBeInTheDocument(); + }); + + test('displays an asterisk (*)', () => { + render(); + const requiredElement = screen.getByText('*'); + expect(requiredElement.textContent).toBe('*'); + }); + + test('has a class name of "text-destructive"', () => { + render(); + const requiredElement = screen.getByText('*'); + expect(requiredElement).toHaveClass('text-destructive'); + }); +}); \ No newline at end of file diff --git a/separator.test.tsx b/separator.test.tsx new file mode 100644 index 0000000..4205f2e --- /dev/null +++ b/separator.test.tsx @@ -0,0 +1,22 @@ +import { render, screen } from '@testing-library/react'; +import { Separator } from './separator'; + +describe('Separator', () => { + it('renders without crashing', () => { + render(); + const separatorElement = screen.getByRole('separator'); + expect(separatorElement).toBeInTheDocument(); + }); + + it('renders with default props', () => { + render(); + const separatorElement = screen.getByRole('separator'); + expect(separatorElement).toHaveClass('shrink-0 bg-border h-[1px] w-full'); + }); + + it('renders correctly with custom props', () => { + render(); + const separatorElement = screen.getByRole('separator'); + expect(separatorElement).toHaveClass('shrink-0 bg-border h-full w-[1px]'); + }); +}); \ No newline at end of file diff --git a/separator.tsx b/separator.tsx index 2d128b5..a366c34 100644 --- a/separator.tsx +++ b/separator.tsx @@ -10,6 +10,7 @@ const Separator = forwardRef< >(({ className, orientation = "horizontal", decorative = true, ...rest }, ref) => (