Skip to content

Commit

Permalink
tests: add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nahoc committed May 7, 2024
1 parent e0037bf commit bf8df17
Show file tree
Hide file tree
Showing 10 changed files with 78 additions and 3 deletions.
1 change: 1 addition & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/bin/bash

bun run check
bun run test
bun run bump:version
git add .
3 changes: 2 additions & 1 deletion .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ components.json
tsconfig.tsbuildinfo
*.test.*
*.spec.*
vitest.config.ts
vitest.config.ts
setupTests.ts
23 changes: 23 additions & 0 deletions loader.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { Loader } from './loader';
import { render, screen } from '@testing-library/react';

describe('Loader', () => {
test('renders image without crashing', () => {
render(<Loader />);
const loaderImage = screen.getByAltText('Loading');
expect(loaderImage).toBeInTheDocument();
});

test('displays default loading text when no loadingText prop is provided', () => {
render(<Loader />);
const loadingText = screen.getByText('Loading…');
expect(loadingText).toBeInTheDocument();
});

test('displays custom loading text when loadingText prop is provided', () => {
const customText = 'Custom loading text';
render(<Loader loadingText={customText} />);
const loadingText = screen.getByText(customText);
expect(loadingText).toBeInTheDocument();
});
});
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@risc0/ui",
"version": "0.0.65",
"version": "0.0.66",
"sideEffects": false,
"type": "module",
"scripts": {
Expand Down Expand Up @@ -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",
Expand Down
22 changes: 22 additions & 0 deletions required.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Required } from './required';
import { render, screen } from '@testing-library/react';

describe('Required', () => {
test('renders without crashing', () => {
render(<Required />);
const requiredElement = screen.getByText('*');
expect(requiredElement).toBeInTheDocument();
});

test('displays an asterisk (*)', () => {
render(<Required />);
const requiredElement = screen.getByText('*');
expect(requiredElement.textContent).toBe('*');
});

test('has a class name of "text-destructive"', () => {
render(<Required />);
const requiredElement = screen.getByText('*');
expect(requiredElement).toHaveClass('text-destructive');
});
});
22 changes: 22 additions & 0 deletions separator.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { render, screen } from '@testing-library/react';
import { Separator } from './separator';

describe('Separator', () => {
it('renders without crashing', () => {
render(<Separator />);
const separatorElement = screen.getByRole('separator');
expect(separatorElement).toBeInTheDocument();
});

it('renders with default props', () => {
render(<Separator />);
const separatorElement = screen.getByRole('separator');
expect(separatorElement).toHaveClass('shrink-0 bg-border h-[1px] w-full');
});

it('renders correctly with custom props', () => {
render(<Separator orientation="vertical" />);
const separatorElement = screen.getByRole('separator');
expect(separatorElement).toHaveClass('shrink-0 bg-border h-full w-[1px]');
});
});
1 change: 1 addition & 0 deletions separator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const Separator = forwardRef<
>(({ className, orientation = "horizontal", decorative = true, ...rest }, ref) => (
<SeparatorPrimitive.Root
ref={ref}
role="separator"
decorative={decorative}
orientation={orientation}
className={cn("shrink-0 bg-border", orientation === "horizontal" ? "h-[1px] w-full" : "h-full w-[1px]", className)}
Expand Down
1 change: 1 addition & 0 deletions setupTests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import "@testing-library/jest-dom";
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"extends": "./tsconfig.base.json",
"compilerOptions": {
/* Path Aliases */
"baseUrl": "."
"baseUrl": ".",
"types": ["node", "jest", "@testing-library/jest-dom"]
},
"include": ["**/*.ts", "**/*.tsx"],
"exclude": ["node_modules"]
Expand Down
1 change: 1 addition & 0 deletions vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export default defineConfig({
environment: "happy-dom",
globals: true,
restoreMocks: true,
setupFiles: "./setupTests.ts",
include: ["**/*.test.?(c|m)[jt]s?(x)"],
},
});

0 comments on commit bf8df17

Please sign in to comment.