|
| 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 Wishpond = require('../lib'); |
| 8 | + |
| 9 | +describe('Wishpond', function() { |
| 10 | + var analytics; |
| 11 | + var wishpond; |
| 12 | + var options = { |
| 13 | + siteId: '696791', |
| 14 | + apiKey: '591bb1914bdf' |
| 15 | + }; |
| 16 | + |
| 17 | + beforeEach(function() { |
| 18 | + analytics = new Analytics(); |
| 19 | + wishpond = new Wishpond(options); |
| 20 | + analytics.use(Wishpond); |
| 21 | + analytics.use(tester); |
| 22 | + analytics.add(wishpond); |
| 23 | + }); |
| 24 | + |
| 25 | + afterEach(function() { |
| 26 | + analytics.restore(); |
| 27 | + analytics.reset(); |
| 28 | + // FIXME(ndhoule): window.addEventListener('message', ...) handlers aren't |
| 29 | + // properly being cleaned up in tests and are causing uncaught exceptions. |
| 30 | + // We should capture these events and remove them to prevent uncaught |
| 31 | + // exceptions in the test suite |
| 32 | + // wishpond.reset(); |
| 33 | + // sandbox(); |
| 34 | + }); |
| 35 | + |
| 36 | + it('should have the right settings', function() { |
| 37 | + analytics.compare(Wishpond, integration('Wishpond') |
| 38 | + .global('Wishpond') |
| 39 | + .option('siteId', '') |
| 40 | + .option('apiKey', '')); |
| 41 | + }); |
| 42 | + |
| 43 | + describe('before loading', function() { |
| 44 | + beforeEach(function() { |
| 45 | + analytics.stub(wishpond, 'load'); |
| 46 | + }); |
| 47 | + |
| 48 | + describe('#initialize', function() { |
| 49 | + it('should create the window.Wishpond object', function() { |
| 50 | + analytics.assert(!window.Wishpond); |
| 51 | + analytics.initialize(); |
| 52 | + analytics.assert(window.Wishpond); |
| 53 | + }); |
| 54 | + |
| 55 | + it('should call #load', function() { |
| 56 | + analytics.initialize(); |
| 57 | + analytics.called(wishpond.load); |
| 58 | + }); |
| 59 | + }); |
| 60 | + }); |
| 61 | + |
| 62 | + describe('loading', function() { |
| 63 | + it('should load', function(done) { |
| 64 | + analytics.load(wishpond, done); |
| 65 | + }); |
| 66 | + }); |
| 67 | + |
| 68 | + describe('after loading', function() { |
| 69 | + beforeEach(function(done) { |
| 70 | + analytics.once('ready', done); |
| 71 | + analytics.initialize(); |
| 72 | + }); |
| 73 | + |
| 74 | + describe('#identify', function() { |
| 75 | + beforeEach(function() { |
| 76 | + analytics.stub(window.Wishpond.Tracker, 'identify'); |
| 77 | + }); |
| 78 | + |
| 79 | + it('should send an id', function() { |
| 80 | + analytics.identify('id'); |
| 81 | + analytics.called(window.Wishpond.Tracker.identify, 'id', { id: 'id' }); |
| 82 | + }); |
| 83 | + |
| 84 | + it('should not send only traits', function() { |
| 85 | + analytics.identify({ trait: true }); |
| 86 | + analytics.didNotCall(window.Wishpond.Tracker.identify); |
| 87 | + }); |
| 88 | + |
| 89 | + it('should send an id and traits', function() { |
| 90 | + analytics.identify('id', { trait: true, email: 'blackwidow@shield.gov' }); |
| 91 | + analytics.called(window.Wishpond.Tracker.identify, 'id', { id: 'id', trait: true, email: 'blackwidow@shield.gov' }); |
| 92 | + }); |
| 93 | + |
| 94 | + it('should alias createdAt to created_at', function() { |
| 95 | + var date = new Date(); |
| 96 | + analytics.identify('id', { createdAt: date }); |
| 97 | + analytics.called(window.Wishpond.Tracker.identify, 'id', { |
| 98 | + id: 'id', |
| 99 | + created_at: date |
| 100 | + }); |
| 101 | + }); |
| 102 | + |
| 103 | + it('should alias updatedAt to updated_at', function() { |
| 104 | + var date = new Date(); |
| 105 | + analytics.identify('id', { updatedAt: date }); |
| 106 | + analytics.called(window.Wishpond.Tracker.identify, 'id', { |
| 107 | + id: 'id', |
| 108 | + updated_at: date |
| 109 | + }); |
| 110 | + }); |
| 111 | + |
| 112 | + it('should alias firstName to first_name', function() { |
| 113 | + var name = 'Anderson'; |
| 114 | + analytics.identify('id', { firstName: name }); |
| 115 | + analytics.called(window.Wishpond.Tracker.identify, 'id', { |
| 116 | + id: 'id', |
| 117 | + first_name: name |
| 118 | + }); |
| 119 | + }); |
| 120 | + |
| 121 | + it('should alias lastName to last_name', function() { |
| 122 | + var name = 'Saunders'; |
| 123 | + analytics.identify('id', { lastName: name }); |
| 124 | + analytics.called(window.Wishpond.Tracker.identify, 'id', { |
| 125 | + id: 'id', |
| 126 | + last_name: name |
| 127 | + }); |
| 128 | + }); |
| 129 | + |
| 130 | + it('should alias phoneNumber to phone_number', function() { |
| 131 | + var phone = '778 681 7804'; |
| 132 | + analytics.identify('id', { phoneNumber: phone }); |
| 133 | + analytics.called(window.Wishpond.Tracker.identify, 'id', { |
| 134 | + id: 'id', |
| 135 | + phone_number: phone |
| 136 | + }); |
| 137 | + }); |
| 138 | + |
| 139 | + it('should alias leadScore to lead_score', function() { |
| 140 | + var score = 5; |
| 141 | + analytics.identify('id', { leadScore: score }); |
| 142 | + analytics.called(window.Wishpond.Tracker.identify, 'id', { |
| 143 | + id: 'id', |
| 144 | + lead_score: score |
| 145 | + }); |
| 146 | + }); |
| 147 | + |
| 148 | + it('should not alias notDefault to not_default', function() { |
| 149 | + var value = 'yes'; |
| 150 | + analytics.identify('id', { notDefault: value }); |
| 151 | + analytics.called(window.Wishpond.Tracker.identify, 'id', { |
| 152 | + id: 'id', |
| 153 | + notDefault: value |
| 154 | + }); |
| 155 | + }); |
| 156 | + }); |
| 157 | + |
| 158 | + describe('#track', function() { |
| 159 | + beforeEach(function() { |
| 160 | + analytics.stub(window.Wishpond.Tracker, 'track'); |
| 161 | + }); |
| 162 | + |
| 163 | + it('should send an event', function() { |
| 164 | + analytics.track('event'); |
| 165 | + analytics.called(window.Wishpond.Tracker.track, 'event'); |
| 166 | + }); |
| 167 | + |
| 168 | + it('should send an event and properties', function() { |
| 169 | + analytics.track('event', { property: true }); |
| 170 | + analytics.called(window.Wishpond.Tracker.track, 'event', { property: true }); |
| 171 | + }); |
| 172 | + }); |
| 173 | + }); |
| 174 | +}); |
0 commit comments