Skip to content

Commit 1ed4631

Browse files
[v19/v21] fix: corrige tempo de notificação (#1370)
* fix: corrige tempo de notificação * fix: desconsidera o titulo * feat: adiciona casos de testes para titulo
1 parent 2e60b60 commit 1ed4631

File tree

2 files changed

+50
-2
lines changed

2 files changed

+50
-2
lines changed

projects/ion/src/lib/notification/component/notification.component.spec.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ describe('Time by words', () => {
151151
...defaultNotification,
152152
// "word" -> 1 word. 1/3 = 0.33. + 1 = 1.33s -> 1000ms.
153153
message: 'word',
154+
title: '',
154155
fixed: false,
155156
},
156157
on: {
@@ -161,6 +162,42 @@ describe('Time by words', () => {
161162
jest.advanceTimersByTime(2500); // 1000ms delay + 1000ms animation + buffer
162163
expect(ionOnCloseSpy).toHaveBeenCalled();
163164
});
165+
166+
it('should emit close event when only title is passed', async () => {
167+
const ionOnCloseSpy = jest.fn();
168+
await render(IonNotificationComponent, {
169+
componentInputs: {
170+
title: 'Notification Title',
171+
message: '',
172+
fixed: false,
173+
},
174+
on: {
175+
ionOnClose: ionOnCloseSpy,
176+
},
177+
});
178+
179+
// "Notification Title" -> 2 words. 2/3 = 0.66. + 1 = 1.66s -> 2000ms.
180+
jest.advanceTimersByTime(3500); // 2000ms delay + 1000ms animation + buffer
181+
expect(ionOnCloseSpy).toHaveBeenCalled();
182+
});
183+
184+
it('should emit close event when title and message are passed', async () => {
185+
const ionOnCloseSpy = jest.fn();
186+
await render(IonNotificationComponent, {
187+
componentInputs: {
188+
title: 'Title',
189+
message: 'This is a message',
190+
fixed: false,
191+
},
192+
on: {
193+
ionOnClose: ionOnCloseSpy,
194+
},
195+
});
196+
197+
// "Title This is a message" -> 5 words. 5/3 = 1.66. + 1 = 2.66s -> 3000ms.
198+
jest.advanceTimersByTime(4500); // 3000ms delay + 1000ms animation + buffer
199+
expect(ionOnCloseSpy).toHaveBeenCalled();
200+
});
164201
});
165202

166203
describe('Pause on hover', () => {

projects/ion/src/lib/notification/component/notification.component.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,21 +92,32 @@ export class IonNotificationComponent implements OnInit, OnDestroy {
9292
if (this.fixed() || !this.pauseOnHover() || this.isClosing) {
9393
return;
9494
}
95-
const timeToClose = this.timeByWords(this.message());
95+
const timeToClose = this.timeByWords(this._getMessageToCheck());
9696
this.autoCloseTimer = setTimeout(() => {
9797
this.closeNotification();
9898
}, timeToClose);
9999
}
100100

101101
ngOnInit(): void {
102102
if (!this.fixed()) {
103-
const timeToClose = this.timeByWords(this.message());
103+
const timeToClose = this.timeByWords(this._getMessageToCheck());
104104
this.autoCloseTimer = setTimeout(() => {
105105
this.closeNotification();
106106
}, timeToClose);
107107
}
108108
}
109109

110+
private _getMessageToCheck(): string {
111+
if (!this.title()) {
112+
return this.message();
113+
}
114+
if (!this.message()) {
115+
return this.title();
116+
}
117+
118+
return `${this.title()} ${this.message()}`;
119+
}
120+
110121
ngOnDestroy(): void {
111122
clearTimeout(this.autoCloseTimer);
112123
clearTimeout(this.closeAnimationTimer);

0 commit comments

Comments
 (0)