Skip to content

Commit e8dbf85

Browse files
authored
Fix Issue/59 syntax error calling custom vendor api without params (#60)
* (refactor) fix error calling api with undefined params * (refactor) validate arguments make it through vendor api * (docs) document custom api method signature
1 parent 19cdf24 commit e8dbf85

File tree

4 files changed

+42
-0
lines changed

4 files changed

+42
-0
lines changed

docs/Guides.md

+6
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ A name of the vendor service to be returned as part of tracking response if defi
3333
An object, a class instance or a function which returns an object which exposes tracking APIs. You don't have to define `pageView` or `track` api, but keep in mind that `react-metrics` will assume those methods to exist for auto page view and declarative tracking and throws when not available.
3434
You can define `name` property in your api, which will be returned as part of tracking response.
3535

36+
Custom `api` methods can take 3 arguments:
37+
38+
```
39+
someMethod(eventType?: string, eventData?: Object, shouldMergeWithDefaultObject?: boolean)
40+
```
41+
3642
Example:
3743

3844
```javascript

src/core/createMetrics.js

+1
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ export class Metrics extends EventEmitter {
165165
*/
166166
_callServices(type, promise) {
167167
return promise.then(params => {
168+
params = params || [];
168169
const results = [];
169170
const services = this.services;
170171
const requestTimeout = this.requestTimeout;

test/core/createMetrics.spec.js

+24
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,30 @@ describe("Metrics", () => {
223223
}, 0);
224224
});
225225

226+
it("allows a custom vendor method without arguments", done => {
227+
const metricsInstance = new Metrics(metricsConfig);
228+
metricsInstance.listen(event => {
229+
expect(event)
230+
.to.have.property("type")
231+
.and.equal("someMethod");
232+
expect(event)
233+
.to.have.property("status")
234+
.and.equal("success");
235+
done();
236+
});
237+
metricsInstance.api.someMethod();
238+
});
239+
240+
it("passes first 2 arguments through custom vendor api methods", done => {
241+
const metricsInstance = new Metrics(metricsConfig);
242+
const stub = sinon.stub(console, "log", (logName, event) => {
243+
expect(logName).to.equal("argumentTest 1 2 undefined");
244+
done();
245+
stub.restore();
246+
});
247+
metricsInstance.api.argumentTest(1, 2, 3);
248+
});
249+
226250
it("should have console log when debug flag is set", done => {
227251
const missingPageDefaultsConfig = Object.assign({}, metricsConfig, {
228252
debug: true

test/metrics.config.js

+11
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,17 @@ export default {
3131
user
3232
});
3333
});
34+
},
35+
someMethod() {
36+
return new Promise(resolve => {
37+
resolve();
38+
});
39+
},
40+
argumentTest(a, b, c) {
41+
console.log(`argumentTest ${a} ${b} ${c}`, b);
42+
return new Promise(resolve => {
43+
resolve();
44+
});
3445
}
3546
}
3647
}

0 commit comments

Comments
 (0)