Skip to content

Commit 3818aaa

Browse files
[Remove Vuetify from Studio] Convert content library filter bar unit tests to Vue Testing Library
1 parent 4665226 commit 3818aaa

2 files changed

Lines changed: 80 additions & 47 deletions

File tree

contentcuration/contentcuration/frontend/channelList/views/Channel/CatalogFilterBar.vue

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
:key="`catalog-filter-${index}`"
88
close
99
class="ma-1"
10+
:data-test="`filter-chip-${index}`"
1011
@input="filter.onclose"
1112
>
1213
{{ filter.text }}
@@ -16,7 +17,7 @@
1617
class="clear-link"
1718
:text="$tr('clearAll')"
1819
appearance="basic-link"
19-
data-test="clear"
20+
data-testid="clear"
2021
@click="clearFilters"
2122
/>
2223
</div>
Lines changed: 78 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
1-
import { mount } from '@vue/test-utils';
1+
import { render, screen, waitFor } from '@testing-library/vue';
2+
import userEvent from '@testing-library/user-event';
23
import { factory } from '../../../store';
34
import router from '../../../router';
45
import CatalogFilterBar from '../CatalogFilterBar';
56

6-
const store = factory();
7-
8-
const collection = {
9-
id: 'test-collection',
10-
};
7+
const collection = { id: 'test-collection' };
118

129
const query = {
1310
keywords: 'testing',
@@ -16,11 +13,21 @@ const query = {
1613
collection: 'some-collection',
1714
};
1815

16+
async function closeChipByText(user, text) {
17+
const chip = await screen.findByText(text);
18+
19+
const closeButton = chip.closest('[data-test^="filter-chip"]').querySelector('.v-chip__close');
20+
21+
expect(closeButton).toBeTruthy();
22+
await user.click(closeButton);
23+
}
24+
1925
function makeWrapper() {
20-
return mount(CatalogFilterBar, {
21-
sync: false,
22-
router,
26+
const store = factory();
27+
28+
return render(CatalogFilterBar, {
2329
store,
30+
router,
2431
computed: {
2532
collections() {
2633
return [collection];
@@ -30,52 +37,77 @@ function makeWrapper() {
3037
}
3138

3239
describe('catalogFilterBar', () => {
33-
let wrapper;
3440
beforeEach(() => {
35-
wrapper = makeWrapper();
41+
router
42+
.push({
43+
name: 'CHANNELS_EDITABLE',
44+
query: { ...query },
45+
})
46+
.catch(() => {});
3647
});
3748

38-
describe('removing filters', () => {
39-
beforeEach(() => {
40-
Object.entries(query).forEach(([key, val]) => {
41-
wrapper.vm[key] = val;
42-
});
43-
router.replace({ query });
49+
afterEach(() => {
50+
router.replace({ query: {} }).catch(() => {});
51+
});
52+
53+
it('clear all button should remove all filters', async () => {
54+
const user = userEvent.setup();
55+
makeWrapper();
56+
57+
await user.click(screen.getByTestId('clear'));
58+
59+
await waitFor(() => {
60+
expect(router.currentRoute.query.keywords).toBeUndefined();
61+
expect(router.currentRoute.query.coach).toBeUndefined();
62+
expect(router.currentRoute.query.collection).toBeUndefined();
63+
expect(router.currentRoute.query.languages).toBeUndefined();
4464
});
45-
it('clear all button should remove all filters', () => {
46-
wrapper.find('[data-test="clear"]').trigger('click');
47-
expect(wrapper.keywords).toBeUndefined();
48-
expect(wrapper.vm.currentFilters).toHaveLength(0);
65+
});
66+
67+
it('removing text-based filter should remove it from the query', async () => {
68+
const user = userEvent.setup();
69+
makeWrapper();
70+
71+
await closeChipByText(user, '"testing"');
72+
73+
await waitFor(() => {
74+
expect(router.currentRoute.query.keywords).toBeUndefined();
75+
expect(router.currentRoute.query.coach).toBe(true);
76+
expect(router.currentRoute.query.collection).toBe('some-collection');
77+
expect(router.currentRoute.query.languages).toBe('en,es');
4978
});
50-
it('removing text-based filter should remove it from the query', () => {
51-
wrapper.vm.resetKeywords();
52-
expect(wrapper.vm.$route.query.keywords).toBeUndefined();
53-
54-
// Make sure other queries weren't affected
55-
expect(wrapper.vm.$route.query.coach).toBeTruthy();
56-
expect(wrapper.vm.$route.query.collection).toBeTruthy();
57-
expect(wrapper.vm.$route.query.languages).toBeTruthy();
79+
});
80+
81+
it('removing boolean-based filter should remove it from the query', async () => {
82+
const user = userEvent.setup();
83+
makeWrapper();
84+
85+
await closeChipByText(user, 'Coach content');
86+
87+
await waitFor(() => {
88+
expect(router.currentRoute.query.coach).toBeUndefined();
89+
expect(router.currentRoute.query.collection).toBe('some-collection');
90+
expect(router.currentRoute.query.languages).toBe('en,es');
91+
expect(router.currentRoute.query.keywords).toBe('testing');
5892
});
59-
it('removing boolean-based filter should remove it from the query', () => {
60-
wrapper.vm.resetCoach();
61-
expect(wrapper.vm.$route.query.coach).toBeUndefined();
62-
63-
// Make sure other queries weren't affected
64-
expect(wrapper.vm.$route.query.collection).toBeTruthy();
65-
expect(wrapper.vm.$route.query.languages).toBeTruthy();
66-
expect(wrapper.vm.$route.query.keywords).toBeTruthy();
93+
});
94+
95+
it('removing list-based filter should only remove that item from the query', async () => {
96+
const user = userEvent.setup();
97+
makeWrapper();
98+
await closeChipByText(user, 'English');
99+
100+
await waitFor(() => {
101+
expect(router.currentRoute.query.languages).toBe('es');
67102
});
68-
it('removing list-based filter should only remove that item from the query', () => {
69-
wrapper.vm.removeLanguage('en');
70-
expect(wrapper.vm.$route.query.languages).toBe('es');
71103

72-
wrapper.vm.removeLanguage('es');
73-
expect(wrapper.vm.$route.query.languages).toBeUndefined();
104+
await closeChipByText(user, 'Español');
74105

75-
// Make sure other queries weren't affected
76-
expect(wrapper.vm.$route.query.coach).toBeTruthy();
77-
expect(wrapper.vm.$route.query.collection).toBeTruthy();
78-
expect(wrapper.vm.$route.query.keywords).toBeTruthy();
106+
await waitFor(() => {
107+
expect(router.currentRoute.query.languages).toBeUndefined();
108+
expect(router.currentRoute.query.coach).toBe(true);
109+
expect(router.currentRoute.query.collection).toBe('some-collection');
110+
expect(router.currentRoute.query.keywords).toBe('testing');
79111
});
80112
});
81113
});

0 commit comments

Comments
 (0)