Skip to content

Commit

Permalink
Merge pull request #19 from arnaudbuchholz-sap/master
Browse files Browse the repository at this point in the history
feat(misc_api): improve error message by detecting missing emarsys-in…
  • Loading branch information
akapa authored Sep 8, 2023
2 parents 621b351 + 712a02c commit 88d4d12
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 5 deletions.
10 changes: 9 additions & 1 deletion lib/client/misc_api.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,15 @@ class MiscApi extends BaseApi {
console.error('No response received for integrationClient request within ' +
this.getResponseTimeout() +
'ms. Make sure emarsys-integration-js is included in the top iframe.');
this.deferreds[eventId].reject(new Error('No response recieved within timeout'));
const myWindow = this.transmitter.global;
const isIFrame = myWindow.self !== myWindow.top;
let errorMessage;
if (!isIFrame && (!myWindow.Emarsys || !myWindow.Emarsys.integration)) {
errorMessage = 'No response received for integrationClient within timeout, emarsys-integration-js is missing';
} else {
errorMessage = 'No response received for integrationClient within timeout';
}
this.deferreds[eventId].reject(new Error(errorMessage));
}, this.getResponseTimeout());

this.transmitter.messageToEmarsys(
Expand Down
35 changes: 31 additions & 4 deletions lib/client/misc_api.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,18 @@

const MiscApi = require('./misc_api');
const Receiver = require('../comm/receiver');
const FakeWindow = require('../../mocks/fake_window');

describe('Misc API', () => {
let topWindow;
let iframeWindow;

beforeEach(function() {
topWindow = FakeWindow.create();
iframeWindow = FakeWindow.create();
iframeWindow.top = topWindow;
});

describe('getUrl', () => {
it('should send get_url event to parent and resolve with response on success', async () => {
let responseListener;
Expand Down Expand Up @@ -66,17 +76,34 @@ describe('Misc API', () => {
}
});

it('should reject if no response is received within timeout', async () => {
const receiver = { addMessageHandler: () => {} };
const transmitter = { messageToEmarsys: sinon.spy() };
it('should reject if no response is received within timeout (simulate IFRAME)', async () => {
const receiver = { addMessageHandler: () => {}, window: iframeWindow };
const transmitter = { messageToEmarsys: sinon.spy(), global: iframeWindow };
const miscApi = new MiscApi(transmitter, receiver);
sinon.stub(miscApi, 'getResponseTimeout').returns(10);

try {
await miscApi.getUrl({ target: 'home' });
throw new Error('should throw before');
} catch (error) {
expect(error.message).to.eql('No response recieved within timeout');
expect(error.message).to.eql('No response received for integrationClient within timeout');
}
});

it('should reject if no response is received within timeout (simulate integration missing)', async () => {
const receiver = { addMessageHandler: () => {}, window: topWindow };
const transmitter = { messageToEmarsys: sinon.spy(), global: topWindow };
delete topWindow.Emarsys.integration;
const miscApi = new MiscApi(transmitter, receiver);
sinon.stub(miscApi, 'getResponseTimeout').returns(10);

try {
await miscApi.getUrl({ target: 'home' });
throw new Error('should throw before');
} catch (error) {
expect(error.message).to.eql(
'No response received for integrationClient within timeout, emarsys-integration-js is missing'
);
}
});
});
Expand Down
3 changes: 3 additions & 0 deletions mocks/fake_window.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ class FakeWindow {
};

this.$ = fakeJQuery.create();

this.self = this;
this.top = this;
}

addEventListener(type, callback) {
Expand Down

0 comments on commit 88d4d12

Please sign in to comment.