diff --git a/LeftOvers/Services/Profiles/ProfileService.ts b/LeftOvers/Services/Profiles/ProfileService.ts index 448f67b..f2f3078 100644 --- a/LeftOvers/Services/Profiles/ProfileService.ts +++ b/LeftOvers/Services/Profiles/ProfileService.ts @@ -5,6 +5,9 @@ import AsyncStorage from "@react-native-async-storage/async-storage"; export default class ProfileService implements IProfileService { async getProfiles(): Promise { const results = await AsyncStorage.getItem('profiles'); + if (results == null) { + return [] + } const tmp = JSON.parse(results) let existingProfiles: Profile[] = [] for (let item of tmp) { diff --git a/LeftOvers/__tests__/ProfileService.test.ts b/LeftOvers/__tests__/ProfileService.test.ts index e69de29..04652f8 100644 --- a/LeftOvers/__tests__/ProfileService.test.ts +++ b/LeftOvers/__tests__/ProfileService.test.ts @@ -0,0 +1,124 @@ +import AsyncStorage from '@react-native-async-storage/async-storage'; +import ProfileService from '../Services/Profiles/ProfileService'; +import Profile from '../Models/Profile'; + +type AsyncStorageMock = { + getItem: jest.Mock, [string]>, + setItem: jest.Mock, [string, string]>, + clear: jest.Mock>, +}; + +jest.mock('@react-native-async-storage/async-storage', () => { + const asyncStorageMock: AsyncStorageMock = { + getItem: jest.fn(), + setItem: jest.fn(), + clear: jest.fn(), + }; + return asyncStorageMock; +}); + +describe('ProfileService', () => { + beforeEach(() => { + (AsyncStorage.getItem as jest.Mock).mockReset(); + (AsyncStorage.setItem as jest.Mock).mockReset(); + (AsyncStorage.clear as jest.Mock).mockReset(); + }); + + describe('getProfiles', () => { + it('should return an empty array if no profiles are stored', async () => { + (AsyncStorage.getItem as jest.Mock).mockResolvedValueOnce(null); + + const profileService = new ProfileService(); + const profiles = await profileService.getProfiles(); + + expect(profiles).toEqual([]); + }); + + it('should return an array of profiles if profiles are stored', async () => { + const storedProfiles = [ + { _name: 'John', _avatar: 'avatar1', _allergy: ['none'], _diets: [] }, + { _name: 'Jane', _avatar: 'avatar2', _allergy: ['peanuts'], _diets: ['vegan'] }, + ]; + + (AsyncStorage.getItem as jest.Mock).mockResolvedValueOnce(JSON.stringify(storedProfiles)); + + const profileService = new ProfileService(); + const profiles = await profileService.getProfiles(); + + expect(profiles.length).toBe(2); + expect(profiles[0]).toBeInstanceOf(Profile); + expect(profiles[0].name).toEqual('John'); + }); + }); + + describe('addProfile', () => { + it('should add a new profile to the stored profiles', async () => { + const existingProfiles = [ + { _name: 'John', _avatar: 'avatar1', _allergy: ['none'], _diets: [] }, + ]; + + (AsyncStorage.getItem as jest.Mock).mockResolvedValueOnce(JSON.stringify(existingProfiles)); + (AsyncStorage.setItem as jest.Mock).mockResolvedValueOnce(null); + + const newProfile = new Profile('Jane', 'avatar2', ['peanuts'], ['vegan']); + + const profileService = new ProfileService(); + const result = await profileService.addProfile(newProfile); + + expect(result).toBe(true); + + expect(AsyncStorage.setItem).toHaveBeenCalledWith('profiles', expect.any(String)); + }); + + it('should not add a profile if it already exists', async () => { + const existingProfiles = [ + { _name: 'John', _avatar: 'avatar1', _allergy: ['none'], _diets: [] }, + ]; + + (AsyncStorage.getItem as jest.Mock).mockResolvedValueOnce(JSON.stringify(existingProfiles)); + + const existingProfile = new Profile('John', 'avatar1', ['none'], []); + + const profileService = new ProfileService(); + const result = await profileService.addProfile(existingProfile); + + expect(result).toBe(false); + + expect(AsyncStorage.setItem).not.toHaveBeenCalled(); + }); + }); + + describe('delProfile', () => { + it('should delete a profile by name', async () => { + const existingProfiles = [ + { _name: 'John', _avatar: 'avatar1', _allergy: ['none'], _diets: [] }, + { _name: 'Jane', _avatar: 'avatar2', _allergy: ['peanuts'], _diets: ['vegan'] }, + ]; + + (AsyncStorage.getItem as jest.Mock).mockResolvedValueOnce(JSON.stringify(existingProfiles)); + (AsyncStorage.setItem as jest.Mock).mockResolvedValueOnce(null); + + const profileService = new ProfileService(); + const result = await profileService.delProfile('John'); + + expect(result).toBe(true); + + expect(AsyncStorage.setItem).toHaveBeenCalledWith('profiles', expect.any(String)); + }); + + it('should return false if the profile does not exist', async () => { + const existingProfiles = [ + { _name: 'John', _avatar: 'avatar1', _allergy: ['none'], _diets: [] }, + ]; + + (AsyncStorage.getItem as jest.Mock).mockResolvedValueOnce(JSON.stringify(existingProfiles)); + + const profileService = new ProfileService(); + const result = await profileService.delProfile('Jane'); + + expect(result).toBe(false); + + expect(AsyncStorage.setItem).not.toHaveBeenCalled(); + }); + }); +});