240 lines
11 KiB
TypeScript
240 lines
11 KiB
TypeScript
import { test, expect, type Page } from '@playwright/test';
|
||
import { createDatabase } from '../packages/database/dist/index.js';
|
||
|
||
if (!process.env.DATABASE_URL?.endsWith('/merchantflow_test'))
|
||
throw new Error('Landing tests require the isolated test database');
|
||
const db = createDatabase(process.env.DATABASE_URL);
|
||
const submissionKeys = new Set<string>();
|
||
test.beforeEach(({ page }) => {
|
||
page.on('request', (request) => {
|
||
if (request.url().endsWith('/api/public/leads')) {
|
||
const key = request.headers()['idempotency-key'];
|
||
if (key) submissionKeys.add(key);
|
||
}
|
||
});
|
||
});
|
||
test.afterEach(async () => {
|
||
await db.merchant.deleteMany({ where: { publicSubmissionKey: { in: [...submissionKeys] } } });
|
||
submissionKeys.clear();
|
||
});
|
||
test.afterAll(() => db.$disconnect());
|
||
|
||
const fields = [
|
||
'name',
|
||
'contactName',
|
||
'contactEmail',
|
||
'website',
|
||
'country',
|
||
'industry',
|
||
'estimatedMonthlyVolumeUsd',
|
||
] as const;
|
||
async function fill(page: Page) {
|
||
const email = `browser-${crypto.randomUUID()}@example.test`;
|
||
await page.locator('#lead-name').fill('MerchantFlow browser fixture');
|
||
await page.locator('#lead-contactName').fill('Test Contact');
|
||
await page.locator('#lead-contactEmail').fill(email);
|
||
await page.locator('#lead-website').fill('example.test');
|
||
await page.locator('#lead-country').selectOption('GB');
|
||
await page.locator('#lead-industry').selectOption('OTHER');
|
||
await page.locator('#lead-estimatedMonthlyVolumeUsd').fill('12500,50');
|
||
return email;
|
||
}
|
||
async function noOverflow(page: Page) {
|
||
expect(await page.evaluate(() => document.documentElement.scrollWidth <= window.innerWidth)).toBe(
|
||
true,
|
||
);
|
||
}
|
||
|
||
for (const language of ['en', 'ru'])
|
||
for (const width of [375, 1440]) {
|
||
test.describe(`landing ${language} ${width}px`, () => {
|
||
test.use({ viewport: { width, height: 1000 } });
|
||
test('accessible consultation, bilingual errors, retained values and real persistence', async ({
|
||
page,
|
||
}, info) => {
|
||
const runtimeErrors: string[] = [];
|
||
page.on('pageerror', (error) => runtimeErrors.push(error.message));
|
||
await page.addInitScript(
|
||
(lang) => localStorage.setItem('merchantflow.language', lang),
|
||
language,
|
||
);
|
||
await page.goto('/');
|
||
await expect(page.locator('html')).toHaveAttribute('lang', language);
|
||
await expect(
|
||
page.getByRole('link', { name: /Staff login|Вход для команды/ }),
|
||
).toBeVisible();
|
||
await expect(page.getByRole('heading', { level: 1 })).toBeVisible();
|
||
await noOverflow(page);
|
||
await page.screenshot({ path: info.outputPath('landing.png'), fullPage: true });
|
||
await page.getByRole('link', { name: /Let’s talk payments|Обсудим платежи/ }).click();
|
||
await expect(page.locator('#lead-name')).toBeFocused();
|
||
const submit = page.locator('button[type="submit"]');
|
||
await submit.click();
|
||
for (const name of fields)
|
||
await expect(page.locator(`#lead-${name}`)).toHaveAttribute('aria-invalid', 'true');
|
||
await expect(page.locator('#lead-name')).toBeFocused();
|
||
await expect(page.locator('#lead-name-error')).toHaveText(
|
||
language === 'en' ? 'Please complete this field.' : 'Заполните это поле.',
|
||
);
|
||
await noOverflow(page);
|
||
await page.screenshot({ path: info.outputPath('validation.png'), fullPage: true });
|
||
const email = await fill(page);
|
||
await page
|
||
.getByRole('button', { name: language === 'en' ? 'Русский' : 'English', exact: true })
|
||
.click();
|
||
await expect(page.locator('#lead-name')).toHaveValue('MerchantFlow browser fixture');
|
||
await expect(page.locator('#lead-estimatedMonthlyVolumeUsd')).toHaveValue('12500,50');
|
||
await expect(page.locator('#lead-country')).toHaveValue('GB');
|
||
await page
|
||
.getByRole('button', { name: language === 'en' ? 'English' : 'Русский', exact: true })
|
||
.click();
|
||
|
||
const trap = page.locator('input[name="companyFax"]');
|
||
await expect(trap).toHaveAttribute('tabindex', '-1');
|
||
await expect(trap.locator('..')).toHaveAttribute('aria-hidden', 'true');
|
||
await page.locator('#lead-estimatedMonthlyVolumeUsd').focus();
|
||
await page.keyboard.press('Tab');
|
||
await expect(submit).toBeFocused();
|
||
expect(await page.locator('form').ariaSnapshot()).not.toContain('companyFax');
|
||
|
||
await page.route('**/api/public/leads', (route) => route.abort('failed'));
|
||
await submit.click();
|
||
await expect(page.locator('.lead-feedback')).toContainText(
|
||
language === 'en' ? 'couldn’t confirm' : 'Не удалось подтвердить',
|
||
);
|
||
await expect(page.locator('#lead-contactEmail')).toHaveValue(email);
|
||
await page.unroute('**/api/public/leads');
|
||
await page.route('**/api/public/leads', (route) =>
|
||
route.fulfill({
|
||
status: 400,
|
||
json: {
|
||
code: 'VALIDATION_ERROR',
|
||
issues: [{ path: 'contactEmail', code: 'validation.email' }],
|
||
},
|
||
}),
|
||
);
|
||
await submit.click();
|
||
await expect(page.locator('#lead-contactEmail-error')).toHaveText(
|
||
language === 'en' ? 'Enter a valid email address.' : 'Введите корректный адрес почты.',
|
||
);
|
||
await expect(page.locator('#lead-contactEmail')).toBeFocused();
|
||
await page.unroute('**/api/public/leads');
|
||
await page.route('**/api/public/leads', (route) =>
|
||
route.fulfill({
|
||
status: 429,
|
||
headers: { 'Retry-After': '1' },
|
||
json: { code: 'TOO_MANY_REQUESTS' },
|
||
}),
|
||
);
|
||
await submit.click();
|
||
await expect(submit).toBeDisabled();
|
||
await expect(page.locator('.lead-feedback')).toContainText(
|
||
language === 'en' ? 'wait' : 'через',
|
||
);
|
||
await expect(submit).toBeEnabled();
|
||
await expect(page.locator('#lead-estimatedMonthlyVolumeUsd')).toHaveValue('12500,50');
|
||
await page.unroute('**/api/public/leads');
|
||
await page.route('**/api/public/leads', (route) =>
|
||
route.fulfill({ status: 503, json: { code: 'DATABASE_UNAVAILABLE' } }),
|
||
);
|
||
await submit.click();
|
||
await expect(page.locator('.lead-feedback')).toContainText(
|
||
language === 'en' ? 'couldn’t complete' : 'Не удалось отправить',
|
||
);
|
||
await page.unroute('**/api/public/leads');
|
||
|
||
let release = () => {};
|
||
const held = new Promise<void>((resolve) => {
|
||
release = resolve;
|
||
});
|
||
await page.route('**/api/public/leads', async (route) => {
|
||
await held;
|
||
await route.continue();
|
||
});
|
||
await submit.click();
|
||
await expect(submit).toBeDisabled();
|
||
await expect(page.locator('form')).toHaveAttribute('aria-busy', 'true');
|
||
release();
|
||
await expect(page.locator('.lead-success')).toBeVisible();
|
||
await expect(page.locator('.lead-success')).toBeFocused();
|
||
await noOverflow(page);
|
||
await page.screenshot({ path: info.outputPath('success.png'), fullPage: true });
|
||
expect(submissionKeys.size).toBe(1);
|
||
const merchants = await db.merchant.findMany({
|
||
where: { contactEmail: email },
|
||
include: { activities: true, events: true, checklist: true },
|
||
});
|
||
expect(merchants).toHaveLength(1);
|
||
expect(merchants[0]?.source).toBe('LANDING_PAGE');
|
||
expect(merchants[0]?.ownerId).toBeNull();
|
||
expect(merchants[0]?.activities).toHaveLength(1);
|
||
expect(merchants[0]?.events).toHaveLength(1);
|
||
expect(merchants[0]?.checklist).toHaveLength(4);
|
||
expect(runtimeErrors).toEqual([]);
|
||
});
|
||
});
|
||
}
|
||
|
||
test('lost response retry reuses its key and system history renders in the CRM', async ({
|
||
page,
|
||
}) => {
|
||
await page.goto('/');
|
||
const email = await fill(page);
|
||
await page.route('**/api/public/leads', async (route) => {
|
||
const response = await route.fetch();
|
||
expect(response.status()).toBe(202);
|
||
await route.abort('failed');
|
||
});
|
||
await page.locator('button[type="submit"]').click();
|
||
await expect(page.locator('.lead-feedback')).toBeVisible();
|
||
expect(await db.merchant.count({ where: { contactEmail: email } })).toBe(1);
|
||
await page.unroute('**/api/public/leads');
|
||
await page.locator('button[type="submit"]').click();
|
||
await expect(page.locator('.lead-success')).toBeVisible();
|
||
expect(submissionKeys.size).toBe(1);
|
||
const records = await db.merchant.findMany({
|
||
where: { contactEmail: email },
|
||
include: { activities: true },
|
||
});
|
||
expect(records).toHaveLength(1);
|
||
expect(records[0]?.activities).toHaveLength(1);
|
||
await page.getByRole('link', { name: 'Staff login' }).click();
|
||
await page.getByLabel('Email address').fill('[email protected]');
|
||
await page.getByLabel('Password', { exact: true }).fill('MerchantFlow123!');
|
||
await page.getByRole('button', { name: 'Sign in to workspace', exact: true }).click();
|
||
await expect(page).toHaveURL(/\/app$/);
|
||
await page.goto(`/app/merchants/${records[0]?.id}`);
|
||
await expect(page.locator('h1')).toHaveText('MerchantFlow browser fixture');
|
||
await expect(page.getByText('Landing page', { exact: true })).toBeVisible();
|
||
await page.getByRole('tab', { name: 'Activity', exact: true }).click();
|
||
await expect(page.getByText('Lead received from landing page', { exact: true })).toBeVisible();
|
||
await page.getByRole('button', { name: 'Русский', exact: true }).click();
|
||
await expect(page.getByText('Заявка получена с сайта', { exact: true })).toBeVisible();
|
||
await page.getByRole('tab', { name: 'Обзор', exact: true }).click();
|
||
await expect(page.getByRole('definition').filter({ hasText: /^Сайт$/ })).toBeVisible();
|
||
});
|
||
|
||
test('changing the normalized payload creates a new key; reduced motion and locale persist', async ({
|
||
page,
|
||
}) => {
|
||
await page.goto('/');
|
||
await fill(page);
|
||
await page.route('**/api/public/leads', (route) => route.abort('failed'));
|
||
await page.locator('button[type="submit"]').click();
|
||
await expect(page.locator('.lead-feedback')).toBeVisible();
|
||
await page.locator('#lead-name').fill('An edited request');
|
||
await page.locator('button[type="submit"]').click();
|
||
await expect(page.locator('.lead-feedback')).toBeVisible();
|
||
await expect.poll(() => submissionKeys.size).toBe(2);
|
||
await page.emulateMedia({ reducedMotion: 'reduce' });
|
||
expect(
|
||
await page
|
||
.locator('.lead-submit')
|
||
.evaluate((el) => parseFloat(getComputedStyle(el).transitionDuration)),
|
||
).toBeLessThan(0.01);
|
||
await page.getByRole('button', { name: 'Русский', exact: true }).click();
|
||
await page.reload();
|
||
await expect(page.locator('html')).toHaveAttribute('lang', 'ru');
|
||
await expect(page.locator('#lead-name')).toHaveValue('');
|
||
});
|