mirror of
https://github.com/Memo-2023/mana-monorepo.git
synced 2026-05-14 20:21:09 +02:00
fix(profile): update profile.test.ts for new avatar upload + email change API
- Replace getAvatarUploadUrl test with changeEmail + uploadAvatar tests - Fix authStore mock: getValidToken instead of getAccessToken - Add getManaApiUrl mock for avatar upload endpoint Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
c412508b95
commit
f5392b8b63
1 changed files with 39 additions and 14 deletions
|
|
@ -8,13 +8,14 @@ vi.mock('$app/environment', () => ({
|
|||
// Mock auth store
|
||||
vi.mock('$lib/stores/auth.svelte', () => ({
|
||||
authStore: {
|
||||
getAccessToken: vi.fn().mockResolvedValue('test-token'),
|
||||
getValidToken: vi.fn().mockResolvedValue('test-token'),
|
||||
},
|
||||
}));
|
||||
|
||||
// Mock config
|
||||
vi.mock('./config', () => ({
|
||||
getManaAuthUrl: vi.fn().mockReturnValue('http://localhost:3001'),
|
||||
getManaApiUrl: vi.fn().mockReturnValue('http://localhost:3060'),
|
||||
}));
|
||||
|
||||
import { profileService, type UserProfile } from './profile';
|
||||
|
|
@ -137,29 +138,53 @@ describe('profileService', () => {
|
|||
});
|
||||
});
|
||||
|
||||
describe('getAvatarUploadUrl', () => {
|
||||
it('should request presigned URL for avatar upload', async () => {
|
||||
const mockResponse = {
|
||||
uploadUrl: 'https://s3.example.com/upload',
|
||||
fileUrl: 'https://s3.example.com/avatar.png',
|
||||
key: 'avatars/user-1/avatar.png',
|
||||
expiresIn: 3600,
|
||||
};
|
||||
describe('changeEmail', () => {
|
||||
it('should send email change request', async () => {
|
||||
global.fetch = vi.fn().mockResolvedValue({
|
||||
ok: true,
|
||||
json: () => Promise.resolve(mockResponse),
|
||||
json: () => Promise.resolve({ success: true, message: 'Verification email sent' }),
|
||||
});
|
||||
|
||||
const result = await profileService.getAvatarUploadUrl('avatar.png');
|
||||
const result = await profileService.changeEmail({ newEmail: 'new@mana.how' });
|
||||
|
||||
expect(result.uploadUrl).toBe('https://s3.example.com/upload');
|
||||
expect(result.success).toBe(true);
|
||||
expect(global.fetch).toHaveBeenCalledWith(
|
||||
'http://localhost:3001/api/v1/storage/avatar/upload-url',
|
||||
'http://localhost:3001/api/v1/auth/change-email',
|
||||
expect.objectContaining({
|
||||
method: 'POST',
|
||||
body: JSON.stringify({ filename: 'avatar.png' }),
|
||||
body: JSON.stringify({ newEmail: 'new@mana.how' }),
|
||||
})
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('uploadAvatar', () => {
|
||||
it('should upload avatar to API and update profile', async () => {
|
||||
const mockFile = new File(['image-data'], 'avatar.png', { type: 'image/png' });
|
||||
|
||||
global.fetch = vi
|
||||
.fn()
|
||||
.mockResolvedValueOnce({
|
||||
ok: true,
|
||||
json: () =>
|
||||
Promise.resolve({ url: 'https://media.mana.how/avatar.png', mediaId: 'media-1' }),
|
||||
})
|
||||
.mockResolvedValueOnce({
|
||||
ok: true,
|
||||
json: () =>
|
||||
Promise.resolve({
|
||||
success: true,
|
||||
user: { id: 'user-1', name: 'Test', email: 'test@mana.how' },
|
||||
}),
|
||||
});
|
||||
|
||||
const result = await profileService.uploadAvatar(mockFile);
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(global.fetch).toHaveBeenCalledWith(
|
||||
'http://localhost:3060/api/v1/storage/avatar/upload',
|
||||
expect.objectContaining({ method: 'POST' })
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue