Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/frontend/src/lib/services/load-user-profile.services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,15 @@ export const loadCertifiedUserProfile = async ({
}
};

export type LoadUserProfileFailureReason = 'signups-closed' | 'unknown';

export const loadUserProfile = async ({
identity,
reload = true
}: {
identity: NullishIdentity;
reload?: boolean;
}): Promise<ResultSuccess> => {
}): Promise<ResultSuccess<LoadUserProfileFailureReason>> => {
// We just want to verify that the store is empty, without being interested in the data.
// So we fetch it imperatively, instead of passing as parameter.
// If it is not empty, and we don't want to reload, we can return early.
Expand Down Expand Up @@ -99,12 +101,16 @@ export const loadUserProfile = async ({
loadCertifiedUserProfile({ identity });
}
} catch (err: unknown) {
if (err instanceof SignupsClosedError) {
return { success: false, err: 'signups-closed' };
}
Comment thread
AntonioVentilii marked this conversation as resolved.

const { settings } = get(i18n);
toastsError({
msg: { text: settings.error.loading_profile },
err
});
return { success: false };
return { success: false, err: 'unknown' };
}

return { success: true };
Expand Down
21 changes: 19 additions & 2 deletions src/frontend/src/tests/lib/services/load-user-profile.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { UserProfile } from '$declarations/backend/backend.did';
import * as backendApi from '$lib/api/backend.api';
import { loadUserProfile } from '$lib/services/load-user-profile.services';
import { userProfileStore } from '$lib/stores/user-profile.store';
import { SignupsClosedError } from '$lib/types/errors';
import en from '$tests/mocks/i18n.mock';
import { mockIdentity } from '$tests/mocks/identity.mock';
import { mockUserProfile } from '$tests/mocks/user-profile.mock';
Expand Down Expand Up @@ -122,7 +123,7 @@ describe('load-user-profile.services', () => {

const result = await loadUserProfile({ identity: mockIdentity });

expect(result).toEqual({ success: false });
expect(result).toEqual({ success: false, err: 'unknown' });
});

it('should handle unknown error from getUserProfile', async () => {
Expand All @@ -132,7 +133,23 @@ describe('load-user-profile.services', () => {

const result = await loadUserProfile({ identity: mockIdentity });

expect(result).toEqual({ success: false });
expect(result).toEqual({ success: false, err: 'unknown' });
});

it('should surface signups-closed when createUserProfile rejects with SignupsClosedError', async () => {
vi.spyOn(backendApi, 'getUserProfile').mockResolvedValue({ Err: { NotFound: null } });
const createUserProfileSpy = vi
.spyOn(backendApi, 'createUserProfile')
.mockRejectedValue(new SignupsClosedError());
Comment thread
AntonioVentilii marked this conversation as resolved.

const result = await loadUserProfile({ identity: mockIdentity });

expect(result).toEqual({ success: false, err: 'signups-closed' });
expect(createUserProfileSpy).toHaveBeenCalledWith({
identity: mockIdentity,
nullishIdentityErrorMessage
});
expect(get(userProfileStore)).toBeNull();
});

it('should handle certified profile load failure gracefully', async () => {
Expand Down
Loading