|
| 1 | +import { |
| 2 | + subscribeToPushNotifications, |
| 3 | + unsubscribeFromPushNotifications, |
| 4 | +} from '../push-notifications'; |
| 5 | +import {createFakeAndroidPostMessage} from './fake-post-message'; |
| 6 | + |
| 7 | +test('subscribeToPushNotifications - success', async () => { |
| 8 | + createFakeAndroidPostMessage({ |
| 9 | + checkMessage: (msg) => { |
| 10 | + expect(msg.type).toBe('SUBSCRIBE_TO_PUSH_NOTIFICATIONS'); |
| 11 | + expect(msg.payload).toBeUndefined(); |
| 12 | + }, |
| 13 | + getResponse: (msg) => ({ |
| 14 | + type: 'SUBSCRIBE_TO_PUSH_NOTIFICATIONS', |
| 15 | + id: msg.id, |
| 16 | + }), |
| 17 | + }); |
| 18 | + |
| 19 | + const res = await subscribeToPushNotifications(); |
| 20 | + expect(res).toBeUndefined(); |
| 21 | +}); |
| 22 | + |
| 23 | +test('subscribeToPushNotifications - missing permission error', async () => { |
| 24 | + createFakeAndroidPostMessage({ |
| 25 | + getResponse: (msg) => ({ |
| 26 | + type: 'ERROR', |
| 27 | + id: msg.id, |
| 28 | + payload: { |
| 29 | + code: 401, |
| 30 | + reason: 'Missing Push notifications permission', |
| 31 | + }, |
| 32 | + }), |
| 33 | + }); |
| 34 | + |
| 35 | + await expect(subscribeToPushNotifications()).rejects.toEqual({ |
| 36 | + code: 401, |
| 37 | + reason: 'Missing Push notifications permission', |
| 38 | + }); |
| 39 | +}); |
| 40 | + |
| 41 | +test('subscribeToPushNotifications - generic error', async () => { |
| 42 | + createFakeAndroidPostMessage({ |
| 43 | + getResponse: (msg) => ({ |
| 44 | + type: 'ERROR', |
| 45 | + id: msg.id, |
| 46 | + payload: {code: 500, reason: 'Internal error'}, |
| 47 | + }), |
| 48 | + }); |
| 49 | + |
| 50 | + await expect(subscribeToPushNotifications()).rejects.toEqual({ |
| 51 | + code: 500, |
| 52 | + reason: 'Internal error', |
| 53 | + }); |
| 54 | +}); |
| 55 | + |
| 56 | +test('unsubscribeFromPushNotifications - success', async () => { |
| 57 | + createFakeAndroidPostMessage({ |
| 58 | + checkMessage: (msg) => { |
| 59 | + expect(msg.type).toBe('UNSUBSCRIBE_FROM_PUSH_NOTIFICATIONS'); |
| 60 | + expect(msg.payload).toBeUndefined(); |
| 61 | + }, |
| 62 | + getResponse: (msg) => ({ |
| 63 | + type: 'UNSUBSCRIBE_FROM_PUSH_NOTIFICATIONS', |
| 64 | + id: msg.id, |
| 65 | + }), |
| 66 | + }); |
| 67 | + |
| 68 | + const res = await unsubscribeFromPushNotifications(); |
| 69 | + expect(res).toBeUndefined(); |
| 70 | +}); |
| 71 | + |
| 72 | +test('unsubscribeFromPushNotifications - generic error', async () => { |
| 73 | + createFakeAndroidPostMessage({ |
| 74 | + getResponse: (msg) => ({ |
| 75 | + type: 'ERROR', |
| 76 | + id: msg.id, |
| 77 | + payload: {code: 500, reason: 'Internal error'}, |
| 78 | + }), |
| 79 | + }); |
| 80 | + |
| 81 | + await expect(unsubscribeFromPushNotifications()).rejects.toEqual({ |
| 82 | + code: 500, |
| 83 | + reason: 'Internal error', |
| 84 | + }); |
| 85 | +}); |
0 commit comments