|
2 | 2 | import { Context } from '@/core/context'
|
3 | 3 | import { Plugin } from '@/core/plugin'
|
4 | 4 | import { JSDOM } from 'jsdom'
|
5 |
| -import { Analytics } from '../../core/analytics' |
| 5 | +import { Analytics, InitOptions } from '../../core/analytics' |
6 | 6 | import { LegacyDestination } from '../../plugins/ajs-destination'
|
7 | 7 | import { PersistedPriorityQueue } from '../../lib/priority-queue/persisted'
|
8 | 8 | // @ts-ignore loadLegacySettings mocked dependency is accused as unused
|
@@ -998,3 +998,137 @@ describe('.Integrations', () => {
|
998 | 998 | `)
|
999 | 999 | })
|
1000 | 1000 | })
|
| 1001 | + |
| 1002 | +describe('Options', () => { |
| 1003 | + beforeEach(async () => { |
| 1004 | + jest.restoreAllMocks() |
| 1005 | + jest.resetAllMocks() |
| 1006 | + |
| 1007 | + const html = ` |
| 1008 | + <!DOCTYPE html> |
| 1009 | + <head> |
| 1010 | + <script>'hi'</script> |
| 1011 | + </head> |
| 1012 | + <body> |
| 1013 | + </body> |
| 1014 | + </html> |
| 1015 | + `.trim() |
| 1016 | + |
| 1017 | + const jsd = new JSDOM(html, { |
| 1018 | + runScripts: 'dangerously', |
| 1019 | + resources: 'usable', |
| 1020 | + url: 'https://localhost', |
| 1021 | + }) |
| 1022 | + |
| 1023 | + const windowSpy = jest.spyOn(global, 'window', 'get') |
| 1024 | + windowSpy.mockImplementation( |
| 1025 | + () => jsd.window as unknown as Window & typeof globalThis |
| 1026 | + ) |
| 1027 | + }) |
| 1028 | + |
| 1029 | + describe('disableAutoISOConversion', () => { |
| 1030 | + it('converts iso strings to dates be default', async () => { |
| 1031 | + const [analytics] = await AnalyticsBrowser.load({ |
| 1032 | + writeKey, |
| 1033 | + }) |
| 1034 | + |
| 1035 | + const amplitude = new LegacyDestination( |
| 1036 | + 'amplitude', |
| 1037 | + 'latest', |
| 1038 | + { |
| 1039 | + apiKey: AMPLITUDE_WRITEKEY, |
| 1040 | + }, |
| 1041 | + {} |
| 1042 | + ) |
| 1043 | + |
| 1044 | + await analytics.register(amplitude) |
| 1045 | + await amplitude.ready() |
| 1046 | + |
| 1047 | + const integrationMock = jest.spyOn(amplitude.integration!, 'track') |
| 1048 | + await analytics.track('Hello!', { |
| 1049 | + date: new Date(), |
| 1050 | + iso: '2020-10-10', |
| 1051 | + }) |
| 1052 | + |
| 1053 | + const [integrationEvent] = integrationMock.mock.lastCall |
| 1054 | + |
| 1055 | + expect(integrationEvent.properties()).toEqual({ |
| 1056 | + date: expect.any(Date), |
| 1057 | + iso: expect.any(Date), |
| 1058 | + }) |
| 1059 | + expect(integrationEvent.timestamp()).toBeInstanceOf(Date) |
| 1060 | + }) |
| 1061 | + |
| 1062 | + it('converts iso strings to dates be default', async () => { |
| 1063 | + const initOptions: InitOptions = { disableAutoISOConversion: false } |
| 1064 | + const [analytics] = await AnalyticsBrowser.load( |
| 1065 | + { |
| 1066 | + writeKey, |
| 1067 | + }, |
| 1068 | + initOptions |
| 1069 | + ) |
| 1070 | + |
| 1071 | + const amplitude = new LegacyDestination( |
| 1072 | + 'amplitude', |
| 1073 | + 'latest', |
| 1074 | + { |
| 1075 | + apiKey: AMPLITUDE_WRITEKEY, |
| 1076 | + }, |
| 1077 | + initOptions |
| 1078 | + ) |
| 1079 | + |
| 1080 | + await analytics.register(amplitude) |
| 1081 | + await amplitude.ready() |
| 1082 | + |
| 1083 | + const integrationMock = jest.spyOn(amplitude.integration!, 'track') |
| 1084 | + await analytics.track('Hello!', { |
| 1085 | + date: new Date(), |
| 1086 | + iso: '2020-10-10', |
| 1087 | + }) |
| 1088 | + |
| 1089 | + const [integrationEvent] = integrationMock.mock.lastCall |
| 1090 | + |
| 1091 | + expect(integrationEvent.properties()).toEqual({ |
| 1092 | + date: expect.any(Date), |
| 1093 | + iso: expect.any(Date), |
| 1094 | + }) |
| 1095 | + expect(integrationEvent.timestamp()).toBeInstanceOf(Date) |
| 1096 | + }) |
| 1097 | + |
| 1098 | + it('does not convert iso strings to dates when `true`', async () => { |
| 1099 | + const initOptions: InitOptions = { disableAutoISOConversion: true } |
| 1100 | + const [analytics] = await AnalyticsBrowser.load( |
| 1101 | + { |
| 1102 | + writeKey, |
| 1103 | + }, |
| 1104 | + initOptions |
| 1105 | + ) |
| 1106 | + |
| 1107 | + const amplitude = new LegacyDestination( |
| 1108 | + 'amplitude', |
| 1109 | + 'latest', |
| 1110 | + { |
| 1111 | + apiKey: AMPLITUDE_WRITEKEY, |
| 1112 | + }, |
| 1113 | + initOptions |
| 1114 | + ) |
| 1115 | + |
| 1116 | + await analytics.register(amplitude) |
| 1117 | + await amplitude.ready() |
| 1118 | + |
| 1119 | + const integrationMock = jest.spyOn(amplitude.integration!, 'track') |
| 1120 | + await analytics.track('Hello!', { |
| 1121 | + date: new Date(), |
| 1122 | + iso: '2020-10-10', |
| 1123 | + }) |
| 1124 | + |
| 1125 | + const [integrationEvent] = integrationMock.mock.lastCall |
| 1126 | + |
| 1127 | + expect(integrationEvent.properties()).toEqual({ |
| 1128 | + date: expect.any(Date), |
| 1129 | + iso: '2020-10-10', |
| 1130 | + }) |
| 1131 | + expect(integrationEvent.timestamp()).toBeInstanceOf(Date) |
| 1132 | + }) |
| 1133 | + }) |
| 1134 | +}) |
0 commit comments