|
| 1 | +import { describe, it, expect, vi, beforeEach, afterEach, type Mock } from 'vitest' |
| 2 | + |
| 3 | +vi.mock('@piwikpro/tracking-base-library', () => ({ |
| 4 | + __esModule: true, |
| 5 | + default: { initialize: vi.fn(), getInitScript: vi.fn() }, |
| 6 | + Miscellaneous: { setTrackingSourceProvider: vi.fn() } |
| 7 | +})) |
| 8 | + |
| 9 | +import PiwikProReact from '../index' |
| 10 | +import PiwikProCore, { Miscellaneous } from '@piwikpro/tracking-base-library' |
| 11 | +import { version } from '../../package.json' |
| 12 | + |
| 13 | +beforeEach(() => vi.clearAllMocks()) |
| 14 | +afterEach(() => vi.unstubAllGlobals()) |
| 15 | + |
| 16 | +describe('initialize()', () => { |
| 17 | + it('sets the source provider before delegating to core', () => { |
| 18 | + PiwikProReact.initialize('id', 'https://example.piwik.pro') |
| 19 | + |
| 20 | + expect(Miscellaneous.setTrackingSourceProvider).toHaveBeenCalledWith('react', version) |
| 21 | + expect( |
| 22 | + (Miscellaneous.setTrackingSourceProvider as Mock).mock.invocationCallOrder[0] |
| 23 | + ).toBeLessThan((PiwikProCore.initialize as Mock).mock.invocationCallOrder[0]) |
| 24 | + }) |
| 25 | + |
| 26 | + it('forwards all arguments to core initialize', () => { |
| 27 | + PiwikProReact.initialize('id', 'https://example.piwik.pro', { nonce: 'abc' }) |
| 28 | + |
| 29 | + expect(PiwikProCore.initialize).toHaveBeenCalledWith('id', 'https://example.piwik.pro', { |
| 30 | + nonce: 'abc' |
| 31 | + }) |
| 32 | + }) |
| 33 | + |
| 34 | + it('skips setting source provider during SSR (no window)', () => { |
| 35 | + vi.stubGlobal('window', undefined) |
| 36 | + |
| 37 | + PiwikProReact.initialize('id', 'https://example.piwik.pro') |
| 38 | + |
| 39 | + expect(Miscellaneous.setTrackingSourceProvider).not.toHaveBeenCalled() |
| 40 | + expect(PiwikProCore.initialize).toHaveBeenCalledWith('id', 'https://example.piwik.pro') |
| 41 | + }) |
| 42 | +}) |
0 commit comments