summaryrefslogtreecommitdiff
path: root/ui/tests/lib/components/ChangePassword.svelte.test.js
blob: 8171efd3332eaa0fde9e30d8f5b150fbd57a8ae0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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();
  });
});