-
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.
Merge pull request #53 from atlp-rwanda/ft/improveproductview
- Loading branch information
Showing
15 changed files
with
591 additions
and
211 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,78 @@ | ||
import React from 'react'; | ||
import { render, screen, waitFor } from '@testing-library/react'; | ||
import '@testing-library/jest-dom'; | ||
import Page from '@/app/products/page'; | ||
import * as nextNavigation from 'next/navigation'; | ||
import request from '@/utils/axios'; | ||
|
||
// Mock dependencies | ||
jest.mock('next/navigation', () => ({ | ||
useSearchParams: jest.fn(), | ||
})); | ||
|
||
jest.mock('@/utils/axios', () => ({ | ||
get: jest.fn(), | ||
})); | ||
|
||
jest.mock('@/components/Side', () => () => <div>Side Component</div>); | ||
jest.mock('@/components/Header', () => () => <div>Header Component</div>); | ||
jest.mock('@/components/Footer', () => () => <div>Footer Component</div>); | ||
jest.mock('@/components/ProductList', () => ({ searchResults }: { searchResults: any[] }) => ( | ||
<div>Product List with {searchResults.length} results</div> | ||
)); | ||
|
||
describe('Page Component', () => { | ||
it('renders without crashing and fetches search results', async () => { | ||
const mockSearchParams = { | ||
toString: jest.fn().mockReturnValue(''), | ||
}; | ||
const mockResponse = [{ id: 1, name: 'Product 1' }, { id: 2, name: 'Product 2' }]; | ||
|
||
(nextNavigation.useSearchParams as jest.Mock).mockReturnValue(mockSearchParams); | ||
(request.get as jest.Mock).mockResolvedValue(mockResponse); | ||
|
||
render(<Page />); | ||
|
||
// Wait for search results to be fetched and rendered | ||
await waitFor(() => { | ||
expect(screen.getByText('Header Component')).toBeInTheDocument(); | ||
expect(screen.getByText('Footer Component')).toBeInTheDocument(); | ||
expect(screen.getByText('Product List with 2 results')).toBeInTheDocument(); | ||
expect(screen.getByText('Side Component')).toBeInTheDocument(); | ||
}); | ||
|
||
// Verify that the select options are rendered | ||
expect(screen.getByRole('combobox')).toBeInTheDocument(); | ||
expect(screen.getByRole('option', { name: 'Popular' })).toBeInTheDocument(); | ||
expect(screen.getByRole('option', { name: 'Recent' })).toBeInTheDocument(); | ||
expect(screen.getByRole('option', { name: 'Clothes' })).toBeInTheDocument(); | ||
expect(screen.getByRole('option', { name: 'Electronics' })).toBeInTheDocument(); | ||
}); | ||
|
||
it('handles error in fetching search results', async () => { | ||
const mockSearchParams = { | ||
toString: jest.fn().mockReturnValue(''), | ||
}; | ||
const mockError = new Error('Network Error'); | ||
|
||
(nextNavigation.useSearchParams as jest.Mock).mockReturnValue(mockSearchParams); | ||
(request.get as jest.Mock).mockRejectedValue(mockError); | ||
|
||
render(<Page />); | ||
|
||
// Wait for error handling | ||
await waitFor(() => { | ||
expect(screen.getByText('Header Component')).toBeInTheDocument(); | ||
expect(screen.getByText('Footer Component')).toBeInTheDocument(); | ||
expect(screen.getByText('Product List with 0 results')).toBeInTheDocument(); | ||
expect(screen.getByText('Side Component')).toBeInTheDocument(); | ||
}); | ||
|
||
// Verify that the select options are rendered | ||
expect(screen.getByRole('combobox')).toBeInTheDocument(); | ||
expect(screen.getByRole('option', { name: 'Popular' })).toBeInTheDocument(); | ||
expect(screen.getByRole('option', { name: 'Recent' })).toBeInTheDocument(); | ||
expect(screen.getByRole('option', { name: 'Clothes' })).toBeInTheDocument(); | ||
expect(screen.getByRole('option', { name: 'Electronics' })).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
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
Oops, something went wrong.