astro/packages/astro/e2e/custom-client-directives.test.js
Bjorn Lu 73ec6f6c16
Implement custom client directives (#7074)
Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca>
2023-05-17 20:51:27 +08:00

92 lines
2.4 KiB
JavaScript

import { expect } from '@playwright/test';
import { testFactory, waitForHydrate } from './test-utils.js';
import testAdapter from '../test/test-adapter.js';
const test = testFactory({
root: './fixtures/custom-client-directives/',
});
test.describe('Custom Client Directives - dev', () => {
let devServer;
test.beforeAll(async ({ astro }) => {
devServer = await astro.startDevServer();
});
test.afterAll(async () => {
await devServer.stop();
});
testClientDirectivesShared();
});
test.describe('Custom Client Directives - build static', () => {
let previewServer;
test.beforeAll(async ({ astro }) => {
await astro.build();
previewServer = await astro.preview();
});
test.afterAll(async () => {
await previewServer.stop();
});
testClientDirectivesShared();
});
test.describe('Custom Client Directives - build server', () => {
let previewServer;
test.beforeAll(async ({ astro }) => {
await astro.build({
adapter: testAdapter(),
});
previewServer = await astro.preview();
});
test.afterAll(async () => {
await previewServer.stop();
});
testClientDirectivesShared();
});
function testClientDirectivesShared() {
test('client:click should work', async ({ astro, page }) => {
await page.goto(astro.resolveUrl('/'));
const incrementBtn = page.locator('#client-click .increment');
const counterValue = page.locator('#client-click pre');
await expect(counterValue).toHaveText('0');
// Component only hydrates on first click
await Promise.all([waitForHydrate(page, counterValue), incrementBtn.click()]);
// Since first click only triggers hydration, this should stay 0
await expect(counterValue).toHaveText('0');
await incrementBtn.click();
// Hydrated, this should be 1
await expect(counterValue).toHaveText('1');
});
test('client:password should work', async ({ astro, page }) => {
await page.goto(astro.resolveUrl('/'));
const incrementBtn = page.locator('#client-password .increment');
const counterValue = page.locator('#client-password pre');
await expect(counterValue).toHaveText('0');
await incrementBtn.click();
// Not hydrated, so this should stay 0
await expect(counterValue).toHaveText('0');
// Type super cool password to activate password!
await Promise.all([waitForHydrate(page, counterValue), page.keyboard.type('hunter2')]);
await incrementBtn.click();
// Hydrated, this should be 1
await expect(counterValue).toHaveText('1');
});
}