From 9903b47dfe15624644767c7dfeb8ccc758c4489e Mon Sep 17 00:00:00 2001 From: "sweep-ai[bot]" <128439645+sweep-ai[bot]@users.noreply.github.com> Date: Sat, 9 Dec 2023 19:29:53 +0000 Subject: [PATCH] feat: Add unit tests for AndroidHealthConnect inve --- .../inventory/android-health-connect.test.ts | 112 ++++++++++++++++++ 1 file changed, 112 insertions(+) create mode 100644 src/utils/inventory/android-health-connect.test.ts diff --git a/src/utils/inventory/android-health-connect.test.ts b/src/utils/inventory/android-health-connect.test.ts new file mode 100644 index 0000000..66908cc --- /dev/null +++ b/src/utils/inventory/android-health-connect.test.ts @@ -0,0 +1,112 @@ +import { InventoryItem, HoF } from 'src/types/GameMechanics'; +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); + }); +});