1import { Location, TaskManager } from 'expo'; 2 3import * as TestUtils from '../TestUtils'; 4 5const DEFINED_TASK_NAME = 'defined task'; 6const UNDEFINED_TASK_NAME = 'undefined task'; 7 8export async function test(t) { 9 const shouldSkipTestsRequiringPermissions = await TestUtils.shouldSkipTestsRequiringPermissionsAsync(); 10 const describeWithPermissions = shouldSkipTestsRequiringPermissions ? t.xdescribe : t.describe; 11 const locationOptions = { 12 accuracy: Location.Accuracy.Low, 13 showsBackgroundLocationIndicator: false, 14 }; 15 16 t.describe('TaskManager', () => { 17 t.describe('isTaskDefined()', () => { 18 t.it('returns true if task is defined', () => { 19 t.expect(TaskManager.isTaskDefined(DEFINED_TASK_NAME)).toBe(true); 20 }); 21 t.it('returns false if task is not defined', () => { 22 t.expect(TaskManager.isTaskDefined(UNDEFINED_TASK_NAME)).toBe(false); 23 }); 24 }); 25 26 describeWithPermissions('isTaskRegisteredAsync()', async () => { 27 t.beforeAll(async () => { 28 await Location.startLocationUpdatesAsync(DEFINED_TASK_NAME); 29 }); 30 31 t.it('returns true for registered tasks', async () => { 32 t.expect(await TaskManager.isTaskRegisteredAsync(DEFINED_TASK_NAME)).toBe(true); 33 }); 34 35 t.it('returns false for unregistered tasks', async () => { 36 t.expect(await TaskManager.isTaskRegisteredAsync(UNDEFINED_TASK_NAME)).toBe(false); 37 }); 38 39 t.afterAll(async () => { 40 await Location.stopLocationUpdatesAsync(DEFINED_TASK_NAME); 41 }); 42 }); 43 44 describeWithPermissions('getTaskOptionsAsync()', async () => { 45 let taskOptions; 46 47 t.it('returns null for unregistered tasks', async () => { 48 taskOptions = await TaskManager.getTaskOptionsAsync(DEFINED_TASK_NAME); 49 t.expect(taskOptions).toBe(null); 50 }); 51 52 t.it('returns correct options after register', async () => { 53 await Location.startLocationUpdatesAsync(DEFINED_TASK_NAME, locationOptions); 54 taskOptions = await TaskManager.getTaskOptionsAsync(DEFINED_TASK_NAME); 55 t.expect(taskOptions).toEqual(t.jasmine.objectContaining(locationOptions)); 56 }); 57 58 t.it('returns null when unregistered', async () => { 59 await Location.stopLocationUpdatesAsync(DEFINED_TASK_NAME); 60 taskOptions = await TaskManager.getTaskOptionsAsync(DEFINED_TASK_NAME); 61 t.expect(taskOptions).toBe(null); 62 }); 63 }); 64 65 describeWithPermissions('getRegisteredTasksAsync()', async () => { 66 let registeredTasks; 67 68 t.it('returns empty array if there are no tasks', async () => { 69 registeredTasks = await TaskManager.getRegisteredTasksAsync(); 70 t.expect(registeredTasks).toBeDefined(); 71 t.expect(registeredTasks.length).toBe(0); 72 }); 73 74 t.it('returns correct array after registering the task', async () => { 75 await Location.startLocationUpdatesAsync(DEFINED_TASK_NAME, locationOptions); 76 77 registeredTasks = await TaskManager.getRegisteredTasksAsync(); 78 79 t.expect(registeredTasks).toBeDefined(); 80 t.expect(registeredTasks.length).toBe(1); 81 t.expect(registeredTasks.find(task => task.taskName === DEFINED_TASK_NAME)).toEqual( 82 t.jasmine.objectContaining({ 83 taskName: DEFINED_TASK_NAME, 84 taskType: 'location', 85 options: locationOptions, 86 }) 87 ); 88 }); 89 90 t.afterAll(async () => { 91 await Location.stopLocationUpdatesAsync(DEFINED_TASK_NAME); 92 }); 93 }); 94 95 describeWithPermissions('unregisterAllTasksAsync()', () => { 96 t.it('unregisters tasks correctly', async () => { 97 await Location.startLocationUpdatesAsync(DEFINED_TASK_NAME, locationOptions); 98 await TaskManager.unregisterAllTasksAsync(); 99 100 t.expect(await TaskManager.isTaskRegisteredAsync(DEFINED_TASK_NAME)).toBe(false); 101 t.expect((await TaskManager.getRegisteredTasksAsync()).length).toBe(0); 102 t.expect(await Location.hasStartedLocationUpdatesAsync(DEFINED_TASK_NAME)).toBe(false); 103 }); 104 }); 105 106 describeWithPermissions('rejections', () => { 107 t.it('should reject when trying to unregister task with different consumer', async () => { 108 await Location.startLocationUpdatesAsync(DEFINED_TASK_NAME, locationOptions); 109 110 let error; 111 try { 112 await Location.stopGeofencingAsync(DEFINED_TASK_NAME, locationOptions); 113 } catch (e) { 114 error = e; 115 } 116 t.expect(error).toBeDefined(); 117 t.expect(error.message).toMatch(/Invalid task consumer/); 118 119 await Location.stopLocationUpdatesAsync(DEFINED_TASK_NAME); 120 }); 121 }); 122 }); 123} 124 125// Empty task so we can properly test some methods. 126TaskManager.defineTask(DEFINED_TASK_NAME, () => {}); 127