novuhq / novu

Open-Source Notification Platform. Embeddable Notification Center, E-mail, Push and Slack Integrations.
https://novu.co
Other
35.26k stars 3.88k forks source link

šŸ› Bug Report: Novu Headless Unit Testing Angular #5409

Open gPriyanka-GIT opened 6 months ago

gPriyanka-GIT commented 6 months ago

šŸ“œ Description

We have configured Novu headless mode and initialized headless service in an Angular component as follows:

`export class NotificationDialogComponent implements OnInit { headlessService: any; constructor(private authService: AuthService {}

ngOnInit(): void { this.authService .loadUserProfile() .then(() => { this.headlessService = new HeadlessService({ applicationIdentifier: environment.novuConfig.applicationIdentifier, subscriberId: this.email, backendUrl: environment.novuConfig.backendUrl, socketUrl: environment.novuConfig.socketUrl, }); this.initSession(); }) .catch(); }

initSession() { this.headlessService.initializeSession({ listener: (res: FetchResult) => { console.log('welcome to novu headless service', res); }, onSuccess: (session: ISession) => { console.log(session, 'Success'); }, onError(error: any) { console.log(error, 'ERROR'); }, }); } }`

It is working fine however, we are unable to unit test this code as we are not able to mock the headless service. Since a new instance of the headless service is created in ngOnInit we are not able to mock it. Is it possible to inject the headless service in the component so that we can provide a mock implementation in the unit tests?

šŸ‘Ÿ Reproduction steps

  1. create a component in angular and import novu headless and its functions like initializeSession, fetchNotifications, listenNotificationRead, etc.
  2. write unit test cases for the component in spec.ts file

šŸ‘ Expected behavior

it should successfully run all the unit test cases without any failures/errors.

šŸ‘Ž Actual Behavior with Screenshots

Not able to mock the headless service. Since a new instance of the headless service is created in ngOnInit we are not able to mock it. Is it possible to inject the headless service in the component so that we can provide a mock implementation in the unit tests?

Novu version

"@novu/headless": "^0.24.0"

npm version

10.2.4

node version

18.19.1

šŸ“ƒ Provide any additional context for the Bug.

No response

šŸ‘€ Have you spent some time to check if this bug has been raised before?

šŸ¢ Have you read the Contributing Guidelines?

Are you willing to submit PR?

None

linear[bot] commented 6 months ago

NV-3649 šŸ› Bug Report: Novu Headless Unit Testing Angular