|
| 1 | +import userEvent from '@testing-library/user-event'; |
| 2 | +import { render } from '~/test-utils'; |
| 3 | +import CategoryNavbar from '../CategoryNavbar.vue'; |
| 4 | +import { paginationData, sortByData } from '~/test-utils/mocks/categoryNavbarMock'; |
| 5 | +import { useUiHelpers, useUiState } from '~/composables'; |
| 6 | +import useUiHelpersMock from '~/test-utils/mocks/useUiHelpersMock'; |
| 7 | +import useUiStateMock from '~/test-utils/mocks/useUiState'; |
| 8 | + |
| 9 | +jest.mock('~/composables', () => ({ |
| 10 | + ...jest.requireActual('~/composables'), |
| 11 | + useUiHelpers: jest.fn(), |
| 12 | + useUiState: jest.fn(), |
| 13 | +})); |
| 14 | + |
| 15 | +const categoryNavbarProps = { props: { pagination: paginationData, sortBy: sortByData } }; |
| 16 | +const loadingCategoryNavbarProps = { props: { pagination: paginationData, sortBy: sortByData, isLoading: true } }; |
| 17 | + |
| 18 | +describe('CategoryNavbar.vue', () => { |
| 19 | + it('Should render Skeleton when loading is true', () => { |
| 20 | + const uiStateMock = useUiStateMock(); |
| 21 | + useUiState.mockReturnValue(uiStateMock); |
| 22 | + const { getAllByTestId } = render(CategoryNavbar, loadingCategoryNavbarProps); |
| 23 | + |
| 24 | + const skeletonLoaders = getAllByTestId('skeletonLoader'); |
| 25 | + |
| 26 | + skeletonLoaders.forEach((skeletonLoader) => { |
| 27 | + expect(skeletonLoader).toBeInTheDocument(); |
| 28 | + }); |
| 29 | + }); |
| 30 | + |
| 31 | + it('Should render "Filters" button and call "toggleFilterSidebar" when clicked', async () => { |
| 32 | + const uiStateMock = useUiStateMock(); |
| 33 | + useUiState.mockReturnValue(uiStateMock); |
| 34 | + const { getByText } = render(CategoryNavbar, categoryNavbarProps); |
| 35 | + const button = getByText('Filters'); |
| 36 | + |
| 37 | + await userEvent.click(button); |
| 38 | + |
| 39 | + expect(uiStateMock.toggleFilterSidebar).toHaveBeenCalledTimes(1); |
| 40 | + }); |
| 41 | + |
| 42 | + it('Should render "sort by" select and change selection and select has all expected options based on its data', async () => { |
| 43 | + const uiHelperMock = useUiHelpersMock(); |
| 44 | + useUiHelpers.mockReturnValue(uiHelperMock); |
| 45 | + |
| 46 | + const { |
| 47 | + getByRole, getByText, getAllByRole, emitted, |
| 48 | + } = render(CategoryNavbar, categoryNavbarProps); |
| 49 | + const sortSelect = getByRole('combobox'); |
| 50 | + const options = getAllByRole('option'); |
| 51 | + expect(options).toHaveLength(categoryNavbarProps.props.sortBy.options.length); |
| 52 | + |
| 53 | + await userEvent.selectOptions(sortSelect, ['name_ASC']); |
| 54 | + expect(getByText('Sort: Name A-Z').value).toBe(sortSelect.value); |
| 55 | + expect(emitted().reloadProducts).toHaveProperty('length', 1); |
| 56 | + }); |
| 57 | + |
| 58 | + it('Should render list view icon and change to "List view" when clicked.', async () => { |
| 59 | + const uiStateMock = useUiStateMock(); |
| 60 | + useUiState.mockReturnValue(uiStateMock); |
| 61 | + |
| 62 | + const { getByLabelText } = render(CategoryNavbar, categoryNavbarProps); |
| 63 | + const listViewIcon = getByLabelText(/change to list view/i); |
| 64 | + |
| 65 | + await userEvent.click(listViewIcon); |
| 66 | + expect(uiStateMock.changeToCategoryListView).toHaveBeenCalledTimes(1); |
| 67 | + }); |
| 68 | + |
| 69 | + it('Should render grid view icon and change to "Grid view" when clicked.', async () => { |
| 70 | + const uiStateMock = useUiStateMock(); |
| 71 | + useUiState.mockReturnValue(uiStateMock); |
| 72 | + |
| 73 | + const { getByLabelText } = render(CategoryNavbar, categoryNavbarProps); |
| 74 | + const gridViewIcon = getByLabelText(/change to grid view/i); |
| 75 | + |
| 76 | + await userEvent.click(gridViewIcon); |
| 77 | + |
| 78 | + expect(uiStateMock.changeToCategoryGridView).toHaveBeenCalledTimes(1); |
| 79 | + }); |
| 80 | +}); |
0 commit comments