|
| 1 | +import * as makeConsoleMock from 'consolemock'; |
1 | 2 | import GoogleTagManager from '../'; |
2 | 3 |
|
| 4 | +beforeAll(() => { |
| 5 | + console = makeConsoleMock(console); |
| 6 | +}); |
| 7 | + |
3 | 8 | beforeEach(() => { |
4 | 9 | window.dataLayer = undefined; |
5 | 10 | window.iAmADataLayer = undefined; |
6 | 11 | }); |
7 | 12 |
|
| 13 | +/* tslint:disable: no-console */ |
| 14 | +afterEach(() => { |
| 15 | + console.clearHistory(); |
| 16 | +}); |
| 17 | + |
8 | 18 | describe('GoogleTagManager({...options})(events)', () => { |
9 | 19 | describe('When given an array of events', () => { |
10 | 20 | it('pushes those events to the data layer', () => { |
@@ -70,23 +80,36 @@ describe('GoogleTagManager({...options})(events)', () => { |
70 | 80 | }); |
71 | 81 |
|
72 | 82 | describe('When default dataLayer is not defined', () => { |
73 | | - it('should throw an error informing the user.', () => { |
| 83 | + it('does not throw an error', () => { |
| 84 | + window.dataLayer = undefined; |
| 85 | + |
| 86 | + expect(() => GoogleTagManager()).not.toThrow(); |
| 87 | + }); |
| 88 | + it('does nothing when events are pushed to the target', () => { |
| 89 | + window.dataLayer = undefined; |
| 90 | + |
74 | 91 | const events = [{ hitType: 'pageview' }]; |
75 | | - expect(() => GoogleTagManager()(events)).toThrow( |
76 | | - 'window.dataLayer is not defined. Have you forgotten to include Google Tag Manager and dataLayer?' |
77 | | - ); |
| 92 | + const target = GoogleTagManager(); |
| 93 | + |
| 94 | + expect(() => target(events)).not.toThrow(); |
| 95 | + }); |
| 96 | + it('logs an error informing the developer that no events are being tracked', () => { |
| 97 | + window.dataLayer = undefined; |
| 98 | + |
| 99 | + const events = [{ hitType: 'pageview' }]; |
| 100 | + GoogleTagManager()(events); |
| 101 | + |
| 102 | + expect(console.printHistory()).toMatchSnapshot(); |
78 | 103 | }); |
79 | 104 | }); |
80 | 105 |
|
81 | 106 | describe('When iAmADataLayer custom named dataLayer is not defined', () => { |
82 | | - it('should throw an error informing the user.', () => { |
| 107 | + it('should log a warning to console informing the user.', () => { |
83 | 108 | const options = { |
84 | 109 | dataLayerName: 'iAmADataLayer', |
85 | 110 | }; |
86 | 111 | const events = [{ hitType: 'pageview' }]; |
87 | | - expect(() => GoogleTagManager(options)(events)).toThrow( |
88 | | - 'window.iAmADataLayer is not defined. Have you forgotten to include Google Tag Manager and dataLayer?' |
89 | | - ); |
| 112 | + expect(console.printHistory()).toMatchSnapshot(); |
90 | 113 | }); |
91 | 114 | }); |
92 | 115 | }); |
0 commit comments