-
Notifications
You must be signed in to change notification settings - Fork 183
/
Alert.spec.js
145 lines (113 loc) · 4.02 KB
/
Alert.spec.js
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
import Alert from '../Alert.svelte';
import {
render,
cleanup,
fireEvent,
waitForElementToBeRemoved
} from '@testing-library/svelte';
beforeEach(cleanup);
describe('Alert', () => {
test('should render with default color and text', () => {
const { queryByRole } = render(Alert, {
props: { children: 'Hello world!' }
});
const alert = queryByRole('alert');
expect(alert.innerHTML.trim()).toBe('Hello world!');
expect(alert.className).toBe('alert alert-success');
});
test('should render specified color', () => {
const { queryByRole } = render(Alert, {
props: { color: 'primary', children: 'Hello world!' }
});
const alert = queryByRole('alert');
expect(alert.className).toBe('alert alert-primary');
});
test('should render alert heading', () => {
const { container } = render(Alert, {
props: { heading: 'Hello world!' }
});
const heading = container.querySelector('.alert-heading');
expect(heading.textContent).toBe('Hello world!');
});
test('should render custom class', () => {
const { queryByRole } = render(Alert, {
props: { color: 'danger', children: 'Hello world!', class: 'boogie' }
});
const alert = queryByRole('alert');
expect(alert.className).toBe('boogie alert alert-danger');
});
test('should render dismissible alert', async () => {
const { queryByRole, queryByLabelText } = render(Alert, {
props: {
color: 'info',
children: 'I can be dismissed!',
dismissible: true
}
});
const alert = queryByRole('alert');
const closeBtn = queryByLabelText('Close');
expect(alert).toBeInTheDocument();
expect(alert.className).toBe('alert alert-info alert-dismissible');
expect(closeBtn).toBeInTheDocument();
fireEvent.click(closeBtn);
await waitForElementToBeRemoved(alert);
expect(alert).not.toBeInTheDocument();
expect(closeBtn).not.toBeInTheDocument();
});
test('should render dismissible alert', async () => {
const { queryByRole, queryByLabelText } = render(Alert, {
props: {
color: 'info',
children: 'I can be dismissed!',
dismissible: true
}
});
const alert = queryByRole('alert');
const closeBtn = queryByLabelText('Close');
expect(alert).toBeInTheDocument();
expect(alert.className).toBe('alert alert-info alert-dismissible');
expect(closeBtn).toBeInTheDocument();
await fireEvent.click(closeBtn);
await waitForElementToBeRemoved(alert, { timeout: 500 });
expect(alert).not.toBeInTheDocument();
expect(closeBtn).not.toBeInTheDocument();
});
test('should render a controlled alert', async () => {
let isOpen = true;
const toggle = jest.fn(() => {
isOpen = false;
});
const { rerender, queryByRole, queryByLabelText } = render(Alert, {
props: { color: 'info', children: 'I can be dismissed!', isOpen, toggle }
});
const alert = queryByRole('alert');
const closeBtn = queryByLabelText('Close');
expect(alert).toBeInTheDocument();
expect(alert.className).toBe('alert alert-info alert-dismissible');
expect(closeBtn).toBeInTheDocument();
await fireEvent.click(closeBtn);
expect(isOpen).toBe(false);
expect(toggle).toHaveBeenCalledTimes(1);
await rerender({ isOpen, toggle });
expect(alert).not.toBeInTheDocument();
});
test('should render alert without fade', async () => {
const { queryByRole, queryByLabelText } = render(Alert, {
props: {
color: 'info',
children: 'I can be dismissed!',
dismissible: true,
fade: false
}
});
const alert = queryByRole('alert');
const closeBtn = queryByLabelText('Close');
expect(alert).toBeInTheDocument();
expect(alert.className).toBe('alert alert-info alert-dismissible');
expect(closeBtn).toBeInTheDocument();
await fireEvent.click(closeBtn);
await waitForElementToBeRemoved(alert, { timeout: 100 });
expect(alert).not.toBeInTheDocument();
expect(closeBtn).not.toBeInTheDocument();
});
});