-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* 383 notification service (#736) * feat: add home icon * feat: add new icon #594 * refactor: replacing config2 icon #594 * feat: #383 creating notification service * feat: #383 ajusting width * feat: #383 updating public-api * feat: inserting notification service in storybooks and separating the notification component * fix: lint --------- Co-authored-by: Lucas <[email protected]> Co-authored-by: Alysson Keysson <[email protected]> Co-authored-by: Alysson Keysson <[email protected]> * Delete yarn.lock * Revert "Delete yarn.lock" This reverts commit 432faba. * chore: remove trim of yarn.lock * fix: fixing tests * refactor: updating notification service ts and spec #383 * refactor: changes pointed on review #383 * refactor: adding test to complete coverage #383 * fix: lint fix * refactor: review changes, refactoring ts file and new test case #383 --------- Co-authored-by: lucasrasec <[email protected]> Co-authored-by: Lucas <[email protected]> Co-authored-by: Alysson Keysson <[email protected]> Co-authored-by: Iury Nogueira <[email protected]> Co-authored-by: Lucas <[email protected]> Co-authored-by: lucas-alcantara-brisa <[email protected]>
- Loading branch information
1 parent
7becdc3
commit c8b43d1
Showing
14 changed files
with
546 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 3 additions & 3 deletions
6
...tification/notification.component.spec.ts → .../component/notification.component.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
projects/ion/src/lib/notification/mock/open-notification-mock.component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import { IonNotificationService } from './../service/notification.service'; | ||
import { Component } from '@angular/core'; | ||
|
||
@Component({ | ||
selector: 'open-notification-button', | ||
template: ` | ||
<div style="display: flex; flex-direction: column; gap: 2rem;"> | ||
<ion-button | ||
class="button" | ||
label="success" | ||
iconType="check" | ||
(ionOnClick)="notificationSuccess()" | ||
></ion-button> | ||
<ion-button | ||
class="button" | ||
label="warning" | ||
type="dashed" | ||
iconType="exclamation" | ||
(ionOnClick)="notificationWarning()" | ||
></ion-button> | ||
<ion-button | ||
class="button" | ||
label="info" | ||
type="secondary" | ||
iconType="info" | ||
(ionOnClick)="notificationInfo()" | ||
></ion-button> | ||
<ion-button | ||
class="button" | ||
label="error" | ||
[danger]="true" | ||
iconType="close" | ||
(ionOnClick)="notificationError()" | ||
></ion-button> | ||
<div></div> | ||
</div> | ||
`, | ||
styles: [ | ||
` | ||
.button { | ||
/deep/ button { | ||
width: 150px !important; | ||
} | ||
} | ||
`, | ||
], | ||
}) | ||
export class OpenNotificationButtonComponent { | ||
constructor(private ionNotificationService: IonNotificationService) {} | ||
|
||
notificationSuccess(): void { | ||
this.ionNotificationService.success( | ||
'Title...', | ||
'Lorem ipsum dolor sit amet, consectetur adipiscing elit...' | ||
); | ||
} | ||
|
||
notificationWarning(): void { | ||
this.ionNotificationService.warning( | ||
'Title...', | ||
'Lorem ipsum dolor sit amet, consectetur adipiscing elit...' | ||
); | ||
} | ||
|
||
notificationInfo(): void { | ||
this.ionNotificationService.info( | ||
'Title...', | ||
'Lorem ipsum dolor sit amet, consectetur adipiscing elit...' | ||
); | ||
} | ||
|
||
notificationError(): void { | ||
this.ionNotificationService.error( | ||
'Title...', | ||
'Lorem ipsum dolor sit amet, consectetur adipiscing elit...' | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,18 @@ | ||
import { IonNotificationContainerComponent } from './service/notification.container.component'; | ||
import { IonNotificationService } from './service/notification.service'; | ||
import { NgModule } from '@angular/core'; | ||
import { CommonModule } from '@angular/common'; | ||
import { IonIconModule } from '../icon/icon.module'; | ||
import { IonNotificationComponent } from './notification.component'; | ||
import { IonNotificationComponent } from './component/notification.component'; | ||
|
||
@NgModule({ | ||
declarations: [IonNotificationComponent], | ||
declarations: [IonNotificationComponent, IonNotificationContainerComponent], | ||
imports: [CommonModule, IonIconModule], | ||
exports: [IonNotificationComponent], | ||
providers: [IonNotificationService], | ||
exports: [IonNotificationComponent, IonNotificationContainerComponent], | ||
entryComponents: [ | ||
IonNotificationComponent, | ||
IonNotificationContainerComponent, | ||
], | ||
}) | ||
export class IonNotificationModule {} |
26 changes: 26 additions & 0 deletions
26
projects/ion/src/lib/notification/service/notification.container.component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { IonNotificationComponent } from '../component/notification.component'; | ||
import { Component, ComponentRef, Renderer2, ElementRef } from '@angular/core'; | ||
|
||
@Component({ | ||
selector: 'notification-container', | ||
template: '', | ||
styleUrls: ['notification.container.scss'], | ||
}) | ||
export class IonNotificationContainerComponent { | ||
constructor(private renderer: Renderer2, private element: ElementRef) {} | ||
|
||
addNotification(notification: ComponentRef<IonNotificationComponent>): void { | ||
notification.instance.ionOnClose.subscribe(() => { | ||
this.removeNotification(notification.location.nativeElement); | ||
}); | ||
|
||
this.renderer.appendChild( | ||
this.element.nativeElement, | ||
notification.location.nativeElement | ||
); | ||
} | ||
|
||
removeNotification(notification: ElementRef): void { | ||
this.renderer.removeChild(this.element.nativeElement, notification); | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
projects/ion/src/lib/notification/service/notification.container.scss
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
:host { | ||
position: absolute; | ||
display: flex; | ||
flex-direction: column; | ||
top: 0; | ||
right: 0; | ||
gap: 1rem; | ||
} |
164 changes: 164 additions & 0 deletions
164
projects/ion/src/lib/notification/service/notification.service.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
import { IonSharedModule } from './../../shared.module'; | ||
import { IonNotificationContainerComponent } from './notification.container.component'; | ||
import { IonNotificationComponent } from '../component/notification.component'; | ||
import { IonNotificationService } from './notification.service'; | ||
import { TestBed } from '@angular/core/testing'; | ||
import { Component, NgModule } from '@angular/core'; | ||
import { fireEvent, screen } from '@testing-library/angular'; | ||
|
||
const NOTIFICATION_ICONS = { | ||
success: 'success-icon', | ||
info: 'info-icon', | ||
warning: 'warning-icon', | ||
negative: 'negative-icon', | ||
}; | ||
|
||
const NOTIFICATION_TYPES = Object.keys(NOTIFICATION_ICONS); | ||
|
||
const DEFAULT_NOTIFICATION_OPTIONS = { | ||
title: 'Titulo Padrão', | ||
message: 'Mensagem Padrão', | ||
}; | ||
|
||
@Component({ | ||
template: '<div></div>', | ||
}) | ||
class ContainerRefTestComponent {} | ||
|
||
@NgModule({ | ||
declarations: [ | ||
ContainerRefTestComponent, | ||
IonNotificationContainerComponent, | ||
IonNotificationComponent, | ||
], | ||
imports: [IonSharedModule], | ||
entryComponents: [ | ||
ContainerRefTestComponent, | ||
IonNotificationContainerComponent, | ||
IonNotificationComponent, | ||
], | ||
}) | ||
class TestModule {} | ||
|
||
jest.setTimeout(1000); | ||
|
||
describe('NotificationService', () => { | ||
let notificationService: IonNotificationService; | ||
beforeEach(() => { | ||
TestBed.configureTestingModule({ | ||
imports: [TestModule], | ||
}).compileComponents(); | ||
|
||
notificationService = TestBed.get(IonNotificationService); | ||
}); | ||
|
||
it('should remove a notification', () => { | ||
notificationService.success( | ||
DEFAULT_NOTIFICATION_OPTIONS.title, | ||
DEFAULT_NOTIFICATION_OPTIONS.message, | ||
{ fixed: true } | ||
); | ||
const removeNotification = screen.getByTestId('btn-remove'); | ||
fireEvent.click(removeNotification); | ||
const elements = document.getElementsByTagName('ion-notification'); | ||
expect(elements).toHaveLength(0); | ||
}); | ||
|
||
it('should emit event when a notification is closed', () => { | ||
const closeEvent = jest.fn(); | ||
notificationService.success( | ||
DEFAULT_NOTIFICATION_OPTIONS.title, | ||
DEFAULT_NOTIFICATION_OPTIONS.message, | ||
{ fixed: true }, | ||
closeEvent | ||
); | ||
const removeNotification = screen.getByTestId('btn-remove'); | ||
fireEvent.click(removeNotification); | ||
expect(closeEvent).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('should create a notification', () => { | ||
notificationService.success( | ||
DEFAULT_NOTIFICATION_OPTIONS.title, | ||
DEFAULT_NOTIFICATION_OPTIONS.message | ||
); | ||
expect(screen.getByTestId('ion-notification')).toBeTruthy(); | ||
}); | ||
|
||
it.each(['title', 'message'])( | ||
'should render a notification with default %s', | ||
(key) => { | ||
expect( | ||
screen.getByText(DEFAULT_NOTIFICATION_OPTIONS[key]) | ||
).toBeInTheDocument(); | ||
} | ||
); | ||
}); | ||
|
||
describe('NotificationService -> notification types', () => { | ||
let notificationService: IonNotificationService; | ||
|
||
let currentIndex = 1; | ||
|
||
let notificationsOnScreen = 5; | ||
|
||
const indexToRemove = [1, 2, 0]; | ||
|
||
const NOTIFICATIONS_CALLS = { | ||
success: (): void => { | ||
notificationService.success('teste', 'teste', {}, () => { | ||
return true; | ||
}); | ||
}, | ||
info: (): void => { | ||
notificationService.info('teste', 'teste', {}, () => { | ||
return true; | ||
}); | ||
}, | ||
warning: (): void => { | ||
notificationService.warning('teste', 'teste', {}, () => { | ||
return true; | ||
}); | ||
}, | ||
negative: (): void => { | ||
notificationService.error('teste', 'teste', {}, () => { | ||
return true; | ||
}); | ||
}, | ||
}; | ||
|
||
beforeEach(() => { | ||
TestBed.configureTestingModule({ | ||
imports: [TestModule], | ||
}).compileComponents(); | ||
|
||
notificationService = TestBed.get(IonNotificationService); | ||
}); | ||
|
||
it.each(NOTIFICATION_TYPES)('should create %s notification', async (type) => { | ||
NOTIFICATIONS_CALLS[type](); | ||
const iconType = screen.getAllByTestId('notification-icon'); | ||
expect(iconType[currentIndex]).toHaveClass(NOTIFICATION_ICONS[type]); | ||
currentIndex += 1; | ||
}); | ||
|
||
it.each(indexToRemove)( | ||
'should remove multiple notifications', | ||
async (index) => { | ||
const elements = document.getElementsByTagName('ion-notification'); | ||
const removeNotification = screen.getAllByTestId('btn-remove'); | ||
fireEvent.click(removeNotification[index]); | ||
notificationsOnScreen -= 1; | ||
expect(elements).toHaveLength(notificationsOnScreen); | ||
} | ||
); | ||
|
||
it.each(NOTIFICATION_TYPES)( | ||
'should add ionOnClose subscription when %s notification is created', | ||
async (type) => { | ||
notificationService.addCloseEventEmitter = jest.fn(); | ||
NOTIFICATIONS_CALLS[type](); | ||
expect(notificationService.addCloseEventEmitter).toHaveBeenCalledTimes(1); | ||
} | ||
); | ||
}); |
Oops, something went wrong.