-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Expand file tree
/
Copy pathtoggle.spec.ts
More file actions
160 lines (131 loc) · 4.95 KB
/
toggle.spec.ts
File metadata and controls
160 lines (131 loc) · 4.95 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
153
154
155
156
157
158
159
160
import { newSpecPage } from '@stencil/core/testing';
import { checkmarkOutline, ellipseOutline, removeOutline } from 'ionicons/icons';
import { config } from '../../../global/config';
import { Toggle } from '../toggle';
describe('toggle', () => {
beforeEach(() => {
config.reset({});
});
const newToggle = async (): Promise<Toggle> => {
const { rootInstance } = await newSpecPage({
components: [Toggle],
html: `<ion-toggle></ion-toggle>`,
});
return rootInstance;
};
describe('enableOnOffLabels', () => {
it('should disable on/off labels when setting to false on component', async () => {
const t = await newToggle();
t.enableOnOffLabels = false;
config.reset({
toggleOnOffLabels: true,
});
expect(t.enableOnOffLabels).toBe(false);
});
it('should enable on/off labels when setting to true on global config', async () => {
config.reset({
toggleOnOffLabels: true,
});
const t = await newToggle();
expect(t.enableOnOffLabels).toBe(true);
});
it('should enable on/off labels when setting to true on component', async () => {
const t = await newToggle();
t.enableOnOffLabels = true;
expect(t.enableOnOffLabels).toBe(true);
});
});
describe('checkedIcon and uncheckedIcon', () => {
it('should set custom checked icon on the instance, overriding config', async () => {
const t = await newToggle();
t.checkedIcon = 'custom-checked-icon-instance';
config.reset({
toggleCheckedIcon: 'custom-checked-icon-config',
});
expect((t as any).getSwitchLabelIcon('md', true)).toBe('custom-checked-icon-instance');
});
it('should set custom unchecked icon on the instance, overriding config', async () => {
const t = await newToggle();
t.uncheckedIcon = 'custom-unchecked-icon-instance';
config.reset({
toggleUncheckedIcon: 'custom-unchecked-icon-config',
});
expect((t as any).getSwitchLabelIcon('md', false)).toBe('custom-unchecked-icon-instance');
});
it('should set custom checked icon in the config', async () => {
const t = await newToggle();
config.reset({
toggleCheckedIcon: 'custom-checked-icon-config',
});
expect((t as any).getSwitchLabelIcon('md', true)).toBe('custom-checked-icon-config');
});
it('should set custom unchecked icon in the config', async () => {
const t = await newToggle();
config.reset({
toggleUncheckedIcon: 'custom-unchecked-icon-config',
});
expect((t as any).getSwitchLabelIcon('md', false)).toBe('custom-unchecked-icon-config');
});
it('should use default icons in md mode', async () => {
const t = await newToggle();
expect((t as any).getSwitchLabelIcon('md', true)).toBe(checkmarkOutline);
expect((t as any).getSwitchLabelIcon('md', false)).toBe(removeOutline);
});
it('should use default icons in ios mode', async () => {
const t = await newToggle();
expect((t as any).getSwitchLabelIcon('ios', true)).toBe(removeOutline);
expect((t as any).getSwitchLabelIcon('ios', false)).toBe(ellipseOutline);
});
});
describe('shadow parts', () => {
it('should have shadow parts', async () => {
const page = await newSpecPage({
components: [Toggle],
html: `<ion-toggle>Label</ion-toggle>`,
});
const toggle = page.body.querySelector('ion-toggle')!;
expect(toggle).toHaveShadowPart('label');
expect(toggle).toHaveShadowPart('track');
expect(toggle).toHaveShadowPart('handle');
});
});
});
describe('ion-toggle: disabled', () => {
it('clicking disabled toggle should not toggle checked state', async () => {
const page = await newSpecPage({
components: [Toggle],
html: `
<ion-toggle disabled="true">Toggle</ion-toggle>
`,
});
const toggle = page.body.querySelector('ion-toggle')!;
expect(toggle.checked).toBe(false);
toggle.click();
await page.waitForChanges();
expect(toggle.checked).toBe(false);
});
});
describe('ion-toggle: required', () => {
it('should have a required attribute in inner input when true', async () => {
const page = await newSpecPage({
components: [Toggle],
html: `
<ion-toggle required="true">Toggle</ion-toggle>
`,
});
const toggle = page.body.querySelector('ion-toggle')!;
const nativeInput = toggle.shadowRoot?.querySelector('input[role=switch]')!;
expect(nativeInput.hasAttribute('required')).toBeTruthy();
});
it('should not have a required attribute in inner input when false', async () => {
const page = await newSpecPage({
components: [Toggle],
html: `
<ion-toggle required="false">Toggle</ion-toggle>
`,
});
const toggle = page.body.querySelector('ion-toggle')!;
const nativeInput = toggle.shadowRoot?.querySelector('input[role=switch]')!;
expect(nativeInput.hasAttribute('required')).toBeFalsy();
});
});