Skip to content

Commit

Permalink
test: added tests for categoryfilters component (#1076)
Browse files Browse the repository at this point in the history
* test(m2-438): wip

* test: added tests for categoryfilters component

Co-authored-by: Alex <[email protected]>
Co-authored-by: Alexander Devitsky <[email protected]>
  • Loading branch information
3 people authored May 31, 2022
1 parent 2b873bd commit 467c842
Show file tree
Hide file tree
Showing 8 changed files with 184 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
<h4 class="heading__title h4 desktop-only">
{{ $t('Filters') }}
</h4>
<div v-if="isLoading">
<div
v-if="isLoading"
data-testid="skeleton-loader"
>
<div
v-for="n in 3"
:key="n"
Expand Down Expand Up @@ -35,6 +38,7 @@
<div
v-for="(filter, i) in filters"
:key="i"
data-testid="category-filter"
>
<SfHeading
:key="`filter-title-${filter.attribute_code}`"
Expand All @@ -51,12 +55,14 @@
<div class="filters__buttons">
<SfButton
class="sf-button--full-width"
data-testid="apply-filters"
@click="doApplyFilters"
>
{{ $t('Apply filters') }}
</SfButton>
<SfButton
class="sf-button--full-width filters__button-clear"
data-testid="clear-filters"
@click="doClearFilters"
>
{{ $t('Clear all') }}
Expand All @@ -67,6 +73,7 @@
:visible="isVisible"
class="sidebar-filters smartphone-only"
title="Filters"
data-testid="mobile-sidebar"
@close="$emit('close')"
>
<SfAccordion class="filters smartphone-only">
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
import userEvent from '@testing-library/user-event';
import { inject, ref, useContext } from '@nuxtjs/composition-api';
import { render } from '~/test-utils';
import CategoryFilters from '../CategoryFilters.vue';
import { useUiHelpers } from '~/composables';
import { Aggregation } from '~/modules/GraphQL/types';
import { categoryFiltersData } from '~/test-utils/mocks/categoryFiltersMock';
import { useUiHelpersMock } from '~/test-utils/mocks/useUiHelpersMock';

jest.mock('@nuxtjs/composition-api', () => {
const compositionApi = jest.requireActual('@nuxtjs/composition-api');
return {
...compositionApi,
inject: jest.fn(),
useContext: jest.fn(),
};
});
jest.mock('~/composables');
jest.mock('~/composables/useApi', () => ({
useApi: jest.fn().mockReturnValue({
query: jest.fn(),
}),
}));

(useContext as jest.Mock).mockReturnValue({
app: {
i18n: {
t: jest.fn((text: string): string => text),
},
},
});

(inject as jest.Mock).mockReturnValue({
isFilterSelected: jest.fn(
(id: string, optVal: string): boolean => id === optVal,
),
});

(useUiHelpers as jest.Mock).mockReturnValue(useUiHelpersMock());

describe('CategoryFilters', () => {
it('Should render Skeleton when isLoading is true', () => {
const { queryByTestId } = render(CategoryFilters, {
props: {
catUid: '1',
},
setup() {
return {
isLoading: ref(true),
};
},
});

expect(queryByTestId('skeleton-loader')).not.toBeNull();
});

it('CategoryFilters Mobile sidebar should be hidden if isVisible false', () => {
const { queryByTestId } = render(CategoryFilters, {
props: {
catUid: '1',
isVisible: false,
},
});

expect(queryByTestId('mobile-sidebar').children).toHaveLength(0);
});

it('Should render filters if these are exist', () => {
const { getAllByTestId } = render(CategoryFilters, {
props: {
catUid: '1',
isVisible: false,
},
setup() {
return {
isLoading: ref(false),
filters: ref(categoryFiltersData as Aggregation[]),
};
},
});
const filters = getAllByTestId('category-filter');

expect(filters).toHaveLength(categoryFiltersData.length);
});

it('CategoryFilters Mobile sidebar should be visible if isVisible true', () => {
const { queryByTestId } = render(CategoryFilters, {
props: {
catUid: '1',
isVisible: true,
},
});

expect(queryByTestId('mobile-sidebar').children).not.toHaveLength(0);
});

it('"Apply filters" and "Clear all" buttons should be clickable', async () => {
const doApplyFilters = jest.fn();
const doClearFilters = jest.fn();
const { getByTestId } = render(CategoryFilters, {
props: {
catUid: '1',
isVisible: false,
},
setup() {
return {
isLoading: ref(false),
filters: ref(categoryFiltersData as Aggregation[]),
doApplyFilters,
doClearFilters,
};
},
});
const applyFiltersButton = getByTestId('apply-filters');
const clearFiltersButton = getByTestId('clear-filters');

await userEvent.click(applyFiltersButton);

expect(doApplyFilters).toHaveBeenCalledTimes(1);

await userEvent.click(clearFiltersButton);

expect(doClearFilters).toHaveBeenCalledTimes(1);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ jest.mock('@nuxtjs/composition-api', () => {

describe('CheckboxType.vue', () => {
it('Should show filters if options are exist', () => {
const { getAllByTestId } = render(CheckboxType, { props: { filter: categoryFiltersData as Aggregation } });
const { getAllByTestId } = render(CheckboxType, { props: { filter: categoryFiltersData[0] as Aggregation } });
const filters = getAllByTestId('category-filter');

expect(filters).toHaveLength(categoryFiltersData.options.length);
expect(filters).toHaveLength(categoryFiltersData[0].options.length);
});

it('Default filter should not be active', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ jest.mock('@nuxtjs/composition-api', () => {

describe('RadioType.vue', () => {
it('Should show filters if options are exist', () => {
const { getAllByTestId } = render(RadioType, { props: { filter: categoryFiltersData as Aggregation } });
const { getAllByTestId } = render(RadioType, { props: { filter: categoryFiltersData[0] as Aggregation } });
const filters = getAllByTestId('category-filter');

expect(filters).toHaveLength(categoryFiltersData.options.length);
expect(filters).toHaveLength(categoryFiltersData[0].options.length);
});

it('Default filter should not be active', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ jest.mock('@nuxtjs/composition-api', () => {

describe('SwatchColorType.vue', () => {
it('Should show filters if options are exist', () => {
const { getAllByRole } = render(SwatchColorType, { props: { filter: categoryFiltersData as Aggregation } });
const { getAllByRole } = render(SwatchColorType, { props: { filter: categoryFiltersData[0] as Aggregation } });
const filters = getAllByRole('button');

expect(filters).toHaveLength(categoryFiltersData.options.length);
expect(filters).toHaveLength(categoryFiltersData[0].options.length);
});

it('Default filter should not be active', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ jest.mock('@nuxtjs/composition-api', () => {

describe('YesNoType.vue', () => {
it('Should show filters if options are exist', () => {
const { getAllByTestId } = render(YesNoType, { props: { filter: categoryFiltersData as Aggregation } });
const { getAllByTestId } = render(YesNoType, { props: { filter: categoryFiltersData[0] as Aggregation } });
const filters = getAllByTestId('category-filter');

expect(filters).toHaveLength(categoryFiltersData.options.length);
expect(filters).toHaveLength(categoryFiltersData[0].options.length);
});

it('Default filter should not be active', () => {
Expand Down
60 changes: 40 additions & 20 deletions packages/theme/test-utils/mocks/categoryFiltersMock.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,44 @@
import type { Aggregation } from '~/modules/GraphQL/types';

export const categoryFiltersData: Aggregation = {
label: 'Pattern',
count: 2,
attribute_code: 'pattern',
position: 0,
options: [
{
count: 11,
label: 'Solid',
value: '196',
},
{
count: 1,
label: '194',
value: '194',
},
],
};
export const categoryFiltersData: Aggregation[] = [
{
label: 'Pattern',
count: 2,
attribute_code: 'pattern',
position: 0,
options: [
{
count: 11,
label: 'Solid',
value: '196',
},
{
count: 1,
label: '194',
value: '194',
},
],
},
{
label: 'Category',
count: 7,
attribute_code: 'category_id',
position: 1,
options: [
{
count: 12,
label: 'Women',
value: '20',
},
{
count: 12,
label: 'Tops',
value: '21',
},
],
},
];

export const categoryFiltersDataWithOneOption: Aggregation = Object.assign(categoryFiltersData, {
options: [categoryFiltersData.options[0]],
export const categoryFiltersDataWithOneOption: Aggregation = Object.assign(categoryFiltersData[0], {
options: [categoryFiltersData[0].options[0]],
});
4 changes: 3 additions & 1 deletion packages/theme/test-utils/mocks/useUiHelpersMock.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
export const useUiHelpersMock = (extend = {}) => ({
getCatLink: jest.fn((val) => val),
changeSorting: jest.fn(),
clearFilters: jest.fn(),
getCatLink: jest.fn((val) => val),
getFacetsFromURL: jest.fn(() => ({ filters: {} })),
...extend,
});

Expand Down

0 comments on commit 467c842

Please sign in to comment.