Skip to content
This repository was archived by the owner on Apr 26, 2026. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions src/__tests__/hooks/useIAP.android.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,55 @@ describe('hooks/useIAP Android', () => {
enableBillingProgramAndroid: 'external-offer',
});
});

it('registers userChoiceBillingAndroid listener when callback is provided', async () => {
const mockUserChoiceBillingListener = jest
.spyOn(IAP, 'userChoiceBillingListenerAndroid' as any)
.mockImplementation(() => ({remove: jest.fn()}));

let api: any;
const onUserChoiceBilling = jest.fn();
const Harness = () => {
api = useIAP({
onUserChoiceBillingAndroid: onUserChoiceBilling,
});
return null;
};

await act(async () => {
TestRenderer.create(React.createElement(Harness));
});
await act(async () => {});

expect(api.connected).toBe(true);
expect(mockUserChoiceBillingListener).toHaveBeenCalled();
});

it('reconnect uses Android billing config', async () => {
let api: any;
const Harness = () => {
api = useIAP({
enableBillingProgramAndroid: 'user-choice-billing',
});
return null;
};

await act(async () => {
TestRenderer.create(React.createElement(Harness));
});
await act(async () => {});

(IAP.initConnection as jest.Mock).mockClear();
jest.spyOn(IAP, 'initConnection').mockResolvedValueOnce(true as any);

let result: boolean | undefined;
await act(async () => {
result = await api.reconnect();
});

expect(result).toBe(true);
expect(IAP.initConnection).toHaveBeenCalledWith({
enableBillingProgramAndroid: 'user-choice-billing',
});
});
});
129 changes: 129 additions & 0 deletions src/__tests__/hooks/useIAP.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -373,4 +373,133 @@ describe('hooks/useIAP (renderer)', () => {
expect(initConnectionSpy).toHaveBeenCalled();
});
});

describe('reconnect', () => {
it('reconnects and returns true on success', async () => {
let api: any;
const Harness = () => {
api = useIAP();
return null;
};

await act(async () => {
TestRenderer.create(React.createElement(Harness));
});
await act(async () => {});

// Reset mock to track reconnect call
(IAP.initConnection as jest.Mock).mockClear();
jest.spyOn(IAP, 'initConnection').mockResolvedValue(true as any);

let result: boolean | undefined;
await act(async () => {
result = await api.reconnect();
});

expect(result).toBe(true);
expect(api.connected).toBe(true);
});

it('returns false and sets connected=false when initConnection returns false', async () => {
let api: any;
const Harness = () => {
api = useIAP();
return null;
};

await act(async () => {
TestRenderer.create(React.createElement(Harness));
});
await act(async () => {});

jest.spyOn(IAP, 'initConnection').mockResolvedValueOnce(false as any);

let result: boolean | undefined;
await act(async () => {
result = await api.reconnect();
});

expect(result).toBe(false);
expect(api.connected).toBe(false);
});

it('calls onError and returns false when reconnect fails', async () => {
const reconnectError = new Error('Reconnect failed');

let api: any;
const onError = jest.fn();
const Harness = () => {
api = useIAP({onError});
return null;
};

await act(async () => {
TestRenderer.create(React.createElement(Harness));
});
await act(async () => {});

jest.spyOn(IAP, 'initConnection').mockRejectedValueOnce(reconnectError);

let result: boolean | undefined;
await act(async () => {
result = await api.reconnect();
});

expect(result).toBe(false);
expect(onError).toHaveBeenCalledWith(reconnectError);
});

it('does not throw when reconnect fails without onError', async () => {
let api: any;
const Harness = () => {
api = useIAP();
return null;
};

await act(async () => {
TestRenderer.create(React.createElement(Harness));
});
await act(async () => {});

jest
.spyOn(IAP, 'initConnection')
.mockRejectedValueOnce(new Error('fail'));

let result: boolean | undefined;
await act(async () => {
result = await api.reconnect();
});

expect(result).toBe(false);
});

it('reconnect re-registers listeners after successful reconnection', async () => {
let api: any;
const onPurchaseSuccess = jest.fn();
const Harness = () => {
api = useIAP({onPurchaseSuccess});
return null;
};

await act(async () => {
TestRenderer.create(React.createElement(Harness));
});
await act(async () => {});

// purchaseUpdatedListener should have been called during init
expect(IAP.purchaseUpdatedListener).toHaveBeenCalled();

// Clear and reconnect
(IAP.purchaseUpdatedListener as jest.Mock).mockClear();
jest.spyOn(IAP, 'initConnection').mockResolvedValueOnce(true as any);

await act(async () => {
await api.reconnect();
});

// Listeners are already active from init, so reconnect skips re-registration
// (guarded by !subscriptionsRef.current.purchaseUpdate check)
expect(api.connected).toBe(true);
});
});
});
Loading
Loading