Skip to content
This repository has been archived by the owner on May 21, 2024. It is now read-only.

Review updates #39

Merged
merged 8 commits into from
Mar 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9,929 changes: 9,929 additions & 0 deletions package-lock.json

Large diffs are not rendered by default.

78 changes: 62 additions & 16 deletions src/help-widget.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { MockWidgetRenderer} from './mocks/mock-renderer';
describe('BBHelpHelpWidget', () => {
let helpWidget: BBHelpHelpWidget;
let originalTimeout: number;
let commReadyStatus: any;
let mockWidgetRenderer: any;
let mockAnalyticsService: any;
let mockCommunicationService: any;
Expand All @@ -24,13 +23,14 @@ describe('BBHelpHelpWidget', () => {
mockAnalyticsService,
mockCommunicationService
);
spyOn(mockAnalyticsService, 'trackEvent');
spyOn(mockCommunicationService, 'postMessage').and.callFake((message: any) => { return; });
spyOn(mockCommunicationService, 'ready').and.callFake((message: any) => commReadyStatus);
spyOn(mockAnalyticsService, 'trackEvent').and.callThrough();
spyOn(mockCommunicationService, 'postMessage').and.callThrough();
spyOn(mockCommunicationService, 'ready').and.callThrough();
spyOn(mockCommunicationService, 'communicationAction').and.callThrough();
});

afterEach(() => {
document.body.innerHTML = '';
jasmine.DEFAULT_TIMEOUT_INTERVAL = originalTimeout;
});

Expand Down Expand Up @@ -76,7 +76,7 @@ describe('BBHelpHelpWidget', () => {
});

it('should return ready when the communication service is ready', (done) => {
commReadyStatus = Promise.resolve();
mockCommunicationService.commReadyStatus = Promise.resolve();
helpWidget['elementsLoaded'] = true;
helpWidget.ready()
.then(() => {
Expand All @@ -90,7 +90,7 @@ describe('BBHelpHelpWidget', () => {
});

it('should not return ready when the communication service is not ready', (done) => {
commReadyStatus = Promise.resolve();
mockCommunicationService.commReadyStatus = Promise.resolve();
const consoleSpy = spyOn(window.console, 'error').and.callFake(() => { return; });
helpWidget['elementsLoaded'] = false;
helpWidget.ready()
Expand Down Expand Up @@ -121,8 +121,6 @@ describe('BBHelpHelpWidget', () => {
config: fakeConfig,
messageType: 'user-config'
});

expect(mockWidgetRenderer.addInvokerStyles).toHaveBeenCalledWith(helpWidget['invoker'], fakeConfig);
done();
});

Expand Down Expand Up @@ -358,41 +356,89 @@ describe('BBHelpHelpWidget', () => {
done();
});

it ('should respond to action responses, Close Widget', (done) => {
it ('should react to actions, Close Widget', (done) => {
spyOn(helpWidget, 'close').and.callThrough();
mockCommunicationService.communicationAction.next('Close Widget');
mockCommunicationService.communicationAction.next({ messageType: 'Close Widget' });
expect(helpWidget.close).toHaveBeenCalled();
expect(document.activeElement.id).toEqual(helpWidget['invoker'].id);
done();
});

it ('should respond to action responses, Child Window Ready (loadCalled false) by not sending the config', (done) => {
mockCommunicationService.communicationAction.next('Child Window Ready');
it ('should react to actions, Child Window Ready (loadCalled false) by not sending the config', (done) => {
const consoleSpy = spyOn(window.console, 'error').and.callFake(() => { return; });
mockCommunicationService.communicationAction.next({ messageType: 'Child Window Ready' });
expect(mockCommunicationService.postMessage).not.toHaveBeenCalled();
expect(consoleSpy).not.toHaveBeenCalled();
done();
});

it ('should respond to action responses, Child Window Ready (loadCalled true) by sending the config', (done) => {
it ('should react to actions, Child Window Ready (loadCalled true) by sending the config', (done) => {
const fakeConfig = {
defaultHelpKey: 'test-default.html'
};

helpWidget.load(fakeConfig);
mockCommunicationService.communicationAction.next('Child Window Ready');
mockCommunicationService.communicationAction.next({ messageType: 'Child Window Ready' });
expect(mockCommunicationService.postMessage).toHaveBeenCalledWith({
config: helpWidget.config,
messageType: 'user-config'
});
done();
});

it ('should react to actions, Config Loaded by updating configs, defaultHelpKey, and render invoker', (done) => {
const originalConfig = {
defaultHelpKey: 'original-default.html'
};

const extendedConfig = {
defaultHelpKey: 'new-default.html',
headerColor: '#fff'
};

const rendererSpy = spyOn(mockWidgetRenderer, 'addInvokerStyles').and.callThrough();
helpWidget.load(originalConfig);
expect(helpWidget.config).toEqual(originalConfig);
expect(helpWidget['defaultHelpKey']).toEqual(originalConfig.defaultHelpKey);
mockCommunicationService.communicationAction.next({
data: JSON.stringify(extendedConfig),
messageType: 'Config Loaded'
});
expect(rendererSpy).toHaveBeenCalledWith(helpWidget['invoker'], extendedConfig);
expect(helpWidget.config).toEqual(extendedConfig);
expect(helpWidget['defaultHelpKey']).toEqual(extendedConfig.defaultHelpKey);
done();
});

it ('should react to actions, Config Loaded by updating configs (no defaultHelpKey) and render invoker', (done) => {
const originalConfig = {
defaultHelpKey: 'original-default.html'
};

const extendedConfig = {
headerColor: '#fff'
};

const rendererSpy = spyOn(mockWidgetRenderer, 'addInvokerStyles').and.callThrough();
helpWidget.load(originalConfig);
expect(helpWidget.config).toEqual(originalConfig);
mockCommunicationService.communicationAction.next({
data: JSON.stringify(extendedConfig),
messageType: 'Config Loaded'
});
expect(rendererSpy).toHaveBeenCalledWith(helpWidget['invoker'], extendedConfig);
expect(helpWidget.config).toEqual(extendedConfig);
expect(helpWidget['defaultHelpKey']).toEqual(originalConfig.defaultHelpKey);
done();
});

it ('should log a console error if no matching action exists', (done) => {
const testAction = 'Test Action No Response';
const testAction = { messageType: 'Test Action No Response' };
spyOn(window.console, 'error').and.callFake(() => {
return;
});
mockCommunicationService.communicationAction.next(testAction);
expect(window.console.error).toHaveBeenCalledWith(`No matching response for action: ${testAction}`);
expect(window.console.error).toHaveBeenCalledWith(`No matching response for action: ${testAction.messageType}`);
done();
});

Expand Down
23 changes: 17 additions & 6 deletions src/help-widget.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { HelpConfig } from './help-config';
import { BBHelpHelpWidgetRenderer } from './help-widget-renderer';
import { CommunicationAction } from './models/communication-action';
import { BBHelpAnalyticsService } from './service/analytics.service';
import { BBHelpCommunicationService } from './service/communication.service';

Expand Down Expand Up @@ -70,8 +71,6 @@ export class BBHelpHelpWidget {
this.getCurrentHelpKey = config.getCurrentHelpKey;
delete config.getCurrentHelpKey;
}

this.renderInvoker();
this.sendConfig();
}

Expand Down Expand Up @@ -179,13 +178,13 @@ export class BBHelpHelpWidget {

private setUpCommunication() {
this.communicationService.bindChildWindowReference(this.iframe);
this.communicationService.communicationAction.subscribe((action: string) => {
this.communicationService.communicationAction.subscribe((action: CommunicationAction) => {
this.actionResponse(action);
});
}

private actionResponse(action: string) {
switch (action) {
private actionResponse(action: CommunicationAction) {
switch (action.messageType) {
case 'Close Widget':
this.invoker.focus();
this.close();
Expand All @@ -195,8 +194,20 @@ export class BBHelpHelpWidget {
this.sendConfig();
}
break;
case 'Config Loaded':
const configData = JSON.parse(action.data);
this.updateConfigKeys(configData);
this.renderInvoker();
break;
default:
console.error(`No matching response for action: ${action}`);
console.error(`No matching response for action: ${action.messageType}`);
}
}

private updateConfigKeys(configOptions: any) {
this.config = configOptions;
if (configOptions.defaultHelpKey) {
this.defaultHelpKey = configOptions.defaultHelpKey;
}
}

Expand Down
47 changes: 1 addition & 46 deletions src/help.spec.ts
Original file line number Diff line number Diff line change
@@ -1,54 +1,9 @@
import { BBHelpClient } from './help';
import { MockHelpWidget } from './mocks/mock-help-widget';

describe('BBHelpClient', () => {
let mockHelpWidget: any;

class MockHelpWidget {
public close() {
return;
}

public load(helpConfig?: any) {
return;
}

public open(helpKey?: string) {
return;
}

public toggleOpen() {
return;
}

public disableWidget() {
return;
}

public enableWidget() {
return;
}

public ready() {
return Promise.resolve();
}

public setHelpKeyToDefault() {
return;
}

public setCurrentHelpKey() {
return;
}

public getCurrentHelpKey() {
return;
}

public getWhatsNewRevision() {
return;
}
}

beforeEach(() => {
mockHelpWidget = new MockHelpWidget();

Expand Down
4 changes: 3 additions & 1 deletion src/help.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { BBHelpStyleUtility } from './help-widget-style-utility';
import { BBHelp } from './models/bbhelp';
import { BBHelpAnalyticsService } from './service/analytics.service';
import { BBHelpCommunicationService } from './service/communication.service';
import { MixpanelKeys } from './service/mixpanel-keys';

declare const BBHELP: any;

Expand All @@ -13,7 +14,8 @@ export abstract class BBHelpClient {
public static initWidget(): BBHelp {
const styleUtility = new BBHelpStyleUtility();
const widgetRenderer = new BBHelpHelpWidgetRenderer(styleUtility);
const analyticsService = new BBHelpAnalyticsService();
const mixpanelKeys = new MixpanelKeys();
const analyticsService = new BBHelpAnalyticsService(mixpanelKeys);
const communicationService = new BBHelpCommunicationService();
const helpWidget = new BBHelpHelpWidget(
widgetRenderer,
Expand Down
12 changes: 3 additions & 9 deletions src/mocks/mock-communication-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { Subject } from 'rxjs';

export class MockCommunicationService {

public commReadyStatus: any = Promise.resolve();

public communicationAction: Subject<any> = new Subject();

public childWindow: HTMLIFrameElement;
Expand All @@ -13,15 +15,7 @@ export class MockCommunicationService {
}

public ready() {
//
}

public messageHandler() {
//
}

public isFromHelpWidget() {
//
return this.commReadyStatus;
}

public postMessage() {
Expand Down
41 changes: 41 additions & 0 deletions src/mocks/mock-help-widget.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
export class MockHelpWidget {
public close() {
return;
}

public load(helpConfig?: any) {
return;
}

public open(helpKey?: string) {
return;
}

public toggleOpen() {
return;
}

public disableWidget() {
return;
}

public enableWidget() {
return;
}

public ready() {
return Promise.resolve();
}

public setHelpKeyToDefault() {
return;
}

public setCurrentHelpKey() {
return;
}

public getWhatsNewRevision() {
return;
}
}
4 changes: 0 additions & 4 deletions src/mocks/mock-style-utilty.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,4 @@ export class MockStyleUtility {
public addAllStyles() {
//
}

public addCssToHead(css: string) {
//
}
}
4 changes: 4 additions & 0 deletions src/models/communication-action.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export interface CommunicationAction {
messageType: string;
data?: any;
}
Loading