import { render, screen } from '@testing-library/svelte'; import userEvent from '@testing-library/user-event'; import { beforeEach, expect, test, describe, it, vi } from 'vitest'; import ChangePassword from '$lib/components/ChangePassword.svelte'; const user = userEvent.setup(); const mocks = vi.hoisted(() => ({ changePassword: vi.fn() })); describe('ChangePassword', async () => { beforeEach(async () => { render(ChangePassword, { changePassword: mocks.changePassword }); }); it('submits valid password changes', async () => { const oldPassword = screen.getByLabelText('current password'); await user.type(oldPassword, 'old password'); const newPassword = screen.getByLabelText('new password'); await user.type(newPassword, 'new password'); const confirmPassword = screen.getByLabelText('confirm new password'); await user.type(confirmPassword, 'new password'); const create = screen.getByRole('button'); await user.click(create); expect(mocks.changePassword).toHaveBeenCalledExactlyOnceWith('old password', 'new password'); }); it('is disabled when old password matches new password', async () => { const oldPassword = screen.getByLabelText('new password'); await user.type(oldPassword, 'old password'); const newPassword = screen.getByLabelText('new password'); await user.type(newPassword, 'new password'); const confirmPassword = screen.getByLabelText('confirm new password'); await user.type(confirmPassword, 'new password'); const create = screen.getByRole('button'); await user.click(create); expect(mocks.changePassword).not.toHaveBeenCalled(); }); it('is disabled when new passwords differ', async () => { const oldPassword = screen.getByLabelText('new password'); await user.type(oldPassword, 'old password'); const newPassword = screen.getByLabelText('new password'); await user.type(newPassword, 'new password A'); const confirmPassword = screen.getByLabelText('confirm new password'); await user.type(confirmPassword, 'new password B'); const create = screen.getByRole('button'); await user.click(create); expect(mocks.changePassword).not.toHaveBeenCalled(); }); });