-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathkeyboard_shortcut.test.js
More file actions
152 lines (119 loc) · 4.2 KB
/
keyboard_shortcut.test.js
File metadata and controls
152 lines (119 loc) · 4.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import { mount } from '@vue/test-utils';
import { DtKeyboardShortcut } from '@/components/keyboard_shortcut';
import { DtText } from '@/components/text';
import { SHORTCUTS_ALIASES_LIST } from './keyboard_shortcut_constants';
const baseProps = {
shortcut: SHORTCUTS_ALIASES_LIST.join('+').trim(),
};
let mockProps = {};
describe('DtKeyboardShortcut Tests', () => {
let wrapper;
let iconComponents;
const updateWrapper = () => {
wrapper = mount(DtKeyboardShortcut, {
propsData: { ...baseProps, ...mockProps },
});
iconComponents = wrapper.findAll('[data-qa="dt-icon"]');
};
beforeEach(() => {
updateWrapper();
});
afterEach(() => {
mockProps = {};
});
describe('Presentation Tests', () => {
it('should render the component', () => {
expect(wrapper.exists()).toBe(true);
});
it('should render 13 icons', () => {
expect(iconComponents.length === 13).toBe(true);
});
});
describe('DtText Rendering', () => {
it('should render text items as DtText with tone="tertiary"', () => {
mockProps = { shortcut: 'X' };
updateWrapper();
const dtText = wrapper.findComponent(DtText);
expect(dtText.exists()).toBe(true);
expect(dtText.props('tone')).toBe('tertiary');
});
it('should render inverted text items as DtText with tone="tertiary"', () => {
mockProps = { shortcut: 'X', inverted: true };
updateWrapper();
const dtText = wrapper.findComponent(DtText);
expect(dtText.exists()).toBe(true);
expect(dtText.props('tone')).toBe('tertiary');
});
});
describe('Accessibility Tests', () => {
describe('Auto-generated aria-label', () => {
it('should generate aria-label with icon aliases converted to text', () => {
mockProps = {
shortcut: '{cmd}+X',
};
updateWrapper();
const srOnlySpan = wrapper.find('.d-keyboard-shortcut--sr-only');
expect(srOnlySpan.text()).toBe('Command plus X');
});
it('should convert key abbreviations to full names', () => {
mockProps = {
shortcut: 'Ctrl+Alt+Del',
};
updateWrapper();
const srOnlySpan = wrapper.find('.d-keyboard-shortcut--sr-only');
expect(srOnlySpan.text()).toBe('Control plus Alt plus Delete');
});
it('should handle mixed icon aliases and key abbreviations', () => {
mockProps = {
shortcut: '{cmd}+Ctrl+X',
};
updateWrapper();
const srOnlySpan = wrapper.find('.d-keyboard-shortcut--sr-only');
expect(srOnlySpan.text()).toBe('Command plus Control plus X');
});
it('should handle arrow key icons', () => {
mockProps = {
shortcut: '{arrow-up}+{arrow-down}',
};
updateWrapper();
const srOnlySpan = wrapper.find('.d-keyboard-shortcut--sr-only');
expect(srOnlySpan.text()).toBe('Up Arrow plus Down Arrow');
});
it('should handle Windows key icon', () => {
mockProps = {
shortcut: '{win}+D',
};
updateWrapper();
const srOnlySpan = wrapper.find('.d-keyboard-shortcut--sr-only');
expect(srOnlySpan.text()).toBe('Windows plus D');
});
it('should handle Option key icon', () => {
mockProps = {
shortcut: '{opt}+C',
};
updateWrapper();
const srOnlySpan = wrapper.find('.d-keyboard-shortcut--sr-only');
expect(srOnlySpan.text()).toBe('Option plus C');
});
});
describe('screenReaderText override', () => {
it('should use screenReaderText when provided', () => {
mockProps = {
shortcut: '{cmd}+X',
screenReaderText: 'Custom accessible text',
};
updateWrapper();
const srOnlySpan = wrapper.find('.d-keyboard-shortcut--sr-only');
expect(srOnlySpan.text()).toBe('Custom accessible text');
});
it('should use auto-generated aria-label when screenReaderText is not provided', () => {
mockProps = {
shortcut: '{cmd}+X',
};
updateWrapper();
const srOnlySpan = wrapper.find('.d-keyboard-shortcut--sr-only');
expect(srOnlySpan.text()).toBe('Command plus X');
});
});
});
});