Update server-related tests to cover forwardCredentials option

This commit is contained in:
Alejandro Celaya 2025-04-20 11:55:34 +02:00
parent e997d11c2c
commit e12cd68010
5 changed files with 39 additions and 12 deletions

View File

@ -35,7 +35,7 @@ describe('ShlinkApiClientBuilder', () => {
expect(secondApiClient === thirdApiClient).toEqual(true);
});
it.only('does not fetch from state when provided param is already selected server', async () => {
it('does not fetch from state when provided param is already selected server', async () => {
const url = 'the_url';
const apiKey = 'the_api_key';
const jsonRequest = vi.fn();

View File

@ -36,7 +36,7 @@ describe('<DeleteServerModal />', () => {
expect(screen.getByText(serverName)).toBeInTheDocument();
});
it.only.each([
it.each([
[() => screen.getByRole('button', { name: 'Cancel' })],
[() => screen.getByLabelText('Close dialog')],
])('closes dialog when clicking cancel button', async (getButton) => {

View File

@ -47,16 +47,16 @@ describe('<EditServer />', () => {
it('display the server info in the form components', () => {
setUp();
expect(screen.getByDisplayValue('the_name')).toBeInTheDocument();
expect(screen.getByDisplayValue('the_url')).toBeInTheDocument();
expect(screen.getByDisplayValue('the_api_key')).toBeInTheDocument();
expect(screen.getByLabelText(/^Name/)).toBeInTheDocument();
expect(screen.getByLabelText(/^URL/)).toBeInTheDocument();
expect(screen.getByLabelText(/^API key/)).toBeInTheDocument();
});
it('edits server and redirects to it when form is submitted', async () => {
const { user, history } = setUp();
await user.type(screen.getByDisplayValue('the_name'), ' edited');
await user.type(screen.getByDisplayValue('the_url'), ' edited');
await user.type(screen.getByLabelText(/^Name/), ' edited');
await user.type(screen.getByLabelText(/^URL/), ' edited');
// TODO Using fire event because userEvent.click on the Submit button does not submit the form
// await user.click(screen.getByRole('button', { name: 'Save' }));
fireEvent.submit(screen.getByRole('form'));
@ -65,9 +65,26 @@ describe('<EditServer />', () => {
name: 'the_name edited',
url: 'the_url edited',
apiKey: 'the_api_key',
forwardCredentials: false,
});
// After saving we go back, to the first route from history's initialEntries
expect(history.location.pathname).toEqual('/foo');
});
it.each([
{ forwardCredentials: true },
{ forwardCredentials: false },
])('edits advanced options - forward credentials', async (serverPartial) => {
const { user } = setUp({ ...defaultSelectedServer, ...serverPartial });
await user.click(screen.getByText('Advanced options'));
await user.click(screen.getByLabelText('Forward credentials (like cookies) to this server on every request.'));
fireEvent.submit(screen.getByRole('form'));
expect(editServerMock).toHaveBeenCalledWith('abc123', expect.objectContaining({
forwardCredentials: !serverPartial.forwardCredentials,
}));
});
});

View File

@ -1,10 +1,11 @@
import { fireEvent, render, screen } from '@testing-library/react';
import { fireEvent, screen } from '@testing-library/react';
import { ServerForm } from '../../../src/servers/helpers/ServerForm';
import { checkAccessibility } from '../../__helpers__/accessibility';
import { renderWithEvents } from '../../__helpers__/setUpTest';
describe('<ServerForm />', () => {
const onSubmit = vi.fn();
const setUp = () => render(<ServerForm onSubmit={onSubmit}>Something</ServerForm>);
const setUp = () => renderWithEvents(<ServerForm onSubmit={onSubmit}>Something</ServerForm>);
it('passes a11y checks', () => checkAccessibility(setUp()));
@ -15,6 +16,7 @@ describe('<ServerForm />', () => {
expect(screen.getByLabelText(/^URL/)).toBeInTheDocument();
expect(screen.getByLabelText(/^API key/)).toBeInTheDocument();
expect(screen.getByText('Something')).toBeInTheDocument();
expect(screen.getByText('Advanced options')).toBeInTheDocument();
});
it('invokes submit callback when submit event is triggered', async () => {
@ -24,4 +26,13 @@ describe('<ServerForm />', () => {
fireEvent.submit(screen.getByRole('form'), { preventDefault: vi.fn() });
expect(onSubmit).toHaveBeenCalled();
});
it('shows advanced options', async () => {
const { user } = setUp();
const forwardCredentialsLabel = 'Forward credentials (like cookies) to this server on every request.';
expect(screen.queryByLabelText(forwardCredentialsLabel)).not.toBeInTheDocument();
await user.click(screen.getByText('Advanced options'));
expect(screen.getByLabelText(forwardCredentialsLabel)).toBeInTheDocument();
});
});

View File

@ -44,7 +44,6 @@ export default defineConfig({
instances: [{ browser: 'chromium' }],
},
globals: true,
allowOnly: true,
setupFiles: './config/test/setupTests.ts',
coverage: {
provider: 'v8',
@ -61,8 +60,8 @@ export default defineConfig({
// Required code coverage. Lower than this will make the check fail
thresholds: {
statements: 95,
branches: 90,
functions: 90,
branches: 95,
functions: 95,
lines: 95,
},
},