From e8dbf854a7ce423a8fda457d19ce244ca9248870 Mon Sep 17 00:00:00 2001 From: Christian Shum-Harden Date: Fri, 16 Mar 2018 10:43:07 -0700 Subject: [PATCH] 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 --- docs/Guides.md | 6 ++++++ src/core/createMetrics.js | 1 + test/core/createMetrics.spec.js | 24 ++++++++++++++++++++++++ test/metrics.config.js | 11 +++++++++++ 4 files changed, 42 insertions(+) diff --git a/docs/Guides.md b/docs/Guides.md index 47499d7..99eb0a5 100644 --- a/docs/Guides.md +++ b/docs/Guides.md @@ -33,6 +33,12 @@ A name of the vendor service to be returned as part of tracking response if defi 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. You can define `name` property in your api, which will be returned as part of tracking response. +Custom `api` methods can take 3 arguments: + +``` +someMethod(eventType?: string, eventData?: Object, shouldMergeWithDefaultObject?: boolean) +``` + Example: ```javascript diff --git a/src/core/createMetrics.js b/src/core/createMetrics.js index c430813..eabbc0c 100644 --- a/src/core/createMetrics.js +++ b/src/core/createMetrics.js @@ -165,6 +165,7 @@ export class Metrics extends EventEmitter { */ _callServices(type, promise) { return promise.then(params => { + params = params || []; const results = []; const services = this.services; const requestTimeout = this.requestTimeout; diff --git a/test/core/createMetrics.spec.js b/test/core/createMetrics.spec.js index 53e83ec..e7a7958 100644 --- a/test/core/createMetrics.spec.js +++ b/test/core/createMetrics.spec.js @@ -223,6 +223,30 @@ describe("Metrics", () => { }, 0); }); + it("allows a custom vendor method without arguments", done => { + const metricsInstance = new Metrics(metricsConfig); + metricsInstance.listen(event => { + expect(event) + .to.have.property("type") + .and.equal("someMethod"); + expect(event) + .to.have.property("status") + .and.equal("success"); + done(); + }); + metricsInstance.api.someMethod(); + }); + + it("passes first 2 arguments through custom vendor api methods", done => { + const metricsInstance = new Metrics(metricsConfig); + const stub = sinon.stub(console, "log", (logName, event) => { + expect(logName).to.equal("argumentTest 1 2 undefined"); + done(); + stub.restore(); + }); + metricsInstance.api.argumentTest(1, 2, 3); + }); + it("should have console log when debug flag is set", done => { const missingPageDefaultsConfig = Object.assign({}, metricsConfig, { debug: true diff --git a/test/metrics.config.js b/test/metrics.config.js index c63c406..3272a1a 100644 --- a/test/metrics.config.js +++ b/test/metrics.config.js @@ -31,6 +31,17 @@ export default { user }); }); + }, + someMethod() { + return new Promise(resolve => { + resolve(); + }); + }, + argumentTest(a, b, c) { + console.log(`argumentTest ${a} ${b} ${c}`, b); + return new Promise(resolve => { + resolve(); + }); } } }