66 lines
2.9 KiB
TypeScript
66 lines
2.9 KiB
TypeScript
// @vitest-environment jsdom
|
|
import '../../test/dom-setup';
|
|
import { describe, it, expect, vi } from 'vitest';
|
|
import { screen } from '@testing-library/react';
|
|
import { renderWithProviders } from '../../test/render-helpers';
|
|
|
|
// react-i18next: pass keys through, matching the convention used by other
|
|
// component tests in this repo (see WorkerStatusWidget.test.tsx). PreferencesForm
|
|
// imports SUPPORTED_LANGUAGES etc. from '../../i18n', which eagerly calls
|
|
// i18n.use(initReactI18next).init(...) — stub that hook too so the module load
|
|
// doesn't throw when react-i18next is mocked wholesale.
|
|
vi.mock('react-i18next', () => ({
|
|
useTranslation: () => ({ t: (key: string) => key, i18n: { resolvedLanguage: 'en', changeLanguage: vi.fn() } }),
|
|
initReactI18next: { type: '3rdParty', init: () => {} },
|
|
}));
|
|
|
|
vi.mock('../../api', () => ({
|
|
fetchMyOrgs: vi.fn(async () => []),
|
|
}));
|
|
|
|
import { PreferencesForm } from './PreferencesForm';
|
|
|
|
describe('PreferencesForm — password change section (issue #799)', () => {
|
|
it('shows the change-password button for a local-credential account', () => {
|
|
renderWithProviders(
|
|
<PreferencesForm
|
|
user={{ defaultVisibility: 'private', defaultVisibilityOrgId: null, hasLocalCredential: true }}
|
|
/>,
|
|
);
|
|
const btn = screen.getByRole('button', { name: 'preferences.password.changeButton' });
|
|
expect(btn).toBeEnabled();
|
|
expect(screen.queryByText('preferences.password.oauthNotice')).not.toBeInTheDocument();
|
|
});
|
|
|
|
it('opens the ChangePasswordDialog when the button is clicked (local account)', async () => {
|
|
const { default: userEvent } = await import('@testing-library/user-event');
|
|
const user = userEvent.setup();
|
|
renderWithProviders(
|
|
<PreferencesForm
|
|
user={{ defaultVisibility: 'private', defaultVisibilityOrgId: null, hasLocalCredential: true }}
|
|
/>,
|
|
);
|
|
await user.click(screen.getByRole('button', { name: 'preferences.password.changeButton' }));
|
|
expect(screen.getByText('change.title')).toBeInTheDocument();
|
|
});
|
|
|
|
it('shows a disabled button + OAuth notice for an account with no local credential', () => {
|
|
renderWithProviders(
|
|
<PreferencesForm
|
|
user={{ defaultVisibility: 'private', defaultVisibilityOrgId: null, hasLocalCredential: false }}
|
|
/>,
|
|
);
|
|
const btn = screen.getByRole('button', { name: 'preferences.password.changeButton' });
|
|
expect(btn).toBeDisabled();
|
|
expect(screen.getByText('preferences.password.oauthNotice')).toBeInTheDocument();
|
|
});
|
|
|
|
it('treats an undefined hasLocalCredential the same as false (fail-closed)', () => {
|
|
renderWithProviders(
|
|
<PreferencesForm user={{ defaultVisibility: 'private', defaultVisibilityOrgId: null }} />,
|
|
);
|
|
expect(screen.getByRole('button', { name: 'preferences.password.changeButton' })).toBeDisabled();
|
|
expect(screen.getByText('preferences.password.oauthNotice')).toBeInTheDocument();
|
|
});
|
|
});
|