cache-service.test.js 10.2 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 Promise, describe, expect, it, beforeEach, afterEach */
define([
    'screens/player/shared/serviceadmin',
    'screens/player/shared/test-helper/service',
    'screens/player/shared/test-helper/mock-store-service',
    'screens/player/shared/test-helper/stub-service-interface',
    'screens/player/firmware/cache/cache',
    'screens/player/firmware/cache/impl/cache-service',
    'screens/player/firmware/cache/spi/cache',
    'screens/player/firmware/core/firmware/firmware',
    'screens/player/store/store'
], function(ServiceAdmin, serviceTestHelper, mockStoreService, stubServiceInterface, Cache, CacheService, CacheSpi, FirmwareSvc, Store) {
    'use strict';

    function createClearAction(action) {
        return { type: action };
    }

    describe('screens/player/firmware/cache/impl/cache-service', function() {

        var cacheService;
        var mockedStoreService;
        var stubCacheSpiService;
        var stubFirmwareService;

        beforeEach(function() {
            mockedStoreService = mockStoreService(stubServiceInterface(ServiceAdmin, Store));
            stubCacheSpiService = stubServiceInterface(ServiceAdmin, CacheSpi);
            stubFirmwareService = stubServiceInterface(ServiceAdmin, FirmwareSvc);
            cacheService = new CacheService();
        });

        afterEach(function() {
            mockedStoreService.restore();
            stubCacheSpiService.restore();
            stubFirmwareService.restore();
        });

        serviceTestHelper.runDefaultTests(new CacheService(), Cache.serviceName);

        describe('API', function() {

            beforeEach(function() {
                return cacheService.activate();
            });

            afterEach(function() {
                return cacheService.deactivate();
            });

            describe('resetFirmware', function() {

                it('should call the resetFirmware method on the SPI', function() {
                    var bridgeStopPromise = Promise.resolve();
                    stubFirmwareService.api.getPlayer.returns({
                        bridge: {
                            stop: function() { return bridgeStopPromise; }
                        }
                  });
                    stubCacheSpiService.api.resetFirmware.returns(Promise.resolve());
                    cacheService.resetFirmware();
                    return bridgeStopPromise.then(function() {
                        expect(stubCacheSpiService.api.resetFirmware).to.have.been.called.calledWithExactly();
                    });
                });

                it('should trigger a "clear firmware" action', function() {
                    var bridgeStopPromise = Promise.resolve();
                    var clearFirmwarePromise = Promise.resolve();
                    stubFirmwareService.api.getPlayer.returns({
                        bridge: {
                            stop: function() { return bridgeStopPromise; }
                        }
                    });
                    stubCacheSpiService.api.resetFirmware.returns(clearFirmwarePromise);
                    cacheService.resetFirmware();
                    return bridgeStopPromise.then(function() {
                        return clearFirmwarePromise;
                    }).then(function() {
                        expect(mockedStoreService.api.dispatch.firstCall).calledWithMatch(createClearAction(Cache.ACTIONS.RESET_FIRMWARE));
                    }).then(function() {
                        expect(mockedStoreService.api.dispatch.secondCall).calledWithMatch(createClearAction(Cache.ACTIONS.CLEAR_DONE));
                    });
                });

            });

            describe('clearChannelsData', function() {

                it('should call the clearChannelsData method on the SPI', function() {
                    var bridgeStopPromise = Promise.resolve();
                    stubFirmwareService.api.getPlayer.returns({
                        bridge: {
                            stop: function() { return bridgeStopPromise; }
                        }
                    });
                    stubCacheSpiService.api.clearChannelsData.returns(Promise.resolve());
                    cacheService.clearChannelsData();
                    return bridgeStopPromise.then(function() {
                        expect(stubCacheSpiService.api.clearChannelsData).to.have.been.called.calledWith();
                    });
                });

                it('should trigger a "clear channels" action', function() {
                    var bridgeStopPromise = Promise.resolve();
                    var clearChannelsDataPromise = Promise.resolve();
                    stubFirmwareService.api.getPlayer.returns({
                        bridge: {
                            stop: function() { return bridgeStopPromise; }
                        }
                    });
                    stubCacheSpiService.api.clearChannelsData.returns(clearChannelsDataPromise);
                    cacheService.clearChannelsData();
                    return bridgeStopPromise.then(function() {
                        return clearChannelsDataPromise;
                    }).then(function() {
                        expect(mockedStoreService.api.dispatch.firstCall).calledWithMatch(createClearAction(Cache.ACTIONS.CLEAR_CHANNELS_DATA));
                    }).then(function() {
                        expect(mockedStoreService.api.dispatch.secondCall).calledWithMatch(createClearAction(Cache.ACTIONS.CLEAR_DONE));
                    });
                });

            });

            describe('clearAppCache', function() {

                it('should call the clearAppCache method on the SPI', function() {
                    var bridgeStopPromise = Promise.resolve();
                    stubFirmwareService.api.getPlayer.returns({
                        bridge: {
                            stop: function() { return bridgeStopPromise; }
                        }
                    });
                    stubCacheSpiService.api.clearAppCache.returns(Promise.resolve());
                    cacheService.clearAppCache();
                    return bridgeStopPromise.then(function() {
                        expect(stubCacheSpiService.api.clearAppCache).to.have.been.called.calledWithExactly();
                    });
                });

                it('should trigger a "clear app" action', function() {
                    var bridgeStopPromise = Promise.resolve();
                    var clearAppCachePromise = Promise.resolve();
                    stubFirmwareService.api.getPlayer.returns({
                        bridge: {
                            stop: function() { return bridgeStopPromise; }
                        }
                    });
                    stubCacheSpiService.api.clearAppCache.returns(clearAppCachePromise);
                    cacheService.clearAppCache();
                    return bridgeStopPromise.then(function() {
                        return clearAppCachePromise;
                    }).then(function() {
                        expect(mockedStoreService.api.dispatch.firstCall).calledWithMatch(createClearAction(Cache.ACTIONS.CLEAR_APP_CACHE));
                    }).then(function() {
                        expect(mockedStoreService.api.dispatch.secondCall).calledWithMatch(createClearAction(Cache.ACTIONS.CLEAR_DONE));
                    });
                });

            });

            describe('clearAll', function() {

                it('should call the clearAll method on the SPI', function(done) {
                    var flags = {};
                    flags[Cache.FLAGS.REBOOT_ON_COMPLETE] = true;
                    var bridgeStopPromise = Promise.resolve();
                    stubFirmwareService.api.getPlayer.returns({
                        bridge: {
                            stop: function() { return bridgeStopPromise; }
                        }
                    });
                    stubCacheSpiService.api.clearChannelsData.returns(Promise.resolve());
                    stubCacheSpiService.api.clearAppCache.returns(Promise.resolve());
                    stubFirmwareService.api.reboot = function() {
                        expect(stubCacheSpiService.api.clearChannelsData).to.have.been.called.calledWith();
                        expect(stubCacheSpiService.api.clearAppCache).to.have.been.called.calledWithExactly();
                        done();
                    };
                    cacheService.clearAll(flags);
                });

                it('should trigger a "clear all" action', function(done) {
                    var flags = {};
                    flags[Cache.FLAGS.REBOOT_ON_COMPLETE] = true;
                    var bridgeStopPromise = Promise.resolve();
                    stubFirmwareService.api.getPlayer.returns({
                        bridge: {
                            stop: function() { return bridgeStopPromise; }
                        }
                    });
                    stubCacheSpiService.api.clearChannelsData.returns(Promise.resolve());
                    stubFirmwareService.api.reboot = function() {
                        stubCacheSpiService.api.clearAppCache.returns(Promise.resolve());
                        expect(mockedStoreService.api.dispatch.firstCall).calledWithMatch(createClearAction(Cache.ACTIONS.CLEAR_ALL));
                        expect(mockedStoreService.api.dispatch.secondCall).calledWithMatch(createClearAction(Cache.ACTIONS.CLEAR_DONE));
                        done();
                    };
                    cacheService.clearAll(flags);
                });
            });

        });

    });
});