forked from vmware-archive/clarity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app-alert.element.spec.ts
164 lines (141 loc) · 6.4 KB
/
app-alert.element.spec.ts
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
161
162
163
164
/*
* Copyright (c) 2016-2020 VMware, Inc. All Rights Reserved.
* This software is released under MIT license.
* The full license information can be found in LICENSE in the root directory of this project.
*/
import { CdsAppAlert } from '@clr/core/alert';
import '@clr/core/alert';
import { CdsIcon } from '@clr/core/icon-shapes';
import { CommonStringsService, CommonStringsServiceInternal } from '@clr/core/internal';
import {
componentIsStable,
createTestElement,
getComponentSlotContent,
removeTestElement,
waitForComponent,
} from '@clr/core/test/utils';
describe('alert element', () => {
let testElement: HTMLElement;
let component: CdsAppAlert;
const placeholderText = 'I am a default alert with no attributes.';
const placeholderActionsText = 'This is where action elements go.';
describe('Basic alert behaviors', () => {
beforeEach(async () => {
testElement = createTestElement();
testElement.innerHTML = `
<cds-app-alert>
<cds-alert-content>${placeholderText}</cds-alert-content>
<cds-alert-actions>${placeholderActionsText}</cds-alert-actions>
</cds-app-alert>
`;
await waitForComponent('cds-app-alert');
component = testElement.querySelector<CdsAppAlert>('cds-app-alert');
});
afterEach(() => {
removeTestElement(testElement);
});
it('should create the component', async () => {
await componentIsStable(component);
const slots = getComponentSlotContent(component);
expect(slots.default).toBe(`<cds-alert-content>${placeholderText}</cds-alert-content>`);
expect(slots.actions).toBe(`<cds-alert-actions slot="actions">${placeholderActionsText}</cds-alert-actions>`);
});
it('should support closable option', async () => {
await componentIsStable(component);
expect(component.closable).toBe(true);
expect(component.hasAttribute('closable')).toBe(true);
let button = component.shadowRoot.querySelector('button.close');
expect(button).not.toBeNull();
expect(button.classList.contains('close')).toBe(true);
component.closable = false;
await componentIsStable(component);
expect(component.hasAttribute('closable')).toBe(false);
button = component.shadowRoot.querySelector<HTMLButtonElement>('button.close');
expect(button).toBeNull();
});
it('should set icon shape based on status', async () => {
await componentIsStable(component);
let icon = component.shadowRoot.querySelector<CdsIcon>('cds-icon');
expect(icon).not.toBeNull();
expect(icon.hasAttribute('shape')).toBe(true);
expect(icon.getAttribute('shape')).toEqual('info-circle');
component.status = 'warning';
await componentIsStable(component);
icon = component.shadowRoot.querySelector<CdsIcon>('cds-icon');
expect(icon).not.toBeNull();
expect(icon.hasAttribute('shape')).toBe(true);
expect(icon.getAttribute('shape')).toEqual('exclamation-triangle');
component.status = 'danger';
await componentIsStable(component);
icon = component.shadowRoot.querySelector<CdsIcon>('cds-icon');
expect(icon).not.toBeNull();
expect(icon.hasAttribute('shape')).toBe(true);
expect(icon.getAttribute('shape')).toEqual('exclamation-circle');
});
it('should support custom icon shape', async () => {
await componentIsStable(component);
let icon = component.shadowRoot.querySelector<CdsIcon>('cds-icon');
component.iconShape = 'exclamation-triangle';
await componentIsStable(component);
icon = component.shadowRoot.querySelector<CdsIcon>('cds-icon');
expect(icon).not.toBeNull();
expect(icon.hasAttribute('shape')).toBe(true);
expect(icon.getAttribute('shape')).toEqual('exclamation-triangle');
});
it('should set icon title based on status', async () => {
await componentIsStable(component);
let icon = component.shadowRoot.querySelector<CdsIcon>('cds-icon');
expect(icon).not.toBeNull();
expect(icon.hasAttribute('title')).toBe(true);
expect(icon.getAttribute('title')).toEqual(CommonStringsService.keys.info);
component.status = 'warning';
await componentIsStable(component);
icon = component.shadowRoot.querySelector<CdsIcon>('cds-icon');
expect(icon).not.toBeNull();
expect(icon).toBeDefined();
expect(icon.hasAttribute('title')).toBe(true);
expect(icon.getAttribute('title')).toEqual(CommonStringsService.keys.warning);
component.status = 'danger';
await componentIsStable(component);
icon = component.shadowRoot.querySelector<CdsIcon>('cds-icon');
expect(icon).not.toBeNull();
expect(icon).toBeDefined();
expect(icon.hasAttribute('title')).toBe(true);
expect(icon.getAttribute('title')).toEqual(CommonStringsService.keys.danger);
});
it('should support custom icon title', async () => {
await componentIsStable(component);
let icon = component.shadowRoot.querySelector<CdsIcon>('cds-icon');
expect(icon).not.toBeNull();
expect(icon.hasAttribute('shape')).toBe(true);
expect(icon.getAttribute('shape')).toEqual('info-circle');
component.iconTitle = 'my-icon-title';
await componentIsStable(component);
icon = component.shadowRoot.querySelector<CdsIcon>('cds-icon');
expect(icon).not.toBeNull();
expect(icon).toBeDefined();
expect(icon.hasAttribute('title')).toBe(true);
expect(icon.getAttribute('title')).toEqual('my-icon-title');
});
it('should set close button aria label using Common Strings Service', async () => {
const service = new CommonStringsServiceInternal();
await componentIsStable(component);
const button = component.shadowRoot.querySelector<HTMLButtonElement>('button.close');
expect(button).not.toBeNull();
expect(button.hasAttribute('aria-label')).toBe(true);
expect(button.getAttribute('aria-label')).toEqual(service.keys.alertCloseButtonAriaLabel);
});
it('should emit a closeChanged event when close button is clicked', async done => {
let value: any;
await componentIsStable(component);
component.addEventListener<any>('closeChange', (e: CustomEvent) => {
value = e.detail;
expect(value).toBe(true);
done();
});
const button = component.shadowRoot.querySelector<HTMLButtonElement>('button.close');
expect(button).toBeDefined();
button.click();
});
});
});