Skip to content

Commit f91b652

Browse files
Convert accessibility options unit tests to Vue Testing Library #5794 (#5889)
* tests: Converted Accessibility Options test to VTL - covered all the original tests. - coverage includes user interaction tests. * refactored test's over-reliance on getByTestId to follow vtl's query priority * [pre-commit.ci lite] apply automatic fixes * fix: refine AccessibilityOptions VTL migration according to recieved reviews. * test: refine AccessibilityOptions VTL migration - Replaced mocked keys with translation strings - configured testIdAttribute at module scope replacing the lifecycle hooks. - Removed redundant smoke test and VueRouter initialization. * [pre-commit.ci lite] apply automatic fixes * replaced hardcoded strings with corresponding constants * test: use getByText and within scoping in AccessibilityOptions VTL tests * fix: fixed imports --------- Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com>
1 parent 1330bf0 commit f91b652

1 file changed

Lines changed: 101 additions & 75 deletions

File tree

Lines changed: 101 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,101 +1,127 @@
1-
import { shallowMount, mount } from '@vue/test-utils';
1+
import { render, screen, within, configure } from '@testing-library/vue';
2+
import userEvent from '@testing-library/user-event';
3+
import VueRouter from 'vue-router';
24
import AccessibilityOptions from '../AccessibilityOptions.vue';
5+
import { AccessibilityCategories } from 'shared/constants';
6+
import { metadataStrings } from 'shared/strings/metadataStrings';
7+
import { ContentKindsNames } from 'shared/leUtils/ContentKinds';
8+
9+
configure({ testIdAttribute: 'data-test' });
310

411
describe('AccessibilityOptions', () => {
5-
it('smoke test', () => {
6-
const wrapper = shallowMount(AccessibilityOptions, {
7-
propsData: {
8-
kind: 'document',
12+
const renderComponent = props => {
13+
return render(AccessibilityOptions, {
14+
props: {
15+
kind: ContentKindsNames.DOCUMENT,
16+
value: [],
17+
...props,
918
},
19+
routes: new VueRouter(),
1020
});
11-
expect(wrapper.exists()).toBe(true);
21+
};
22+
23+
it('shows document-specific accessibility options to the user', () => {
24+
renderComponent({ kind: ContentKindsNames.DOCUMENT });
25+
26+
const checkboxes = screen.getAllByRole('checkbox');
27+
expect(checkboxes).toHaveLength(3);
28+
29+
expect(screen.getByText(metadataStrings.$tr('altText'))).toBeInTheDocument();
30+
expect(screen.getByText(metadataStrings.$tr('highContrast'))).toBeInTheDocument();
31+
expect(screen.getByText(metadataStrings.$tr('taggedPdf'))).toBeInTheDocument();
1232
});
1333

14-
it('should display the correct list of accessibility options if resource is a document', () => {
15-
const wrapper = mount(AccessibilityOptions, {
16-
propsData: {
17-
kind: 'document',
18-
},
19-
});
34+
it('shows video-specific accessibility options to the user', () => {
35+
renderComponent({ kind: ContentKindsNames.VIDEO });
36+
37+
const checkboxes = screen.getAllByRole('checkbox');
38+
expect(checkboxes).toHaveLength(3);
2039

21-
expect(wrapper.findComponent('[data-test="checkbox-altText"]').exists()).toBe(true);
22-
expect(wrapper.findComponent('[data-test="checkbox-highContrast"]').exists()).toBe(true);
23-
expect(wrapper.findComponent('[data-test="checkbox-taggedPdf"]').exists()).toBe(true);
24-
expect(wrapper.findComponent('[data-test="checkbox-signLanguage"]').exists()).toBe(false);
25-
expect(wrapper.findComponent('[data-test="checkbox-audioDescription"]').exists()).toBe(false);
40+
expect(screen.getByText(metadataStrings.$tr('signLanguage'))).toBeInTheDocument();
41+
expect(screen.getByText(metadataStrings.$tr('audioDescription'))).toBeInTheDocument();
42+
expect(screen.getByText(metadataStrings.$tr('captionsSubtitles'))).toBeInTheDocument();
43+
44+
expect(screen.queryByTestId('tooltip-captionsSubtitles')).not.toBeInTheDocument();
2645
});
2746

28-
it('should display the correct list of accessibility options if resource is a video', () => {
29-
const wrapper = mount(AccessibilityOptions, {
30-
propsData: {
31-
kind: 'video',
32-
},
33-
});
47+
it('shows exercise-specific accessibility options to the user', () => {
48+
renderComponent({ kind: ContentKindsNames.EXERCISE });
49+
50+
const checkboxes = screen.getAllByRole('checkbox');
51+
expect(checkboxes).toHaveLength(1);
3452

35-
expect(wrapper.findComponent('[data-test="checkbox-altText"]').exists()).toBe(false);
36-
expect(wrapper.findComponent('[data-test="checkbox-highContrast"]').exists()).toBe(false);
37-
expect(wrapper.findComponent('[data-test="checkbox-taggedPdf"]').exists()).toBe(false);
38-
expect(wrapper.findComponent('[data-test="checkbox-signLanguage"]').exists()).toBe(true);
39-
expect(wrapper.findComponent('[data-test="checkbox-audioDescription"]').exists()).toBe(true);
40-
expect(wrapper.findComponent('[data-test="checkbox-captionsSubtitles"]').exists()).toBe(true);
41-
expect(wrapper.findComponent('[data-test="tooltip-captionsSubtitles"]').exists()).toBe(false);
53+
expect(screen.getByText(metadataStrings.$tr('altText'))).toBeInTheDocument();
4254
});
4355

44-
it('should display the correct list of accessibility options if resource is an exercise/practice', () => {
45-
const wrapper = mount(AccessibilityOptions, {
46-
propsData: {
47-
kind: 'exercise',
48-
},
49-
});
56+
it('shows HTML5-specific accessibility options to the user', () => {
57+
renderComponent({ kind: ContentKindsNames.HTML5 });
58+
59+
const checkboxes = screen.getAllByRole('checkbox');
60+
expect(checkboxes).toHaveLength(2);
5061

51-
expect(wrapper.findComponent('[data-test="checkbox-altText"]').exists()).toBe(true);
52-
expect(wrapper.findComponent('[data-test="checkbox-highContrast"]').exists()).toBe(false);
53-
expect(wrapper.findComponent('[data-test="checkbox-taggedPdf"]').exists()).toBe(false);
54-
expect(wrapper.findComponent('[data-test="checkbox-signLanguage"]').exists()).toBe(false);
55-
expect(wrapper.findComponent('[data-test="checkbox-audioDescription"]').exists()).toBe(false);
62+
expect(screen.getByText(metadataStrings.$tr('altText'))).toBeInTheDocument();
63+
expect(screen.getByText(metadataStrings.$tr('highContrast'))).toBeInTheDocument();
5664
});
5765

58-
it('should display the correct list of accessibility options if resource is html5/zip', () => {
59-
const wrapper = mount(AccessibilityOptions, {
60-
propsData: {
61-
kind: 'html5',
62-
},
63-
});
66+
it('shows audio-specific accessibility options to the user', () => {
67+
renderComponent({ kind: ContentKindsNames.AUDIO });
6468

65-
expect(wrapper.findComponent('[data-test="checkbox-altText"]').exists()).toBe(true);
66-
expect(wrapper.findComponent('[data-test="checkbox-highContrast"]').exists()).toBe(true);
67-
expect(wrapper.findComponent('[data-test="checkbox-taggedPdf"]').exists()).toBe(false);
68-
expect(wrapper.findComponent('[data-test="checkbox-signLanguage"]').exists()).toBe(false);
69-
expect(wrapper.findComponent('[data-test="checkbox-audioDescription"]').exists()).toBe(false);
69+
const checkboxes = screen.getAllByRole('checkbox');
70+
expect(checkboxes).toHaveLength(1);
71+
72+
expect(screen.getByText(metadataStrings.$tr('captionsSubtitles'))).toBeInTheDocument();
73+
expect(screen.queryByTestId('tooltip-captionsSubtitles')).not.toBeInTheDocument();
7074
});
7175

72-
it('should display the correct list of accessibility options if resource is an audio', () => {
73-
const wrapper = mount(AccessibilityOptions, {
74-
propsData: {
75-
kind: 'audio',
76-
},
77-
});
76+
it('renders informative tooltips next to the corresponding options', () => {
77+
renderComponent({ kind: ContentKindsNames.DOCUMENT });
7878

79-
expect(wrapper.findComponent('[data-test="checkbox-captionsSubtitles"]').exists()).toBe(true);
80-
expect(wrapper.findComponent('[data-test="tooltip-captionsSubtitles"]').exists()).toBe(false);
79+
expect(screen.getByTestId('tooltip-altText')).toBeInTheDocument();
80+
expect(screen.getByTestId('tooltip-highContrast')).toBeInTheDocument();
81+
expect(screen.getByTestId('tooltip-taggedPdf')).toBeInTheDocument();
8182
});
8283

83-
it('should render appropriate tooltips along with the checkbox', () => {
84-
const wrapper = mount(AccessibilityOptions, {
85-
propsData: {
86-
kind: 'document',
87-
},
84+
describe('User Interactions', () => {
85+
it('emits an input event with the updated array when a user checks an option', async () => {
86+
const { emitted } = renderComponent({ kind: ContentKindsNames.DOCUMENT, value: [] });
87+
88+
const altTextCheckbox = within(screen.getByTestId('checkbox-altText')).getByRole('checkbox');
89+
await userEvent.click(altTextCheckbox);
90+
91+
expect(emitted()).toHaveProperty('input');
92+
expect(emitted().input[0][0]).toEqual([AccessibilityCategories.ALT_TEXT]);
8893
});
8994

90-
expect(wrapper.findComponent('[data-test="checkbox-altText"]').exists()).toBe(true);
91-
expect(wrapper.findComponent('[data-test="tooltip-altText"]').exists()).toBe(true);
92-
expect(wrapper.findComponent('[data-test="checkbox-highContrast"]').exists()).toBe(true);
93-
expect(wrapper.findComponent('[data-test="tooltip-highContrast"]').exists()).toBe(true);
94-
expect(wrapper.findComponent('[data-test="checkbox-taggedPdf"]').exists()).toBe(true);
95-
expect(wrapper.findComponent('[data-test="tooltip-taggedPdf"]').exists()).toBe(true);
96-
expect(wrapper.findComponent('[data-test="checkbox-signLanguage"]').exists()).toBe(false);
97-
expect(wrapper.findComponent('[data-test="tooltip-signLanguage"]').exists()).toBe(false);
98-
expect(wrapper.findComponent('[data-test="checkbox-audioDescription"]').exists()).toBe(false);
99-
expect(wrapper.findComponent('[data-test="tooltip-audioDescription"]').exists()).toBe(false);
95+
it('emits an updated array with the item removed when a user unchecks a pre-checked option', async () => {
96+
const { emitted } = renderComponent({
97+
kind: ContentKindsNames.VIDEO,
98+
value: [AccessibilityCategories.SIGN_LANGUAGE],
99+
});
100+
101+
const signLanguageCheckbox = within(screen.getByTestId('checkbox-signLanguage')).getByRole(
102+
'checkbox',
103+
);
104+
await userEvent.click(signLanguageCheckbox);
105+
106+
expect(emitted()).toHaveProperty('input');
107+
expect(emitted().input[0][0]).toEqual([]);
108+
});
109+
110+
it('renders correctly when accessibility options are pre-checked via v-model', () => {
111+
renderComponent({
112+
kind: ContentKindsNames.VIDEO,
113+
value: [AccessibilityCategories.SIGN_LANGUAGE, AccessibilityCategories.CAPTIONS_SUBTITLES],
114+
});
115+
116+
expect(
117+
within(screen.getByTestId('checkbox-signLanguage')).getByRole('checkbox'),
118+
).toBeChecked();
119+
expect(
120+
within(screen.getByTestId('checkbox-captionsSubtitles')).getByRole('checkbox'),
121+
).toBeChecked();
122+
expect(
123+
within(screen.getByTestId('checkbox-audioDescription')).getByRole('checkbox'),
124+
).not.toBeChecked();
125+
});
100126
});
101127
});

0 commit comments

Comments
 (0)