Git Product home page Git Product logo

mockcookie's Introduction

mockCookie

A simple way to mock document.cookie functionality in your Javascript and be able to test using Jasmine.

My recommendation is always to define and use an internal variable using window or document (or any other browser global object). This will give you the possibility to mock the behaviour of your code and make it flexible and testable.

Usage

We are gonna write an object that an awesome called parameter is present in the url and, if it is, it will check if the value of a cookie called awesomeCookie is the same, if is not the code will update the cookie value with the awesome parameter value.

var AwesomeObj = function(window, document) {
    this.location = window.location;
    this.document = document;
    this.checkAwesomePrameterExistence();
};

AwesomeObj.prototype.checkAwesomePrameterExistence = function() {
    var awesomeQueryParam = this.getParameterByName('awesome');
    var awesomeCookieValue = this.getCookie('awesomeCookie');
    
    if(awesomeQueryParam){
        if(awesomeQueryParam !== awesomeCookieValue){
             this.setCookie("awesomeCookie", awesomeQueryParam, 30);
        }
    }
};

AwesomeObj.prototype.getParameterByName = function(name) {
    name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
    var regex = new RegExp("[\\?&]" + name + "=([^&#]*)", "i"),
        results = regex.exec(this.location.search);
    return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
};

AwesomeObj.prototype.getCookie = function(cname) {
    var name = cname + "=";
    var ca = this.document.cookie.split(';');
    for(var i=0; i<ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1);
        if (c.indexOf(name) === 0) return c.substring(name.length,c.length);
    }
    return "";
};

AwesomeObj.prototype.setCookie = function(cname, cvalue, exdays) {
    var d = new Date();
    d.setTime(d.getTime() + (exdays*24*60*60*1000));
    var expires = "expires="+d.toUTCString();
    this.document.cookie = cname + "=" + cvalue + "; domain=; path=/; " + expires;
};

//Object Constructor
new AwesomeObj(window, document);

TESTS

When you test you'll be able to use our mockDocument to see if the cookie has been set:

describe("AwesomeObj Spec", function() {
    
    var mockWindow;
    var getMockCookieValue;
    var mockDocument;
    var mockAwesomeParam;
    var mockCookieValue;
    var cookieValue;
    
    beforeEach(function(){
        
        mockWindow = {
            location: {
                search: ""
            }
        };

        mockDocument = {
            value_: {}, 

            get cookie() {
                var output = [];
                for (var cookieName in this.value_) {
                    output.push(cookieName + "=" + this.value_[cookieName]);
                }
                return output.join(";");
            },

            set cookie(s) {
                if(s){
                    var indexOfSeparator = s.indexOf("=");
                    var key = s.substr(0, indexOfSeparator);
                    var value = s.substring(indexOfSeparator + 1);
                    this.value_[key] = value;
                    return key + "=" + value;
                }
            }
        };

        getMockCookieValue = function(cname){
            var name = cname + "=";
            var ca = mockDocument.cookie;
            ca = ca.split(';');
            for(var i=0; i<ca.length; i++) {
                var c = ca[i];
                while (c.charAt(0)==' ') c = c.substring(1);
                if (c.indexOf(name) === 0) return c.substring(name.length,c.length);
            }
            return "";
        };
        
        mockAwesomeParam = "1234";
    });
    
    describe("When awesome paramenter IS present in the query", function() {
    
        describe("Expect awesome cookie", function() {
            
            beforeEach(function(){
                mockWindow.location.search = "?awesome=" + mockAwesomeParam;
                cookieValue = "not-awesome";
                mockDocument.cookie = "awesomeCookie=" + cookieValue + "; domain=; path=/; expires=1434555910537";
                
                new AwesomeObj(mockWindow, mockDocument);
            });

            it("to be updated with the new value", function() {
                expect(getMockCookieValue("awesomeCookie")).toBe(mockAwesomeParam);
            });
            
        });
    });
    
    
    describe("When awesome paramenter IS NOT present in the query", function() {
        
        describe("And awesome cookie is empty/not defined", function() {
    
            beforeEach(function(){
                mockWindow.location.search = "";
                mockDocument.cookie = "";

                new AwesomeObj(mockWindow, mockDocument);
            });

            it("Expect asosAffiliate cookie to be empty/not defined", function() {
                expect(getMockCookieValue("awesomeCookie")).toBe("");
            });
        });
    
        describe("And awesome cookie is already defined ", function() {
    
            beforeEach(function(){
                mockWindow.location.search = "";
                cookieValue = "awesome";
                mockDocument.cookie = "awesomeCookie=" + cookieValue + "; domain=; path=/; expires=1434555910537";

                new AwesomeObj(mockWindow, mockDocument);
            });

            it("Expect awesomeCookie to remain the same", function() {
                expect(getMockCookieValue("awesomeCookie")).toBe(cookieValue);
            });
        });
    });
    
});

mockcookie's People

Contributors

giopunt avatar

Stargazers

Matt Saywell avatar

Watchers

 avatar

Forkers

alurvishant

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.