Skip to content

Commit

Permalink
Make tests more concise
Browse files Browse the repository at this point in the history
  • Loading branch information
jooohhn committed Apr 30, 2021
1 parent 3e51fff commit 1d85a3d
Showing 1 changed file with 11 additions and 18 deletions.
29 changes: 11 additions & 18 deletions test/base-cookie.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,23 +51,26 @@ describe('cookie', function () {
});

describe('areCookiesEnabled', () => {
describe('when it can write to a cookie', () => {
afterEach(() => {
restoreCookie();
});
before(() => {
sinon.stub(Math, 'random').returns(1);
});
after(() => {
sinon.restore();
});
afterEach(() => {
restoreCookie();
sinon.restore();
});

describe('when it can write to a cookie', () => {
it('should return true', () => {
assert.isTrue(cookie.areCookiesEnabled());
});

it('should cleanup cookies', () => {
const stub = sinon.stub(Math, 'random').returns(12345678);

const cookieName = Constants.COOKIE_TEST_PREFIX + base64Id();
cookie.areCookiesEnabled();
assert.isNull(cookie.get(`${cookieName}=`), null);

stub.restore();
});
});

Expand All @@ -76,38 +79,28 @@ describe('cookie', function () {
mockCookie({ disabled: true });
});

afterEach(() => {
restoreCookie();
});

it('should return false', () => {
assert.isFalse(cookie.areCookiesEnabled());
});

it('should cleanup cookies', () => {
const stub = sinon.stub(Math, 'random').returns(12345678);
const cookieName = Constants.COOKIE_TEST_PREFIX + base64Id();

cookie.areCookiesEnabled();
assert.isNull(cookie.get(`${cookieName}=`));

stub.restore();
});
});

describe('when error is thrown during check', () => {
it('should cleanup cookies', () => {
const stub = sinon.stub(Math, 'random').returns(12345678);
const stubLogInfo = sinon.stub(utils.log, 'info').throws('Stubbed Exception');
const spyLogWarning = sinon.spy(utils.log, 'warn');
const cookieName = Constants.COOKIE_TEST_PREFIX + base64Id();

const res = cookie.areCookiesEnabled();
assert.isFalse(res);
assert.isTrue(spyLogWarning.calledWith('Error thrown when checking for cookies. Reason: "Stubbed Exception"'));
assert.isNull(cookie.get(`${cookieName}=`));

stub.restore();
stubLogInfo.restore();
spyLogWarning.restore();
});
Expand Down

0 comments on commit 1d85a3d

Please sign in to comment.