Skip to content

Commit 1aae250

Browse files
committed
test: add tests for event-driven subscription ID/token wait functionality
1 parent d597bae commit 1aae250

File tree

1 file changed

+86
-0
lines changed

1 file changed

+86
-0
lines changed

src/index.test.ts

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,48 @@ describe('OneSignal', () => {
454454
OneSignal.User.pushSubscription.getIdAsync(),
455455
).rejects.toThrow('OneSignal native module not loaded');
456456
});
457+
458+
test('should wait for subscription id using native method', async () => {
459+
// Mock the native wait method to resolve with ID after delay
460+
mockRNOneSignal.waitForPushSubscriptionIdAsync.mockResolvedValue(
461+
PUSH_ID,
462+
);
463+
464+
const result = await OneSignal.User.pushSubscription.getIdAsync({
465+
timeout: 5000,
466+
});
467+
468+
expect(result).toBe(PUSH_ID);
469+
expect(
470+
mockRNOneSignal.waitForPushSubscriptionIdAsync,
471+
).toHaveBeenCalledWith(5000);
472+
});
473+
474+
test('should return null if id not available after timeout', async () => {
475+
mockRNOneSignal.waitForPushSubscriptionIdAsync.mockResolvedValue(null);
476+
477+
const result = await OneSignal.User.pushSubscription.getIdAsync({
478+
timeout: 1000,
479+
});
480+
481+
expect(result).toBeNull();
482+
expect(
483+
mockRNOneSignal.waitForPushSubscriptionIdAsync,
484+
).toHaveBeenCalledWith(1000);
485+
});
486+
487+
test('should use default timeout if not specified', async () => {
488+
mockRNOneSignal.waitForPushSubscriptionIdAsync.mockResolvedValue(
489+
PUSH_ID,
490+
);
491+
492+
const result = await OneSignal.User.pushSubscription.getIdAsync();
493+
494+
expect(result).toBe(PUSH_ID);
495+
expect(
496+
mockRNOneSignal.waitForPushSubscriptionIdAsync,
497+
).toHaveBeenCalledWith(5000); // Default timeout
498+
});
457499
});
458500

459501
describe('getPushSubscriptionToken (deprecated)', () => {
@@ -502,6 +544,50 @@ describe('OneSignal', () => {
502544
OneSignal.User.pushSubscription.getTokenAsync(),
503545
).rejects.toThrow('OneSignal native module not loaded');
504546
});
547+
548+
test('should wait for subscription token using native method', async () => {
549+
// Mock the native wait method to resolve with token
550+
vi.mocked(
551+
mockRNOneSignal.waitForPushSubscriptionTokenAsync,
552+
).mockResolvedValue(PUSH_TOKEN);
553+
554+
const result = await OneSignal.User.pushSubscription.getTokenAsync({
555+
timeout: 5000,
556+
});
557+
558+
expect(result).toBe(PUSH_TOKEN);
559+
expect(
560+
mockRNOneSignal.waitForPushSubscriptionTokenAsync,
561+
).toHaveBeenCalledWith(5000);
562+
});
563+
564+
test('should return null if token not available after timeout', async () => {
565+
vi.mocked(
566+
mockRNOneSignal.waitForPushSubscriptionTokenAsync,
567+
).mockResolvedValue(null);
568+
569+
const result = await OneSignal.User.pushSubscription.getTokenAsync({
570+
timeout: 1000,
571+
});
572+
573+
expect(result).toBeNull();
574+
expect(
575+
mockRNOneSignal.waitForPushSubscriptionTokenAsync,
576+
).toHaveBeenCalledWith(1000);
577+
});
578+
579+
test('should use default timeout if not specified', async () => {
580+
vi.mocked(
581+
mockRNOneSignal.waitForPushSubscriptionTokenAsync,
582+
).mockResolvedValue(PUSH_TOKEN);
583+
584+
const result = await OneSignal.User.pushSubscription.getTokenAsync();
585+
586+
expect(result).toBe(PUSH_TOKEN);
587+
expect(
588+
mockRNOneSignal.waitForPushSubscriptionTokenAsync,
589+
).toHaveBeenCalledWith(5000); // Default timeout
590+
});
505591
});
506592

507593
describe('getOptedIn (deprecated)', () => {

0 commit comments

Comments
 (0)