Files
CollabTable/shared/packages/web-ui/tests/ProfileDialog.test.ts
T

68 lines
2.6 KiB
TypeScript
Raw Normal View History

import { cleanup, fireEvent, render, screen, waitFor } from '@testing-library/svelte';
import { afterEach, describe, expect, it, vi } from 'vitest';
import ProfileDialog from '../src/lib/components/ProfileDialog.svelte';
afterEach(cleanup);
const fieldLabels: Record<string, string> = {
username: 'Username',
email: 'Email',
password: 'New password',
confirmPassword: 'Confirm new password',
currentPassword: 'Current password',
totpToken: 'Current authenticator code',
};
const field = (name: string): HTMLInputElement => screen.getByLabelText(fieldLabels[name]) as HTMLInputElement;
async function fill(name: string, value: string): Promise<void> {
await fireEvent.input(field(name), { target: { value } });
}
async function submit(): Promise<void> {
const form = document.querySelector('form');
if (!form) throw new Error('no form');
await fireEvent.submit(form);
}
describe('ProfileDialog password changes', () => {
it('requires current password and a current TOTP code when enabled', async () => {
const onSave = vi.fn().mockResolvedValue(undefined);
render(ProfileDialog, { props: { open: true, onSave, totpEnabled: true } });
await fill('username', 'alice');
await fill('email', 'alice@example.com');
await fill('password', 'new-password');
await fill('confirmPassword', 'new-password');
await submit();
expect(onSave).not.toHaveBeenCalled();
expect(screen.getByText('Enter your current password to change your email or password.')).toBeTruthy();
await fill('currentPassword', 'old-password');
await submit();
expect(onSave).not.toHaveBeenCalled();
expect(screen.getByText('Enter a current authenticator code to change your email or password.')).toBeTruthy();
await fill('totpToken', '123456');
await submit();
await waitFor(() => expect(onSave).toHaveBeenCalledTimes(1));
expect(onSave).toHaveBeenCalledWith({
username: 'alice',
email: 'alice@example.com',
password: 'new-password',
currentPassword: 'old-password',
totpToken: '123456',
});
});
it('does not ask for password proof when only profile fields change', async () => {
const onSave = vi.fn().mockResolvedValue(undefined);
render(ProfileDialog, { props: { open: true, onSave, totpEnabled: true } });
await fill('username', 'alice');
await fill('email', 'alice@example.com');
await submit();
await waitFor(() => expect(onSave).toHaveBeenCalledTimes(1));
expect(onSave).toHaveBeenCalledWith({ username: 'alice', email: 'alice@example.com' });
expect(screen.queryByLabelText('Current password')).toBeNull();
});
});