|
| 1 | +/** |
| 2 | + * Copyright 2023, Optimizely |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +import sendBeaconDispatcher, { Event } from '../lib/plugins/event_dispatcher/send_beacon_dispatcher'; |
| 17 | +import { anyString, anything, capture, instance, mock, reset, when } from 'ts-mockito'; |
| 18 | + |
| 19 | +describe('dispatchEvent', function() { |
| 20 | + const mockNavigator = mock<Navigator>(); |
| 21 | + |
| 22 | + afterEach(function() { |
| 23 | + reset(mockNavigator); |
| 24 | + }); |
| 25 | + |
| 26 | + it('should call sendBeacon with correct url, data and type', async () => { |
| 27 | + var eventParams = { testParam: 'testParamValue' }; |
| 28 | + var eventObj: Event = { |
| 29 | + url: 'https://cdn.com/event', |
| 30 | + httpVerb: 'POST', |
| 31 | + params: eventParams, |
| 32 | + }; |
| 33 | + |
| 34 | + when(mockNavigator.sendBeacon(anyString(), anything())).thenReturn(true); |
| 35 | + const navigator = instance(mockNavigator); |
| 36 | + if (!global.navigator) { |
| 37 | + global.navigator = navigator; |
| 38 | + } |
| 39 | + |
| 40 | + global.navigator.sendBeacon = navigator.sendBeacon; |
| 41 | + |
| 42 | + sendBeaconDispatcher.dispatchEvent(eventObj, () => {}); |
| 43 | + |
| 44 | + const [url, data] = capture(mockNavigator.sendBeacon).last(); |
| 45 | + const blob = data as Blob; |
| 46 | + |
| 47 | + const reader = new FileReader(); |
| 48 | + reader.readAsBinaryString(blob); |
| 49 | + |
| 50 | + const sentParams = await new Promise((resolve) => { |
| 51 | + reader.onload = () => {`` |
| 52 | + resolve(reader.result); |
| 53 | + }; |
| 54 | + }); |
| 55 | + |
| 56 | + |
| 57 | + expect(url).toEqual(eventObj.url); |
| 58 | + expect(blob.type).toEqual('application/json'); |
| 59 | + expect(sentParams).toEqual(JSON.stringify(eventObj.params)); |
| 60 | + }); |
| 61 | + |
| 62 | + it('should call call callback with status 200 on sendBeacon success', (done) => { |
| 63 | + var eventParams = { testParam: 'testParamValue' }; |
| 64 | + var eventObj: Event = { |
| 65 | + url: 'https://cdn.com/event', |
| 66 | + httpVerb: 'POST', |
| 67 | + params: eventParams, |
| 68 | + }; |
| 69 | + |
| 70 | + when(mockNavigator.sendBeacon(anyString(), anything())).thenReturn(true); |
| 71 | + const navigator = instance(mockNavigator); |
| 72 | + global.navigator.sendBeacon = navigator.sendBeacon; |
| 73 | + |
| 74 | + sendBeaconDispatcher.dispatchEvent(eventObj, (res: { statusCode: number }) => { |
| 75 | + try { |
| 76 | + expect(res.statusCode).toEqual(200); |
| 77 | + done(); |
| 78 | + } catch(err) { |
| 79 | + done(err); |
| 80 | + } |
| 81 | + }); |
| 82 | + }); |
| 83 | + |
| 84 | + it('should call call callback with status 200 on sendBeacon failure', (done) => { |
| 85 | + var eventParams = { testParam: 'testParamValue' }; |
| 86 | + var eventObj: Event = { |
| 87 | + url: 'https://cdn.com/event', |
| 88 | + httpVerb: 'POST', |
| 89 | + params: eventParams, |
| 90 | + }; |
| 91 | + |
| 92 | + when(mockNavigator.sendBeacon(anyString(), anything())).thenReturn(false); |
| 93 | + const navigator = instance(mockNavigator); |
| 94 | + global.navigator.sendBeacon = navigator.sendBeacon; |
| 95 | + |
| 96 | + sendBeaconDispatcher.dispatchEvent(eventObj, (res: { statusCode: number }) => { |
| 97 | + try { |
| 98 | + expect(res.statusCode).toEqual(500); |
| 99 | + done(); |
| 100 | + } catch(err) { |
| 101 | + done(err); |
| 102 | + } |
| 103 | + }); |
| 104 | + }); |
| 105 | +}); |
0 commit comments