Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix when two or more instances of sixpack loads #27

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "JS client for SeatGeek's Sixpack AB testing framework.",
"main": "sixpack.js",
"scripts": {
"test": "mocha"
"test": "mocha --globals document,window"
},
"repository": {
"type": "git",
Expand Down
29 changes: 15 additions & 14 deletions sixpack.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,33 @@
// Object.assign polyfill from https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Object/assign#Polyfill
Object.assign||Object.defineProperty(Object,"assign",{enumerable:!1,configurable:!0,writable:!0,value:function(e){"use strict";if(void 0===e||null===e)throw new TypeError("Cannot convert first argument to object");for(var r=Object(e),t=1;t<arguments.length;t++){var n=arguments[t];if(void 0!==n&&null!==n){n=Object(n);for(var o=Object.keys(Object(n)),a=0,c=o.length;c>a;a++){var i=o[a],b=Object.getOwnPropertyDescriptor(n,i);void 0!==b&&b.enumerable&&(r[i]=n[i])}}}return r}});

var sixpack = {
// check if on node
var on_node = typeof window === "undefined";

var sixpack = (!on_node && window.sixpack) ? window.sixpack : {
base_url: "http://localhost:5000",
ip_address: null,
user_agent: null,
timeout: 1000,
persist: true,
cookie_name: "sixpack_client_id",
cookie_domain: null,
ignore_alternates_warning: null
ignore_alternates_warning: false
};

// check if on node, else expose on browser's global window object
var on_node = false;
if (typeof window === "undefined") {
on_node = true;
} else {
window["sixpack"] = sixpack;
if (!on_node) {
window.sixpack = sixpack;
}

sixpack.generate_client_id = function () {
function generate_uuidv4() {
// from http://stackoverflow.com/questions/105034
var client_id = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);
return v.toString(16);
});
}

sixpack.generate_client_id = function () {
var client_id = generate_uuidv4();
if (!on_node && this.persist) {
var cookie_value = this.cookie_name + "=" + client_id + "; expires=Tue, 19 Jan 2038 03:14:07 GMT; path=/";
if (this.cookie_domain) {
Expand Down Expand Up @@ -163,8 +165,6 @@
}
};

var counter = 0;

var _request = function(uri, params, timeout, callback) {
var timed_out = false;
var timeout_handle = setTimeout(function () {
Expand All @@ -173,7 +173,8 @@
}, timeout);

if (!on_node) {
var cb = "callback" + (++counter);
var suffix = generate_uuidv4().replace(/-/g, '');
var cb = "callback" + suffix;
params.callback = "sixpack." + cb
sixpack[cb] = function (res) {
if (!timed_out) {
Expand Down
35 changes: 33 additions & 2 deletions test/sixpack-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,21 @@ var mocha = require('mocha');
var assert = require('chai').assert;
var expect = require('chai').expect;

describe("Sixpack", function () {
function createSixpackInstance(on_node) {
if (!on_node) {
global.document = global.document || {};
global.window = global.window || {};
}
delete require.cache[require.resolve('../')]
return require('../');
}

describe("Sixpack in node", function () {
var sixpack;
var session;

beforeEach( () => {
sixpack = require('../');
sixpack = createSixpackInstance(true);
session = new sixpack.Session();

// Override default base_url when the SIXPACK_BASE_URL
Expand Down Expand Up @@ -212,3 +221,25 @@ describe("Sixpack", function () {
});
});
});

describe('Sixpack in browser', () => {
afterEach(() => {
delete global.document;
delete global.window;
});

it('should create sixpack instance in browser', () => {
global.window = {};
expect(window.sixpack).to.be.undefined;
createSixpackInstance(false);
expect(window.sixpack).to.be.an('object');
});

it('should not create another sixpack instance if exists in browser', () => {
var globalSixpack = {};
global.window = { sixpack: globalSixpack };
createSixpackInstance(false);

expect(window.sixpack).to.be.equal(globalSixpack);
});
});