test: Add test to check hostname length

This adds a test to check the length of the hostname.
This commit is contained in:
regexowl 2024-12-12 10:14:13 +01:00 committed by Michal Gold
parent dc24ba24e4
commit 3bd4dc89c4
2 changed files with 25 additions and 10 deletions

View file

@ -92,14 +92,14 @@ export const isNtpServerValid = (ntpServer: string) => {
};
export const isHostnameValid = (hostname: string) => {
switch (true) {
case hostname.length > 63:
return false;
case hostname !== '':
return /^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9-]*[A-Za-z0-9])$/.test(
hostname
);
default:
return true;
if (!hostname) {
return true;
}
return (
hostname.length < 65 &&
/^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9-]*[A-Za-z0-9])$/.test(
hostname
)
);
};

View file

@ -44,7 +44,7 @@ const goToReviewStep = async () => {
};
const enterHostname = async (hostname: string) => {
const user = userEvent.setup();
const user = userEvent.setup({ delay: null });
const hostnameInput = await screen.findByPlaceholderText(/Add a hostname/i);
await waitFor(() => user.type(hostnameInput, hostname));
};
@ -113,6 +113,21 @@ describe('Step Hostname', () => {
expect(screen.queryByText(/Invalid hostname/)).not.toBeInTheDocument();
});
test('hostname is invalid for more than 64 chars', async () => {
await renderCreateMode();
await goToHostnameStep();
const nextButton = await getNextButton();
// enter invalid hostname
const invalidHostname = 'a'.repeat(65);
await enterHostname(invalidHostname);
expect(nextButton).toBeDisabled();
// enter valid hostname
await clearHostname();
expect(nextButton).toBeEnabled();
});
test('revisit step button on Review works', async () => {
await renderCreateMode();
await goToHostnameStep();