-
Notifications
You must be signed in to change notification settings - Fork 2
chore: unit test implementation #30
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
a990f94
chore: unit test implementation
hidaytrahman 3a2a79a
test: unit test cases for all the possible components
hidaytrahman 739c0af
test: more file coverage and igore unnecessary files
hidaytrahman 3974d5e
doc: best practice
hidaytrahman 3653243
chore: address PR suggestions
hidaytrahman c386d57
fix: coverage with all thresolds unit and cos
hidaytrahman f93dcc8
version changes
hidaytrahman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| # Best Practices for React + TypeScript + Webpack Template | ||
|
|
||
| This project is a modern template for scalable React applications using TypeScript and Webpack. Follow these best practices to ensure maintainability, performance, and code quality. | ||
|
|
||
| --- | ||
|
|
||
| ## 1. Unit Testing | ||
| - **Write tests for all logic and UI components.** Use Jest and React Testing Library for unit and integration tests. | ||
| - **Aim for 90%+ coverage.** Exclude boilerplate, config, and index files from coverage. | ||
| - **Test edge cases and error states.** Mock APIs and test both success and failure scenarios. | ||
| - **Use descriptive test names** and group related tests with `describe` blocks. | ||
| - **Run tests locally and in CI** before merging code. | ||
|
|
||
| ## Test Coverage Caveats | ||
| - While this template aims for 90%+ branch coverage globally, 100% branch coverage is not always achievable for every file (e.g., service files with implicit returns or unreachable branches). | ||
| - In such cases, the Jest configuration slightly lowers the branch threshold for those files. All meaningful logic and error paths are still covered by tests. | ||
|
|
||
| ## 2. Performance | ||
| - **Code splitting:** Use dynamic `import()` for large or rarely used components. | ||
| - **Memoization:** Use `React.memo`, `useMemo`, and `useCallback` to avoid unnecessary re-renders. | ||
| - **Optimize images:** Use modern formats and lazy loading for images. | ||
| - **Minimize bundle size:** Remove unused dependencies and use tree-shaking. | ||
| - **Use production builds:** Always deploy using `yarn build` (Webpack production mode). | ||
|
|
||
| ## 3. Code Quality & DRY Principles | ||
| - **Keep components small and focused.** Each component should do one thing well. | ||
| - **Reuse components and utilities.** Place shared logic in `utils/` or as custom hooks. | ||
| - **Avoid code duplication.** Extract repeated logic into functions or components. | ||
| - **Use TypeScript for type safety.** Define clear types and interfaces for props, state, and API responses. | ||
| - **Consistent naming conventions.** Use PascalCase for components, camelCase for variables/functions. | ||
| - **Comment complex logic.** Use comments to explain non-obvious code. | ||
|
|
||
| ## 4. Project Structure | ||
| - **Organize by feature or domain.** Group related files (components, views, services) together. | ||
| - **Use index files for exports.** Simplifies imports and improves maintainability. | ||
| - **Separate config, mocks, and layout.** Keep configuration, mock data, and layout components in their own folders. | ||
|
|
||
| ## 5. Improvements & Recommendations | ||
| - **Add E2E tests** (e.g., Cypress or Playwright) for critical user flows. | ||
| - **Linting and formatting:** Use ESLint and Prettier for consistent code style. | ||
| - **Accessibility:** Use semantic HTML and test with screen readers. | ||
| - **Error boundaries:** Use and customize `ErrorBoundary.tsx` for robust error handling. | ||
| - **Environment variables:** Store secrets and endpoints in `.env` files, not in code. | ||
| - **Continuous Integration:** Use GitHub Actions or similar for automated testing and deployment. | ||
|
|
||
| ## 6. Documentation | ||
| - **Document components and utilities.** Use JSDoc or TypeScript doc comments. | ||
| - **Update README.md** with setup, usage, and contribution guidelines. | ||
|
|
||
| --- | ||
|
|
||
| By following these best practices, you ensure your projects built from this template are robust, maintainable, and scalable. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| module.exports = { | ||
| preset: 'ts-jest', | ||
| testEnvironment: 'jsdom', | ||
| setupFilesAfterEnv: ['<rootDir>/jest.setup.js'], | ||
| moduleNameMapper: { | ||
| '^@/(.*)$': '<rootDir>/src/$1', | ||
| '\\.(css|less|scss|sass)$': 'identity-obj-proxy' | ||
| }, | ||
| collectCoverage: true, | ||
| collectCoverageFrom: [ | ||
| 'src/**/*.{ts,tsx}', | ||
| '!src/main.tsx', | ||
| '!src/index.tsx', | ||
| '!src/mocks/**', | ||
| '!src/types.ts', | ||
| '!src/**/*.d.ts', | ||
| '!src/**/index.ts', // ignore all index.ts files | ||
| '!src/config/**', // ignore config files | ||
| '!src/layout/**', // ignore layout files | ||
| '!src/App.tsx', // ignore root App | ||
| '!src/ErrorBoundary.tsx', // ignore error boundary | ||
| '!src/Root.tsx' // ignore root entry | ||
| ], | ||
| coverageThreshold: { | ||
| global: { | ||
| branches: 85, | ||
| functions: 90, | ||
| lines: 90, | ||
| statements: 90 | ||
| }, | ||
| 'src/services/app.services.ts': { | ||
| branches: 60, | ||
| functions: 100, | ||
| lines: 100, | ||
| statements: 100 | ||
| } | ||
| } | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| require('@testing-library/jest-dom'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| import { render, screen } from '@testing-library/react'; | ||
| import { ThemeProvider, createTheme } from '@mui/material/styles'; | ||
| import Footer from './Footer'; | ||
|
|
||
| describe('Footer', () => { | ||
| it('renders the footer with copyright and link', () => { | ||
| render( | ||
| <ThemeProvider theme={createTheme({ palette: { mode: 'light', grey: { 200: '#eee', 800: '#222' } } })}> | ||
| <Footer /> | ||
| </ThemeProvider> | ||
| ); | ||
| const year = new Date().getFullYear(); | ||
| expect(screen.getByText(new RegExp(`${year}`))).toBeInTheDocument(); | ||
| expect(screen.getByText(/Powered By/i)).toBeInTheDocument(); | ||
| const link = screen.getByRole('link', { name: /webtechpie.com/i }); | ||
| expect(link).toHaveAttribute('href', 'https://webtechpie.com/'); | ||
| expect(link).toHaveAttribute('target', '_blank'); | ||
| }); | ||
|
|
||
| it('renders with dark mode background', () => { | ||
| render( | ||
| <ThemeProvider theme={createTheme({ palette: { mode: 'dark', grey: { 200: '#eee', 800: '#222' } } })}> | ||
| <Footer /> | ||
| </ThemeProvider> | ||
| ); | ||
| expect(screen.getByText(/Powered By/i)).toBeInTheDocument(); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| import { render, screen, fireEvent } from '@testing-library/react'; | ||
| import Navigation from './Navigation'; | ||
| // import * as ReactRouter from 'react-router'; | ||
|
|
||
| // Mock router config | ||
| jest.mock('../../config/router.config', () => ({ | ||
| MAIN_ROUTES: [ | ||
| { path: '/', title: 'Home', name: 'home' }, | ||
| { path: '/about', title: 'About', name: 'about' } | ||
| ], | ||
| DASHBOARD_NESTED_ROUTES: [ | ||
| { path: '/dashboard', title: 'Dashboard', name: 'dashboard' } | ||
| ] | ||
| })); | ||
|
|
||
| // Mock Link to render an anchor | ||
| jest.mock('react-router', () => ({ | ||
| Link: ({ to, children }: any) => <a href={to}>{children}</a> | ||
| })); | ||
hidaytrahman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| describe('Navigation', () => { | ||
| it('renders navigation links', () => { | ||
| render(<Navigation />); | ||
| expect(screen.getAllByText('Home').length).toBeGreaterThan(0); | ||
| expect(screen.getAllByText('About').length).toBeGreaterThan(0); | ||
| expect(screen.getAllByText('Dashboard').length).toBeGreaterThan(0); | ||
| }); | ||
|
|
||
| it('toggles drawer on icon click (mobile)', () => { | ||
| render(<Navigation />); | ||
| const button = screen.getByLabelText(/open drawer/i); | ||
| fireEvent.click(button); | ||
| // After click, the drawer should be rendered (RTW text is in drawer) | ||
| expect(screen.getAllByText('RTW')[0]).toBeInTheDocument(); | ||
| }); | ||
| }); | ||
14 changes: 14 additions & 0 deletions
14
src/components/StandardImageList/StandardImageList.test.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| import { render, screen } from '@testing-library/react'; | ||
| import StandardImageList from './StandardImageList'; | ||
|
|
||
| describe('StandardImageList', () => { | ||
| it('renders a list of images with alt text', () => { | ||
| render(<StandardImageList />); | ||
| // There are 12 items in itemData | ||
| const images = screen.getAllByRole('img'); | ||
| expect(images.length).toBeGreaterThanOrEqual(12); | ||
| // Check for a specific alt text from the data | ||
| expect(screen.getByAltText('Breakfast')).toBeInTheDocument(); | ||
| expect(screen.getByAltText('Bike')).toBeInTheDocument(); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| import { fetchUser, createTodo } from "./app.services"; | ||
| import axios from "axios"; | ||
|
|
||
| jest.mock("axios"); | ||
| const mockedAxios = axios as jest.Mocked<typeof axios>; | ||
|
|
||
| describe("app.services", () => { | ||
| beforeEach(() => { | ||
| jest.clearAllMocks(); | ||
| }); | ||
|
|
||
| describe("fetchUser", () => { | ||
| it("returns user data on success", async () => { | ||
| mockedAxios.get.mockResolvedValueOnce({ data: { name: "Test User" } }); | ||
| const data = await fetchUser(); | ||
| expect(data).toEqual({ name: "Test User" }); | ||
| expect(mockedAxios.get).toHaveBeenCalledWith( | ||
| "https://raw.githubusercontent.com/hidaytrahman/hidaytrahman/main/me.json" | ||
| ); | ||
| }); | ||
|
|
||
| it("logs error and returns undefined on failure", async () => { | ||
| const error = new Error("Network error"); | ||
| mockedAxios.get.mockRejectedValueOnce(error); | ||
| const spy = jest.spyOn(console, "error").mockImplementation(() => {}); | ||
| const data = await fetchUser(); | ||
| expect(data).toBeUndefined(); | ||
| expect(spy).toHaveBeenCalledWith(error); | ||
| spy.mockRestore(); | ||
| }); | ||
|
|
||
| it("logs non-Error and returns undefined on fetchUser failure", async () => { | ||
| mockedAxios.get.mockRejectedValueOnce({ foo: "bar" }); | ||
| const spy = jest.spyOn(console, "error").mockImplementation(() => {}); | ||
| const data = await fetchUser(); | ||
| expect(data).toBeUndefined(); | ||
| expect(spy).toHaveBeenCalled(); | ||
| spy.mockRestore(); | ||
| }); | ||
| }); | ||
|
|
||
| describe("createTodo", () => { | ||
| it("returns data on success", async () => { | ||
| mockedAxios.post.mockResolvedValueOnce({ | ||
| data: { id: 1, title: "Test", completed: false }, | ||
| }); | ||
| const result = await createTodo("Test", false); | ||
| expect(result).toEqual({ | ||
| data: { id: 1, title: "Test", completed: false }, | ||
| error: null, | ||
| }); | ||
| expect(mockedAxios.post).toHaveBeenCalledWith( | ||
| "https://jsonplaceholder.typicode.com/todos", | ||
| { title: "Test", completed: false } | ||
| ); | ||
| }); | ||
|
|
||
| it("returns error on failure", async () => { | ||
| const error = new Error("Post error"); | ||
| mockedAxios.post.mockRejectedValueOnce(error); | ||
| const spy = jest.spyOn(console, "error").mockImplementation(() => {}); | ||
| const result = await createTodo("Test", false); | ||
| expect(result).toEqual({ data: null, error: "Post error" }); | ||
| expect(spy).toHaveBeenCalledWith(error); | ||
| spy.mockRestore(); | ||
| }); | ||
|
|
||
| it("returns error with unknown error type", async () => { | ||
| // Simulate a non-Error object thrown | ||
| mockedAxios.post.mockRejectedValueOnce({ foo: "bar" }); | ||
| const spy = jest.spyOn(console, "error").mockImplementation(() => {}); | ||
| const result = await createTodo("Test", false); | ||
| expect(result).toEqual({ data: null, error: "Unknown error" }); | ||
| expect(spy).toHaveBeenCalled(); | ||
| spy.mockRestore(); | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| import { theme } from './theme.utils'; | ||
|
|
||
| describe('theme.utils', () => { | ||
| it('should have primary and secondary colors', () => { | ||
| expect(theme.palette.primary.main).toBe('#8A784E'); | ||
| expect(theme.palette.secondary.main).toBe('#edf2ff'); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| import { render, screen } from '@testing-library/react'; | ||
| import About from './About'; | ||
|
|
||
| describe('About', () => { | ||
| it('renders About Us heading and team members', () => { | ||
| render(<About />); | ||
| expect(screen.getByRole('heading', { name: /about us/i })).toBeInTheDocument(); | ||
| expect(screen.getByText(/welcome to our company/i)).toBeInTheDocument(); | ||
| expect(screen.getByRole('heading', { name: /meet our team/i })).toBeInTheDocument(); | ||
| expect(screen.getByText('John Doe')).toBeInTheDocument(); | ||
| expect(screen.getByText('Jane Smith')).toBeInTheDocument(); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| import { render, screen } from '@testing-library/react'; | ||
| import Contact from './Contact'; | ||
|
|
||
| describe('Contact', () => { | ||
| it('renders Contact Us heading and mission text', () => { | ||
| render(<Contact />); | ||
| expect(screen.getByRole('heading', { name: /contact us/i })).toBeInTheDocument(); | ||
| expect(screen.getByText(/welcome to our company/i)).toBeInTheDocument(); | ||
| expect(screen.getByText(/continuous improvement/i)).toBeInTheDocument(); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| import { render, screen } from '@testing-library/react'; | ||
| import Home from './Home'; | ||
|
|
||
| describe('Home', () => { | ||
| it('renders Home heading', () => { | ||
| render(<Home />); | ||
| expect(screen.getByRole('heading', { name: /home/i })).toBeInTheDocument(); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.