Skip to content
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
99 changes: 99 additions & 0 deletions e2e/documents-sort.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import { expect, test, type Page } from '@playwright/test';

const doc = (id: string, name: string, createdAt: number, updatedAt: number) => ({
id,
schemaVersion: 1,
name,
createdAt,
updatedAt,
pages: {
p1: {
id: 'p1',
boundaries: { horizontal: { start: 100, end: 700 }, vertical: { start: 100, end: 1000 } },
elements: {}
}
}
});

// Names are chosen so each sort order gives a different, easy to read list
const docs = [
doc('a', 'Invoice 10', 100, 5000),
doc('b', 'invoice 2', 300, 9000),
doc('c', 'Zebra', 200, 1000),
doc('d', 'apple', 400, 7000)
];

async function seed(page: Page) {
await page.goto('/documents');
await page.getByText('No documents').waitFor();
await page.evaluate(
(records) =>
new Promise<void>((resolve, reject) => {
const open = indexedDB.open('lienzo-db', 1);
open.onsuccess = () => {
const tx = open.result.transaction('documents', 'readwrite');
for (const record of records) tx.objectStore('documents').put(record);
tx.oncomplete = () => resolve();
tx.onerror = () => reject(tx.error);
};
open.onerror = () => reject(open.error);
}),
docs
);
await page.reload();
await page.waitForLoadState('networkidle');
}

const names = (page: Page) => page.locator('main h3').allTextContents();

test.describe('Documents sorting', () => {
test('starts with the most recently updated, and each option reorders the list', async ({
page
}) => {
await seed(page);
const select = page.getByTestId('sort-select');
await expect(select).toHaveValue('updated-desc');
expect(await names(page)).toEqual(['invoice 2', 'apple', 'Invoice 10', 'Zebra']);

await select.selectOption('updated-asc');
expect(await names(page)).toEqual(['Zebra', 'Invoice 10', 'apple', 'invoice 2']);

await select.selectOption('created-desc');
expect(await names(page)).toEqual(['apple', 'invoice 2', 'Zebra', 'Invoice 10']);

await select.selectOption('name-asc');
expect(await names(page)).toEqual(['apple', 'invoice 2', 'Invoice 10', 'Zebra']);

await select.selectOption('name-desc');
expect(await names(page)).toEqual(['Zebra', 'Invoice 10', 'invoice 2', 'apple']);
});

test('the choice is remembered, and the search keeps the order', async ({ page }) => {
await seed(page);
await page.getByTestId('sort-select').selectOption('name-asc');

await page.reload();
await page.waitForLoadState('networkidle');
await expect(page.getByTestId('sort-select')).toHaveValue('name-asc');
expect(await names(page)).toEqual(['apple', 'invoice 2', 'Invoice 10', 'Zebra']);

await page.getByLabel('Search documents').fill('invoice');
await expect.poll(() => names(page)).toEqual(['invoice 2', 'Invoice 10']);
});

test('on a phone the control sits on the right', async ({ browser }) => {
const context = await browser.newContext({ viewport: { width: 375, height: 812 } });
const page = await context.newPage();
await seed(page);
const box = await page.getByTestId('sort-select').boundingBox();
expect(box).not.toBeNull();
expect(box!.x + box!.width).toBeGreaterThan(375 - 24); // against the right edge, not the left
await context.close();
});

test('an unknown saved choice falls back to the default', async ({ page }) => {
await page.addInitScript(() => localStorage.setItem('lienzo-documents-sort', 'sideways'));
await seed(page);
await expect(page.getByTestId('sort-select')).toHaveValue('updated-desc');
});
});
44 changes: 43 additions & 1 deletion e2e/seo.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,56 @@ test.describe('SEO', () => {
});
}

test('sitemap lists only the landing page', async ({ request }) => {
test('sitemap lists the landing page, not the private pages', async ({ request }) => {
const res = await request.get('/sitemap.xml');
expect(res.ok()).toBe(true);
const body = await res.text();
expect(body).toContain(`<loc>${SITE}/</loc>`);
expect(body).not.toContain('/canvas');
});

test('every template has its own indexable page, listed in the sitemap', async ({
page,
request
}) => {
const body = await (await request.get('/sitemap.xml')).text();
expect(body).toContain(`<loc>${SITE}/templates</loc>`);
expect(body).toContain(`<loc>${SITE}/templates/resume</loc>`);
const listed = body.match(/\/templates\/[a-z-]+</g) ?? [];
expect(listed.length).toBeGreaterThanOrEqual(22);

await page.goto('/templates/invoice');
await expect(page).toHaveTitle(/Invoice template/);
await expect(page.locator('meta[name="robots"]')).toHaveCount(0);
await expect(page.locator('link[rel="canonical"]')).toHaveAttribute(
'href',
`${SITE}/templates/invoice`
);
await expect(page.getByRole('heading', { level: 1 })).toHaveText('Invoice template');
const jsonLd = JSON.parse(
(await page.locator('script[type="application/ld+json"]').textContent()) ?? '{}'
);
expect(jsonLd['@graph'].map((n: { '@type': string }) => n['@type'])).toEqual([
'CreativeWork',
'BreadcrumbList'
]);

// links in the prerendered page are absolute: relative ones pointed at /templates/templates
const hrefs = await page
.locator('main a')
.evaluateAll((all) => all.map((a) => a.getAttribute('href')));
expect(hrefs.length).toBeGreaterThan(0);
expect(hrefs.every((h) => h?.startsWith('/'))).toBe(true);

await page.getByTestId('use-this-template').click();
await expect(page).toHaveURL(/\/canvas$/);
await expect(page.getByTestId('document-title')).toHaveText('Invoice');
});

test('an unknown template is a 404', async ({ request }) => {
expect((await request.get('/templates/not-a-template')).status()).toBe(404);
});

test('robots.txt points to the sitemap', async ({ request }) => {
const res = await request.get('/robots.txt');
expect(await res.text()).toContain(`Sitemap: ${SITE}/sitemap.xml`);
Expand Down
110 changes: 110 additions & 0 deletions e2e/templates.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import { expect, test } from '@playwright/test';

test.describe('Template gallery', () => {
test('lists every template, and the blank document first', async ({ page }) => {
await page.goto('/templates');
await expect(page.getByRole('heading', { level: 1 })).toHaveText('Start from a template');
await expect(page.getByTestId('template-blank')).toBeVisible();
expect(await page.getByTestId('template-card').count()).toBeGreaterThanOrEqual(22);
});

test('filters by category and by search', async ({ page }) => {
await page.goto('/templates');
await page.waitForLoadState('networkidle');
const cards = page.getByTestId('template-card');
const all = await cards.count();

await page.getByTestId('category-Events').click();
await expect(page.getByTestId('category-Events')).toHaveAttribute('aria-pressed', 'true');
const events = await cards.count();
expect(events).toBeGreaterThan(0);
expect(events).toBeLessThan(all);
await expect(page.getByTestId('use-resume')).toHaveCount(0);

await page.getByTestId('category-All').click();
await page.getByLabel('Search templates').fill('invoice');
await expect(cards).toHaveCount(1);
await expect(page.getByTestId('use-invoice')).toBeVisible();

await page.getByLabel('Search templates').fill('zzzz');
await expect(cards).toHaveCount(0);
await expect(page.getByRole('status')).toContainText('No templates match');
});

test('a template opens in the editor as its own editable document', async ({ page }) => {
await page.goto('/templates');
await page.waitForLoadState('networkidle');
await page.getByTestId('use-resume').click();

await expect(page).toHaveURL(/\/canvas$/);
await expect(page.getByTestId('document-title')).toHaveText('Resume');
const texts = page.locator('textarea');
await expect(texts.first()).toBeVisible();
expect(await texts.count()).toBeGreaterThan(8);
const values = await texts.evaluateAll((all) =>
all.map((t) => (t as HTMLTextAreaElement).value)
);
expect(values).toContain('Alex Morgan');
});

test('each card links to the template’s own page', async ({ page }) => {
await page.goto('/templates');
await page.getByRole('link', { name: 'Resume', exact: true }).click();
await expect(page).toHaveURL(/\/templates\/resume$/);
await expect(page.getByRole('heading', { level: 1 })).toHaveText('Resume template');
await expect(page.getByRole('navigation', { name: 'Breadcrumb' })).toContainText('Templates');
});

test('the blank document starts empty', async ({ page }) => {
await page.goto('/templates');
await page.waitForLoadState('networkidle');
await page.getByTestId('template-blank').click();

await expect(page).toHaveURL(/\/canvas$/);
await expect(page.locator('textarea')).toHaveCount(0);
});

test('the home page shows a few of them, and links to all', async ({ page, request }) => {
await page.goto('/');
await page.waitForLoadState('networkidle');
await expect(page.getByRole('heading', { name: 'Start from a template' })).toBeVisible();
await expect(page.getByTestId('featured-templates').getByRole('listitem')).toHaveCount(6);
await expect(page.getByTestId('browse-templates')).toHaveAttribute('href', '/templates');

const sitemap = await (await request.get('/sitemap.xml')).text();
expect(sitemap).toContain('/templates');
});

test('a template on the home page opens in the editor', async ({ page }) => {
await page.goto('/');
await page.waitForLoadState('networkidle');
await page.getByTestId('featured-invoice').click();

await expect(page).toHaveURL(/\/canvas$/);
await expect(page.getByTestId('document-title')).toHaveText('Invoice');
});

test('fits a phone: no sideways scroll, and the previews stay white in the dark theme', async ({
browser
}) => {
const context = await browser.newContext({
viewport: { width: 375, height: 812 },
colorScheme: 'dark'
});
const page = await context.newPage();
await page.goto('/templates');
await page.waitForLoadState('networkidle');
const overflow = await page.evaluate(
() => document.documentElement.scrollWidth - document.documentElement.clientWidth
);
expect(overflow).toBeLessThanOrEqual(0);
const paper = await page
.getByTestId('template-card')
.nth(1)
.locator('[aria-hidden="true"]')
.first()
.evaluate((el) => getComputedStyle(el).backgroundColor);
expect(paper).toBe('rgb(255, 255, 255)');
await context.close();
});
});
56 changes: 56 additions & 0 deletions src/lib/templates/TemplatePreview.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<script lang="ts">
import { CANVAS_HEIGHT, CANVAS_WIDTH, DISPLAY_SCALE } from '$lib/const/dimension';
import ShapeElementView from '../../routes/canvas/(components)/elements/ShapeElementView.svelte';
import type { ShapeElement } from '$lib/types/canvas';
import type { Template } from './types';

const { template, width = 200 }: { template: Template; width?: number } = $props();

const zoom = $derived(width / CANVAS_WIDTH);
// Text is sized in editor pixels; the page here is drawn in page units
const unit = 1 / DISPLAY_SCALE;
</script>

<!-- The real page, drawn small: what you see is what you get when you use the template -->
<div
class="relative flex-none overflow-hidden bg-paper shadow-sm ring-1 ring-black/10"
style:width="{width}px"
style:height="{CANVAS_HEIGHT * zoom}px"
aria-hidden="true"
>
<div
class="absolute top-0 left-0 origin-top-left"
style:width="{CANVAS_WIDTH}px"
style:height="{CANVAS_HEIGHT}px"
style:transform="scale({zoom})"
>
{#each template.elements as element, i (i)}
<div
class="absolute overflow-hidden"
style:left="{element.x}px"
style:top="{element.y}px"
style:width="{element.width}px"
style:height="{element.height}px"
style:z-index={i}
>
{#if element.type === 'text'}
<div
class="h-full w-full leading-[normal] whitespace-pre-wrap"
style:padding="{8 * unit}px"
style:font-family="{element.fontFamily}, sans-serif"
style:font-size="{element.fontSize * unit}px"
style:font-weight={element.fontWeight}
style:font-style={element.fontStyle}
style:text-transform={element.textTransform}
style:text-align={element.textAlign}
style:color={element.color}
>
{element.text}
</div>
{:else if element.type === 'shape'}
<ShapeElementView element={element as unknown as ShapeElement} />
{/if}
</div>
{/each}
</div>
</div>
Loading
Loading