import cookie from 'js-cookie' import { Analytics } from '../../../core/analytics' import { SegmentioSettings } from '../../segmentio' import { ddFormTracking } from '..' import { waitFor } from '@testing-library/dom' const DRIFT_PAYLOAD = { data: { email: 'john.doe@example.com', }, } describe('Drift Plugin', () => { let analytics: Analytics let settings: SegmentioSettings let identifyMock: jest.SpyInstance let trackMock: jest.SpyInstance beforeEach(() => { jest.resetAllMocks() jest.restoreAllMocks() settings = { apiKey: 'foo' } analytics = new Analytics({ writeKey: settings.apiKey }) identifyMock = jest.spyOn(analytics, 'identify') trackMock = jest.spyOn(analytics, 'track') window.drift = { on: async (_, callback) => { await callback(DRIFT_PAYLOAD) }, } }) afterEach(() => { analytics.reset() Object.keys(cookie.get()).map((k) => cookie.remove(k)) window.localStorage.clear() window.drift = undefined }) it('should track and identify on event', async () => { await analytics.register( ddFormTracking({ drift: { eventName: 'my-track-name' } }) ) expect(identifyMock.mock.calls).toHaveLength(1) expect(identifyMock.mock.calls[0][1].email).toBe('john.doe@example.com') await waitFor(() => expect(trackMock.mock.calls).toHaveLength(1)) expect(trackMock.mock.calls[0][0]).toBe('my-track-name') }) })