Skip to content

Commit fa3bfb7

Browse files
committed
test(sdk): check that a profile save leaves out metadata
1 parent 6703503 commit fa3bfb7

3 files changed

Lines changed: 54 additions & 8 deletions

File tree

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { profileUpdateBody } from './profile-update-body';
2+
3+
describe('profileUpdateBody', () => {
4+
const storedUser = {
5+
id: 'user-1',
6+
name: 'jane_doe',
7+
title: 'Jane Doe',
8+
email: 'jane@raystack.org',
9+
avatar: 'data:image/png;base64,old',
10+
metadata: { onboarded: true, team: 'core' }
11+
};
12+
13+
it('leaves out metadata when only the avatar changes', () => {
14+
const formValues = { ...storedUser, avatar: 'data:image/png;base64,new' };
15+
16+
const body = profileUpdateBody(formValues, storedUser.name);
17+
18+
expect(body).toEqual({
19+
title: 'Jane Doe',
20+
email: 'jane@raystack.org',
21+
avatar: 'data:image/png;base64,new',
22+
name: 'jane_doe'
23+
});
24+
expect(body).not.toHaveProperty('metadata');
25+
});
26+
27+
it('leaves out metadata when the title changes', () => {
28+
const formValues = { ...storedUser, title: 'Jane Smith' };
29+
30+
const body = profileUpdateBody(formValues, storedUser.name);
31+
32+
expect(body.title).toBe('Jane Smith');
33+
expect(body).not.toHaveProperty('metadata');
34+
});
35+
});
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
export interface ProfileFormValues {
2+
title: string;
3+
email: string;
4+
avatar?: string;
5+
}
6+
7+
export function profileUpdateBody(
8+
values: ProfileFormValues,
9+
currentName: string | undefined
10+
) {
11+
return {
12+
title: values.title,
13+
email: values.email,
14+
avatar: values.avatar,
15+
name: currentName
16+
};
17+
}

‎web/sdk/client/views/profile/profile-view.tsx‎

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import { DeleteUserDialog } from '../../components/delete-user-dialog';
2727
import styles from './profile-view.module.css';
2828

2929
import { handleConnectError } from '~/utils/error';
30+
import { profileUpdateBody } from './profile-update-body';
3031

3132
const profileSchema = yup
3233
.object({
@@ -101,14 +102,7 @@ export function ProfileView({
101102
async function onSubmit(data: FormData) {
102103
try {
103104
if (!user?.id) return;
104-
await updateCurrentUser({
105-
body: {
106-
title: data.title,
107-
email: data.email,
108-
avatar: data.avatar,
109-
name: user.name
110-
}
111-
});
105+
await updateCurrentUser({ body: profileUpdateBody(data, user.name) });
112106
toastManager.add({ title: 'Updated user', type: 'success' });
113107
} catch (err) {
114108
handleConnectError(err, {

0 commit comments

Comments
 (0)