From 35c7abddb94f47ace3e41793e76dd242105be49b Mon Sep 17 00:00:00 2001 From: Rahul R Subramaniam Date: Wed, 16 Mar 2022 10:52:25 +0530 Subject: [PATCH] Adding a new listener to cookieConsent (#118) * Adding new subscribeConsent method to listen to changes in Consent object * Updated Readme * Added unscubscribe method * Adding subscribeToConsentStore method * Adding subscribeToConsentStore method * Adding Test cases for cookieConsent * EFixed each callback getting the same reference to the store * Assiging each callback a different reference to the consent Object * Adding Unsubscribe Test Case * Creating new test suite for cookieConsent.subscribeToConsentStore * 2.6.0 * Npm version minor patch --- lib/cookie/README.md | 2 +- lib/cookie/index.js | 8 +++++ lib/cookieConsent/README.md | 3 ++ lib/cookieConsent/index.js | 45 ++++++++++++++++++++++++++- package-lock.json | 2 +- package.json | 2 +- test/unit/cookieConsent/index.spec.js | 31 +++++++++++++++++- 7 files changed, 88 insertions(+), 5 deletions(-) diff --git a/lib/cookie/README.md b/lib/cookie/README.md index a989c17..b5117a4 100644 --- a/lib/cookie/README.md +++ b/lib/cookie/README.md @@ -5,7 +5,7 @@ The cookie module provides methods for interacting with browser cookies. ```js var cookie = require('bv-ui-core/lib/cookie'); -cookie.write('RememberMe', '1', 365); +cookie.create('RememberMe', '1', 365); console.log(cookie.read('RememberMe')); // '1' cookie.remove('RememberMe'); ``` diff --git a/lib/cookie/index.js b/lib/cookie/index.js index 75b1f88..dd20526 100644 --- a/lib/cookie/index.js +++ b/lib/cookie/index.js @@ -79,6 +79,14 @@ module.exports = { if (consentPresent) { createCookie(name, value, days, domain, secure); } + cookieConsent.subscribe(name,'add',function (consent) { + if (consent) { + createCookie(name,value,days,domain,secure); + } + else { + removeCookie(name,domain) + } + }) cookieConsent.subscribe(name, 'enable', function () { createCookie(name, value, days, domain, secure); diff --git a/lib/cookieConsent/README.md b/lib/cookieConsent/README.md index 98c2cc8..6d1629e 100644 --- a/lib/cookieConsent/README.md +++ b/lib/cookieConsent/README.md @@ -30,6 +30,9 @@ cookieConsent.subscribe('cookie3', 'enable', function (data) {}); // Subscribe to consent 'disable' event. Triggers when a cookie consent is set to false var event = cookieConsent.subscribe('cookie3', 'disable', function (data) {}); +// Subscribe to the store change event. The latest consent store object is passed as parameter to the callback function +var event = subscribeToConsentStore(function (store){}); + // Unsubscribe events event.unsubscribe(); diff --git a/lib/cookieConsent/index.js b/lib/cookieConsent/index.js index bc4e9ca..1dd8983 100644 --- a/lib/cookieConsent/index.js +++ b/lib/cookieConsent/index.js @@ -4,6 +4,9 @@ var cookieConsent = (function () { var store = {}; var subscribers = {}; var events = ['add', 'enable', 'disable', 'change']; + var storeCallbacks = {}; + + /** * _publish: Calls subscriber callbacks * @param {String} consentKey Consent key @@ -17,6 +20,18 @@ var cookieConsent = (function () { }) } } + + /** + * _publishStore: calls Callbacks with the store object passed + */ + + function _publishStore () { + if (Object.values(storeCallbacks).length > 0) { + Object.values(storeCallbacks).forEach(function (callback) { + callback(Object.assign({}, store)); + }) + } + } /** * _set: Set store data * @param {String} consentKey Consent key @@ -56,6 +71,15 @@ var cookieConsent = (function () { delete subscribers[this.consentKey][this.eventName][this.key]; } } + + /** + * _unsubscribeStore: Unsubscribes subscribers from the consent store + */ + function _unsubscribeStore () { + if (storeCallbacks[this.key]) { + delete storeCallbacks[this.key]; + } + } /** * Get consent disabled * @returns Boolean @@ -119,10 +143,15 @@ var cookieConsent = (function () { if (!(consent && !Array.isArray(consent) && typeof consent === 'object')) { throw new TypeError('cookieConsent (setConsent): consent should be an object.') } + var store var keys = Object.keys(consent); for (var i = 0; i < keys.length; i++) { _set(keys[i], consent[keys[i]]); } + var storeCopy=Object.assign({},store) + if (JSON.stringify(storeCopy)!==JSON.stringify(store)) { + _publishStore() + } return true; } @@ -172,12 +201,26 @@ var cookieConsent = (function () { }; } + function subscribeToConsentStore (callback) { + if (typeof callback !== 'function') { + throw new TypeError('cookieConsent (subscribeToConsentStore): callback should be a function.'); + } + + var key = Math.random().toString(36).substr(2, 5); + storeCallbacks[key] = callback; + + return { + unsubscribe: _unsubscribeStore.bind({ key: key }) + } + } + return { initConsent: initConsent, getConsent: getConsent, getConsentDisabled: getConsentDisabled, setConsent: setConsent, - subscribe: subscribe + subscribe: subscribe, + subscribeToConsentStore: subscribeToConsentStore }; })(); diff --git a/package-lock.json b/package-lock.json index cdedeb3..0bda3e8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "bv-ui-core", - "version": "2.5.2", + "version": "2.6.0", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 179a340..86c910b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "bv-ui-core", - "version": "2.5.2", + "version": "2.6.0", "license": "Apache 2.0", "description": "Bazaarvoice UI-related JavaScript", "repository": { diff --git a/test/unit/cookieConsent/index.spec.js b/test/unit/cookieConsent/index.spec.js index 8abc6bd..907cc36 100644 --- a/test/unit/cookieConsent/index.spec.js +++ b/test/unit/cookieConsent/index.spec.js @@ -84,7 +84,36 @@ describe('lib/cookieConsent', function () { function test5 () { return cookieConsent.subscribe('key1', 'enable', function () {}); } - expect(test5()).to.be.an('object'); }); + it('cookieConsent.subscribeToConsentStore', function () { + // Error checks - Correct errors are thrown + function test6 () { + cookieConsent.subscribeToConsentStore('Callback') + } + expect(test6).to.throw(TypeError,'cookieConsent (subscribeToConsentStore): callback should be a function.'); + // Subscriber creation test - The subscription gets created correctly + function test7 () { + return cookieConsent.subscribeToConsentStore(function () {}); + } + expect(test7()).to.be.an('object'); + + // Event listener test - The subscriber callback fires on store change + var fn = sinon.spy() + function test8 () { + cookieConsent.subscribeToConsentStore(fn) + cookieConsent.setConsent({ cookie4: true }) + } + test8 () + sinon.assert.calledOnce(fn) + // Unsubscribe test - Should be able to unsubscribe successfully (Should not fire the callback after unsubscription) + var fn2 = sinon.spy() + function test9 () { + var event = cookieConsent.subscribeToConsentStore(fn2) + event.unsubscribe() + cookieConsent.setConsent({ cookie5: true }) + } + test9() + sinon.assert.notCalled(fn2) + }); });