Skip to content

Commit 5385df1

Browse files
authored
Merge pull request #4 from Scode-Njnjas/test/update-test-code-for-lib
test: update test code for lib and corrrecting step run example app in README
2 parents 5a2d8a3 + 7ad83c0 commit 5385df1

File tree

6 files changed

+107
-154
lines changed

6 files changed

+107
-154
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ DerivedData
2929
*.xcuserstate
3030
project.xcworkspace
3131
example/ios/.xcode.env.local
32+
xcshareddata
3233

3334

3435
# Android/IJ

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,9 @@ Returns an object with the following methods:
8787

8888
Check the `example` folder for a complete implementation example.
8989

90+
> [!NOTE]
91+
> If you encounter an error while installing pods in `example/ios`, please refer to this solution: [https://stackoverflow.com/a/78874710/12355129](https://stackoverflow.com/a/78874710/12355129)
92+
9093
## Development
9194

9295
To develop the SDK locally:
@@ -110,7 +113,7 @@ To develop the SDK locally:
110113
- iOS:
111114

112115
```bash
113-
cd example/ios && pod install && cd ..
116+
cd example/ios && pod install && cd ../..
114117
yarn example ios
115118
```
116119

example/ios/ConvertedInSdkExample.xcworkspace/xcshareddata/swiftpm/Package.resolved

Lines changed: 0 additions & 123 deletions
This file was deleted.
Lines changed: 81 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,97 @@
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';
69

7-
jest.mock('../../src/RNConvertedInSdkModule', () => ({
10+
jest.mock('../RNConvertedInSdkModule', () => ({
811
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(),
919
}));
1020

1121
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(() => {
1549
render(
16-
<RNConvertInSDKProvider pixelId="" storeUrl="">
50+
<RNConvertInSDKProvider pixelId="" storeUrl={mockStoreUrl}>
1751
<></>
1852
</RNConvertInSDKProvider>
19-
)
20-
).toThrow(
53+
);
54+
}).toThrow(
2155
'Both pixelId and storeUrl must be provided to initialize the SDK'
2256
);
23-
});
2457

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'
3366
);
3467

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+
);
3884
});
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');
3996
});
4097
});

src/__tests__/useConvertedInSdk.test.tsx

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// @ts-ignore
22
import * as React from 'react';
3-
import { renderHook } from '@testing-library/react-hooks/native';
3+
import { act, renderHook } from '@testing-library/react-hooks/native';
44
import { RNConvertInSDKProvider } from '../context/RNConvertedInSdkProvider';
55
import { useConvertedIn } from '../hooks/useConvertedInSdk';
66

@@ -16,14 +16,25 @@ jest.mock('../../src/RNConvertedInSdkModule', () => ({
1616
}));
1717

1818
describe('useConvertedIn', () => {
19+
beforeEach(() => {
20+
jest.clearAllMocks();
21+
});
22+
1923
it('should throw an error when used outside of RNConvertInSDKProvider', () => {
24+
const consoleErrorSpy = jest
25+
.spyOn(console, 'error')
26+
.mockImplementation(() => {});
27+
2028
const { result } = renderHook(() => useConvertedIn());
29+
2130
expect(result.error).toEqual(
2231
Error('useConvertedIn must be used within a ConvertedInProvider')
2332
);
33+
34+
consoleErrorSpy.mockRestore();
2435
});
2536

26-
it('should return all SDK functions when used within RNConvertInSDKProvider', () => {
37+
it('should return all SDK functions when used within RNConvertInSDKProvider', async () => {
2738
const wrapper = ({ children }: { children: React.ReactNode }) => (
2839
<RNConvertInSDKProvider
2940
pixelId="test-pixel-id"
@@ -35,6 +46,10 @@ describe('useConvertedIn', () => {
3546

3647
const { result } = renderHook(() => useConvertedIn(), { wrapper });
3748

49+
await act(async () => {
50+
await result.current.initializeSDK();
51+
});
52+
3853
expect(result.current).toHaveProperty('initializeSDK');
3954
expect(result.current).toHaveProperty('identifyUser');
4055
expect(result.current).toHaveProperty('addEvent');

src/context/RNConvertedInSdkProvider.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ interface RNConvertInSDKProps {
1717
storeUrl: string;
1818
}
1919

20-
interface RNConvertInSDKType {
20+
export interface RNConvertInSDKContextType {
2121
isInitialized: boolean;
2222
initializeSDK: () => Promise<void>;
2323
identifyUser: (email: string, countryCode: string, phone: string) => void;
@@ -38,9 +38,9 @@ interface RNConvertInSDKType {
3838
registerEvent: () => void;
3939
}
4040

41-
export const RNConvertInSDK = createContext<RNConvertInSDKType | undefined>(
42-
undefined
43-
);
41+
export const RNConvertInSDK = createContext<
42+
RNConvertInSDKContextType | undefined
43+
>(undefined);
4444

4545
export const RNConvertInSDKProvider: React.FC<RNConvertInSDKProps> = ({
4646
children,

0 commit comments

Comments
 (0)