From 1d85a3d5032bf591dee09dc652e0decbf5c0e68b Mon Sep 17 00:00:00 2001 From: John Tran Date: Fri, 30 Apr 2021 02:32:18 -0700 Subject: [PATCH] Make tests more concise --- test/base-cookie.js | 29 +++++++++++------------------ 1 file changed, 11 insertions(+), 18 deletions(-) diff --git a/test/base-cookie.js b/test/base-cookie.js index a498e3ed..e8ac6de3 100644 --- a/test/base-cookie.js +++ b/test/base-cookie.js @@ -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(); }); }); @@ -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(); });