Skip to content

Commit

Permalink
test for errorReport() and addEventListener in ampcontext
Browse files Browse the repository at this point in the history
  • Loading branch information
tiendt committed Aug 21, 2017
1 parent 625c405 commit a3b145b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 11 deletions.
5 changes: 2 additions & 3 deletions 3p/ampcontext.js
Expand Up @@ -318,10 +318,9 @@ export class AbstractAmpContext {
* Send 3p error to parent iframe
*/
errorReport() {
const self = this;
this.win_.addEventListener('error', function(event) {
this.win_.addEventListener('error', event => {
if (!!event.error) {
self.client_.sendMessage(MessageType.USER_ERROR, dict({
this.client_.sendMessage(MessageType.USER_ERROR, dict({
'error': event.error,
'message': event.error.message,
}));
Expand Down
34 changes: 26 additions & 8 deletions test/functional/test-amp-context.js
Expand Up @@ -33,7 +33,6 @@ describe('3p ampcontext.js', () => {
win = {
addEventListener: (eventType, handlerFn) => {
console.log('event type: ' + eventType);
// expect(eventType).to.equal('message');
// expect(windowMessageHandler).to.not.be.ok;
windowMessageHandler = handlerFn;
},
Expand Down Expand Up @@ -62,18 +61,37 @@ describe('3p ampcontext.js', () => {
windowMessageHandler = undefined;
});

it.only('should call addEventListener with correct eventType', () => {
// win.addEventListener = sandbox.spy();
// expect(win.addEventListener).to.have.been.called;
// expect(win.addEventListener)
// .to.have.been.calledWith('message');

it('should call addEventListener with correct eventType', () => {
win.name = generateSerializedAttributes();
const context = new AmpContext(win);
expect(context).to.be.ok;
const addEventListenerSpy = sandbox.spy(win, 'addEventListener');
context.errorReport();

expect(addEventListenerSpy).to.have.been.called;
expect(addEventListenerSpy)
.to.have.been.calledWith('message');
.to.have.been.calledWith('error');
});

it('should send error and message when errorReport()', () => {
win.name = generateSerializedAttributes();
const context = new AmpContext(win);
expect(context).to.be.ok;

// Resetting since a message is sent on construction.
windowPostMessageSpy.reset();
context.errorReport();

// window.context should have sent postMessage sending 3p errors
expect(windowPostMessageSpy.calledWith({
sentinel: '1-291921',
type: MessageType.USER_ERROR,
error: sinon.match.any,
message: sinon.match.any,
}, '*'));
});


it('should add metadata to window.context using name as per 3P.', () => {
win.name = generateSerializedAttributes();
const context = new AmpContext(win);
Expand Down

0 comments on commit a3b145b

Please sign in to comment.