-
Notifications
You must be signed in to change notification settings - Fork 116
test: categorynavbar component #952
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
bartoszherba
merged 1 commit into
vuestorefront:develop
from
AbsoluteWebServices:test/m2-4339-add-test-category-navbar-component
May 18, 2022
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
80 changes: 80 additions & 0 deletions
80
packages/theme/modules/catalog/category/components/navbar/__tests__/CategoryNavbar.spec.js
This file contains hidden or 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,80 @@ | ||
| import userEvent from '@testing-library/user-event'; | ||
| import { render } from '~/test-utils'; | ||
| import CategoryNavbar from '../CategoryNavbar.vue'; | ||
| import { paginationData, sortByData } from '~/test-utils/mocks/categoryNavbarMock'; | ||
| import { useUiHelpers, useUiState } from '~/composables'; | ||
| import useUiHelpersMock from '~/test-utils/mocks/useUiHelpersMock'; | ||
| import useUiStateMock from '~/test-utils/mocks/useUiState'; | ||
|
|
||
| jest.mock('~/composables', () => ({ | ||
| ...jest.requireActual('~/composables'), | ||
| useUiHelpers: jest.fn(), | ||
| useUiState: jest.fn(), | ||
| })); | ||
|
|
||
| const categoryNavbarProps = { props: { pagination: paginationData, sortBy: sortByData } }; | ||
| const loadingCategoryNavbarProps = { props: { pagination: paginationData, sortBy: sortByData, isLoading: true } }; | ||
|
|
||
| describe('CategoryNavbar.vue', () => { | ||
| it('Should render Skeleton when loading is true', () => { | ||
| const uiStateMock = useUiStateMock(); | ||
| useUiState.mockReturnValue(uiStateMock); | ||
| const { getAllByTestId } = render(CategoryNavbar, loadingCategoryNavbarProps); | ||
|
|
||
| const skeletonLoaders = getAllByTestId('skeletonLoader'); | ||
|
|
||
| skeletonLoaders.forEach((skeletonLoader) => { | ||
| expect(skeletonLoader).toBeInTheDocument(); | ||
| }); | ||
| }); | ||
|
|
||
| it('Should render "Filters" button and call "toggleFilterSidebar" when clicked', async () => { | ||
| const uiStateMock = useUiStateMock(); | ||
| useUiState.mockReturnValue(uiStateMock); | ||
| const { getByText } = render(CategoryNavbar, categoryNavbarProps); | ||
| const button = getByText('Filters'); | ||
|
|
||
| await userEvent.click(button); | ||
|
|
||
| expect(uiStateMock.toggleFilterSidebar).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it('Should render "sort by" select and change selection and select has all expected options based on its data', async () => { | ||
| const uiHelperMock = useUiHelpersMock(); | ||
| useUiHelpers.mockReturnValue(uiHelperMock); | ||
|
|
||
| const { | ||
| getByRole, getByText, getAllByRole, emitted, | ||
| } = render(CategoryNavbar, categoryNavbarProps); | ||
| const sortSelect = getByRole('combobox'); | ||
| const options = getAllByRole('option'); | ||
| expect(options).toHaveLength(categoryNavbarProps.props.sortBy.options.length); | ||
|
|
||
| await userEvent.selectOptions(sortSelect, ['name_ASC']); | ||
| expect(getByText('Sort: Name A-Z').value).toBe(sortSelect.value); | ||
| expect(emitted().reloadProducts).toHaveProperty('length', 1); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @bartoszherba Here I check that changing sorting action emits 'reloadProducts' event |
||
| }); | ||
|
|
||
| it('Should render list view icon and change to "List view" when clicked.', async () => { | ||
| const uiStateMock = useUiStateMock(); | ||
| useUiState.mockReturnValue(uiStateMock); | ||
|
|
||
| const { getByLabelText } = render(CategoryNavbar, categoryNavbarProps); | ||
| const listViewIcon = getByLabelText(/change to list view/i); | ||
|
|
||
| await userEvent.click(listViewIcon); | ||
| expect(uiStateMock.changeToCategoryListView).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it('Should render grid view icon and change to "Grid view" when clicked.', async () => { | ||
| const uiStateMock = useUiStateMock(); | ||
| useUiState.mockReturnValue(uiStateMock); | ||
|
|
||
| const { getByLabelText } = render(CategoryNavbar, categoryNavbarProps); | ||
| const gridViewIcon = getByLabelText(/change to grid view/i); | ||
|
|
||
| await userEvent.click(gridViewIcon); | ||
|
|
||
| expect(uiStateMock.changeToCategoryGridView).toHaveBeenCalledTimes(1); | ||
| }); | ||
| }); | ||
This file contains hidden or 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,35 @@ | ||
| import { AgnosticPagination } from '~/composables/types'; | ||
|
|
||
| export const paginationData: AgnosticPagination = { | ||
| currentPage: 1, | ||
| totalPages: 2, | ||
| totalItems: 11, | ||
| itemsPerPage: 10, | ||
| pageOptions: [10, 50, 100], | ||
| }; | ||
|
|
||
| export const sortByData = { | ||
| options: [ | ||
| { | ||
| label: 'Sort: Default', | ||
| value: '', | ||
| }, | ||
| { | ||
| label: 'Sort: Name A-Z', | ||
| value: 'name_ASC', | ||
| }, | ||
| { | ||
| label: 'Sort: Name Z-A', | ||
| value: 'name_DESC', | ||
| }, | ||
| { | ||
| label: 'Sort: Price from low to high', | ||
| value: 'price_ASC', | ||
| }, | ||
| { | ||
| label: 'Sort: Price from high to low', | ||
| value: 'price_DESC', | ||
| }, | ||
| ], | ||
| selected: 'name_DESC', | ||
| }; |
This file contains hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bartoszherba Check if the component is in loading state all skeletons are rendered as expected