Skip to content

Commit

Permalink
checkpoint testing for android health connect
Browse files Browse the repository at this point in the history
  • Loading branch information
kibagateaux committed Dec 10, 2023
1 parent 9ac4be0 commit dcbac86
Show file tree
Hide file tree
Showing 13 changed files with 278 additions and 152 deletions.
3 changes: 3 additions & 0 deletions __mocks__/@segment/analytics-react-native.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
createClient: jest.fn(),
};
16 changes: 16 additions & 0 deletions __mocks__/react-native-health-connect.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module.exports = {
openHealthConnectSettings: jest.fn(),
// assume installed for testing purposes. not worth getting into weeds
initialize: () => true,
getSdkStatus: () => 3,
requestPermission: jest.fn(),
readRecords: jest.fn(),
revokeAllPermissions: jest.fn(),
getGrantedPermissions: jest.fn(),
SdkAvailabilityStatus: jest.fn(),
SdkAvailabilityStatus: {
SDK_UNAVAILABLE: 1,
SDK_UNAVAILABLE_PROVIDER_UPDATE_REQUIRED: 2,
SDK_AVAILABLE: 3,
},
};
11 changes: 11 additions & 0 deletions __mocks__/sentry-expo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module.exports = {
Native: {
captureException: jest.fn(),
captureMessage: jest.fn(),
},
Browser: {
captureException: jest.fn(),
captureMessage: jest.fn(),
},
init: jest.fn(),
};
2 changes: 2 additions & 0 deletions setupTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
jest.mock('sentry-expo');
jest.mock('@segment/analytics-react-native');
5 changes: 4 additions & 1 deletion src/app/inventory/[item].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,10 @@ const ItemPage: React.FC<ItemPageProps> = () => {
? await item.equip(promptAsync)
: await item.equip();
// if result.error = "transceive fai" try majik ritual again
if (result) setStatus('post-equip');
if (result) {
setStatus('post-equip');
// TODO api request to add item to their avatar (:DataProvider or :Resource?)
}

// assume failure
setStatus('unequipped');
Expand Down
1 change: 1 addition & 0 deletions src/types/GameMechanics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ export interface ItemAbility {
// TODO I feel like this should all be rolled into InventoryItem
export interface InventoryIntegration {
item: InventoryItem;
permissions?: string[];
checkEligibility: () => Promise<boolean>;
getPermissions: () => Promise<boolean>;
initPermissions: () => Promise<boolean>;
Expand Down
123 changes: 123 additions & 0 deletions src/utils/inventory/__tests__/android-health-connect.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
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;
});

describe('InventoryItem', () => {
it('should have correct properties', () => {
expect(item.item).toHaveProperty('id');
expect(item.item).toHaveProperty('name');
expect(item.item).toHaveProperty('image');
expect(item.item).toHaveProperty('tags');
expect(item.item).toHaveProperty('attributes');
expect(item.item).toHaveProperty('datasource');
expect(item.item).toHaveProperty('installLink');
expect(item.item).toHaveProperty('checkStatus');
expect(item.item).toHaveProperty('canEquip');
expect(item.item).toHaveProperty('equip');
expect(item.item).toHaveProperty('unequip');
});
});

// too many issues with platform mocking and stuff so just assume android and installed
describe('checkEligibility', () => {
// TODO figure out how to mock Platform.OS. Jest defaults to ios
// it('should return false if platform is not android', async () => {
// jest.mock('react-native', () => ({
// Platform: {
// OS: 'ios',
// },
// expect(await item.checkEligibility()).toBe(false);
// jest.mock('react-native', () => ({
// Platform: {
// OS: 'web',
// },
// expect(await item.checkEligibility()).toBe(false);
// });
// it('should return true if platform is android', async () => {
// initialize: () => true;
// 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');
// });
});

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);
});

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[];
expect(await item.initPermissions()).toBe(true);
});

it('should return false if permissions are not granted', async () => {
mockHealth.requestPermission = async () => [];
mockHealth.getGrantedPermissions = async () => [];
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);
});

it('should return false if no permissions are granted', async () => {
// mockHealth.requestPermission = async (perms) => perms;
mockHealth.getGrantedPermissions = async () => [];
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[];
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);
});

it('should return true if permissions are initialized', async () => {
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);
});
});

describe('unequip', () => {
it('should return true', async () => {
expect(await item.unequip()).toBe(true);
});
});
111 changes: 0 additions & 111 deletions src/utils/inventory/android-health-connect.test.ts

This file was deleted.

Loading

0 comments on commit dcbac86

Please sign in to comment.