diff --git a/src/app/core/services/fresh-chat.service.spec.ts b/src/app/core/services/fresh-chat.service.spec.ts index 15d5f11c64..d4f0395abb 100644 --- a/src/app/core/services/fresh-chat.service.spec.ts +++ b/src/app/core/services/fresh-chat.service.spec.ts @@ -1,12 +1,13 @@ -import { TestBed } from '@angular/core/testing'; +import { TestBed, fakeAsync, tick } from '@angular/core/testing'; import { FreshChatService } from './fresh-chat.service'; import { AuthService } from './auth.service'; import { StorageService } from './storage.service'; import { OrgUserSettingsService } from './org-user-settings.service'; import { NetworkService } from './network.service'; import { of } from 'rxjs'; -import { orgUserSettingsData } from '../mock-data/org-user-settings.data'; +import { orgUserSettingsData, orgUserSettingsData2 } from '../mock-data/org-user-settings.data'; import { apiEouRes } from '../mock-data/extended-org-user.data'; +import { FreshChatWidget } from '../models/fresh-chat-widget.model'; describe('FreshChatService', () => { let freshChatService: FreshChatService; @@ -73,4 +74,111 @@ describe('FreshChatService', () => { //@ts-ignore expect(freshChatService.initFreshChat).toHaveBeenCalledTimes(1); }); + + describe('setupNetworkWatcher():', () => { + beforeEach(() => { + //@ts-ignore + spyOn(freshChatService, 'initiateCall'); + networkService.isOnline.and.returnValue(of(true)); + authService.getEou.and.resolveTo(apiEouRes); + orgUserSettingsService.get.and.returnValue(of(orgUserSettingsData)); + }); + + it('should setup network watcher', fakeAsync(() => { + freshChatService.setupNetworkWatcher(); + tick(100); + + expect(networkService.connectivityWatcher).toHaveBeenCalledTimes(1); + expect(networkService.isOnline).toHaveBeenCalledTimes(1); + //@ts-ignore + expect(freshChatService.initiateCall).toHaveBeenCalledTimes(1); + expect(storageService.set).toHaveBeenCalledOnceWith('inAppChatRestoreId', null); + })); + + it('should not setup freshchat sdk if fresh chat settings is undefined', fakeAsync(() => { + orgUserSettingsService.get.and.returnValue(of({ ...orgUserSettingsData, in_app_chat_settings: undefined })); + freshChatService.setupNetworkWatcher(); + tick(100); + + expect(networkService.connectivityWatcher).toHaveBeenCalledTimes(1); + expect(networkService.isOnline).toHaveBeenCalledTimes(1); + //@ts-ignore + expect(freshChatService.initiateCall).not.toHaveBeenCalled(); + expect(storageService.set).not.toHaveBeenCalled(); + })); + + it('should not setup freshchat sdk if org user setting is undefined', fakeAsync(() => { + orgUserSettingsService.get.and.returnValue(of(undefined)); + freshChatService.setupNetworkWatcher(); + tick(100); + + expect(networkService.connectivityWatcher).toHaveBeenCalledTimes(1); + expect(networkService.isOnline).toHaveBeenCalledTimes(1); + //@ts-ignore + expect(freshChatService.initiateCall).not.toHaveBeenCalled(); + expect(storageService.set).not.toHaveBeenCalled(); + })); + }); + + it('getWindow(): should get window object', () => { + expect(freshChatService.getWindow()).toEqual(window); + }); + + describe('openLiveChatSupport():', () => { + it('should open live chat support', () => { + const openSpy = jasmine.createSpy('open'); + const mockWindow = jasmine.createSpyObj('window', ['fcWidget']); + mockWindow.fcWidget = { + open: openSpy, + }; + spyOn(freshChatService, 'getWindow').and.returnValue(mockWindow); + freshChatService.openLiveChatSupport(); + expect(openSpy).toHaveBeenCalledTimes(1); + }); + + it('should not open live chat support if fresh chat widget is undefined', () => { + const openSpy = jasmine.createSpy('open'); + const mockWindow = jasmine.createSpyObj('window', ['fcWidget']); + mockWindow.fcWidget = undefined; + spyOn(freshChatService, 'getWindow').and.returnValue(mockWindow); + freshChatService.openLiveChatSupport(); + expect(openSpy).not.toHaveBeenCalled(); + }); + + it('should not open live chat support if window is undefined', () => { + const openSpy = jasmine.createSpy('open'); + spyOn(freshChatService, 'getWindow').and.returnValue(undefined); + freshChatService.openLiveChatSupport(); + expect(openSpy).not.toHaveBeenCalled(); + }); + }); + + describe('destroy():', () => { + it('should destroy fresh chat widget', () => { + const destroySpy = jasmine.createSpy('destroy'); + const mockWindow = jasmine.createSpyObj('window', ['fcWidget']); + mockWindow.fcWidget = { + destroy: destroySpy, + }; + spyOn(freshChatService, 'getWindow').and.returnValue(mockWindow); + freshChatService.destroy(); + expect(destroySpy).toHaveBeenCalledTimes(1); + }); + + it('should not destroy if fresh chat widget is undefined', () => { + const destroySpy = jasmine.createSpy('destroy'); + const mockWindow = jasmine.createSpyObj('window', ['fcWidget']); + mockWindow.fcWidget = undefined; + spyOn(freshChatService, 'getWindow').and.returnValue(mockWindow); + freshChatService.destroy(); + expect(destroySpy).not.toHaveBeenCalled(); + }); + + it('should not destroy fresh chat widget if window is undefined', () => { + const destroySpy = jasmine.createSpy('destroy'); + spyOn(freshChatService, 'getWindow').and.returnValue(undefined); + freshChatService.destroy(); + expect(destroySpy).not.toHaveBeenCalled(); + }); + }); }); diff --git a/src/app/core/services/fresh-chat.service.ts b/src/app/core/services/fresh-chat.service.ts index 154d680254..c344215c52 100644 --- a/src/app/core/services/fresh-chat.service.ts +++ b/src/app/core/services/fresh-chat.service.ts @@ -27,6 +27,10 @@ export class FreshChatService { private networkService: NetworkService ) {} + getWindow(): Window { + return window; + } + setupNetworkWatcher(): void { const networkWatcherEmitter = new EventEmitter(); this.networkService.connectivityWatcher(networkWatcherEmitter); @@ -43,12 +47,12 @@ export class FreshChatService { } openLiveChatSupport(): void { - return window && window.fcWidget && window.fcWidget.open(); + return this.getWindow()?.fcWidget?.open(); } destroy(): void { - if (window && window.fcWidget && window.fcWidget.destroy) { - window.fcWidget.destroy(); + if (this.getWindow()?.fcWidget?.destroy) { + this.getWindow().fcWidget.destroy(); } } @@ -69,7 +73,7 @@ export class FreshChatService { device = 'ANDROID'; } - window.fcWidget.init({ + this.getWindow().fcWidget.init({ config: { disableNotifications: true, }, @@ -79,29 +83,29 @@ export class FreshChatService { restoreId: inAppChatRestoreId, // that id is used to restore chat for the user }); - window.fcWidget.on('widget:loaded', () => { + this.getWindow().fcWidget.on('widget:loaded', () => { if (document.getElementById('fc_frame')) { document.getElementById('fc_frame').style.display = 'none'; } }); - window.fcWidget.on('widget:closed', () => { + this.getWindow().fcWidget.on('widget:closed', () => { if (document.getElementById('fc_frame')) { document.getElementById('fc_frame').style.display = 'none'; } }); - window.fcWidget.on('widget:opened', () => { + this.getWindow().fcWidget.on('widget:opened', () => { if (document.getElementById('fc_frame')) { document.getElementById('fc_frame').style.display = 'flex'; } }); - window.fcWidget.user.get((resp: { status: number; data: { restoreId: string } }) => { + this.getWindow().fcWidget.user.get((resp: { status: number; data: { restoreId: string } }) => { // Freshchat here calls an API to check if there is any user with the above externalId const status = resp && resp.status; const data = resp && resp.data; if (status !== 200) { // If there is no user with the above externalId; then set the below properties - window.fcWidget.user.setProperties({ + this.getWindow().fcWidget.user.setProperties({ firstName: eou.us.full_name, // user's first name email: eou.us.email, // user's email address orgName: eou.ou.org_name, // user's org name @@ -109,7 +113,7 @@ export class FreshChatService { userId: eou.us.id, // user's user id device, // storing users device }); - window.fcWidget.on('user:created', async (resp: { status: number; data: { restoreId: string } }) => { + this.getWindow().fcWidget.on('user:created', async (resp: { status: number; data: { restoreId: string } }) => { // When that new user tries to initiate a chat Freshchat creates a users with above properties const status = resp && resp.status; const data = resp && resp.data;