logger.receiver.test.js 3.68 KB
/*
 * ADOBE CONFIDENTIAL
 * __________________
 *
 *  Copyright 2016 Adobe Systems Incorporated
 *  All Rights Reserved.
 *
 * NOTICE:  All information contained herein is, and remains
 * the property of Adobe Systems Incorporated and its suppliers,
 * if any.  The intellectual and technical concepts contained
 * herein are proprietary to Adobe Systems Incorporated and its
 * suppliers and are protected by trade secret or copyright law.
 * Dissemination of this information or reproduction of this material
 * is strictly forbidden unless prior written permission is obtained
 * from Adobe Systems Incorporated.
 */

/* eslint max-nested-callbacks: [2, 10]*/
/* eslint no-new: 0 */
/* globals console, sinon, describe, expect, it, beforeEach, afterEach */
define([
    'screens/player/log/writer/delegate',
    'screens/player/log/receiver'
], function(DelegateWriter, LogReceiver) {
    'use strict';

    var receiver;

    function getOrigin() {
        var origin = location.origin;
        if (!origin) {
            origin = location.protocol + '//' + location.host;
        }
        return origin;
    }

    describe('screens/player/log/receiver', function() {

        describe('# initialisation', function() {

            it('event has to be bound on given window', function() {
                var bindSpy = sinon.spy();

                receiver = new LogReceiver({}, null, {
                    addEventListener: bindSpy
                });

                bindSpy.should.have.been.calledOnce;
            });

        });

        describe('# receiving logs', function() {

            it('writer has to be called when receiving a message', function() {
                var callback;
                var spy = sinon.spy();

                receiver = new LogReceiver({
                    write: spy
                }, null, {
                    addEventListener: function(type, fn) {
                        expect(type).to.eql('message');
                        callback = fn;
                    }
                });

                callback({
                    origin: getOrigin(),
                    data: {
                        module: 'screens/player/log/writer/delegate'
                    }
                });

                spy.should.have.been.calledOnce;
            });

            it('writer must not be called when receiving a message with an unexpected origin', function() {
                var callback;
                var spy = sinon.spy();

                receiver = new LogReceiver({
                    write: spy
                }, null, {
                    addEventListener: function(type, fn) {
                        expect(type).to.eql('message');
                        callback = fn;
                    }
                });

                callback({
                    origin: 'http://adobe.com',
                    data: {
                        module: 'screens/player/log/writer/delegate'
                    }
                });

                spy.should.not.have.been.called;
            });


            it('writer must not be called when receiving a random message', function() {
                var callback;
                var spy = sinon.spy();

                receiver = new LogReceiver({
                    write: spy
                }, null, {
                    addEventListener: function(type, fn) {
                        expect(type).to.eql('message');
                        callback = fn;
                    }
                });

                callback({
                    origin: getOrigin(),
                    data: {
                    }
                });

                spy.should.not.have.been.called;
            });

        });

    });
});