Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/Test Case config and some test cases #1231

Merged
merged 3 commits into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
"build:start": "cd dist && PORT=8080 npx serve",
"start": "env-cmd -f .env.dev vite dev",
"start:live": "vite dev",
"test": "vitest tests/",
"test": "vitest",
"test:watch": "vitest --watch",
"test:ui": "vitest --ui",
"test:coverage": "vitest --coverage",
"lint": "eslint --fix --ext .js,.jsx,.ts,.tsx src"
},
"license": "GPL-3.0-only",
Expand Down
69 changes: 69 additions & 0 deletions src/frontend/src/components/__test__/Button.test.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { describe, expect, test } from 'vitest';
import { render, screen, fireEvent } from '@testing-library/react';
import Button from '../common/Button';
import { useState } from 'react';

describe('Button', () => {
test('renders', () => {
render(<Button btnText="NEXT" />);
expect(screen.getByText('NEXT')).toBeDefined();
});
/**
* Test suite for the Button component
*/
describe('Button', () => {
/**
* Test to verify that the Button component renders with the correct text
*/
test('renders', () => {
render(<Button btnText="NEXT" />);
expect(screen.getByText('NEXT')).toBeDefined();
});

/**
* Test to verify that the count increases when the button is clicked
*/
test('should increase count by 1', () => {
// Arrange
const ButtonWithCount = () => {
const [count, setCount] = useState(0);
return (
<>
<div data-testid="count">{count}</div>

<Button btnText="Click" onClick={() => setCount(count + 1)} />
</>
);
};

// Act
render(<ButtonWithCount />);

const count = screen.getByTestId('count');
const button = screen.getByText('Click');

fireEvent.click(button);

// Assert
expect(count.textContent).toBe('1');
});

/**
* Test to verify that the Button component renders a count element if provided
*/
it('should render a count element if provided', () => {
render(<Button btnText="Test Button" count={5} />);
expect(screen.getByText('5')).toBeInTheDocument();
});

/**
* Test to verify the styling of the Button component based on the btnType prop
*/
it("should render a button with a red border and text if btnType prop is 'other'", () => {
render(<Button btnText="Test Button" btnType="other" />);
expect(screen.getByTestId('test-button')).toHaveClass(
'fmtm-py-1 fmtm-px-4 fmtm-text-red-600 fmtm-rounded-lg fmtm-border-[1px] fmtm-border-red-600 hover:fmtm-text-red-700 hover:fmtm-border-red-700',
);
});
});
});
1 change: 1 addition & 0 deletions src/frontend/src/components/common/Button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ const Button = ({
}: IButton) => (
<div className="fmtm-w-fit">
<button
data-testid="test-button"
type={type === 'submit' ? 'submit' : 'button'}
onClick={onClick}
className={`fmtm-text-lg fmtm-group fmtm-flex fmtm-items-center fmtm-gap-2 ${btnStyle(
Expand Down
3 changes: 3 additions & 0 deletions src/frontend/src/sum.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function sum(a, b) {
return a + b;
}
8 changes: 8 additions & 0 deletions src/frontend/tests/App.test.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { sum } from '../src/sum';
import { describe, expect, test } from 'vitest';

describe('Test', () => {
test('adds 1 + 2 to equal 3', () => {
expect(sum(1, 2)).toBe(3);
});
});
38 changes: 0 additions & 38 deletions src/frontend/tests/App.test.tsx

This file was deleted.

20 changes: 0 additions & 20 deletions src/frontend/tests/CreateProject.test.tsx

This file was deleted.

2 changes: 1 addition & 1 deletion src/frontend/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"useDefineForClassFields": true,
"lib": ["DOM", "DOM.Iterable", "ESNext"],
"allowJs": false,
"checkJs": false,
"checkJs": true,
"skipLibCheck": true,
"esModuleInterop": false,
"allowSyntheticDefaultImports": true,
Expand Down
1 change: 1 addition & 0 deletions src/frontend/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export default defineConfig({
},
},
test: {
globals: true,
environment: 'jsdom',
setupFiles: './setupTests.ts',
},
Expand Down
Loading