Skip to content

Commit b58dd33

Browse files
authored
Merge pull request #42 from launchdarkly/ag/ch2334/warn-against-unknown-custom-even-key
[ch2334] warn against unknown custom event key
2 parents 052984a + 7583077 commit b58dd33

File tree

4 files changed

+97
-2
lines changed

4 files changed

+97
-2
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
npm-debug.log
33
node_modules
44
dist
5+
.idea

src/__tests__/LDClient-test.js

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
var LDClient = require('../index');
21
var semverCompare = require('semver-compare');
32

3+
var LDClient = require('../index');
4+
var messages = require('../messages');
5+
46
describe('LDClient', function() {
57
var xhr;
68
var requests = [];
@@ -131,6 +133,72 @@ describe('LDClient', function() {
131133
}, 1);
132134
});
133135
});
136+
137+
it('should not warn when tracking an known custom goal event', function(done) {
138+
var user = {key: 'user'};
139+
var client = LDClient.initialize('UNKNOWN_ENVIRONMENT_ID', user, {
140+
bootstrap: {} // so the client doesn't request settings
141+
});
142+
143+
var warnSpy = sinon.spy(console, 'warn');
144+
145+
requests[0].respond(
146+
200,
147+
{ 'Content-Type': 'application/json' },
148+
'[{"key": "known", "kind": "custom"}]'
149+
);
150+
151+
client.on('ready', function() {
152+
client.track('known');
153+
expect(warnSpy.calledWith('Custom event key does not exist')).to.be.false;
154+
warnSpy.restore();
155+
done();
156+
});
157+
});
158+
159+
it('should throw when tracking a non-string custom goal event', function(done) {
160+
var user = {key: 'user'};
161+
var client = LDClient.initialize('UNKNOWN_ENVIRONMENT_ID', user, {
162+
bootstrap: {} // so the client doesn't request settings
163+
});
164+
165+
const track = function(key) {
166+
return function() {
167+
client.track(key);
168+
};
169+
};
170+
171+
client.on('ready', function() {
172+
expect(track(123)).to.throw(messages.invalidKey());
173+
expect(track([])).to.throw(messages.invalidKey());
174+
expect(track({})).to.throw(messages.invalidKey());
175+
expect(track(null)).to.throw(messages.invalidKey());
176+
expect(track(undefined)).to.throw(messages.invalidKey());
177+
done();
178+
});
179+
});
180+
181+
it('should warn when tracking an unknown custom goal event', function(done) {
182+
var user = {key: 'user'};
183+
var client = LDClient.initialize('UNKNOWN_ENVIRONMENT_ID', user, {
184+
bootstrap: {} // so the client doesn't request settings
185+
});
186+
187+
var warnSpy = sinon.spy(console, 'warn');
188+
189+
requests[0].respond(
190+
200,
191+
{ 'Content-Type': 'application/json' },
192+
'[{"key": "known", "kind": "custom"}]'
193+
);
194+
195+
client.on('ready', function() {
196+
client.track('unknown');
197+
expect(warnSpy.calledWith(messages.unknownCustomEventKey('unknown'))).to.be.true;
198+
warnSpy.restore();
199+
done();
200+
});
201+
});
134202
});
135203

136204
});

src/index.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ var Stream = require('./Stream');
55
var Requestor = require('./Requestor');
66
var Identity = require('./Identity');
77
var utils = require('./utils');
8+
var messages = require('./messages');
89

910
var flags = {};
1011
var environment;
@@ -113,9 +114,26 @@ function allFlags() {
113114
return results;
114115
}
115116

117+
function customEventExists(key) {
118+
if (!goals || goals.length === 0) { return false; }
119+
120+
for (var i=0 ; i < goals.length ; i++) {
121+
if (goals[i].kind === 'custom' && goals[i].key === key) {
122+
return true;
123+
}
124+
}
125+
126+
return false;
127+
}
128+
116129
function track(key, data) {
117130
if (typeof key !== 'string') {
118-
throw 'Event key must be a string';
131+
throw messages.invalidKey();
132+
}
133+
134+
// Validate key if we have goals
135+
if (!!goals && !customEventExists(key)) {
136+
console.warn(messages.unknownCustomEventKey(key));
119137
}
120138

121139
events.enqueue({

src/messages.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module.exports ={
2+
invalidKey: function() {
3+
return 'Event key must be a string';
4+
},
5+
unknownCustomEventKey: function(key) {
6+
return 'Custom event "' + key + '" does not exist'
7+
}
8+
};

0 commit comments

Comments
 (0)