Skip to content

Commit

Permalink
[test-suite] Add token emitting test to NewNotifications
Browse files Browse the repository at this point in the history
  • Loading branch information
sjchmiela committed Jan 14, 2020
1 parent 7ba9597 commit d76791e
Showing 1 changed file with 29 additions and 2 deletions.
31 changes: 29 additions & 2 deletions apps/test-suite/tests/NewNotifications.js
Expand Up @@ -4,6 +4,7 @@ import { Platform } from '@unimodules/core';
import * as Notifications from 'expo-notifications';

import * as TestUtils from '../TestUtils';
import { waitFor } from './helpers';

export const name = 'expo-notifications';

Expand All @@ -13,19 +14,45 @@ export async function test(t) {

describeWithPermissions('expo-notifications', () => {
t.describe('getDevicePushTokenAsync', () => {
let subscription = null;
let tokenFromEvent = null;
let tokenFromMethodCall = null;

t.beforeAll(() => {
subscription = Notifications.addTokenListener(newEvent => {
tokenFromEvent = newEvent;
});
});

t.afterAll(() => {
if (subscription) {
subscription.remove();
subscription = null;
}
});

if (Platform.OS === 'android' || Platform.OS === 'ios') {
t.it('resolves with a string', async () => {
const devicePushToken = await Notifications.getDevicePushTokenAsync();
t.expect(devicePushToken.data).toBe('string');
t.expect(typeof devicePushToken.data).toBe('string');
tokenFromMethodCall = devicePushToken;
});
}

if (Platform.OS === 'web') {
t.it('resolves with an object', async () => {
const devicePushToken = await Notifications.getDevicePushTokenAsync();
t.expect(devicePushToken.data).toBe('object');
t.expect(typeof devicePushToken.data).toBe('object');
tokenFromMethodCall = devicePushToken;
});
}

t.it('emits an event with token (or not, if getDevicePushTokenAsync failed)', async () => {
// It would be better to do `if (!tokenFromMethodCall) { pending(); } else { ... }`
// but `t.pending()` still doesn't work.
await waitFor(500);
t.expect(tokenFromEvent).toEqual(tokenFromMethodCall);
});
});
});
}

0 comments on commit d76791e

Please sign in to comment.