Skip to content

Commit

Permalink
test: some branches in freshchat service (#2509)
Browse files Browse the repository at this point in the history
  • Loading branch information
suyashpatil78 authored Oct 16, 2023
1 parent 325a914 commit 9f114b8
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 12 deletions.
112 changes: 110 additions & 2 deletions src/app/core/services/fresh-chat.service.spec.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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();
});
});
});
24 changes: 14 additions & 10 deletions src/app/core/services/fresh-chat.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ export class FreshChatService {
private networkService: NetworkService
) {}

getWindow(): Window {
return window;
}

setupNetworkWatcher(): void {
const networkWatcherEmitter = new EventEmitter<boolean>();
this.networkService.connectivityWatcher(networkWatcherEmitter);
Expand All @@ -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();
}
}

Expand All @@ -69,7 +73,7 @@ export class FreshChatService {
device = 'ANDROID';
}

window.fcWidget.init({
this.getWindow().fcWidget.init({
config: {
disableNotifications: true,
},
Expand All @@ -79,37 +83,37 @@ 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
orgId: eou.ou.org_id, // user's org id
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;
Expand Down

0 comments on commit 9f114b8

Please sign in to comment.