This required adding annotation to the whole project. I need to double-check whether I have the license banner of AGPL in place everywhere. Signed-off-by: André Jaenisch <andre.jaenisch@posteo.de>
303 lines
7.5 KiB
TypeScript
303 lines
7.5 KiB
TypeScript
/* Component test for LoginForm organism.
|
|
* Copyright (C) 2024 André Jaenisch
|
|
* SPDX-FileCopyrightText: 2024 André Jaenisch
|
|
* SPDX-License-Identifier: AGPL-3.0-or-later
|
|
*
|
|
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
|
|
*
|
|
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU Affero General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
import '@testing-library/jest-dom';
|
|
import { render, screen } from '@testing-library/svelte';
|
|
import { init, locale, register } from 'svelte-i18n';
|
|
|
|
import LoginForm from '../../../src/lib/components/organisms/LoginForm.svelte';
|
|
import enMessages from '../../../src/lib/i18n/locales/en.json';
|
|
|
|
describe('LoginForm.svelte', () => {
|
|
beforeEach(() => {
|
|
register('en', () => import('../../../src/lib/i18n/locales/en.json'));
|
|
init({ fallbackLocale: 'en', initialLocale: 'en' });
|
|
locale.set('en');
|
|
});
|
|
|
|
it('should mount', () => {
|
|
// Arrange
|
|
const form = {};
|
|
const i18n = {
|
|
fields: {
|
|
account: {
|
|
label: enMessages.page.login.form.fields.account.label
|
|
},
|
|
passphrase: {
|
|
label: enMessages.page.login.form.fields.passphrase.label
|
|
},
|
|
server: {
|
|
label: enMessages.page.login.form.fields.server.label
|
|
}
|
|
}
|
|
};
|
|
const servers = [];
|
|
|
|
// Act
|
|
const { container } = render(LoginForm, { form, i18n, servers });
|
|
|
|
// Assert
|
|
expect(container).toBeTruthy();
|
|
});
|
|
|
|
it('should have a form', () => {
|
|
// Arrange
|
|
const form = {};
|
|
const i18n = {
|
|
fields: {
|
|
account: {
|
|
label: enMessages.page.login.form.fields.account.label
|
|
},
|
|
passphrase: {
|
|
label: enMessages.page.login.form.fields.passphrase.label
|
|
},
|
|
server: {
|
|
label: enMessages.page.login.form.fields.server.label
|
|
}
|
|
}
|
|
};
|
|
const servers = [];
|
|
|
|
// Act
|
|
render(LoginForm, { form, i18n, servers });
|
|
const formElement = screen.getByRole('form');
|
|
|
|
// Assert
|
|
expect(formElement).toBeInTheDocument();
|
|
});
|
|
|
|
it('should have a server select control', () => {
|
|
// Arrange
|
|
const form = {};
|
|
const i18n = {
|
|
fields: {
|
|
account: {
|
|
label: enMessages.page.login.form.fields.account.label
|
|
},
|
|
passphrase: {
|
|
label: enMessages.page.login.form.fields.passphrase.label
|
|
},
|
|
server: {
|
|
label: enMessages.page.login.form.fields.server.label
|
|
}
|
|
}
|
|
};
|
|
const servers = [];
|
|
|
|
// Act
|
|
render(LoginForm, { form, i18n, servers });
|
|
const server = screen.getByLabelText(enMessages.page.login.form.fields.server.label);
|
|
|
|
// Assert
|
|
expect(server).toBeInTheDocument();
|
|
});
|
|
|
|
it('should have a account input control', () => {
|
|
// Arrange
|
|
const form = {};
|
|
const i18n = {
|
|
fields: {
|
|
account: {
|
|
label: enMessages.page.login.form.fields.account.label
|
|
},
|
|
passphrase: {
|
|
label: enMessages.page.login.form.fields.passphrase.label
|
|
},
|
|
server: {
|
|
label: enMessages.page.login.form.fields.server.label
|
|
}
|
|
}
|
|
};
|
|
const servers = [];
|
|
|
|
// Act
|
|
render(LoginForm, { form, i18n, servers });
|
|
const account = screen.getByLabelText(enMessages.page.login.form.fields.account.label);
|
|
|
|
// Assert
|
|
expect(account).toBeInTheDocument();
|
|
});
|
|
|
|
it('should have a passphrase input control', () => {
|
|
// Arrange
|
|
const form = {};
|
|
const i18n = {
|
|
fields: {
|
|
account: {
|
|
label: enMessages.page.login.form.fields.account.label
|
|
},
|
|
passphrase: {
|
|
label: enMessages.page.login.form.fields.passphrase.label
|
|
},
|
|
server: {
|
|
label: enMessages.page.login.form.fields.server.label
|
|
}
|
|
}
|
|
};
|
|
const servers = [];
|
|
|
|
// Act
|
|
render(LoginForm, { form, i18n, servers });
|
|
const passphrase = screen.getByLabelText(enMessages.page.login.form.fields.passphrase.label);
|
|
|
|
// Assert
|
|
expect(passphrase).toBeInTheDocument();
|
|
});
|
|
|
|
it('should have a reset passphrase link', () => {
|
|
// Arrange
|
|
const form = {};
|
|
const i18n = {
|
|
fields: {
|
|
account: {
|
|
label: enMessages.page.login.form.fields.account.label
|
|
},
|
|
passphrase: {
|
|
label: enMessages.page.login.form.fields.passphrase.label
|
|
},
|
|
server: {
|
|
label: enMessages.page.login.form.fields.server.label
|
|
}
|
|
},
|
|
reset: enMessages.page.login.form.reset
|
|
};
|
|
const servers = [];
|
|
|
|
// Act
|
|
render(LoginForm, { form, i18n, servers });
|
|
const reset = screen.getByRole('link', { name: enMessages.page.login.form.reset });
|
|
|
|
// Assert
|
|
expect(reset).toBeInTheDocument();
|
|
});
|
|
|
|
it('should have a submit button', () => {
|
|
// Arrange
|
|
const form = {};
|
|
const i18n = {
|
|
fields: {
|
|
account: {
|
|
label: enMessages.page.login.form.fields.account.label
|
|
},
|
|
passphrase: {
|
|
label: enMessages.page.login.form.fields.passphrase.label
|
|
},
|
|
server: {
|
|
label: enMessages.page.login.form.fields.server.label
|
|
}
|
|
},
|
|
submit: enMessages.page.login.form.submit
|
|
};
|
|
const servers = [];
|
|
|
|
// Act
|
|
render(LoginForm, { form, i18n, servers });
|
|
const submit = screen.getByRole('button', { name: enMessages.page.login.form.submit });
|
|
|
|
// Assert
|
|
expect(submit).toBeInTheDocument();
|
|
});
|
|
|
|
it('should have no validation errors', () => {
|
|
// Arrange
|
|
const form = {};
|
|
const i18n = {
|
|
fields: {
|
|
account: {
|
|
label: enMessages.page.login.form.fields.account.label
|
|
},
|
|
passphrase: {
|
|
label: enMessages.page.login.form.fields.passphrase.label
|
|
},
|
|
server: {
|
|
label: enMessages.page.login.form.fields.server.label
|
|
}
|
|
}
|
|
};
|
|
const servers = [];
|
|
|
|
// Act
|
|
render(LoginForm, { form, i18n, servers });
|
|
|
|
// Assert
|
|
expect(
|
|
screen.queryByText(enMessages.page.login.form.validation.incorrect)
|
|
).not.toBeInTheDocument();
|
|
expect(
|
|
screen.queryByText(enMessages.page.login.form.validation.missing)
|
|
).not.toBeInTheDocument();
|
|
});
|
|
|
|
describe('when validation failed', () => {
|
|
it('should show an error on required inputs', () => {
|
|
// Arrange
|
|
const form = {
|
|
missing: true
|
|
};
|
|
const i18n = {
|
|
fields: {
|
|
account: {
|
|
label: enMessages.page.login.form.fields.account.label
|
|
},
|
|
passphrase: {
|
|
label: enMessages.page.login.form.fields.passphrase.label
|
|
},
|
|
server: {
|
|
label: enMessages.page.login.form.fields.server.label
|
|
}
|
|
},
|
|
validation: {
|
|
missing: enMessages.page.login.form.validation.missing
|
|
}
|
|
};
|
|
const servers = [];
|
|
|
|
// Act
|
|
render(LoginForm, { form, i18n, servers });
|
|
const intro = screen.getByText(enMessages.page.login.form.validation.missing);
|
|
|
|
// Assert
|
|
expect(intro).toBeInTheDocument();
|
|
});
|
|
|
|
it('should show an error on invalid inputs', () => {
|
|
// Arrange
|
|
const form = {
|
|
incorrect: true
|
|
};
|
|
const i18n = {
|
|
fields: {
|
|
account: {
|
|
label: enMessages.page.login.form.fields.account.label
|
|
},
|
|
passphrase: {
|
|
label: enMessages.page.login.form.fields.passphrase.label
|
|
},
|
|
server: {
|
|
label: enMessages.page.login.form.fields.server.label
|
|
}
|
|
},
|
|
validation: {
|
|
incorrect: enMessages.page.login.form.validation.incorrect
|
|
}
|
|
};
|
|
const servers = [];
|
|
|
|
// Act
|
|
render(LoginForm, { form, i18n, servers });
|
|
const intro = screen.getByText(enMessages.page.login.form.validation.incorrect);
|
|
|
|
// Assert
|
|
expect(intro).toBeInTheDocument();
|
|
});
|
|
});
|
|
});
|