Skip to content

Commit 6c1b014

Browse files
authored
fix (project config manager): Fix onUpdate dispose function (#21)
Summary: Fix dispose function returned from onUpdate of ProjectConfigManager. I forgot to bind the dispose function. Test plan: Added new unit test. Manually tested.
1 parent 309fc01 commit 6c1b014

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

packages/optimizely-sdk/lib/core/project_config/project_config_manager.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ ProjectConfigManager.prototype.onUpdate = function(listener) {
252252
if (index > -1) {
253253
this.__updateListeners.splice(index, 1);
254254
}
255-
};
255+
}.bind(this);
256256
};
257257

258258
/**

packages/optimizely-sdk/lib/core/project_config/project_config_manager.tests.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,42 @@ describe('lib/core/project_config/project_config_manager', function() {
254254
});
255255
});
256256

257+
it('can remove onUpdate listeners using the function returned from onUpdate', function() {
258+
datafileManager.DatafileManager.returns({
259+
start: sinon.stub(),
260+
stop: sinon.stub(),
261+
get: sinon.stub().returns(testData.getTestProjectConfigWithFeatures()),
262+
on: sinon.stub().returns(function() {}),
263+
onReady: sinon.stub().returns(Promise.resolve())
264+
});
265+
var manager = new projectConfigManager.ProjectConfigManager({
266+
sdkKey: '12345',
267+
});
268+
return manager.onReady().then(function() {
269+
var onUpdateSpy = sinon.spy();
270+
var unsubscribe = manager.onUpdate(onUpdateSpy);
271+
272+
var fakeDatafileManager = datafileManager.DatafileManager.getCall(0).returnValue;
273+
var updateListener = fakeDatafileManager.on.getCall(0).args[1];
274+
var newDatafile = testData.getTestProjectConfigWithFeatures();
275+
newDatafile.revision = '36';
276+
fakeDatafileManager.get.returns(newDatafile);
277+
updateListener({ datafile: newDatafile });
278+
279+
sinon.assert.calledOnce(onUpdateSpy);
280+
281+
unsubscribe();
282+
283+
newDatafile = testData.getTestProjectConfigWithFeatures();
284+
newDatafile.revision = '37';
285+
fakeDatafileManager.get.returns(newDatafile);
286+
updateListener({ datafile: newDatafile });
287+
// // Should not call onUpdateSpy again since we unsubscribed
288+
updateListener({ datafile: testData.getTestProjectConfigWithFeatures() });
289+
sinon.assert.calledOnce(onUpdateSpy);
290+
});
291+
});
292+
257293
it('rejects its ready promise when the datafile manager emits an invalid datafile', function(done) {
258294
var invalidDatafile = testData.getTestProjectConfig();
259295
delete invalidDatafile['projectId'];

0 commit comments

Comments
 (0)