@envato/cookie-consent
Advanced tools
Comparing version 1.2.2 to 1.3.0
@@ -10,1 +10,2 @@ export declare enum Consent { | ||
export declare const consented: (consent: Consent) => boolean; | ||
export declare const userHasOptedOutOfCookiesForCategory: (consent: Consent) => boolean; |
@@ -74,2 +74,34 @@ "use strict"; | ||
}; | ||
exports.userHasOptedOutOfCookiesForCategory = function (consent) { | ||
if (!(typeof window !== 'undefined')) { | ||
return false; | ||
} | ||
// when cookiebot is avaiable | ||
if (!!(window && window.Cookiebot && window.Cookiebot.consent)) { | ||
if (window.Cookiebot.hasResponse) { | ||
return !window.Cookiebot.consent[consent]; | ||
} | ||
else { | ||
return false; | ||
} | ||
} | ||
// manually parse cookie if Cookiebot is not avaiable | ||
var CookieConsent = Cookies.get('CookieConsent'); | ||
if (!CookieConsent) { | ||
return false; | ||
} | ||
// For user outside of targeted area | ||
if (CookieConsent === '-1') { | ||
return false; | ||
} | ||
try { | ||
var parsedCookieConsent = JSON.parse(CookieConsent.replace(/%2c/g, ',') | ||
.replace(/'/g, '"') | ||
.replace(/([{\[,])\s*([a-zA-Z0-9_]+?):/g, '$1"$2":')); | ||
return parsedCookieConsent && !parsedCookieConsent[consent]; | ||
} | ||
catch (e) { | ||
return false; | ||
} | ||
}; | ||
//# sourceMappingURL=index.js.map |
"use strict"; | ||
var __importStar = (this && this.__importStar) || function (mod) { | ||
if (mod && mod.__esModule) return mod; | ||
var result = {}; | ||
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k]; | ||
result["default"] = mod; | ||
return result; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var mockCookie = __importStar(require("js-cookie")); | ||
var js_cookie_1 = __importDefault(require("js-cookie")); | ||
var index_1 = require("./index"); | ||
var castedMockCookie = mockCookie; | ||
var castedMockCookie = js_cookie_1.default; | ||
jest.mock('js-cookie', function () { return ({ | ||
@@ -170,2 +166,85 @@ get: jest.fn(), | ||
}); | ||
describe('userHasOptedOutOfCookiesForCategory()', function () { | ||
beforeEach(function () { | ||
castedMockCookie.get.mockReset(); | ||
castedMockCookie.set.mockReset(); | ||
window.Cookiebot = null; | ||
}); | ||
describe('default', function () { | ||
it('to false', function () { | ||
expect(index_1.userHasOptedOutOfCookiesForCategory(index_1.Consent.statistics)).toBe(false); | ||
}); | ||
}); | ||
describe('with cookiebot loaded but no response', function () { | ||
beforeAll(function () { | ||
; | ||
window.Cookiebot = { | ||
consent: { | ||
statistics: false, | ||
marketing: false | ||
}, | ||
hasResponse: false | ||
}; | ||
}); | ||
it('return false', function () { | ||
expect(index_1.userHasOptedOutOfCookiesForCategory(index_1.Consent.statistics)).toBe(false); | ||
}); | ||
}); | ||
describe('with cookiebot loaded and with response to false', function () { | ||
beforeEach(function () { | ||
; | ||
window.Cookiebot = { | ||
consent: { | ||
statistics: false, | ||
marketing: false | ||
}, | ||
hasResponse: true | ||
}; | ||
}); | ||
it('return true', function () { | ||
expect(index_1.userHasOptedOutOfCookiesForCategory(index_1.Consent.statistics)).toBe(true); | ||
}); | ||
}); | ||
describe('with cookiebot loaded and with response to true', function () { | ||
beforeEach(function () { | ||
; | ||
window.Cookiebot = { | ||
consent: { | ||
statistics: true, | ||
marketing: false | ||
}, | ||
hasResponse: true | ||
}; | ||
}); | ||
it('return false', function () { | ||
expect(index_1.userHasOptedOutOfCookiesForCategory(index_1.Consent.statistics)).toBe(false); | ||
}); | ||
}); | ||
describe('with cookiebot not loaded', function () { | ||
it('return false when no cookie', function () { | ||
castedMockCookie.get.mockImplementation(function () { return undefined; }); | ||
expect(index_1.userHasOptedOutOfCookiesForCategory(index_1.Consent.statistics)).toBe(false); | ||
}); | ||
it('return false when outside or region', function () { | ||
castedMockCookie.get.mockImplementation(function () { return '-1'; }); | ||
expect(index_1.userHasOptedOutOfCookiesForCategory(index_1.Consent.statistics)).toBe(false); | ||
}); | ||
it('return false if having issue parse cookie', function () { | ||
castedMockCookie.get.mockImplementation(function () { return '{bad: "json}'; }); | ||
expect(index_1.userHasOptedOutOfCookiesForCategory(index_1.Consent.statistics)).toBe(false); | ||
}); | ||
it('return true when user has consent set to true', function () { | ||
castedMockCookie.get.mockImplementation(function () { | ||
return "{stamp:'xJB03YkuI2LicNIfQSnHClMF+YsNEHjsCyQgEKSFPW5UbP8sXHXM1g==',necessary:true,preferences:true,statistics:true,marketing:true,ver:1}"; | ||
}); | ||
expect(index_1.userHasOptedOutOfCookiesForCategory(index_1.Consent.statistics)).toBe(false); | ||
}); | ||
it('return false when user has consent set to false', function () { | ||
castedMockCookie.get.mockImplementation(function () { | ||
return "{stamp:'xJB03YkuI2LicNIfQSnHClMF+YsNEHjsCyQgEKSFPW5UbP8sXHXM1g==',necessary:true,preferences:true,statistics:false,marketing:true,ver:1}"; | ||
}); | ||
expect(index_1.userHasOptedOutOfCookiesForCategory(index_1.Consent.statistics)).toBe(true); | ||
}); | ||
}); | ||
}); | ||
//# sourceMappingURL=index.test.js.map |
{ | ||
"name": "@envato/cookie-consent", | ||
"version": "1.2.2", | ||
"version": "1.3.0", | ||
"description": "Some helper functions to deal with cookie-consent", | ||
@@ -8,3 +8,3 @@ "main": "dist/index.js", | ||
"build": "tsc", | ||
"test": "jest", | ||
"test": "jest ./src/*.test.ts", | ||
"prepublishOnly": "npm test && npm run build" | ||
@@ -11,0 +11,0 @@ }, |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
35838
527