-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(test-cov): increase test coverage
- add more tests to cover uncovered lines [#Fixes #124]
- Loading branch information
Showing
6 changed files
with
203 additions
and
6 deletions.
There are no files selected for viewing
This file contains 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 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 |
---|---|---|
|
@@ -3,13 +3,13 @@ import axios from 'axios'; | |
import MockAdapter from 'axios-mock-adapter'; | ||
import { Store, configureStore } from '@reduxjs/toolkit'; | ||
import { waitFor } from '@testing-library/dom'; | ||
import { render, screen } from '@testing-library/react'; | ||
import { fireEvent, render, screen } from '@testing-library/react'; | ||
import { Provider } from 'react-redux'; | ||
import { MemoryRouter, Route, Routes } from 'react-router'; | ||
import productsReducer, { | ||
fetchProductDetails, | ||
} from '@/features/Products/ProductSlice'; | ||
import signInReducer from '@/features/Auth/SignInSlice'; | ||
import signInReducer, { SignInState } from '@/features/Auth/SignInSlice'; | ||
import { AppDispatch, RootState } from '@/app/store'; | ||
import ProductDetails from '@/pages/ProductDetails'; | ||
import bestSellingReducer from '@/features/Popular/bestSellingProductSlice'; | ||
|
@@ -24,8 +24,22 @@ const mockProduct = { | |
totalQtySold: 25, | ||
longDesc: 'This is a mock product used for testing purposes.', | ||
shortDesc: 'This is a short description', | ||
category: 'Electronics', | ||
similarProducts: [], | ||
category: { | ||
id: 5, | ||
name: 'Electronics', | ||
}, | ||
similarProducts: [ | ||
{ | ||
id: 3, | ||
name: 'Mock Similar Product', | ||
image: '/images/mock.png', | ||
averageRating: 0, | ||
salesPrice: 100, | ||
regularPrice: 200, | ||
longDesc: 'This is a mock product used for testing purposes.', | ||
shortDesc: 'This is a short description', | ||
}, | ||
], | ||
reviews: [ | ||
{ | ||
id: 1, | ||
|
@@ -42,7 +56,8 @@ const mockProduct = { | |
gallery: [], | ||
tags: ['testTag'], | ||
vendor: { | ||
name: 'Tester', | ||
firstName: 'Tester', | ||
lastName: 'Testing', | ||
email: '[email protected]', | ||
picture: 'https://fake.png', | ||
}, | ||
|
@@ -57,6 +72,22 @@ const renderWithProviders = ( | |
bestSellingProducts: bestSellingReducer, | ||
signIn: signInReducer, | ||
}, | ||
preloadedState: { | ||
signIn: { | ||
token: 'fake token', | ||
user: null, | ||
role: null, | ||
loading: null, | ||
error: null, | ||
message: null, | ||
needsVerification: false, | ||
needs2FA: false, | ||
vendor: { | ||
id: null, | ||
email: null, | ||
}, | ||
} as unknown as SignInState, | ||
}, | ||
}), | ||
} = {} | ||
) => { | ||
|
@@ -101,6 +132,12 @@ describe('ProductDetails Page', () => { | |
expect(screen.getByText(/\$149.99/i)).toBeInTheDocument(); | ||
expect(screen.getByText('1')).toBeInTheDocument(); | ||
expect(screen.getByText('25')).toBeInTheDocument(); | ||
expect(screen.getByText('33% Off')).toBeInTheDocument(); | ||
expect(screen.getByText('Add to Cart')).toBeInTheDocument(); | ||
expect(screen.getByText('Add to wishlist')).toBeInTheDocument(); | ||
// expect(screen.getAllByTestId('ratingStar').length).toBe(4); | ||
// expect(screen.getAllByTestId('halfStar').length).toBe(1); | ||
|
||
expect( | ||
screen.getByText(/This is a mock product used for testing purposes./i) | ||
).toBeInTheDocument(); | ||
|
@@ -123,6 +160,128 @@ describe('ProductDetails Page', () => { | |
).toBeInTheDocument(); | ||
}); | ||
}); | ||
|
||
it('should display similar products', async () => { | ||
mock | ||
.onGet(`${import.meta.env.VITE_BASE_URL}/buyer/get_product/1`) | ||
.reply(200, { product: mockProduct }); | ||
|
||
renderWithProviders(<ProductDetails />); | ||
|
||
await waitFor(() => { | ||
expect(screen.getByText('Mock Similar Prod...')).toBeInTheDocument(); | ||
expect(screen.getByText('$100')).toBeInTheDocument(); | ||
expect(screen.getByText('50% Off')).toBeInTheDocument(); | ||
}); | ||
}); | ||
|
||
it('should display product details, reviews, about store', async () => { | ||
mock | ||
.onGet(`${import.meta.env.VITE_BASE_URL}/buyer/get_product/1`) | ||
.reply(200, { product: mockProduct }); | ||
|
||
renderWithProviders(<ProductDetails />); | ||
|
||
await waitFor(() => { | ||
expect(screen.getByText('Product Details')).toBeInTheDocument(); | ||
expect( | ||
screen.getByText('This is a mock product used for testing purposes.') | ||
).toBeInTheDocument(); | ||
expect( | ||
screen.getByText('This is a short description') | ||
).toBeInTheDocument(); | ||
}); | ||
|
||
fireEvent.click(screen.getByRole('button', { name: 'Reviews (1)' })); | ||
|
||
await waitFor(() => { | ||
expect(screen.getByText('new user')).toBeInTheDocument(); | ||
expect(screen.getByText('excellent product')).toBeInTheDocument(); | ||
}); | ||
|
||
fireEvent.click(screen.getByRole('button', { name: 'About Store' })); | ||
|
||
await waitFor(() => { | ||
expect(screen.getAllByText('Tester Testing')[0]).toBeInTheDocument(); | ||
expect( | ||
screen.getAllByText('[email protected]')[0] | ||
).toBeInTheDocument(); | ||
}); | ||
}); | ||
|
||
it('should display error message when no reviews are found', async () => { | ||
mock | ||
.onGet(`${import.meta.env.VITE_BASE_URL}/buyer/get_product/1`) | ||
.reply(200, { product: { ...mockProduct, reviews: [] } }); | ||
|
||
renderWithProviders(<ProductDetails />); | ||
|
||
await waitFor(() => { | ||
fireEvent.click(screen.getByRole('button', { name: 'Reviews (0)' })); | ||
}); | ||
|
||
await waitFor(() => { | ||
expect(screen.getByText('No reviews found')).toBeInTheDocument(); | ||
}); | ||
}); | ||
|
||
it('should submit a review successfully', async () => { | ||
mock | ||
.onGet(`${import.meta.env.VITE_BASE_URL}/buyer/get_product/1`) | ||
.reply(200, { product: mockProduct }); | ||
|
||
mock | ||
.onPost(`${import.meta.env.VITE_BASE_URL}/review`) | ||
.reply(200, { message: 'Review submitted successfully' }); | ||
|
||
renderWithProviders(<ProductDetails />); | ||
|
||
mock | ||
.onGet(`${import.meta.env.VITE_BASE_URL}/buyer/get_product/1`) | ||
.reply(200, { | ||
product: { | ||
...mockProduct, | ||
reviews: [ | ||
{ | ||
id: 1, | ||
user: { | ||
id: 1, | ||
firstName: 'new', | ||
lastName: 'rating', | ||
picture: 'http://fake.png', | ||
}, | ||
rating: 1, | ||
content: 'this is a bad product', | ||
}, | ||
], | ||
}, | ||
}); | ||
|
||
await waitFor(() => { | ||
const star = screen.getAllByTitle('inputStar')[0]; | ||
fireEvent.click(star); | ||
const contentTextArea = screen.getByTitle('inputContent'); | ||
fireEvent.change(contentTextArea, { | ||
target: { | ||
value: 'this is a bad product', | ||
}, | ||
}); | ||
}); | ||
|
||
await waitFor(() => { | ||
const submitBtn = screen.getByText('Submit'); | ||
fireEvent.click(submitBtn); | ||
}); | ||
|
||
await waitFor(() => { | ||
fireEvent.click(screen.getByRole('button', { name: 'Reviews (1)' })); | ||
}); | ||
|
||
await waitFor(() => { | ||
expect(screen.getByText('this is a bad product')).toBeInTheDocument(); | ||
expect(screen.getByText('new rating')).toBeInTheDocument(); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('Product Details async action', () => { | ||
|
This file contains 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 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 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 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