Skip to content

Commit 11196ad

Browse files
committed
use the OPENED event when it makes sense.
1 parent 23f5c29 commit 11196ad

File tree

1 file changed

+36
-16
lines changed

1 file changed

+36
-16
lines changed

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

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ function sleep(ms: number) {
1818
return new Promise(resolve => setTimeout(resolve, ms));
1919
}
2020

21-
const animationDuration: number = 25; // still needs to be some time, otherwise it goes too fast for the rendering and the test to work properly.
21+
const ANIMATION_DURATION: number = 25; // still needs to be some time, otherwise it goes too fast for the rendering and the test to work properly.
2222

2323
describe('UUIToastNotificationElement', () => {
2424
let element: UUIToastNotificationElement;
@@ -29,7 +29,7 @@ describe('UUIToastNotificationElement', () => {
2929
);
3030
element.style.setProperty(
3131
'--uui-toast-notification-animation-duration',
32-
animationDuration + 'ms'
32+
ANIMATION_DURATION + 'ms'
3333
);
3434
});
3535

@@ -61,7 +61,7 @@ describe('UUIToastNotificationElement', () => {
6161
.to.have.property('_getAnimationDuration')
6262
.that.is.a('function');
6363
expect((element as any)._getAnimationDuration()).to.be.equal(
64-
animationDuration
64+
ANIMATION_DURATION
6565
);
6666
});
6767
});
@@ -87,13 +87,22 @@ describe('UUIToastNotificationElement', () => {
8787

8888
describe('closing', () => {
8989
it('emits a closing event when elements starts to closing', async () => {
90+
const openedListener = oneEvent(
91+
element,
92+
UUIToastNotificationEvent.OPENED
93+
);
94+
const closingListener = oneEvent(
95+
element,
96+
UUIToastNotificationEvent.CLOSING
97+
);
98+
9099
element.open = true;
91-
await elementUpdated(element);
92-
const listener = oneEvent(element, UUIToastNotificationEvent.CLOSING);
93-
await sleep(animationDuration + 1); // enough time for opening-animation to be done.
100+
await openedListener;
101+
94102
expect(element.open).to.be.true;
95103
element.open = false;
96-
const event = await listener;
104+
const event = await closingListener;
105+
97106
expect(event).to.exist;
98107
expect(event.type).to.equal(UUIToastNotificationEvent.CLOSING);
99108
expect(element.open).to.be.false;
@@ -102,7 +111,7 @@ describe('UUIToastNotificationElement', () => {
102111
element.open = true;
103112
await elementUpdated(element);
104113
const listener = oneEvent(element, UUIToastNotificationEvent.CLOSING);
105-
await sleep(animationDuration / 2); // enough time for the rendering and opening-animation to start.
114+
await sleep(ANIMATION_DURATION / 2); // enough time for the rendering and opening-animation to start, but not finished.
106115
expect(element.open).to.be.true;
107116
element.open = false;
108117
const event = await listener;
@@ -117,7 +126,7 @@ describe('UUIToastNotificationElement', () => {
117126
element.addEventListener(UUIToastNotificationEvent.CLOSING, e => {
118127
e.preventDefault();
119128
});
120-
await sleep(animationDuration / 2); // enough time for the rendering and opening-animation to start.
129+
await sleep(ANIMATION_DURATION / 2); // enough time for the rendering and opening-animation to start, but not finished.
121130
expect(element.open).to.be.true;
122131
element.open = false;
123132
const event = await listener;
@@ -128,22 +137,33 @@ describe('UUIToastNotificationElement', () => {
128137
});
129138
describe('closed', () => {
130139
it('emits a opening event when elements is closed', async () => {
131-
element.open = true;
132-
await elementUpdated(element);
140+
const openedListener = oneEvent(
141+
element,
142+
UUIToastNotificationEvent.OPENED
143+
);
133144
const listener = oneEvent(element, UUIToastNotificationEvent.CLOSED);
134-
await sleep(animationDuration + 1); // enough time for opening-animation to be done.
145+
146+
element.open = true;
147+
await openedListener;
148+
135149
expect(element.open).to.be.true;
150+
136151
element.open = false;
137152
const event = await listener;
138153
expect(event).to.exist;
139154
expect(event.type).to.equal(UUIToastNotificationEvent.CLOSED);
140155
expect(element.open).to.be.false;
141156
});
142157
it('emits a close event though toast is still running its opening-animation', async () => {
143-
element.open = true;
144-
await elementUpdated(element);
158+
const openedListener = oneEvent(
159+
element,
160+
UUIToastNotificationEvent.OPENED
161+
);
145162
const listener = oneEvent(element, UUIToastNotificationEvent.CLOSED);
146-
await sleep(animationDuration / 2); // enough time for the rendering and opening-animation to start.
163+
164+
element.open = true;
165+
await openedListener;
166+
147167
expect(element.open).to.be.true;
148168
element.open = false;
149169
const event = await listener;
@@ -165,7 +185,7 @@ describe('UUIToastNotificationElement', () => {
165185
expect(openEvent.type).to.equal(UUIToastNotificationEvent.OPENING);
166186
expect(element.open).to.be.true;
167187

168-
await sleep(animationDuration / 2); // enough time for the rendering and opening-animation to start.
188+
await sleep(ANIMATION_DURATION / 2); // enough time for the rendering and opening-animation to start, but not finished.
169189

170190
const closeListener = oneEvent(
171191
element,

0 commit comments

Comments
 (0)