|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +var Analytics = require('@segment/analytics.js-core').constructor; |
| 4 | +var integration = require('@segment/analytics.js-integration'); |
| 5 | +var sandbox = require('@segment/clear-env'); |
| 6 | +var tester = require('@segment/analytics.js-integration-tester'); |
| 7 | +var Autosend = require('../lib/'); |
| 8 | + |
| 9 | +describe('Autosend', function() { |
| 10 | + var analytics; |
| 11 | + var autosend; |
| 12 | + var options = { |
| 13 | + appKey: '7677b3b8a09744c9ba8bcef3fac82fe2' |
| 14 | + }; |
| 15 | + |
| 16 | + beforeEach(function() { |
| 17 | + analytics = new Analytics(); |
| 18 | + autosend = new Autosend(options); |
| 19 | + analytics.use(Autosend); |
| 20 | + analytics.use(tester); |
| 21 | + analytics.add(autosend); |
| 22 | + }); |
| 23 | + |
| 24 | + afterEach(function() { |
| 25 | + analytics.restore(); |
| 26 | + analytics.reset(); |
| 27 | + autosend.reset(); |
| 28 | + sandbox(); |
| 29 | + }); |
| 30 | + |
| 31 | + it('should have the right settings', function() { |
| 32 | + analytics.compare(Autosend, integration('Autosend') |
| 33 | + .global('_autosend') |
| 34 | + .option('appKey', '')); |
| 35 | + }); |
| 36 | + |
| 37 | + describe('before loading', function() { |
| 38 | + beforeEach(function() { |
| 39 | + analytics.stub(autosend, 'load'); |
| 40 | + }); |
| 41 | + |
| 42 | + describe('#initialize', function() { |
| 43 | + it('should create window._autosend object', function() { |
| 44 | + analytics.assert(!window._autosend); |
| 45 | + analytics.initialize(); |
| 46 | + analytics.page(); |
| 47 | + analytics.assert(window._autosend); |
| 48 | + }); |
| 49 | + |
| 50 | + it('should call #load', function() { |
| 51 | + analytics.initialize(); |
| 52 | + analytics.page(); |
| 53 | + analytics.called(autosend.load); |
| 54 | + }); |
| 55 | + }); |
| 56 | + }); |
| 57 | + |
| 58 | + describe('loading', function() { |
| 59 | + it('should load', function(done) { |
| 60 | + analytics.load(autosend, done); |
| 61 | + }); |
| 62 | + }); |
| 63 | + |
| 64 | + describe('after loading', function() { |
| 65 | + beforeEach(function(done) { |
| 66 | + analytics.once('ready', done); |
| 67 | + analytics.initialize(); |
| 68 | + analytics.page(); |
| 69 | + }); |
| 70 | + |
| 71 | + describe('#identify', function() { |
| 72 | + beforeEach(function() { |
| 73 | + analytics.stub(window._autosend, 'identify'); |
| 74 | + }); |
| 75 | + |
| 76 | + it('should send an id', function() { |
| 77 | + analytics.identify('id'); |
| 78 | + analytics.called(window._autosend.identify, { id: 'id' }); |
| 79 | + }); |
| 80 | + |
| 81 | + it('shouldnt send without an id', function() { |
| 82 | + analytics.identify({ trait: true }); |
| 83 | + analytics.didNotCall(window._autosend.identify); |
| 84 | + }); |
| 85 | + |
| 86 | + it('should send an id and traits', function() { |
| 87 | + analytics.identify('id', { trait: true }); |
| 88 | + analytics.called(window._autosend.identify, { id: 'id', trait: true }); |
| 89 | + }); |
| 90 | + }); |
| 91 | + |
| 92 | + describe('#track', function() { |
| 93 | + beforeEach(function() { |
| 94 | + analytics.stub(window._autosend, 'track'); |
| 95 | + }); |
| 96 | + |
| 97 | + it('should send an event', function() { |
| 98 | + analytics.track('event'); |
| 99 | + analytics.called(window._autosend.track, 'event'); |
| 100 | + }); |
| 101 | + }); |
| 102 | + }); |
| 103 | +}); |
0 commit comments