|
| 1 | +import React from 'react'; |
| 2 | +import { render, waitFor, fireEvent } from '@testing-library/react'; |
| 3 | +import Sort from '../../src/components/Sort/Sort'; |
| 4 | +import CioPlp from '../../src/components/CioPlp'; |
| 5 | +import { DEMO_API_KEY } from '../../src/constants'; |
| 6 | +import '@testing-library/jest-dom'; |
| 7 | +import { transformSearchResponse } from '../../src/utils/transformers'; |
| 8 | +import mockSearchResponse from '../local_examples/apiSearchResponse.json'; |
| 9 | + |
| 10 | +describe('Testing Component: Sort', () => { |
| 11 | + let location; |
| 12 | + const mockLocation = new URL('https://example.com'); |
| 13 | + |
| 14 | + beforeEach(() => { |
| 15 | + // Mock console error to de-clutter the console for expected errors |
| 16 | + const spy = jest.spyOn(console, 'error'); |
| 17 | + spy.mockImplementation(() => {}); |
| 18 | + |
| 19 | + location = window.location; |
| 20 | + delete window.location; |
| 21 | + window.location = mockLocation; |
| 22 | + mockLocation.href = 'https://example.com/'; |
| 23 | + }); |
| 24 | + |
| 25 | + afterAll(() => { |
| 26 | + window.location = location; |
| 27 | + jest.resetAllMocks(); // This will reset all mocks after each test |
| 28 | + }); |
| 29 | + |
| 30 | + const searchResponse = transformSearchResponse(mockSearchResponse); |
| 31 | + const responseSortOptions = searchResponse.sortOptions; |
| 32 | + |
| 33 | + it('Should throw error if used outside the CioPlp', () => { |
| 34 | + expect(() => render(<Sort searchOrBrowseResponse={searchResponse} />)).toThrow(); |
| 35 | + }); |
| 36 | + |
| 37 | + it('Should render sort options based on search or browse response', async () => { |
| 38 | + const { getByText } = render( |
| 39 | + <CioPlp apiKey={DEMO_API_KEY}> |
| 40 | + <Sort searchOrBrowseResponse={searchResponse} /> |
| 41 | + </CioPlp>, |
| 42 | + ); |
| 43 | + |
| 44 | + await waitFor(() => { |
| 45 | + responseSortOptions.forEach((option) => { |
| 46 | + expect(getByText(option.displayName)).toBeInTheDocument(); |
| 47 | + }); |
| 48 | + }); |
| 49 | + }); |
| 50 | + |
| 51 | + it('Should change selected sort option in component correctly', async () => { |
| 52 | + const { container } = render( |
| 53 | + <CioPlp apiKey={DEMO_API_KEY}> |
| 54 | + <Sort searchOrBrowseResponse={searchResponse} /> |
| 55 | + </CioPlp>, |
| 56 | + ); |
| 57 | + |
| 58 | + const defaultSort = responseSortOptions.find((option) => option.status === 'selected'); |
| 59 | + expect(container.querySelector('input:checked')).toBeDefined(); |
| 60 | + expect(container.querySelector('input:checked').value).toBe(JSON.stringify(defaultSort)); |
| 61 | + |
| 62 | + const newSortOption = responseSortOptions.find((option) => option.status !== 'selected'); |
| 63 | + fireEvent.click( |
| 64 | + container?.querySelector(`#${newSortOption.sortBy}-${newSortOption.sortOrder}`), |
| 65 | + ); |
| 66 | + |
| 67 | + expect(container.querySelector('input:checked')).toBeDefined(); |
| 68 | + expect(container.querySelector('input:checked').value).toBe(JSON.stringify(newSortOption)); |
| 69 | + }); |
| 70 | + |
| 71 | + it('Should change selected sort option in url correctly', async () => { |
| 72 | + const { container } = render( |
| 73 | + <CioPlp apiKey={DEMO_API_KEY}> |
| 74 | + <Sort searchOrBrowseResponse={searchResponse} /> |
| 75 | + </CioPlp>, |
| 76 | + ); |
| 77 | + |
| 78 | + const newSortOption = responseSortOptions.find((option) => option.status !== 'selected'); |
| 79 | + fireEvent.click( |
| 80 | + container?.querySelector(`#${newSortOption.sortBy}-${newSortOption.sortOrder}`), |
| 81 | + ); |
| 82 | + |
| 83 | + const urlObject = new URL(window.location.href); |
| 84 | + expect(urlObject.searchParams.get('sortBy')).toEqual(newSortOption.sortBy); |
| 85 | + expect(urlObject.searchParams.get('sortOrder')).toEqual(newSortOption.sortOrder); |
| 86 | + }); |
| 87 | + |
| 88 | + it('Should render correctly with render props', () => { |
| 89 | + const mockChildren = jest.fn().mockReturnValue(<div>Custom Sort</div>); |
| 90 | + |
| 91 | + const sortProps = { |
| 92 | + searchOrBrowseResponse: searchResponse, |
| 93 | + children: mockChildren, |
| 94 | + }; |
| 95 | + |
| 96 | + const { getByText } = render( |
| 97 | + <CioPlp apiKey={DEMO_API_KEY}> |
| 98 | + <Sort {...sortProps} /> |
| 99 | + </CioPlp>, |
| 100 | + ); |
| 101 | + expect(mockChildren).toHaveBeenCalled(); |
| 102 | + expect(getByText('Custom Sort')).toBeInTheDocument(); |
| 103 | + }); |
| 104 | +}); |
0 commit comments