Skip to content

Commit 7526be5

Browse files
committed
Replace sleep() calls with OPENED event
1 parent 6da993c commit 7526be5

File tree

1 file changed

+68
-15
lines changed

1 file changed

+68
-15
lines changed

packages/uui-toast-notification-container/lib/uui-toast-notification-container.test.ts

Lines changed: 68 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,16 @@ describe('UUIToastNotificationContainerElement', () => {
100100

101101
element.appendChild(toastElement);
102102

103-
await sleep(600); // Enough time to cover if it did happen that the element opened and auto-closed.
103+
// Wait for OPENED event
104+
const openedListener = oneEvent(
105+
toastElement,
106+
UUIToastNotificationEvent.OPENED
107+
);
108+
const openedEvent = await openedListener;
109+
expect(openedEvent).to.exist;
110+
expect(openedEvent.type).to.equal(UUIToastNotificationEvent.OPENED);
111+
112+
await sleep(element.autoClose + 1); // Enough time to cover if it did happen that the element opened and auto-closed.
104113

105114
// Check that its open.
106115
expect(toastElement.open).to.be.true;
@@ -113,28 +122,51 @@ describe('UUIToastNotificationContainerElement', () => {
113122
element.appendChild(toastElement);
114123

115124
await elementUpdated(element);
116-
await sleep(100);
125+
126+
// Wait for OPENING event
127+
const openingListener = oneEvent(
128+
toastElement,
129+
UUIToastNotificationEvent.OPENING
130+
);
131+
const openingEvent = await openingListener;
132+
expect(openingEvent).to.exist;
133+
expect(openingEvent.type).to.equal(UUIToastNotificationEvent.OPENING);
117134

118135
element.pauseAutoClose();
119136

120-
await sleep(500); // Enough time to cover if it did happen that the element opened and auto-closed.
137+
// Wait for OPENED event
138+
const openedListener = oneEvent(
139+
toastElement,
140+
UUIToastNotificationEvent.OPENED
141+
);
142+
const openedEvent = await openedListener;
143+
expect(openedEvent).to.exist;
144+
expect(openedEvent.type).to.equal(UUIToastNotificationEvent.OPENED);
121145

122146
// Check that its still open, pause actually did work.
123147
expect(toastElement.open).to.be.true;
124148
});
125149

126150
it('pausing autoClose when open', async () => {
127-
element.autoClose = 200;
151+
element.autoClose = 20;
128152
await elementUpdated(element);
129153

130154
element.appendChild(toastElement);
131155

132156
await elementUpdated(element);
133-
await sleep(500);
157+
158+
// Wait for OPENED event
159+
const openedListener = oneEvent(
160+
toastElement,
161+
UUIToastNotificationEvent.OPENED
162+
);
163+
const openedEvent = await openedListener;
164+
expect(openedEvent).to.exist;
165+
expect(openedEvent.type).to.equal(UUIToastNotificationEvent.OPENED);
134166

135167
element.pauseAutoClose();
136168

137-
await sleep(300); // Enough time to cover if it did happen that the element auto-closed.
169+
await sleep(element.autoClose + 1); // Enough time to cover if it did happen that the element auto-closed.
138170

139171
// Check that its still open, pause actually did work.
140172
expect(toastElement.open).to.be.true;
@@ -146,7 +178,14 @@ describe('UUIToastNotificationContainerElement', () => {
146178

147179
element.appendChild(toastElement);
148180

149-
await sleep(100); // Enough time to cover if it did happen that the element opened and auto-closed.
181+
// Wait for OPENED event
182+
const openedListener = oneEvent(
183+
toastElement,
184+
UUIToastNotificationEvent.OPENED
185+
);
186+
const openedEvent = await openedListener;
187+
expect(openedEvent).to.exist;
188+
expect(openedEvent.type).to.equal(UUIToastNotificationEvent.OPENED);
150189

151190
// Check that its still open, pause actually did work.
152191
expect(toastElement.open).to.be.true;
@@ -168,19 +207,26 @@ describe('UUIToastNotificationContainerElement', () => {
168207
element.appendChild(toastElement);
169208

170209
await elementUpdated(element);
171-
await sleep(100);
210+
211+
// Wait for OPENED event
212+
const openedListener = oneEvent(
213+
toastElement,
214+
UUIToastNotificationEvent.OPENED
215+
);
216+
const openedEvent = await openedListener;
217+
expect(openedEvent).to.exist;
218+
expect(openedEvent.type).to.equal(UUIToastNotificationEvent.OPENED);
172219

173220
toastElement.dispatchEvent(new Event('focus'));
174221

175-
await sleep(400); // Enough time to cover if it did happen that the element opened and auto-closed.
222+
await sleep(element.autoClose + 1); // Enough time to cover if it did happen that the element opened and auto-closed.
176223

177224
// Check that its still open, pause actually did work.
178-
expect((toastElement as any)._animate).to.be.false; // Checking private _animate to ensure that guessed animation time was good.
179225
expect(toastElement.open).to.be.true;
180226

181227
toastElement.dispatchEvent(new Event('blur'));
182228

183-
await sleep(40); // Enough time to cover if it did happen that the element opened and auto-closed.
229+
await sleep(element.autoClose + 1); // Enough time to cover if it did happen that the element opened and auto-closed.
184230

185231
// Check that its still open, pause actually did work.
186232
expect(toastElement.open).to.be.false;
@@ -192,19 +238,26 @@ describe('UUIToastNotificationContainerElement', () => {
192238
element.appendChild(toastElement);
193239

194240
await elementUpdated(element);
195-
await sleep(100);
241+
242+
// Wait for OPENED event
243+
const openedListener = oneEvent(
244+
toastElement,
245+
UUIToastNotificationEvent.OPENED
246+
);
247+
const openedEvent = await openedListener;
248+
expect(openedEvent).to.exist;
249+
expect(openedEvent.type).to.equal(UUIToastNotificationEvent.OPENED);
196250

197251
toastElement.dispatchEvent(new Event('mouseenter'));
198252

199-
await sleep(400); // Enough time to cover if it did happen that the element opened and auto-closed.
253+
await sleep(element.autoClose + 1); // Enough time to cover if it did happen that the element opened and auto-closed.
200254

201255
// Check that its still open, pause actually did work.
202-
expect((toastElement as any)._animate).to.be.false; // Checking private _animate to ensure that guessed animation time was good.
203256
expect(toastElement.open).to.be.true;
204257

205258
toastElement.dispatchEvent(new Event('mouseleave'));
206259

207-
await sleep(40); // Enough time to cover if it did happen that the element opened and auto-closed.
260+
await sleep(element.autoClose + 1); // Enough time to cover if it did happen that the element opened and auto-closed.
208261

209262
// Check that its still open, pause actually did work.
210263
expect(toastElement.open).to.be.false;

0 commit comments

Comments
 (0)