|
1 |
| -// @ts-ignore |
2 |
| -import * as React from 'react'; |
3 |
| -import { render } from '@testing-library/react-native'; |
4 |
| -import { RNConvertInSDKProvider } from '../../src/context/RNConvertedInSdkProvider'; |
5 |
| -import * as RNConvertedInSdkModule from '../../src/RNConvertedInSdkModule'; |
| 1 | +import { render, waitFor } from '@testing-library/react-native'; |
| 2 | +import React from 'react'; |
| 3 | +import { |
| 4 | + RNConvertInSDK, |
| 5 | + RNConvertInSDKProvider, |
| 6 | +} from '../context/RNConvertedInSdkProvider'; |
| 7 | +import * as RNConvertedInSdkModule from '../RNConvertedInSdkModule'; |
| 8 | +import type { RNConvertInSDKContextType } from '../context/RNConvertedInSdkProvider'; |
6 | 9 |
|
7 |
| -jest.mock('../../src/RNConvertedInSdkModule', () => ({ |
| 10 | +jest.mock('../RNConvertedInSdkModule', () => ({ |
8 | 11 | initializeSDK: jest.fn(),
|
| 12 | + identifyUser: jest.fn(), |
| 13 | + addEvent: jest.fn(), |
| 14 | + viewContentEvent: jest.fn(), |
| 15 | + addToCartEvent: jest.fn(), |
| 16 | + initiateCheckoutEvent: jest.fn(), |
| 17 | + purchaseEvent: jest.fn(), |
| 18 | + registerEvent: jest.fn(), |
9 | 19 | }));
|
10 | 20 |
|
11 | 21 | describe('RNConvertInSDKProvider', () => {
|
12 |
| - it('should throw an error if pixelId or storeUrl is not provided', () => { |
13 |
| - console.error = jest.fn(); // Suppress console.error for this test |
14 |
| - expect(() => |
| 22 | + const mockPixelId = 'test-pixel-id'; |
| 23 | + const mockStoreUrl = 'https://test-store.com'; |
| 24 | + |
| 25 | + beforeEach(() => { |
| 26 | + jest.clearAllMocks(); |
| 27 | + }); |
| 28 | + |
| 29 | + it('initializes SDK on mount', async () => { |
| 30 | + await waitFor(() => { |
| 31 | + render( |
| 32 | + <RNConvertInSDKProvider pixelId={mockPixelId} storeUrl={mockStoreUrl}> |
| 33 | + <></> |
| 34 | + </RNConvertInSDKProvider> |
| 35 | + ); |
| 36 | + }); |
| 37 | + |
| 38 | + expect(RNConvertedInSdkModule.initializeSDK).toHaveBeenCalledWith({ |
| 39 | + pixelId: mockPixelId, |
| 40 | + storeUrl: mockStoreUrl, |
| 41 | + }); |
| 42 | + }); |
| 43 | + |
| 44 | + it('throws error if pixelId or storeUrl is not provided', () => { |
| 45 | + const originalConsoleError = console.error; |
| 46 | + console.error = jest.fn(); |
| 47 | + |
| 48 | + expect(() => { |
15 | 49 | render(
|
16 |
| - <RNConvertInSDKProvider pixelId="" storeUrl=""> |
| 50 | + <RNConvertInSDKProvider pixelId="" storeUrl={mockStoreUrl}> |
17 | 51 | <></>
|
18 | 52 | </RNConvertInSDKProvider>
|
19 |
| - ) |
20 |
| - ).toThrow( |
| 53 | + ); |
| 54 | + }).toThrow( |
21 | 55 | 'Both pixelId and storeUrl must be provided to initialize the SDK'
|
22 | 56 | );
|
23 |
| - }); |
24 | 57 |
|
25 |
| - it('should initialize SDK on mount', () => { |
26 |
| - render( |
27 |
| - <RNConvertInSDKProvider |
28 |
| - pixelId="test-pixel-id" |
29 |
| - storeUrl="https://test-store.com" |
30 |
| - > |
31 |
| - <></> |
32 |
| - </RNConvertInSDKProvider> |
| 58 | + expect(() => { |
| 59 | + render( |
| 60 | + <RNConvertInSDKProvider pixelId={mockPixelId} storeUrl=""> |
| 61 | + <></> |
| 62 | + </RNConvertInSDKProvider> |
| 63 | + ); |
| 64 | + }).toThrow( |
| 65 | + 'Both pixelId and storeUrl must be provided to initialize the SDK' |
33 | 66 | );
|
34 | 67 |
|
35 |
| - expect(RNConvertedInSdkModule.initializeSDK).toHaveBeenCalledWith({ |
36 |
| - pixelId: 'test-pixel-id', |
37 |
| - storeUrl: 'https://test-store.com', |
| 68 | + console.error = originalConsoleError; |
| 69 | + }); |
| 70 | + |
| 71 | + it('provides SDK functions through context', async () => { |
| 72 | + let contextValue: RNConvertInSDKContextType | undefined; |
| 73 | + const TestComponent = () => { |
| 74 | + contextValue = React.useContext(RNConvertInSDK); |
| 75 | + return null; |
| 76 | + }; |
| 77 | + |
| 78 | + await waitFor(() => { |
| 79 | + render( |
| 80 | + <RNConvertInSDKProvider pixelId={mockPixelId} storeUrl={mockStoreUrl}> |
| 81 | + <TestComponent /> |
| 82 | + </RNConvertInSDKProvider> |
| 83 | + ); |
38 | 84 | });
|
| 85 | + |
| 86 | + expect(contextValue).toBeDefined(); |
| 87 | + expect(contextValue?.isInitialized).toBe(true); |
| 88 | + expect(typeof contextValue?.initializeSDK).toBe('function'); |
| 89 | + expect(typeof contextValue?.identifyUser).toBe('function'); |
| 90 | + expect(typeof contextValue?.addEvent).toBe('function'); |
| 91 | + expect(typeof contextValue?.viewContentEvent).toBe('function'); |
| 92 | + expect(typeof contextValue?.addToCartEvent).toBe('function'); |
| 93 | + expect(typeof contextValue?.initiateCheckoutEvent).toBe('function'); |
| 94 | + expect(typeof contextValue?.purchaseEvent).toBe('function'); |
| 95 | + expect(typeof contextValue?.registerEvent).toBe('function'); |
39 | 96 | });
|
40 | 97 | });
|
0 commit comments