Skip to content

Commit

Permalink
feat: Add unit tests for AndroidHealthConnect inve
Browse files Browse the repository at this point in the history
  • Loading branch information
sweep-ai[bot] authored Dec 9, 2023
1 parent dc017b1 commit 9903b47
Showing 1 changed file with 112 additions and 0 deletions.
112 changes: 112 additions & 0 deletions src/utils/inventory/android-health-connect.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
import { InventoryItem, HoF } from 'src/types/GameMechanics';

Check failure on line 1 in src/utils/inventory/android-health-connect.test.ts

View workflow job for this annotation

GitHub Actions / build

'InventoryItem' is defined but never used

Check failure on line 1 in src/utils/inventory/android-health-connect.test.ts

View workflow job for this annotation

GitHub Actions / build

'HoF' is defined but never used
import {
checkEligibility,
getPermissions,
initPermissions,
equip,
unequip,
item as androidHealthItem,
} from 'src/utils/inventory/android-health-connect';

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

describe('checkEligibility', () => {
it('should return false if platform is not android', async () => {
jest.mock('react-native', () => ({
Platform: {
OS: 'ios',
},
}));
expect(await checkEligibility()).toBe(false);
});

it('should return false if SDK is not available', async () => {
jest.mock('react-native-health-connect', () => ({
getSdkStatus: () => 'SDK_NOT_AVAILABLE',
}));
expect(await checkEligibility()).toBe(false);
});

it('should throw error if SDK fails to initialize', async () => {
jest.mock('react-native-health-connect', () => ({
getSdkStatus: () => 'SDK_AVAILABLE',
initialize: () => false,
}));
await expect(checkEligibility()).rejects.toThrow('Unable to initialize Android Health');
});
});

describe('getPermissions', () => {
it('should return false if checkEligibility returns false', async () => {
jest.mock('./android-health-connect', () => ({
checkEligibility: () => false,
}));
expect(await getPermissions()).toBe(false);
});

it('should return false if no permissions are granted', async () => {
jest.mock('react-native-health-connect', () => ({
getGrantedPermissions: () => [],
}));
expect(await getPermissions()).toBe(false);
});

it('should return true if permissions are granted', async () => {
jest.mock('react-native-health-connect', () => ({
getGrantedPermissions: () => ['Steps'],
}));
expect(await getPermissions()).toBe(true);
});
});

describe('initPermissions', () => {
it('should return false if checkEligibility returns false', async () => {
jest.mock('./android-health-connect', () => ({
checkEligibility: () => false,
}));
expect(await initPermissions()).toBe(false);
});

it('should return true if permissions are granted', async () => {
jest.mock('react-native-health-connect', () => ({
requestPermission: () => true,
}));
expect(await initPermissions()).toBe(true);
});
});

describe('equip', () => {
it('should return false if checkEligibility returns false', async () => {
jest.mock('./android-health-connect', () => ({
checkEligibility: () => false,
}));
expect(await equip()).toBe(false);
});

it('should return true if permissions are initialized', async () => {
jest.mock('./android-health-connect', () => ({
initPermissions: () => true,
}));
expect(await equip()).toBe(true);
});
});

describe('unequip', () => {
it('should return true', async () => {
expect(await unequip()).toBe(true);
});
});

0 comments on commit 9903b47

Please sign in to comment.