From 762bedc5717972455fd8a93f5f392817fadec258 Mon Sep 17 00:00:00 2001 From: Chris Laubacher Date: Tue, 13 Sep 2016 17:02:36 -0700 Subject: [PATCH] Add tests and clean up dtm adapter --- addon/metrics-adapters/dynamic-tag-manager.js | 37 +++++------------- addon/metrics-adapters/google-analytics.js | 1 - .../dynamic-tag-manager-test.js | 38 +++++++++++++++++-- 3 files changed, 43 insertions(+), 33 deletions(-) diff --git a/addon/metrics-adapters/dynamic-tag-manager.js b/addon/metrics-adapters/dynamic-tag-manager.js index 2c92058b..f5aa0385 100644 --- a/addon/metrics-adapters/dynamic-tag-manager.js +++ b/addon/metrics-adapters/dynamic-tag-manager.js @@ -6,29 +6,27 @@ import BaseAdapter from './base'; const { assert, get, - set + set, + A: emberArray } = Ember; const { compact } = objectTransforms; export default BaseAdapter.extend({ - - eventQueue: [], - toStringExtension() { return 'DynamicTagManager'; }, - init () { + init() { const config = get(this, 'config'); const { src } = config; assert(`[ember-metrics] You must pass a valid \`src\` to the ${this.toString()} adapter`, src); set(this, 'dtmSrc', src); - set(this, 'dataLayerNameString', config['dataLayerName'] || 'dtmDataLayer'); - window[get(this, 'dataLayerNameString')] = {}; + let dataLayerNameString = set(this, 'dataLayerNameString', config['dataLayerName'] || 'dtmDataLayer'); + window[dataLayerNameString] = {}; if (canUseDOM) { /* jshint ignore:start */ @@ -39,7 +37,7 @@ export default BaseAdapter.extend({ /* jshint ignore:end */ } - this.checkForQueue(); + this.eventQueue = emberArray(); }, pushAndCheck(dtmEvent) { @@ -48,7 +46,6 @@ export default BaseAdapter.extend({ }, checkForQueue() { - if (this.get('eventQueue').length) { if (this.isSatelliteDefined()) { this.sendEvents(); @@ -70,27 +67,12 @@ export default BaseAdapter.extend({ this.set('eventQueue', []); }, - dtmObject(compactedOptions) { - - const dtmEvent = {'event': compactedOptions['event']}; - delete compactedOptions['event']; - for (let key in compactedOptions) { - dtmEvent[`${key}`] = compactedOptions[key]; - } - return dtmEvent; - - }, - isSatelliteDefined() { - if (typeof(window._satellite) === "object") { - return true; - } else { - return false; - } + return typeof(window._satellite) === "object"; }, trackEvent(options = {}) { - const dtmEvent = this.dtmObject(compact(options)); + const dtmEvent = compact(options); this.pushAndCheck(dtmEvent); return dtmEvent; }, @@ -102,7 +84,7 @@ export default BaseAdapter.extend({ }, trackPage(options = {}) { - const dtmEvent = this.dtmObject(compact(options)); + const dtmEvent = compact(options); dtmEvent['event'] = 'vpv'; this.pushAndCheck(dtmEvent); return dtmEvent; @@ -112,5 +94,4 @@ export default BaseAdapter.extend({ delete window['_satellite']; delete window[get(this, 'dataLayerNameString')]; } - }); diff --git a/addon/metrics-adapters/google-analytics.js b/addon/metrics-adapters/google-analytics.js index e20efd9f..364a8049 100644 --- a/addon/metrics-adapters/google-analytics.js +++ b/addon/metrics-adapters/google-analytics.js @@ -21,7 +21,6 @@ export default BaseAdapter.extend({ init() { const config = copy(get(this, 'config')); - console.log(config); const { id } = config; assert(`[ember-metrics] You must pass a valid \`id\` to the ${this.toString()} adapter`, id); diff --git a/tests/unit/metrics-adapters/dynamic-tag-manager-test.js b/tests/unit/metrics-adapters/dynamic-tag-manager-test.js index 13ff915e..39877a10 100644 --- a/tests/unit/metrics-adapters/dynamic-tag-manager-test.js +++ b/tests/unit/metrics-adapters/dynamic-tag-manager-test.js @@ -8,11 +8,41 @@ moduleFor('ember-metrics@metrics-adapter:dynamic-tag-manager', 'dynamic-tag-mana config = { src: '//path.com/to.src.js' }; + window._satellite = { track() {} }; + }, + afterEach() { + sandbox.restore(); } }); -// Replace this with your real tests. -test('it exists', function(assert) { - var adapter = this.subject({ config }); - assert.ok(adapter); +test('#trackEvent calls `_satellite.track`', function(assert) { + const adapter = this.subject({ config }); + const stub = sandbox.stub(window._satellite, 'track'); + + const event = 'click-button'; + const eventData = { + event, + category: 'button', + action: 'click', + label: 'nav buttons', + value: 4 + }; + + const result = adapter.trackEvent(eventData); + + assert.ok(stub.calledWith(event), 'it sends the right argument'); + assert.deepEqual(result, eventData, 'it returns the right response'); +}); + +test('#trackPage calls `_satellite.track`', function(assert) { + const adapter = this.subject({ config }); + const stub = sandbox.stub(window._satellite, 'track'); + + const event = 'vpv'; + const eventData = { event }; + + const result = adapter.trackPage(); + + assert.ok(stub.calledWith(event), 'it sends the right argument'); + assert.deepEqual(result, eventData, 'it returns the right response'); });