Skip to content

Commit

Permalink
Merge pull request #6 from customink/test-fix
Browse files Browse the repository at this point in the history
Remove Chai and Sinon from `client_backed.test.js`
  • Loading branch information
Kate Piette authored Feb 3, 2022
2 parents a3dca6a + 3178ec3 commit c9f52e0
Showing 1 changed file with 44 additions and 44 deletions.
88 changes: 44 additions & 44 deletions test/client_backed.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import {assert} from 'chai';
import sinon from 'sinon';
import {Promise} from 'es6-promise';
import md5 from 'blueimp-md5';
import Cookies from 'cookies-js';
Expand Down Expand Up @@ -45,104 +43,106 @@ describe('Signaler (client backed)', () => {
describe('setup', () => {
it('sets features', () => {
const signal = new Signaler(features);
assert.isFunction(signal.featureFlags);
assert.isFunction(signal.featureFlag);
assert.isFunction(signal.setFeatureFlag);
expect(signal.featureFlags).toBeInstanceOf(Function);
expect(signal.featureFlag).toBeInstanceOf(Function);
expect(signal.setFeatureFlag).toBeInstanceOf(Function);
});
});

describe('featureFlags', () => {
it('returns current cookie values of feature flags', done => {
it('returns current cookie values of feature flags', async () => {
const returnedFlags = {
featureOne: 'control',
featureTwo: 'test',
featureThree: 'something',
notSetOne: undefined,
notSetTwo: undefined,
notSetThree: undefined
}
const signal = new Signaler(features);
const flags = signal.featureFlags();

flags.then(data => {
assert.deepEqual(data, {
featureOne: 'control',
featureTwo: 'test',
featureThree: 'something',
notSetOne: undefined,
notSetTwo: undefined,
notSetThree: undefined
});
done();
await flags.then(data => {
expect(data).toEqual(returnedFlags);
});
});
});

describe('featureFlag', () => {
describe('feature is stored in a cookie already', () => {
it('returns the feature flag value', done => {
it('returns the feature flag value', async () => {
const signal = new Signaler(features);
const flag = signal.featureFlag('featureOne');
const flag2 = signal.featureFlag('featureTwo');
Promise.all([flag, flag2]).then(([flagData, flag2Data]) => {
assert.equal(flagData, 'control');
assert.equal(flag2Data, 'test');
done();
await Promise.all([flag, flag2]).then(([flagData, flag2Data]) => {
expect(flagData).toEqual('control');
expect(flag2Data).toEqual('test');
});
});
});

describe('feature is not stored in a cookie', () => {
describe('response.expires is a string', () => {
it('gets the flag value and sets it to a cookie', done => {
it('gets the flag value and sets it to a cookie', async () => {
const signal = new Signaler(features);
const featureName = 'notSetOne';
const flag = signal.featureFlag(featureName);

flag.then(data => {
assert.match(data, /^test|control$/);
await flag.then(data => {
expect(data).toMatch(/^test|control$/);
const cookieVal = Cookies.get(md5(featureName));
assert.match(cookieVal, /^test|control$/);
done();
expect(cookieVal).toMatch(/^test|control$/);
});
});
});

describe('response.expires is a number', () => {
it('gets the flag value and sets it to a cookie with the expires option being the number of days after the current date', done => {
it('gets the flag value and sets it to a cookie with the expires option being the number of days after the current date', async () => {
const signal = new Signaler(features);
const featureName = 'notSetTwo';
const flag = signal.featureFlag(featureName);

flag.then(data => {
assert.match(data, /^test|control$/);
await flag.then(data => {
expect(data).toMatch(/^test|control$/);
const cookieVal = Cookies.get(md5(featureName));
assert.match(cookieVal, /^test|control$/);
done();
expect(cookieVal).toMatch(/^test|control$/);
});
});
});

describe('response.expires is not defined', () => {
it('gets the flag value and sets it to a cookie', done => {
it('gets the flag value and sets it to a cookie', async () => {
const signal = new Signaler(features);
const featureName = 'notSetThree';
const flag = signal.featureFlag(featureName);

flag.then(data => {
assert.match(data, /^flag|flag2/);
await flag.then(data => {
expect(data).toMatch(/^flag|flag2/);
const cookieVal = Cookies.get(md5(featureName));
assert.match(cookieVal, /^flag|flag2/);
done();
expect(cookieVal).toMatch(/^flag|flag2/);
});
});
});
});
});

describe('setFeatureFlag', () => {
const cookieSpy = jest.spyOn(Cookies, 'set');

afterAll(() => {
jest.restoreAllMocks();
});

it('sets the cookie with the options passed in', () => {
const signal = new Signaler(features);
const featureName = 'newFeature';
const featureVal = 'myVal';
const cookieSpy = sinon.spy(Cookies, 'set');

signal.setFeatureFlag(featureName, featureVal);
const cookieVal = Cookies.get(md5(featureName));
sinon.assert.calledWith(cookieSpy, md5(featureName), featureVal, {});
assert.equal(cookieVal, 'myVal');
cookieSpy.restore();

expect(cookieSpy).toHaveBeenCalledWith(md5(featureName), featureVal, {});
expect(cookieVal).toEqual('myVal');
});

it('transforms cookie options', () => {
Expand All @@ -154,15 +154,15 @@ describe('Signaler (client backed)', () => {
});
const featureName = 'newFeature';
const featureVal = 'myVal';
const cookieSpy = sinon.spy(Cookies, 'set');

signal.setFeatureFlag(featureName, featureVal, {domain: 'domain'});
const cookieVal = Cookies.get(md5(featureName));
sinon.assert.calledWith(cookieSpy, md5(featureName), featureVal, {

expect(cookieSpy).toHaveBeenCalledWith(md5(featureName), featureVal, {
path: '/secret',
domain: 'domain'
});
assert.equal(cookieVal, 'myVal');
cookieSpy.restore();
expect(cookieVal).toEqual('myVal');
});
});
});

0 comments on commit c9f52e0

Please sign in to comment.