-
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 3 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,32 @@ | ||
| 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: 90, | ||
| functions: 90, | ||
| lines: 90, | ||
| statements: 90 | ||
| } | ||
| } | ||
| }; | ||
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,14 @@ | ||
| import { render, screen } from '@testing-library/react'; | ||
| import Footer from './Footer'; | ||
|
|
||
| describe('Footer', () => { | ||
| it('renders the footer with copyright and link', () => { | ||
| render(<Footer />); | ||
| 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'); | ||
| }); | ||
| }); |
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 React from 'react'; | ||
|
|
||
| function HelloWorld() { | ||
| return <div>Hello, World!</div>; | ||
| } | ||
|
|
||
| describe('HelloWorld', () => { | ||
| it('renders greeting', () => { | ||
| render(<HelloWorld />); | ||
| expect(screen.getByText(/hello, world/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'; | ||
hidaytrahman marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| // 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,45 @@ | ||
| import { fetchUser, createTodo } from './app.services'; | ||
| import axios from 'axios'; | ||
|
|
||
| jest.mock('axios'); | ||
| const mockedAxios = axios as jest.Mocked<typeof axios>; | ||
|
|
||
| describe('app.services', () => { | ||
| 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(); | ||
| }); | ||
| }); | ||
|
|
||
| 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(); | ||
| }); | ||
| }); | ||
| }); | ||
hidaytrahman marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
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(); | ||
| }); | ||
| }); |
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,24 @@ | ||
| import { render, screen, waitFor } from '@testing-library/react'; | ||
| import Profile from './Profile'; | ||
|
|
||
| jest.mock('@/services/app.services', () => ({ | ||
| fetchUser: jest.fn().mockResolvedValue({ | ||
| name: 'Test User', | ||
| greet: 'Hello!', | ||
| location: 'Test City', | ||
| intro: 'Test intro', | ||
| totalExperience: 5 | ||
| }) | ||
| })); | ||
|
|
||
| describe('Profile', () => { | ||
| it('renders loading and then user profile', async () => { | ||
| render(<Profile />); | ||
| expect(screen.getByText(/loading/i)).toBeInTheDocument(); | ||
| await waitFor(() => { | ||
| expect(screen.getByRole('img', { name: 'Test User' })).toBeInTheDocument(); | ||
| expect(screen.getByRole('heading', { name: 'Hello!' })).toBeInTheDocument(); | ||
| expect(screen.getByText('Test intro')).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 Settings from './Settings'; | ||
|
|
||
| describe('Settings', () => { | ||
| it('renders Settings heading', () => { | ||
| render(<Settings />); | ||
| expect(screen.getByRole('heading', { name: /settings/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,12 @@ | ||
| import { render, screen } from '@testing-library/react'; | ||
| import Landing from './Landing'; | ||
|
|
||
| describe('Landing', () => { | ||
| it('renders landing page with StandardImageList', () => { | ||
| render(<Landing />); | ||
| expect(screen.getByRole('heading', { name: /hello, react with typescript and webpack/i })).toBeInTheDocument(); | ||
| expect(screen.getByText(/simple react application/i)).toBeInTheDocument(); | ||
| // StandardImageList renders at least one image | ||
| expect(screen.getAllByRole('img').length).toBeGreaterThan(0); | ||
| }); | ||
| }); |
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
Oops, something went wrong.
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.