|
| 1 | +import axios, { AxiosInstance } from 'axios'; |
| 2 | + |
| 3 | +import { testEvent1, testEvent2 } from '../mocks/testEvents'; |
| 4 | +import { SkyAnalyticsService } from './SkyAnalyticsService'; |
| 5 | +import { IMapcodeService } from './MapcodeService'; |
| 6 | + |
| 7 | +let skyAnalyticsService: SkyAnalyticsService; |
| 8 | +let mapcodeService: IMapcodeService; |
| 9 | +let axiosInstanceMock: AxiosInstance; |
| 10 | + |
| 11 | +const testEvent1Transformed = { |
| 12 | + t: 'lift-off', |
| 13 | + Engines: 4, |
| 14 | + Fuel: 78, |
| 15 | + Successful: true, |
| 16 | + Temperature: { |
| 17 | + engine: 80, |
| 18 | + cabin: 31, |
| 19 | + }, |
| 20 | + Timestamp: 1595244264059, |
| 21 | + 'Lat-lon': 'CYZ7V.DYDG', |
| 22 | +}; |
| 23 | + |
| 24 | +const testEvent2Transformed = { |
| 25 | + t: 'landing', |
| 26 | + Engines: 1, |
| 27 | + Fuel: 26, |
| 28 | + Successful: true, |
| 29 | + Temperature: { |
| 30 | + engine: 80, |
| 31 | + cabin: 31, |
| 32 | + }, |
| 33 | + Timestamp: 1595524813145, |
| 34 | + 'Lat-lon': 'V07HV.VS2D', |
| 35 | +}; |
| 36 | + |
| 37 | +beforeEach(() => { |
| 38 | + axiosInstanceMock = { |
| 39 | + request: jest.fn().mockResolvedValue('OK'), |
| 40 | + } as any; |
| 41 | + (axios as any).create = jest.fn(() => axiosInstanceMock); |
| 42 | + mapcodeService = { |
| 43 | + convertLocationToMapcode: jest.fn((input) => { |
| 44 | + switch (input.join(',')) { |
| 45 | + case '-16.270183,168.110748': |
| 46 | + return { international: { mapcode: 'CYZ7V.DYDG' } }; |
| 47 | + case '51.769455,182.81861': |
| 48 | + return { international: { mapcode: 'V07HV.VS2D' } }; |
| 49 | + } |
| 50 | + }), |
| 51 | + } as any; |
| 52 | + |
| 53 | + skyAnalyticsService = new SkyAnalyticsService(mapcodeService); |
| 54 | +}); |
| 55 | + |
| 56 | +describe('SkyAnalyticsService', () => { |
| 57 | + test('should exist', () => { |
| 58 | + expect(skyAnalyticsService).toBeTruthy(); |
| 59 | + }); |
| 60 | + |
| 61 | + test.each([ |
| 62 | + ['event1', testEvent1, testEvent1Transformed], |
| 63 | + ['event2', testEvent2, testEvent2Transformed], |
| 64 | + ])('should send tracking event "%s" properly', async (eventName, input, result) => { |
| 65 | + expect(await skyAnalyticsService.send(input)).toBe('OK'); |
| 66 | + |
| 67 | + expect((axios as any).create).toBeCalledWith({ |
| 68 | + baseURL: skyAnalyticsService.URL, |
| 69 | + }); |
| 70 | + |
| 71 | + expect(axiosInstanceMock.request).toBeCalledWith({ |
| 72 | + method: 'POST', |
| 73 | + data: result, |
| 74 | + }); |
| 75 | + }); |
| 76 | +}); |
0 commit comments