Files
Compass/backend/api/tests/unit/update-profile.unit.test.ts
Okechi Jones-Williams ab612a3eca Add backend API unit tests (#21)
* setting up test structure

* .

* added playwright config file, deleted original playwright folder and moved "some.test" file

* continued test structure setup

* Updating test folder structure

* Added database seeding script and backend testing folder structure

* removed the database test

* Replaced db seeding script

* Updated userInformation.ts to use values from choices.tsx

* merge prep

* removing extra unit test, moving api test to correct folder

* Pushing to get help with sql Unit test

* Updating get-profiles unit tests

* Added more unit tests

* .

* Added more unit tests

* Added getSupabaseToken unit test

* .

* excluding supabase token test so ci can pass

* .

* Seperated the seedDatabase func into its own file so it can be accessed seperatly

* Fixed failing test

* .

* .

* Fix tests

* Fix lint

* Clean

---------

Co-authored-by: MartinBraquet <martin.braquet@gmail.com>
2025-11-30 00:03:16 +01:00

86 lines
2.7 KiB
TypeScript

jest.mock("shared/supabase/init");
jest.mock("shared/supabase/utils");
import { AuthedUser } from "api/helpers/endpoint";
import { updateProfile } from "api/update-profile";
import * as supabaseInit from "shared/supabase/init";
import * as supabaseUtils from "shared/supabase/utils";
describe('updateProfiles', () => {
let mockPg: any;
beforeEach(() => {
mockPg = {
oneOrNone: jest.fn(),
};
(supabaseInit.createSupabaseDirectClient as jest.Mock)
.mockReturnValue(mockPg);
jest.clearAllMocks();
});
describe('should', () => {
it('update an existing profile when provided the user id', async () => {
const mockUserProfile = {
user_id: '234',
diet: 'Nothing',
gender: 'female',
is_smoker: true,
}
const mockUpdateMade = {
gender: 'male'
}
const mockUpdatedProfile = {
user_id: '234',
diet: 'Nothing',
gender: 'male',
is_smoker: true,
}
const mockParams = {} as any;
const mockAuth = {
uid: '234'
}
mockPg.oneOrNone.mockResolvedValue(mockUserProfile);
(supabaseUtils.update as jest.Mock).mockResolvedValue(mockUpdatedProfile);
const result = await updateProfile(
mockUpdateMade,
mockAuth as AuthedUser,
mockParams
);
expect(mockPg.oneOrNone.mock.calls.length).toBe(1);
expect(mockPg.oneOrNone.mock.calls[0][1]).toEqual([mockAuth.uid]);
expect(result).toEqual(mockUpdatedProfile);
});
it('throw an error if a profile is not found', async () => {
mockPg.oneOrNone.mockResolvedValue(null);
expect(updateProfile({} as any, {} as any, {} as any,))
.rejects
.toThrowError('Profile not found');
});
it('throw an error if unable to update the profile', async () => {
const mockUserProfile = {
user_id: '234',
diet: 'Nothing',
gender: 'female',
is_smoker: true,
}
const data = null;
const error = true;
const mockError = {
data,
error
}
mockPg.oneOrNone.mockResolvedValue(mockUserProfile);
(supabaseUtils.update as jest.Mock).mockRejectedValue(mockError);
expect(updateProfile({} as any, {} as any, {} as any,))
.rejects
.toThrowError('Error updating profile');
});
});
});