util.test.js 10.1 KB
/*
 * ADOBE CONFIDENTIAL
 * __________________
 *
 *  Copyright 2015 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 describe, expect, it, sinon */
define(['screens/player/shared/util'], function(util) {
    'use strict';

    describe('screens/player/shared/util', function() {

        describe('#makePath', function() {

            it('returns empty string if input is empty', function() {
                expect(util.makePath('')).equals('');
            });

            it('cuts of last character if it is a slash', function() {
                expect(util.makePath('foo/')).equals('foo');
                expect(util.makePath('foo')).equals('foo');
            });

            it('returns the concatenations', function() {
                expect(util.makePath('/a', 'b', 'c')).equals('/a/b/c');
                expect(util.makePath('/a', 'b/c')).equals('/a/b/c');
                expect(util.makePath('/a', '/b', 'c')).equals('/a/b/c');
                expect(util.makePath('/a', 'b/', 'c')).equals('/a/b/c');
            });

            it('works with arrays', function() {
                expect(util.makePath(['/a', 'b', 'c'])).equals('/a/b/c');
                expect(util.makePath(['/a', 'b/c'])).equals('/a/b/c');
                expect(util.makePath(['/a', '/b', 'c'])).equals('/a/b/c');
                expect(util.makePath(['/a', 'b/', 'c'])).equals('/a/b/c');
            });

            it('preserves leading slashy', function() {
                expect(util.makePath('/a/', '/b/', '/c')).equals('/a/b/c');
                expect(util.makePath(['/a/', '/b/', '/c'])).equals('/a/b/c');
            });

        });

        describe('#makeId', function() {

            it('removes the first character', function() {
                expect(util.makeId('')).equals('');
                expect(util.makeId('a')).equals('');
                expect(util.makeId('/a')).equals('a');
            });

            it('replaces slashes with dots', function() {
                expect(util.makeId('//')).equals('.');
                expect(util.makeId('/a/b/')).equals('a.b.');
                expect(util.makeId('/a/b/c')).equals('a.b.c');
                expect(util.makeId('/a.b/c')).equals('a.b.c');
            });

        });

        describe('#randomId', function() {

            it('has a default length of 32', function() {
                expect(util.randomId().length).equals(32);
            });

            it('has to respect the passed length', function() {
                expect(util.randomId(10).length).equals(10);
            });

            it('has to respect the passed characters', function() {
                var chars = ['a'];

                var random = util.randomId(5, chars);

                for (var i = 0; i < random.length; i++) {
                    expect(random[i]).to.eql('a');
                }

            });

        });

        describe('#trimTrailingSlash', function() {

            it('removes the slashes', function() {
                expect(util.trimTrailingSlash('/foo/')).equals('/foo');
                expect(util.trimTrailingSlash('/foo/bar///')).equals('/foo/bar');
                expect(util.trimTrailingSlash('/a')).equals('/a');
                expect(util.trimTrailingSlash('/a/')).equals('/a');
            });

            it('keeps the root slash', function() {
                expect(util.trimTrailingSlash('/')).equals('/');
            });

            it('handles empty string', function() {
                expect(util.trimTrailingSlash('')).equals('');
            });

        });

        describe('#get', function() {
            var testString = 'Hello, world.';
            var testObject = {
                foo: {
                    bar: {
                        s: testString,
                        b: false,
                        zero: 0
                    }
                }
            };

            it('gets the proper value', function() {
                expect(util.get(testObject, 'foo.bar.s')).equals(testString);
                expect(util.get(testObject, 'foo.bar.b', true)).equals(false);
                expect(util.get(testObject, 'foo.bar.zero', 1)).equals(0);
            });

            it('gets the default', function() {
                expect(util.get(testObject, 'foo.bar.not-existing', 'default')).equals('default');
                expect(util.get(testObject, 'foo.not-existing', 'default')).equals('default');
                expect(util.get(testObject, 'not-existing', 'default')).equals('default');
            });

        });

        describe('#set', function() {
            var testString = 'Hello, world.';

            it('sets a deep value', function() {
                var testObject = util.set({}, 'foo.bar.s', testString);
                expect(util.get(testObject, 'foo.bar.s')).equals(testString);
            });
        });

        describe('#formatUnit', function() {
            it('returns the correct value', function() {
                expect(util.formatUnit(100, util.UNITS_DI_METRIC, 0)).equals('100 bytes');
                expect(util.formatUnit(1000, util.UNITS_DI_METRIC, 0)).equals('1 kB');
                expect(util.formatUnit(1000, util.UNITS_DI_METRIC, 2)).equals('1.00 kB');
                expect(util.formatUnit(1500000, util.UNITS_DI_METRIC, 2)).equals('1.50 MB');
                expect(util.formatUnit(8192, util.UNITS_DI_IEC, 0)).equals('8 KiB');
                expect(util.formatUnit(1200000, [1000, 'm', 'km'], 0)).equals('1200 km');
            });
            it('returns NaN for undefined values', function() {
                expect(util.formatUnit()).equals('NaN');
            });
        });

        describe('#getSchedule', function() {

            it('returns null if missing required parameters', function() {
                expect(util.getSchedule()).to.be.null;
                expect(util.getSchedule(null, 'at 4:00 am')).to.be.null;
                expect(util.getSchedule({}, null)).to.be.null;
            });

            it('returns null if there was an error parsing the expression', function() {
                var later = { parse: {
                    text: sinon.stub(),
                    cron: sinon.stub()
                }};
                later.parse.text.returns({ error: 1 });
                later.parse.cron.returns({ error: 1 });
                expect(util.getSchedule(later, 'at 4:00 am')).to.be.null;
                expect(later.parse.text).to.be.calledWithMatch('at 4:00 am');
                expect(later.parse.cron).to.be.calledWithMatch('at 4:00 am');
            });

            it('returns the parsed text expression if it parses without error', function() {
                var later = { parse: {
                    text: sinon.stub(),
                    cron: sinon.stub()
                }};
                var schedule = { error: -1 };
                later.parse.text.returns(schedule);
                later.parse.cron.returns({ error: 1 });
                expect(util.getSchedule(later, 'at 4:00 am')).to.equal(schedule);
                expect(later.parse.text).to.be.calledWithMatch('at 4:00 am');
                expect(later.parse.cron).to.not.be.called;
            });

            it('returns the parsed cron expression if the text parsing failed', function() {
                var later = { parse: {
                    text: sinon.stub(),
                    cron: sinon.stub()
                }};
                var schedule = { error: -1 };
                later.parse.text.returns({ error: 1 });
                later.parse.cron.returns(schedule);
                expect(util.getSchedule(later, 'at 4:00 am')).to.equal(schedule);
                expect(later.parse.text).to.be.calledWithMatch('at 4:00 am');
                expect(later.parse.cron).to.be.calledWithMatch('at 4:00 am');
            });

        });

        describe('#getNextScheduleOccurance', function() {

            it('returns null if missing required parameters', function() {
                expect(util.getNextScheduleOccurance()).to.be.null;
                expect(util.getNextScheduleOccurance(null, {})).to.be.null;
                expect(util.getNextScheduleOccurance({}, null)).to.be.null;
            });

            it('returns the next occurence for the schedule', function() {
                var later = { schedule: sinon.stub() };
                var schedule = {
                    next: sinon.stub(),
                    isValid: sinon.stub()
                };
                var next = [{}, {}];
                later.schedule.returns(schedule);
                schedule.next.returns(next[0]);
                schedule.next.withArgs(2).returns(next);
                schedule.isValid.returns(false);
                expect(util.getNextScheduleOccurance(later, {})).to.equal(next[0]);
                expect(later.schedule).to.be.called;
                expect(schedule.isValid).to.be.called;
            });

            it('returns the second occurence for the schedule if the current one is already active', function() {
                var later = { schedule: sinon.stub() };
                var schedule = {
                    next: sinon.stub(),
                    isValid: sinon.stub()
                };
                var next = [1, 2];
                later.schedule.returns(schedule);
                schedule.next.returns(next[0]);
                schedule.next.withArgs(2).returns(next);
                schedule.isValid.returns(true);
                expect(util.getNextScheduleOccurance(later, {})).to.equal(next[1]);
                expect(later.schedule).to.be.called;
                expect(schedule.isValid).to.be.called;
            });

        });

    });
});