Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(components): gs-app: validate attributes #593

Merged
merged 1 commit into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions components/src/web-components/app.stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,21 @@ export const Default: StoryObj<{ lapis: string }> = {
},
};

export const WithNoLapisUrl: StoryObj<{ lapis: string }> = {
...Default,
args: {
...Default.args,
lapis: 'notAValidUrl',
},
play: async ({ canvasElement }) => {
const canvas = within(canvasElement);

await waitFor(() => {
expect(canvas.getByText("Error: Invalid LAPIS URL: 'notAValidUrl'", { exact: false })).toBeVisible();
});
},
};

export const DelayFetchingReferenceGenome: StoryObj<{ lapis: string }> = {
...Template,
parameters: {
Expand All @@ -83,13 +98,13 @@ export const DelayFetchingReferenceGenome: StoryObj<{ lapis: string }> = {
export const FailsToFetchReferenceGenome: StoryObj<{ lapis: string }> = {
...Template,
args: {
lapis: 'definitely-not-a-valid-url',
lapis: 'https://url.to.lapis-definitely-not-a-valid-url',
},
play: async ({ canvasElement }) => {
const canvas = within(canvasElement);

await waitFor(() => {
expect(canvas.getByText('Error', { exact: false })).toBeVisible();
expect(canvas.getByText('Error: Cannot fetch reference genome.', { exact: false })).toBeVisible();
});
},
};
Expand Down
22 changes: 17 additions & 5 deletions components/src/web-components/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@ import { Task } from '@lit/task';
import { html, LitElement } from 'lit';
import { customElement, property } from 'lit/decorators.js';
import { type DetailedHTMLProps, type HTMLAttributes } from 'react';
import z from 'zod';

import { lapisContext } from './lapis-context';
import { referenceGenomeContext } from './reference-genome-context';
import { type ReferenceGenome } from '../lapisApi/ReferenceGenome';
import { fetchReferenceGenome } from '../lapisApi/lapisApi';

const lapisUrlSchema = z.string().url();

/**
* ## Context
*
Expand Down Expand Up @@ -50,18 +53,23 @@ export class App extends LitElement {
*/
private updateReferenceGenome = new Task(this, {
task: async () => {
this.referenceGenome = await fetchReferenceGenome(this.lapis);
const lapisUrl = lapisUrlSchema.parse(this.lapis);

this.referenceGenome = await fetchReferenceGenome(lapisUrl);
},
args: () => [this.lapis],
});

override render() {
return this.updateReferenceGenome.render({
complete: () => html``, // Children will be rendered in the light DOM anyway. We can't use slots without a shadow DOM.
error: () =>
html` <div class="m-2 w-full alert alert-error">
Error: Cannot fetch reference genome. Is LAPIS available?
</div>`,
error: (error) => {
if (error instanceof z.ZodError) {
return GsAppError(`Invalid LAPIS URL: '${this.lapis}'`);
}

return GsAppError('Cannot fetch reference genome. Is LAPIS available?');
},
});
}

Expand All @@ -70,6 +78,10 @@ export class App extends LitElement {
}
}

function GsAppError(error: string) {
return html` <div class="m-2 w-full alert alert-error">Error: ${error}</div>`;
}

declare global {
interface HTMLElementTagNameMap {
'gs-app': App;
Expand Down
Loading