|
1 | 1 | import sinon from 'sinon';
|
2 | 2 | import cookie from '../src/base-cookie';
|
| 3 | +import base64Id from '../src/base64Id'; |
| 4 | +import Constants from '../src/constants'; |
| 5 | +import utils from '../src/utils'; |
3 | 6 | import { mockCookie, restoreCookie, getCookie } from './mock-cookie';
|
4 | 7 |
|
5 | 8 | describe('cookie', function () {
|
@@ -43,18 +46,64 @@ describe('cookie', function () {
|
43 | 46 | });
|
44 | 47 |
|
45 | 48 | it('should return null when attempting to retrieve a cookie that does not exist', () => {
|
46 |
| - assert.equal(cookie.get('key='), null); |
| 49 | + assert.isNull(cookie.get('key=')); |
47 | 50 | });
|
48 | 51 | });
|
49 | 52 |
|
50 | 53 | describe('areCookiesEnabled', () => {
|
51 |
| - it('return false when it cannot write to a cookie', () => { |
52 |
| - mockCookie({ disabled: true }); |
53 |
| - assert.equal(cookie.areCookiesEnabled(), false); |
| 54 | + before(() => { |
| 55 | + sinon.stub(Math, 'random').returns(1); |
54 | 56 | });
|
| 57 | + after(() => { |
| 58 | + sinon.restore(); |
| 59 | + }); |
| 60 | + afterEach(() => { |
| 61 | + restoreCookie(); |
| 62 | + sinon.restore(); |
| 63 | + }); |
| 64 | + |
| 65 | + describe('when it can write to a cookie', () => { |
| 66 | + it('should return true', () => { |
| 67 | + assert.isTrue(cookie.areCookiesEnabled()); |
| 68 | + }); |
| 69 | + |
| 70 | + it('should cleanup cookies', () => { |
| 71 | + const cookieName = Constants.COOKIE_TEST_PREFIX + base64Id(); |
| 72 | + cookie.areCookiesEnabled(); |
| 73 | + assert.isNull(cookie.get(`${cookieName}=`), null); |
| 74 | + }); |
| 75 | + }); |
| 76 | + |
| 77 | + describe('when it cannot write to a cookie', () => { |
| 78 | + beforeEach(() => { |
| 79 | + mockCookie({ disabled: true }); |
| 80 | + }); |
| 81 | + |
| 82 | + it('should return false', () => { |
| 83 | + assert.isFalse(cookie.areCookiesEnabled()); |
| 84 | + }); |
| 85 | + |
| 86 | + it('should cleanup cookies', () => { |
| 87 | + const cookieName = Constants.COOKIE_TEST_PREFIX + base64Id(); |
| 88 | + |
| 89 | + cookie.areCookiesEnabled(); |
| 90 | + assert.isNull(cookie.get(`${cookieName}=`)); |
| 91 | + }); |
| 92 | + }); |
| 93 | + |
| 94 | + describe('when error is thrown during check', () => { |
| 95 | + it('should cleanup cookies', () => { |
| 96 | + const stubLogInfo = sinon.stub(utils.log, 'info').throws('Stubbed Exception'); |
| 97 | + const spyLogWarning = sinon.spy(utils.log, 'warn'); |
| 98 | + const cookieName = Constants.COOKIE_TEST_PREFIX + base64Id(); |
| 99 | + const res = cookie.areCookiesEnabled(); |
| 100 | + assert.isFalse(res); |
| 101 | + assert.isTrue(spyLogWarning.calledWith('Error thrown when checking for cookies. Reason: "Stubbed Exception"')); |
| 102 | + assert.isNull(cookie.get(`${cookieName}=`)); |
55 | 103 |
|
56 |
| - it('should return true when it can write to a cookie', () => { |
57 |
| - assert.equal(cookie.areCookiesEnabled(), true); |
| 104 | + stubLogInfo.restore(); |
| 105 | + spyLogWarning.restore(); |
| 106 | + }); |
58 | 107 | });
|
59 | 108 | });
|
60 | 109 | });
|
0 commit comments