Skip to content

Commit 35c7abd

Browse files
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
1 parent 891c2ab commit 35c7abd

File tree

7 files changed

+88
-5
lines changed

7 files changed

+88
-5
lines changed

lib/cookie/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ The cookie module provides methods for interacting with browser cookies.
55
```js
66
var cookie = require('bv-ui-core/lib/cookie');
77

8-
cookie.write('RememberMe', '1', 365);
8+
cookie.create('RememberMe', '1', 365);
99
console.log(cookie.read('RememberMe')); // '1'
1010
cookie.remove('RememberMe');
1111
```

lib/cookie/index.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,14 @@ module.exports = {
7979
if (consentPresent) {
8080
createCookie(name, value, days, domain, secure);
8181
}
82+
cookieConsent.subscribe(name,'add',function (consent) {
83+
if (consent) {
84+
createCookie(name,value,days,domain,secure);
85+
}
86+
else {
87+
removeCookie(name,domain)
88+
}
89+
})
8290

8391
cookieConsent.subscribe(name, 'enable', function () {
8492
createCookie(name, value, days, domain, secure);

lib/cookieConsent/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ cookieConsent.subscribe('cookie3', 'enable', function (data) {});
3030
// Subscribe to consent 'disable' event. Triggers when a cookie consent is set to false
3131
var event = cookieConsent.subscribe('cookie3', 'disable', function (data) {});
3232

33+
// Subscribe to the store change event. The latest consent store object is passed as parameter to the callback function
34+
var event = subscribeToConsentStore(function (store){});
35+
3336
// Unsubscribe events
3437
event.unsubscribe();
3538

lib/cookieConsent/index.js

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ var cookieConsent = (function () {
44
var store = {};
55
var subscribers = {};
66
var events = ['add', 'enable', 'disable', 'change'];
7+
var storeCallbacks = {};
8+
9+
710
/**
811
* _publish: Calls subscriber callbacks
912
* @param {String} consentKey Consent key
@@ -17,6 +20,18 @@ var cookieConsent = (function () {
1720
})
1821
}
1922
}
23+
24+
/**
25+
* _publishStore: calls Callbacks with the store object passed
26+
*/
27+
28+
function _publishStore () {
29+
if (Object.values(storeCallbacks).length > 0) {
30+
Object.values(storeCallbacks).forEach(function (callback) {
31+
callback(Object.assign({}, store));
32+
})
33+
}
34+
}
2035
/**
2136
* _set: Set store data
2237
* @param {String} consentKey Consent key
@@ -56,6 +71,15 @@ var cookieConsent = (function () {
5671
delete subscribers[this.consentKey][this.eventName][this.key];
5772
}
5873
}
74+
75+
/**
76+
* _unsubscribeStore: Unsubscribes subscribers from the consent store
77+
*/
78+
function _unsubscribeStore () {
79+
if (storeCallbacks[this.key]) {
80+
delete storeCallbacks[this.key];
81+
}
82+
}
5983
/**
6084
* Get consent disabled
6185
* @returns Boolean
@@ -119,10 +143,15 @@ var cookieConsent = (function () {
119143
if (!(consent && !Array.isArray(consent) && typeof consent === 'object')) {
120144
throw new TypeError('cookieConsent (setConsent): consent should be an object.')
121145
}
146+
var store
122147
var keys = Object.keys(consent);
123148
for (var i = 0; i < keys.length; i++) {
124149
_set(keys[i], consent[keys[i]]);
125150
}
151+
var storeCopy=Object.assign({},store)
152+
if (JSON.stringify(storeCopy)!==JSON.stringify(store)) {
153+
_publishStore()
154+
}
126155
return true;
127156
}
128157

@@ -172,12 +201,26 @@ var cookieConsent = (function () {
172201
};
173202
}
174203

204+
function subscribeToConsentStore (callback) {
205+
if (typeof callback !== 'function') {
206+
throw new TypeError('cookieConsent (subscribeToConsentStore): callback should be a function.');
207+
}
208+
209+
var key = Math.random().toString(36).substr(2, 5);
210+
storeCallbacks[key] = callback;
211+
212+
return {
213+
unsubscribe: _unsubscribeStore.bind({ key: key })
214+
}
215+
}
216+
175217
return {
176218
initConsent: initConsent,
177219
getConsent: getConsent,
178220
getConsentDisabled: getConsentDisabled,
179221
setConsent: setConsent,
180-
subscribe: subscribe
222+
subscribe: subscribe,
223+
subscribeToConsentStore: subscribeToConsentStore
181224
};
182225
})();
183226

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "bv-ui-core",
3-
"version": "2.5.2",
3+
"version": "2.6.0",
44
"license": "Apache 2.0",
55
"description": "Bazaarvoice UI-related JavaScript",
66
"repository": {

test/unit/cookieConsent/index.spec.js

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,36 @@ describe('lib/cookieConsent', function () {
8484
function test5 () {
8585
return cookieConsent.subscribe('key1', 'enable', function () {});
8686
}
87-
8887
expect(test5()).to.be.an('object');
8988
});
89+
it('cookieConsent.subscribeToConsentStore', function () {
90+
// Error checks - Correct errors are thrown
91+
function test6 () {
92+
cookieConsent.subscribeToConsentStore('Callback')
93+
}
94+
expect(test6).to.throw(TypeError,'cookieConsent (subscribeToConsentStore): callback should be a function.');
95+
// Subscriber creation test - The subscription gets created correctly
96+
function test7 () {
97+
return cookieConsent.subscribeToConsentStore(function () {});
98+
}
99+
expect(test7()).to.be.an('object');
100+
101+
// Event listener test - The subscriber callback fires on store change
102+
var fn = sinon.spy()
103+
function test8 () {
104+
cookieConsent.subscribeToConsentStore(fn)
105+
cookieConsent.setConsent({ cookie4: true })
106+
}
107+
test8 ()
108+
sinon.assert.calledOnce(fn)
109+
// Unsubscribe test - Should be able to unsubscribe successfully (Should not fire the callback after unsubscription)
110+
var fn2 = sinon.spy()
111+
function test9 () {
112+
var event = cookieConsent.subscribeToConsentStore(fn2)
113+
event.unsubscribe()
114+
cookieConsent.setConsent({ cookie5: true })
115+
}
116+
test9()
117+
sinon.assert.notCalled(fn2)
118+
});
90119
});

0 commit comments

Comments
 (0)