-
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.
- Loading branch information
1 parent
86a5319
commit a59f02e
Showing
20 changed files
with
792 additions
and
30 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
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 |
---|---|---|
@@ -0,0 +1,141 @@ | ||
import React from 'react'; | ||
import { fireEvent, render, screen } from '@testing-library/react'; | ||
import WishlistPage from '../../pages/WishlistPage/WishlistPage'; | ||
import { Provider } from 'react-redux'; | ||
import store from '../../redux/store'; | ||
import axios from 'axios'; | ||
import { vi } from 'vitest'; | ||
import { setOnWishlistPage, setWishlist } from '../../redux/reducers/wishlistReducer'; | ||
import { setCredentials } from '../../redux/reducers/authReducer'; | ||
|
||
vi.mock('axios'); | ||
vi.mock('../../utils/errorHandler'); | ||
|
||
const mockedAxios = axios as jest.Mocked<typeof axios>; | ||
|
||
const userToken = 'Testing Login'; | ||
|
||
const mockProducts = [ | ||
{ | ||
wishListDetails: { | ||
createdAt: new Date(Date.now()), | ||
id: 1, | ||
productId: '1' | ||
}, | ||
productInfo: { | ||
id: '1', | ||
name: 'Product 1', | ||
images: ['image1.jpg'], | ||
categories: [], | ||
newPrice: '100', | ||
oldPrice: '120', | ||
updatedAt: new Date(), | ||
createdAt: new Date(Date.now()), | ||
description: '', | ||
isAvailable: false, | ||
quantity: '' | ||
} | ||
}, | ||
{ | ||
wishListDetails: { | ||
createdAt: new Date(Date.now()), | ||
id: 2, | ||
productId: '2' | ||
}, | ||
productInfo: { | ||
id: '2', | ||
name: 'Product 2', | ||
images: ['image2.jpg'], | ||
categories: [], | ||
newPrice: '200', | ||
oldPrice: '220', | ||
updatedAt: new Date(), | ||
createdAt: new Date(Date.now()), | ||
description: '', | ||
isAvailable: false, | ||
quantity: '' | ||
} | ||
}, | ||
{ | ||
wishListDetails: { | ||
createdAt: new Date(Date.now()), | ||
id: 3, | ||
productId: '3' | ||
}, | ||
productInfo: { | ||
id: '3', | ||
name: 'Product 3', | ||
images: ['image3.jpg'], | ||
categories: [], | ||
newPrice: '200', | ||
oldPrice: '220', | ||
updatedAt: new Date(), | ||
createdAt: new Date(Date.now()), | ||
description: '', | ||
isAvailable: false, | ||
quantity: '' | ||
} | ||
} | ||
]; | ||
|
||
describe('WishlistPage', () => { | ||
beforeEach(() => { | ||
mockedAxios.get.mockResolvedValueOnce({ | ||
data: { productsForBuyer: mockProducts } | ||
}); | ||
store.dispatch(setOnWishlistPage(true)); | ||
store.dispatch(setCredentials(userToken)); | ||
vi.resetAllMocks(); | ||
}); | ||
|
||
it('should tell if there are no products in whichlist', () => { | ||
render( | ||
<Provider store={store}> | ||
<WishlistPage /> | ||
</Provider> | ||
); | ||
|
||
const paragraph = screen.getByText(/wishlist is empty/i); | ||
expect(paragraph).toBeInTheDocument(); | ||
|
||
const clearAllButton = screen.queryByText(/Clear All/i); | ||
expect(clearAllButton).not.toBeInTheDocument(); | ||
}); | ||
|
||
it('renders wishlist page with products', async () => { | ||
store.dispatch(setWishlist(mockProducts)); | ||
|
||
render( | ||
<Provider store={store}> | ||
<WishlistPage /> | ||
</Provider> | ||
); | ||
|
||
const heading = await screen.findByRole('heading', { name: 'Wishlist' }); | ||
expect(heading).toBeInTheDocument(); | ||
|
||
expect(screen.getAllByTestId('productDiv').length).toBe(3); | ||
|
||
mockProducts.forEach((product) => { | ||
expect(screen.getByText(product.productInfo.name)).toBeInTheDocument(); | ||
}); | ||
|
||
const deleteButtons = screen.getAllByTestId('deleteButton'); | ||
|
||
expect(deleteButtons[0]).toBeInTheDocument(); | ||
fireEvent.click(deleteButtons[0]); | ||
}); | ||
|
||
it('All products should be cleared when clearAll is clicked', async () => { | ||
store.dispatch(setWishlist(mockProducts)); | ||
|
||
render( | ||
<Provider store={store}> | ||
<WishlistPage /> | ||
</Provider> | ||
); | ||
|
||
const clearAllButton = await screen.findByText(/Clear All/i); | ||
expect(clearAllButton).toBeInTheDocument; | ||
}); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import wishlistReducer, { setWishlist, setOnWishlistPage } from '../../../redux/reducers/wishlistReducer'; | ||
import { prodInWishlistProps } from '../../../redux/reducers/wishlistReducer'; | ||
|
||
describe('wishlistReducer', () => { | ||
let initialState: { products: prodInWishlistProps[]; onWihlistPage: boolean }; | ||
|
||
beforeEach(() => { | ||
initialState = { | ||
products: [], | ||
onWihlistPage: false | ||
}; | ||
}); | ||
|
||
it('should return the initial state when given an undefined state', () => { | ||
expect(wishlistReducer(undefined, { type: 'undefined' })).toEqual(initialState); | ||
}); | ||
|
||
it('should handle setWishlist action', () => { | ||
const mockProducts: prodInWishlistProps[] = [ | ||
{ | ||
wishListDetails: { | ||
createdAt: new Date(Date.now()), | ||
id: 1, | ||
productId: '1' | ||
}, | ||
productInfo: { | ||
id: '1', | ||
name: 'Product 1', | ||
images: ['image1.jpg'], | ||
categories: [], | ||
newPrice: '100', | ||
oldPrice: '120', | ||
updatedAt: new Date(), | ||
createdAt: new Date(Date.now()), | ||
description: '', | ||
isAvailable: false, | ||
quantity: '' | ||
} | ||
} | ||
]; | ||
|
||
const newState = wishlistReducer(initialState, setWishlist(mockProducts)); | ||
expect(newState).toEqual({ | ||
products: mockProducts, | ||
onWihlistPage: false | ||
}); | ||
}); | ||
|
||
it('should handle setOnWishlistPage action', () => { | ||
const newState = wishlistReducer(initialState, setOnWishlistPage(true)); | ||
expect(newState).toEqual({ | ||
products: [], | ||
onWihlistPage: true | ||
}); | ||
}); | ||
|
||
it('should handle setOnWishlistPage action to false', () => { | ||
const newState = wishlistReducer({ ...initialState, onWihlistPage: true }, setOnWishlistPage(false)); | ||
expect(newState).toEqual({ | ||
products: [], | ||
onWihlistPage: false | ||
}); | ||
}); | ||
}); |
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
Oops, something went wrong.