Skip to content

Commit

Permalink
Adding a new listener to cookieConsent (#118)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
RahulSubramaniam17 authored Mar 16, 2022
1 parent 891c2ab commit 35c7abd
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 5 deletions.
2 changes: 1 addition & 1 deletion lib/cookie/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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');
```
Expand Down
8 changes: 8 additions & 0 deletions lib/cookie/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 3 additions & 0 deletions lib/cookieConsent/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
45 changes: 44 additions & 1 deletion lib/cookieConsent/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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
};
})();

Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
31 changes: 30 additions & 1 deletion test/unit/cookieConsent/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
});
});

0 comments on commit 35c7abd

Please sign in to comment.