import test from 'ava'; import sinon from 'sinon'; import { initializeAnalytics } from './analytics'; import { createTestMessageBroker } from './message-broker/test-message-broker'; test('sends analytics event', (t) => { const sendMessage = sinon.fake(); const broker = createTestMessageBroker(sendMessage); const analytics = initializeAnalytics(broker); analytics.track({ type: 'pickup', source: 'pickup-source-with-product', productCategory: 'test-product-1', pageTitle: 'test-page-name', pageType: 'test-page-type', }); analytics.track({ type: 'pickup', source: 'pickup-source-no-product', pageTitle: 'test-page-name', pageType: 'test-page-type', }); analytics.track({ type: 'click', source: 'click-source-with-product', productCategory: 'test-product-2', pageTitle: 'test-page-name', pageType: 'test-page-type', }); analytics.track({ type: 'click', source: 'click-source-no-product', pageTitle: 'test-page-name', pageType: 'test-page-type', }); t.is(sendMessage.callCount, 4); t.truthy( sendMessage.calledWith('analytics-event', { type: 'pickup', source: 'pickup-source-with-product', productCategory: 'test-product-1', pageTitle: 'test-page-name', pageType: 'test-page-type', }) ); t.truthy( sendMessage.calledWith('analytics-event', { type: 'pickup', source: 'pickup-source-no-product', pageTitle: 'test-page-name', pageType: 'test-page-type', }) ); t.truthy( sendMessage.calledWith('analytics-event', { type: 'click', source: 'click-source-with-product', productCategory: 'test-product-2', pageTitle: 'test-page-name', pageType: 'test-page-type', }) ); t.truthy( sendMessage.calledWith('analytics-event', { type: 'click', source: 'click-source-no-product', pageTitle: 'test-page-name', pageType: 'test-page-type', }) ); }); test('ignores unsupported events', (t) => { const sendMessage = sinon.fake(); const broker = createTestMessageBroker(sendMessage); const analytics = initializeAnalytics(broker); analytics.track({ type: 'unsupported', source: 'unsupported-source', }); t.is(sendMessage.callCount, 0); }); test('ignores events without a source', (t) => { const sendMessage = sinon.fake(); const broker = createTestMessageBroker(sendMessage); const analytics = initializeAnalytics(broker); analytics.track({ type: 'click', source: '', }); t.is(sendMessage.callCount, 0); });