import { render, waitFor } from '@testing-library/react'; import { vi } from 'vitest'; import { withAudit } from '~/tests/utils'; import Pixel, { type PixelProps } from './index'; describe('', () => { it('should immediatly send selected event', async () => { const sendEventMock = vi.fn(); render(withAudit(, { lib: { sendEvent: sendEventMock, }, })); await waitFor(() => expect(sendEventMock).toHaveBeenCalled()); }); it('should not resend event if cookies_enabled change but ' + 'reuse is false', async () => { const sendEventMock = vi.fn(); const config = { cookies_enabled: false }; render(withAudit(, { lib: { sendEvent: sendEventMock }, config, })); expect(sendEventMock).toHaveBeenCalledTimes(1); config.cookies_enabled = true; await waitFor(() => expect(sendEventMock).toHaveBeenCalledTimes(1)); }); it('should resend event if cookies_enabled change and ' + 'reuse is truthy', async () => { const sendEventMock = vi.fn(); const config = { cookies_enabled: false, }; render(withAudit(, { lib: { sendEvent: sendEventMock }, config, })); expect(sendEventMock).toHaveBeenCalledTimes(1); config.cookies_enabled = true; await waitFor(() => expect(sendEventMock).toHaveBeenCalledTimes(2)); }); it('should send page-view with data if its asked for', async () => { const sendEventMock = vi.fn(); const data: PixelProps['data'] = { type: 'premium' }; render(withAudit( , { lib: { sendEvent: sendEventMock, }, })); await waitFor(() => expect(sendEventMock) .toHaveBeenCalledWith('page-view', data, undefined)); }); it('should send options if its asked for', async () => { const sendEventMock = vi.fn(); const options = { beacon: true }; render(withAudit( , { lib: { sendEvent: sendEventMock, }, })); await waitFor(() => expect(sendEventMock) .toHaveBeenCalledWith('page-view', undefined, options)); }); });