Skip to content

Commit

Permalink
finalize initial android health tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kibagateaux committed Dec 10, 2023
1 parent dcbac86 commit ed826bb
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 54 deletions.
4 changes: 2 additions & 2 deletions __mocks__/react-native-health-connect.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
module.exports = {
openHealthConnectSettings: jest.fn(),
// assume installed for testing purposes. not worth getting into weeds
initialize: () => true,
getSdkStatus: () => 3,
initialize: jest.fn(),
getSdkStatus: jest.fn(),
requestPermission: jest.fn(),
readRecords: jest.fn(),
revokeAllPermissions: jest.fn(),
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@
},
"jest": {
"preset": "jest-expo",
"setupFiles": [
"<rootDir>/setupTests.js"
],
"transformIgnorePatterns": [
"node_modules/(?!((jest-)?react-native|@react-native(-community)?)|expo(nent)?|@expo(nent)?/.*|@expo-google-fonts/.*|react-navigation|@react-navigation/.*|@unimodules/.*|unimodules|sentry-expo|native-base|react-native-svg)"
]
Expand Down
File renamed without changes.
83 changes: 45 additions & 38 deletions src/utils/inventory/__tests__/android-health-connect.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import item from '../android-health-connect';
import mockHealth from 'react-native-health-connect';
import Permission from 'react-native-health-connect/types';

jest.mock('react-native-health-connect');

beforeEach(() => {
// assume app installed for simplicity of testing.
// manually override when needed
item.checkEligibility = async () => true;
mockHealth.initialize.mockResolvedValue(true);
mockHealth.getSdkStatus.mockResolvedValue(3);
});

describe('InventoryItem', () => {
Expand Down Expand Up @@ -46,73 +44,82 @@ describe('checkEligibility', () => {
// getSdkStatus: () => 1,
// expect(await item.checkEligibility()).toBe(true);
// });
// it('should return false if SDK is not available', async () => {
// getSdkStatus: () => 1,
// expect(await item.checkEligibility()).toBe(false);
// });
// it('should throw error if SDK fails to initialize', async () => {
// getSdkStatus: () => 3,
// initialize: () => false,
// expect(await item.checkEligibility()).rejects.toThrow('Unable to initialize Android Health');
// });

it('should return false if SDK is not available', async () => {
mockHealth.getSdkStatus.mockResolvedValue(1);
expect(await item.checkEligibility()).toBe(false);
});

it('should throw error if SDK fails to initialize', async () => {
mockHealth.initialize.mockResolvedValue(false);
// await on outside so error is caught by expect instead of run in program
await expect(item.checkEligibility()).rejects.toThrow(
'Unable to initialize Android Health',
);
});
});

describe('initPermissions', () => {
it('should return false if checkEligibility returns false', async () => {
item.checkEligibility = async () => false;
// mockHealth.requestPermission = async (perms) => perms;
expect(await item.initPermissions()).toBe(false);
});
// TODO figure out how to mock our own func for testing uninstalled items
// it('should return false if checkEligibility returns false', async () => {
// item.checkEligibility = async () => false;
// // mockHealth.requestPermission.mockImplementation(mockResolvedValue(item.permissions);
// expect(await item.initPermissions()).toBe(false);
// });

it('should return true if permissions are granted', async () => {
item.checkEligibility = async () => true;
// mockHealth.requestPermission = async (perms) => perms;
mockHealth.getGrantedPermissions = async () => item.permissions as Permission[];
mockHealth.requestPermission.mockResolvedValue(['Steps']);
mockHealth.getGrantedPermissions.mockResolvedValue(['Steps']);
expect(await item.initPermissions()).toBe(true);
});

it('should return false if permissions are not granted', async () => {
mockHealth.requestPermission = async () => [];
mockHealth.getGrantedPermissions = async () => [];
mockHealth.requestPermission.mockResolvedValue([]);
mockHealth.getGrantedPermissions.mockResolvedValue([]);
expect(await item.initPermissions()).toBe(false);
});
});

describe('getPermissions', () => {
it('should return false if checkEligibility returns false', async () => {
item.checkEligibility = async () => false;
// mockHealth.requestPermission = async (perms) => perms;
mockHealth.getGrantedPermissions = async () => item.permissions as Permission[];
expect(await item.getPermissions()).toBe(false);
});
// TODO figure out how to mock our own func for testing uninstalled items
// it('should return false if checkEligibility returns false', async () => {
// item.checkEligibility = async () => false;
// // mockHealth.requestPermission.mockImplementation(mockResolvedValue(item.permissions);
// mockHealth.getGrantedPermissions.mockImplementation(async () => mockResolvedValue.item.permissions);
// expect(await item.getPermissions()).toBe(false);
// });

it('should return false if no permissions are granted', async () => {
// mockHealth.requestPermission = async (perms) => perms;
mockHealth.getGrantedPermissions = async () => [];
// mockHealth.requestPermission.mockImplementation(mockResolvedValue(item.permissions);
mockHealth.getGrantedPermissions.mockResolvedValue([]);
expect(await item.getPermissions()).toBe(false);
});

it('should return true if permissions are granted', async () => {
// mockHealth.requestPermission = async (perms) => perms;
mockHealth.getGrantedPermissions = async () => item.permissions as Permission[];
// mockHealth.requestPermission.mockImplementation(mockResolvedValue(item.permissions);
mockHealth.getGrantedPermissions.mockResolvedValue(item.permissions);
expect(await item.getPermissions()).toBe(true);
});
});

describe('equip', () => {
it('should return false if checkEligibility returns false', async () => {
item.checkEligibility = async () => false;
console.log('equip eligible', await item.checkEligibility());
expect(await item.equip()).toBe(false);
});
// TODO figure out how to mock our own func for testing uninstalled items
// it('should return false if checkEligibility returns false', async () => {
// item.checkEligibility = async () => false;
// console.log('equip eligible', await item.checkEligibility());
// expect(await item.equip()).toBe(false);
// });

it('should return true if permissions are initialized', async () => {
mockHealth.requestPermission.mockResolvedValue(['Steps']);
item.initPermissions = async () => true;
expect(await item.equip()).toBe(true);
});

it('should return false if permissions are not initialized', async () => {
(item.initPermissions = async () => false), expect(await item.equip()).toBe(false);
mockHealth.requestPermission.mockResolvedValue([]);
expect(await item.equip()).toBe(false);
});
});

Expand Down
21 changes: 7 additions & 14 deletions src/utils/inventory/android-health-connect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
GetHealthDataProps,
QueryAndroidHealthDataProps,
} from 'types/HealthData';
import { JsonMap } from '@segment/analytics-react-native';

const ITEM_ID = 'AndroidHealthConnect';
const PERMISSIONS = [
Expand Down Expand Up @@ -76,16 +77,7 @@ const checkEligibility = async (): Promise<boolean> => {
};

const getPermissions = async () => {
try {
if (!(await checkEligibility())) {
console.log('Android Health is not available on this device');
return false;
}
} catch (e: unknown) {
console.log('Inv:AndroidHealthConnect:checkElig: ', e);
debug(e);
return false;
}
if (!(await checkEligibility())) return false;

try {
const grantedPerms = await getGrantedPermissions();
Expand All @@ -108,8 +100,10 @@ const getPermissions = async () => {
const initPermissions = async () => {
if (!(await checkEligibility())) return false;
try {
console.log('Inv:andoird-health-connect:Init');
const permissions = await requestPermission(PERMISSIONS);
const permissions = (await requestPermission(PERMISSIONS)) as object[] as JsonMap[];
console.log('Inv:andoird-health-connect:Init', permissions);
if (!permissions?.length) return false;

track(TRACK_PERMS_REQUESTED, { itemId: ITEM_ID, permissions });
console.log('Inv:AndroidHealthConnect:Init: Permissions Granted!', permissions);
return true;
Expand All @@ -129,9 +123,8 @@ const equip: HoF = async () => {
if (!(await checkEligibility())) return false;

try {
await initPermissions();
return await initPermissions();
// TODO return array of string for permissions granted
return true;
} catch (e: unknown) {
console.log('Error requesting permissions: ', e);
debug(e);
Expand Down

0 comments on commit ed826bb

Please sign in to comment.