naissa12
7/25/2017 - 2:54 PM

Unit tests

Unit tests

/*
 * ADOBE CONFIDENTIAL
 *
 * Copyright (c) 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.
 */

/*jslint vars: true, plusplus: true, devel: true, nomen: true, indent: 4, bitwise: true, node: true, expr: true, mocha: true, browser: true */
/*global define, require, _, $, module, sinon, root, expect */

var currentQueue;

describe ('queue', function(){

    var sandbox,
        _ = require('underscore'),
        Queue;

    function checkElements(elements, queueElements) {
        var diff = _.difference(queueElements, elements);
        return diff.length > 0;
    }

    before(function (done) {
        Queue = require( '../../src/js/utils/queue');
        done();
    });

    beforeEach(function () {
        sandbox = sinon.sandbox.create();
        currentQueue = new Queue();
    });

    afterEach(function () {
        currentQueue = null;
        sandbox.restore();
    });

    it ('should be able to add elements to queue', function () {
        currentQueue.enqueue(1);
        currentQueue.enqueue(2);
        currentQueue.enqueue(3);
        currentQueue.enqueue(4);
        expect(currentQueue.getLength()).to.equal(4);
        expect(checkElements([1,2,3,4], currentQueue.getElements())).to.equal(false);
    });

    it ('should be able to remove elements from queue', function () {
        currentQueue.enqueue(1);
        currentQueue.enqueue(2);
        currentQueue.enqueue(3);
        currentQueue.enqueue(4);
        expect(currentQueue.getLength()).to.equal(4);
        currentQueue.dequeue(4);
        expect(currentQueue.getLength()).to.equal(3);
        expect(checkElements([1,2,3], currentQueue.getElements())).to.equal(false);
    });

    it ('should be empty when its empty', function () {
        expect(currentQueue.isEmpty()).to.equal(true);
    });

    it ('should have length = 0 when its empty', function () {
        expect(currentQueue.getLength()).to.equal(0);
    });

    it ('should be able to peek', function () {
        currentQueue.enqueue(3);
        currentQueue.enqueue(1);
        expect(currentQueue.peek()).to.equal(1);
    });
});


/*
 * ADOBE CONFIDENTIAL
 *
 * Copyright (c) 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.
 */

/*jslint vars: true, plusplus: true, devel: true, nomen: true, indent: 4, browser: true */
/*global define, marvel*/

define([
    'lodash',
    'backbone'
], function (_, Backbone) {

    var Queue =  Backbone.Model.extend({
        elements: [],

        getElements: function () {
            return this.elements;
        },

        topenqueue: function (element) {
           this.elements.unshift(element);
        },

        enqueue: function(element) {
            this.elements.push(element);
        },

        _localStorageRetrieval: function (name) {
            var arr = [];

            if (window.localStorage) {
                try {

                    var items = window.localStorage.getItem(name);
                    if (items) {
                        arr = JSON.parse(items);
                        if (!arr || _.isEmpty(arr) || !_.isArray(arr)) {
                            arr = [];
                        }
                    }
                }
                catch (ex) {}
            }
            return arr;
        },

        localStorageStoreTop: function (name, element) {
            if (window.localStorage) {
                try {
                    var arr = this._localStorageRetrieval(name);
                    arr.unshift(element);
                    window.localStorage.setItem(name, JSON.stringify(arr));
                } catch(ex) {}
            }
        },

        localStorageStore: function (name, element) {
            if (window.localStorage) {
                try {
                    var arr = this._localStorageRetrieval(name);
                    arr.push(element);
                    window.localStorage.setItem(name, JSON.stringify(arr));
                } catch(ex) {}
            }
        },

        localStorageRemove: function (name, element) {
            if (window.localStorage) {
                try {
                    var items = window.localStorage.getItem(name);
                    var arr = [];
                    if (items) {
                        arr = JSON.parse(items);

                        if (!arr || _.isEmpty(arr) || !_.isArray(arr)) {
                            arr = [];
                        } else {
                            arr = _.filter(arr, function (i) {
                                return (i.id == element.id);
                            });
                        }
                    }
                    window.localStorage.setItem(name, JSON.stringify(arr));
                } catch(ex) {}
            }
        },

        localStorageToQueue: function (name) {
            if (window.localStorage) {
                try {
                    var arr = this._localStorageRetrieval(name);

                    if (_.isEmpty(arr)) {
                        return false;
                    }

                    _.each(arr, function (a) {
                        this.enqueue(a);
                    }.bind(this));
                }
                catch (ex) {}
            }
        },

        dequeue: function(index) {
            var item;
            if (this.elements.length === 0) {
                return null;
            }

            if (_.isUndefined(index)) {
                item = this.elements[0];
                this.elements.shift();
            } else {
                if (this.elements[index]) {
                    item = this.elements[index];
                    var elements = _.filter(this.elements, function (item, key) {
                        return key !== index;
                    });
                    this.elements = elements;
                } else {
                    return null;
                }
            }
            return item;
        },

        getLength: function () {
            return this.elements.length;
        },

        isEmpty: function () {
            return (this.getLength() === 0);
        },

        peek: function(){
            if (this.elements.length === 0) {
                return null;
            }
            return this.elements[0];
        }
    });

    return Queue;
});