refactor: extract Welcome page into component

This allows for proper testing and documentation in Storybook.
I also moved the image into its own component. This required me to
adjust the vitest.config.ts slightly because it wasn't finding the image
during tests.

Since components are fully translatable, this means we have new keys
now.

Signed-off-by: André Jaenisch <andre.jaenisch@posteo.de>
This commit is contained in:
André Jaenisch 2024-07-06 18:29:39 +02:00
parent a8097d3363
commit 7484bfeb1a
No known key found for this signature in database
GPG key ID: 5A668E771F1ED854
20 changed files with 591 additions and 20 deletions

4
package-lock.json generated
View file

@ -1,12 +1,12 @@
{
"name": "anvil",
"version": "0.0.6",
"version": "0.0.7",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "anvil",
"version": "0.0.6",
"version": "0.0.7",
"dependencies": {
"axios": "1.7.2",
"cheerio": "1.0.0-rc.12"

View file

@ -1,6 +1,6 @@
{
"name": "anvil",
"version": "0.0.6",
"version": "0.0.7",
"private": true,
"scripts": {
"dev": "vite dev",

View file

@ -12,6 +12,8 @@ You should have received a copy of the GNU Affero General Public License along w
-->
<script>
import { _ } from 'svelte-i18n';
// c.f. https://kit.svelte.dev/docs/images#vite-s-built-in-handling
import logo from '$lib/assets/anvil.png';
@ -21,6 +23,13 @@ You should have received a copy of the GNU Affero General Public License along w
*/
let klass = '';
export { klass as class };
/**
* Translation keys.
*/
export let i18n = {
alt: ''
};
</script>
<img src={logo} alt="Anvil logo" class={klass} />
<img src={logo} alt={$_(i18n.alt)} class={klass} />

View file

@ -15,27 +15,61 @@ You should have received a copy of the GNU Affero General Public License along w
import { _ } from 'svelte-i18n';
import AnvilLogo from '../atoms/AnvilLogo.svelte';
/**
* Translation keys.
*/
export let i18n = {
create: '',
headline: '',
intro: '',
login: '',
logo: {
alt: ''
},
reset: ''
};
/**
* Allow for linking stories in Storybook.
*/
export let sbCreate = '';
/**
* Allow for linking stories in Storybook.
*/
export let sbLogin = '';
/**
* Allow for linking stories in Storybook.
*/
export let sbReset = '';
</script>
<main class="flex h-full">
<div
class="max-w-[400px] mx-auto flex flex-col self-center justify-start items-center content-stretch bg-surface-50 border border-rounded border-surface-200 drop-shadow p-8"
>
<AnvilLogo class="items-center w-[77px]" />
<h1 class="font-semibold text-4xl text-surface-500">{$_('page.welcome.headline')}</h1>
<p class="text-surface-500">{$_('page.welcome.intro')}</p>
<AnvilLogo i18n={i18n.logo} class="items-center w-[77px]" />
<h1 class="font-semibold text-4xl text-surface-500">{$_(i18n.headline)}</h1>
<p class="text-surface-500">{$_(i18n.intro)}</p>
<div class="space-y-3">
<div>
<a href="/account/create" class="btn variant-filled-primary">Create F2 account</a>
<a href="/account/create" class="btn variant-filled-primary" data-sb-kind={sbCreate}
>{$_(i18n.create)}</a
>
</div>
<div>
<a href="/account/login" class="btn text-primary-500 font-semibold variant-filled-ghost"
>I have an account</a
<a
href="/account/login"
class="btn text-primary-500 font-semibold variant-filled-ghost"
data-sb-kind={sbLogin}>{$_(i18n.login)}</a
>
</div>
</div>
<div class="text-center">
<a href="/" class="btn bg-initial text-surface-500">Reset passphrase</a>
<a href="/" class="btn bg-initial text-surface-500" data-sb-kind={sbReset}>{$_(i18n.reset)}</a
>
</div>
</div>
</main>

View file

@ -0,0 +1,18 @@
<!--
Welcome page.
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/>.
-->
<script>
import Welcome from '../templates/Welcome.svelte';
</script>
<Welcome />

View file

@ -0,0 +1,32 @@
<!--
Welcome template.
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/>.
-->
<script>
import Welcome from '../molecules/Welcome.svelte';
/**
* Translation keys.
*/
const i18n = {
create: 'page.welcome.create',
headline: 'page.welcome.headline',
intro: 'page.welcome.intro',
login: 'page.welcome.login',
logo: {
alt: 'page.welcome.logo.alt'
},
reset: 'page.welcome.reset'
};
</script>
<Welcome {i18n} />

View file

@ -128,8 +128,14 @@
}
},
"welcome": {
"create": "",
"intro": "",
"headline": "",
"intro": ""
"login": "",
"logo": {
"alt": ""
},
"reset": ""
}
}
}

View file

@ -128,8 +128,14 @@
}
},
"welcome": {
"create": "",
"intro": "",
"headline": "Willkommen bei Anvil",
"intro": ""
"login": "",
"logo": {
"alt": ""
},
"reset": ""
}
}
}

View file

@ -128,8 +128,14 @@
}
},
"welcome": {
"create": "Create F2 account",
"headline": "Welcome to Anvil",
"intro": "Anvil is a F2 („ForgeFed”) client. If you don't have an F2 account you need to create one on an F2 server."
"intro": "Anvil is a F2 („ForgeFed”) client. If you don't have an F2 account you need to create one on an F2 server.",
"login": "I have an account",
"logo": {
"alt": "Anvil logo"
},
"reset": "Reset passphrase"
}
}
}

View file

@ -128,8 +128,14 @@
}
},
"welcome": {
"create": "",
"headline": "",
"intro": ""
"intro": "",
"login": "",
"logo": {
"alt": ""
},
"reset": ""
}
}
}

View file

@ -128,8 +128,14 @@
}
},
"welcome": {
"create": "",
"headline": "Witamy w Anvil",
"intro": ""
"intro": "",
"login": "",
"logo": {
"alt": ""
},
"reset": ""
}
}
}

View file

@ -8,7 +8,7 @@ SPDX-License-Identifier: AGPL-3.0-or-later
<script lang="ts">
import { _ } from 'svelte-i18n';
import Welcome from '$lib/components/molecules/Welcome.svelte';
import Welcome from '$lib/components/pages/Welcome.svelte';
</script>
<Welcome />

View file

@ -17,10 +17,33 @@ import Welcome from '$lib/components/molecules/Welcome.svelte';
const meta = {
title: 'Molecules/Welcome',
component: Welcome,
tags: ['autodocs']
tags: ['autodocs'],
args: {
sbCreate: 'Templates/Login',
sbLogin: 'Templates/Login',
sbReset: 'Templates/Login'
},
argTypes: {
sbCreate: { control: 'radio', options: ['Templates/Login'] },
sbLogin: { control: 'radio', options: ['Templates/Login'] },
sbReset: { control: 'radio', options: ['Templates/Login'] }
}
} satisfies Meta<Welcome>;
export default meta;
type Story = StoryObj<typeof meta>;
export const Plain: Story = {};
export const Plain: Story = {
args: {
i18n: {
create: 'page.welcome.create',
headline: 'page.welcome.headline',
intro: 'page.welcome.intro',
login: 'page.welcome.login',
logo: {
alt: 'page.welcome.logo.alt'
},
reset: 'page.welcome.reset'
}
}
};

View file

@ -0,0 +1,26 @@
/* Stories for Welcome page.
* 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 type { Meta, StoryObj } from '@storybook/svelte';
import Welcome from '$lib/components/pages/Welcome.svelte';
const meta = {
title: 'Pages/Welcome',
component: Welcome,
tags: ['autodocs']
} satisfies Meta<Welcome>;
export default meta;
type Story = StoryObj<typeof meta>;
export const Plain: Story = {};

View file

@ -0,0 +1,26 @@
/* Stories for Welcome template.
* 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 type { Meta, StoryObj } from '@storybook/svelte';
import Welcome from '$lib/components/templates/Welcome.svelte';
const meta = {
title: 'Templates/Welcome',
component: Welcome,
tags: ['autodocs']
} satisfies Meta<Welcome>;
export default meta;
type Story = StoryObj<typeof meta>;
export const Plain: Story = {};

View file

@ -0,0 +1,74 @@
/* Component test for AnvilLogo atom.
* 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 AnvilLogo from '../../../src/lib/components/atoms/AnvilLogo.svelte';
import enMessages from '../../../src/lib/i18n/locales/en.json';
describe('AnvilLogo.svelte', () => {
beforeEach(() => {
register('en', () => import('../../../src/lib/i18n/locales/en.json'));
init({ fallbackLocale: 'en', initialLocale: 'en' });
locale.set('en');
});
it('should mount', () => {
// Arrange
// Nothing to prepare
// Act
const { container } = render(AnvilLogo);
// Assert
expect(container).toBeTruthy();
});
it('should have a logo', () => {
// Arrange
// Nothing to prepare
// Act
render(AnvilLogo);
// Assert
// No alt text and therefore no accessible name. Turns role img into presentation
expect(screen.getByRole('presentation')).toBeInTheDocument();
});
it('should have a logo', () => {
// Arrange
const i18n = {
alt: 'Testing Anvil'
};
// Act
render(AnvilLogo, { i18n });
// Assert
expect(screen.getByRole('img')).toBeInTheDocument();
});
it('should accept arbitrary classes', () => {
// Arrange
const klass = 'w-full';
// Act
render(AnvilLogo, { class: klass });
// Assert
expect(screen.getByRole('presentation')).toBeInTheDocument();
expect(screen.getByRole('presentation')).toHaveClass('w-full');
});
});

View file

@ -0,0 +1,73 @@
/* Component test for Welcome molecule.
* 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 Welcome from '../../../src/lib/components/molecules/Welcome.svelte';
import enMessages from '../../../src/lib/i18n/locales/en.json';
describe('Welcome.svelte', () => {
beforeEach(() => {
register('en', () => import('../../../src/lib/i18n/locales/en.json'));
init({ fallbackLocale: 'en', initialLocale: 'en' });
locale.set('en');
});
it('should mount', () => {
// Arrange
// Nothing to prepare
// Act
const { container } = render(Welcome);
// Assert
expect(container).toBeTruthy();
});
it('should have a logo', () => {
// Arrange
// Nothing to prepare
// Act
render(Welcome);
// Assert
// No alt text and therefore no accessible name. Turns role img into presentation
expect(screen.getByRole('presentation')).toBeInTheDocument();
});
it('should have a h1', () => {
// Arrange
// Nothing to prepare
// Act
render(Welcome);
const h1 = screen.getByRole('heading', { level: 1 });
// Assert
expect(h1).toBeInTheDocument();
});
it('should have three links', () => {
// Arrange
// Nothing to prepare
// Act
render(Welcome);
const buttons = screen.getAllByRole('link');
// Assert
expect(buttons).toHaveLength(3);
});
});

View file

@ -0,0 +1,109 @@
/* Component test for Welcome page.
* 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 Welcome from '../../../src/lib/components/pages/Welcome.svelte';
import enMessages from '../../../src/lib/i18n/locales/en.json';
describe('Welcome.svelte', () => {
beforeEach(() => {
register('en', () => import('../../../src/lib/i18n/locales/en.json'));
init({ fallbackLocale: 'en', initialLocale: 'en' });
locale.set('en');
});
it('should mount', () => {
// Arrange
// Nothing to prepare
// Act
const { container } = render(Welcome);
// Assert
expect(container).toBeTruthy();
});
it('should have a logo', () => {
// Arrange
// Nothing to prepare
// Act
render(Welcome);
// Assert
expect(screen.getByRole('img')).toBeInTheDocument();
expect(screen.getByRole('img')).toHaveAccessibleName(enMessages.page.welcome.logo.alt);
});
it('should have a h1', () => {
// Arrange
// Nothing to prepare
// Act
render(Welcome);
const h1 = screen.getByRole('heading', { level: 1 });
// Assert
expect(h1).toBeInTheDocument();
expect(h1).toHaveTextContent(enMessages.page.welcome.headline);
});
it('should have an intro text', () => {
// Arrange
// Nothing to prepare
// Act
render(Welcome);
// Assert
expect(screen.getByText(enMessages.page.welcome.intro)).toBeInTheDocument();
});
it('should have a signup link', () => {
// Arrange
// Nothing to prepare
// Act
render(Welcome);
const button = screen.getByRole('link', { name: enMessages.page.welcome.create });
// Assert
expect(button).toBeInTheDocument();
});
it('should have a signin link', () => {
// Arrange
// Nothing to prepare
// Act
render(Welcome);
const button = screen.getByRole('link', { name: enMessages.page.welcome.login });
// Assert
expect(button).toBeInTheDocument();
});
it('should have a reset link', () => {
// Arrange
// Nothing to prepare
// Act
render(Welcome);
const button = screen.getByRole('link', { name: enMessages.page.welcome.reset });
// Assert
expect(button).toBeInTheDocument();
});
});

View file

@ -0,0 +1,109 @@
/* Component test for Welcome template.
* 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 Welcome from '../../../src/lib/components/templates/Welcome.svelte';
import enMessages from '../../../src/lib/i18n/locales/en.json';
describe('Welcome.svelte', () => {
beforeEach(() => {
register('en', () => import('../../../src/lib/i18n/locales/en.json'));
init({ fallbackLocale: 'en', initialLocale: 'en' });
locale.set('en');
});
it('should mount', () => {
// Arrange
// Nothing to prepare
// Act
const { container } = render(Welcome);
// Assert
expect(container).toBeTruthy();
});
it('should have a logo', () => {
// Arrange
// Nothing to prepare
// Act
render(Welcome);
// Assert
expect(screen.getByRole('img')).toBeInTheDocument();
expect(screen.getByRole('img')).toHaveAccessibleName(enMessages.page.welcome.logo.alt);
});
it('should have a h1', () => {
// Arrange
// Nothing to prepare
// Act
render(Welcome);
const h1 = screen.getByRole('heading', { level: 1 });
// Assert
expect(h1).toBeInTheDocument();
expect(h1).toHaveTextContent(enMessages.page.welcome.headline);
});
it('should have an intro text', () => {
// Arrange
// Nothing to prepare
// Act
render(Welcome);
// Assert
expect(screen.getByText(enMessages.page.welcome.intro)).toBeInTheDocument();
});
it('should have a signup link', () => {
// Arrange
// Nothing to prepare
// Act
render(Welcome);
const button = screen.getByRole('link', { name: enMessages.page.welcome.create });
// Assert
expect(button).toBeInTheDocument();
});
it('should have a signin link', () => {
// Arrange
// Nothing to prepare
// Act
render(Welcome);
const button = screen.getByRole('link', { name: enMessages.page.welcome.login });
// Assert
expect(button).toBeInTheDocument();
});
it('should have a reset link', () => {
// Arrange
// Nothing to prepare
// Act
render(Welcome);
const button = screen.getByRole('link', { name: enMessages.page.welcome.reset });
// Assert
expect(button).toBeInTheDocument();
});
});

View file

@ -10,14 +10,22 @@
* 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 { resolve } from 'node:path';
import { svelte } from '@sveltejs/vite-plugin-svelte';
import { defineConfig } from 'vitest/config';
export default defineConfig({
resolve: {
alias: {
// Help vitest find imported images
$lib: resolve('./src/lib')
}
},
plugins: [svelte({ hot: !process.env.VITEST })],
test: {
coverage: {
include: ['src/**/*.svelte']
include: ['src']
},
include: ['tests/**/*.test.ts'],
environment: 'jsdom',