|
| 1 | +// Copyright (c) 2022 Snowplow Analytics Ltd. All rights reserved. |
| 2 | +// |
| 3 | +// This program is licensed to you under the Apache License Version 2.0, |
| 4 | +// and you may not use this file except in compliance with the Apache License Version 2.0. |
| 5 | +// You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0. |
| 6 | +// |
| 7 | +// Unless required by applicable law or agreed to in writing, |
| 8 | +// software distributed under the Apache License Version 2.0 is distributed on an |
| 9 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | +// See the Apache License Version 2.0 for the specific language governing permissions and limitations there under. |
| 11 | + |
| 12 | +import { |
| 13 | + trackWebViewEvent, |
| 14 | +} from '../src'; |
| 15 | + |
| 16 | +describe('iOS interface', () => { |
| 17 | + let windowSpy: any; |
| 18 | + let messageHandler = jest.fn(); |
| 19 | + |
| 20 | + beforeEach(() => { |
| 21 | + windowSpy = jest.spyOn(window, 'window', 'get'); |
| 22 | + windowSpy.mockImplementation(() => ({ |
| 23 | + location: { href: 'http://test.com' }, |
| 24 | + webkit: { |
| 25 | + messageHandlers: { |
| 26 | + snowplowV2: { |
| 27 | + postMessage: messageHandler, |
| 28 | + }, |
| 29 | + }, |
| 30 | + }, |
| 31 | + })); |
| 32 | + }); |
| 33 | + |
| 34 | + afterEach(() => { |
| 35 | + windowSpy.mockRestore(); |
| 36 | + }); |
| 37 | + |
| 38 | + it('tracks a webview primitive event', () => { |
| 39 | + const atomic = { |
| 40 | + eventName: 'pv', |
| 41 | + trackerVersion: 'webview', |
| 42 | + url: 'http://test.com', |
| 43 | + title: 'test title', |
| 44 | + }; |
| 45 | + |
| 46 | + trackWebViewEvent(atomic, null, null, ['ns1', 'ns2']); |
| 47 | + |
| 48 | + expect(messageHandler).toHaveBeenCalledWith({ |
| 49 | + atomicProperties: JSON.stringify(atomic), |
| 50 | + selfDescribingEventData: null, |
| 51 | + entities: null, |
| 52 | + trackers: ['ns1', 'ns2'] |
| 53 | + }); |
| 54 | + }); |
| 55 | + |
| 56 | + it('tracks a webview self-describing event', () => { |
| 57 | + const atomic = { |
| 58 | + eventName: 'ue', |
| 59 | + trackerVersion: 'webview-0.3.0', |
| 60 | + }; |
| 61 | + const event = { |
| 62 | + event: { |
| 63 | + schema: 'schema', |
| 64 | + data: { |
| 65 | + abc: 1, |
| 66 | + }, |
| 67 | + }, |
| 68 | + } |
| 69 | + |
| 70 | + trackWebViewEvent(atomic, event, null, null); |
| 71 | + |
| 72 | + expect(messageHandler).toHaveBeenCalledWith({ |
| 73 | + atomicProperties: JSON.stringify(atomic), |
| 74 | + selfDescribingEventData: JSON.stringify(event), |
| 75 | + entities: null, |
| 76 | + trackers: null |
| 77 | + }); |
| 78 | + }); |
| 79 | + |
| 80 | + it('adds context entities', () => { |
| 81 | + const entity = { |
| 82 | + schema: 'iglu:schema', |
| 83 | + data: { |
| 84 | + abc: 1, |
| 85 | + }, |
| 86 | + }; |
| 87 | + |
| 88 | + trackWebViewEvent({}, null, [entity], null); |
| 89 | + |
| 90 | + expect(messageHandler).toHaveBeenCalledWith({ |
| 91 | + atomicProperties: '{}', |
| 92 | + selfDescribingEventData: null, |
| 93 | + entities: JSON.stringify([entity]), |
| 94 | + trackers: null |
| 95 | + }); |
| 96 | + }); |
| 97 | +}); |
0 commit comments