0

I have an Angular component which uses the AWS connect-streams library. However I am having problems trying to mock this library for unit test purposes.

In my component.ts file I have the following:

public ngOnInit() {    
  connect.core.initCCP(el, {
    ccpUrl: this.ccpUrl,
    loginPopup: false,
    loginPopupAutoClose: true,
  });
  connect.contact((contactSessionObj) => {
    contactSessionObj.onIncoming((_evt) => {
      this.onIncomingHandler(_evt, contactSessionObj);
    });
    contactSessionObj.onConnected((_evt: any) => {
      this.onConnectedHandler(_evt, contactSessionObj);
    });
  });

private onIncomingHandler(_evt, _contact) {
  if (this.isClosed) {
    this.toggleWindow();
  }
}
   
private onConnectedHandler(_evt, contact) {
  const contactAttributes = contact.getAttributes();
  const phoneNumber = contactAttributes.phoneNumber.value;
}

I am trying to mock the connect object but cannot work out the structure that would enable me to test that this.onIncomingHandler() and this.onConnectedHandler() are called when the onIncoming and onConnected callbacks are invoked. This is where I'm at but I realise this approach doesnt quite line up:

window['connect'] = {
  core: {
    initCCP: () => { },
  },
  contact: () => {
    const contactObj = {};
    contactObj['onIncoming'] = (cb) => { cb(); };
    contactObj['onConnected'] = (cb) => { cb(); };
    return contactObj;
  }
};
Jonathan Smith
  • 2,390
  • 1
  • 34
  • 60
  • Unfortunately, as TypeScript and Jasmine have progressed, it is next to near impossible to mock an npm import library. You can check out the thread linked in this answer and by the way, this answer seems to be the best way of doing it by creating a wrapper class for the npm library you require. https://stackoverflow.com/questions/60259259/error-supportsscrollbehavior-is-not-declared-configurable/62935131#62935131 – AliF50 Feb 11 '22 at 17:58
  • Why don't you have `connect` provided via dependency injection? Then it's easy AF to mock it in the `spec` file... (`{ provide: CONNECT_SERVICE, useClass: ConnectMockService }`) – Pieterjan Feb 11 '22 at 19:01

0 Answers0