2023-06-01 05:55:03 +03:00
|
|
|
import {expect, test} from '@playwright/test';
|
2023-05-24 16:05:45 +03:00
|
|
|
import {initialize} from '../utils/e2e';
|
|
|
|
|
|
|
|
test.describe('Form', async () => {
|
2023-05-30 12:57:56 +03:00
|
|
|
test.describe('Display options', () => {
|
|
|
|
test('Displays the title', async ({page}) => {
|
|
|
|
const {frame} = await initialize({page, title: 'Sign up to get the latest news and updates.'});
|
2023-05-24 16:05:45 +03:00
|
|
|
|
2023-05-30 12:57:56 +03:00
|
|
|
// Check the Frame
|
|
|
|
const h1 = frame.getByRole('heading');
|
|
|
|
expect(await h1.innerText()).toBe('Sign up to get the latest news and updates.');
|
|
|
|
});
|
2023-05-24 16:05:45 +03:00
|
|
|
|
2023-05-30 12:57:56 +03:00
|
|
|
test('Displays the description', async ({page}) => {
|
|
|
|
const {frame} = await initialize({page, title: 'Title', description: 'Sign up to get the latest news and updates.'});
|
2023-05-24 16:05:45 +03:00
|
|
|
|
2023-05-30 12:57:56 +03:00
|
|
|
// Check the Frame
|
|
|
|
const p = frame.getByRole('paragraph');
|
|
|
|
expect(await p.innerText()).toBe('Sign up to get the latest news and updates.');
|
|
|
|
});
|
2023-05-24 16:05:45 +03:00
|
|
|
|
2023-06-01 05:55:03 +03:00
|
|
|
test('Uses the background color for full layout', async ({page}) => {
|
|
|
|
// Need rgb notation here, because getComputedStyle returns rgb notation
|
|
|
|
const color = 'rgb(123, 255, 0)';
|
|
|
|
const {frame} = await initialize({page, title: 'Title', backgroundColor: color});
|
|
|
|
const wrapper = frame.getByTestId('wrapper');
|
|
|
|
|
|
|
|
// Check calculated background color of the button
|
|
|
|
const backgroundColor = await wrapper.evaluate((el) => {
|
|
|
|
return window.getComputedStyle(el).backgroundColor;
|
|
|
|
});
|
|
|
|
expect(backgroundColor).toBe(color);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Uses the button color', async ({page}) => {
|
2023-05-30 12:57:56 +03:00
|
|
|
// Need rgb notation here, because getComputedStyle returns rgb notation
|
|
|
|
const color = 'rgb(255, 123, 0)';
|
2023-06-01 05:55:03 +03:00
|
|
|
const {frame} = await initialize({page, buttonColor: color});
|
2023-05-30 12:57:56 +03:00
|
|
|
const submitButton = frame.getByRole('button');
|
2023-05-24 16:05:45 +03:00
|
|
|
|
2023-05-30 12:57:56 +03:00
|
|
|
// Check calculated background color of the button
|
|
|
|
const backgroundColor = await submitButton.evaluate((el) => {
|
|
|
|
return window.getComputedStyle(el).backgroundColor;
|
|
|
|
});
|
|
|
|
expect(backgroundColor).toBe(color);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Has a minimal style when title is missing', async ({page}) => {
|
|
|
|
let {frame} = await initialize({page});
|
|
|
|
|
|
|
|
// Check no title or description present
|
|
|
|
await expect(frame.getByRole('heading')).toHaveCount(0);
|
|
|
|
await expect(frame.getByRole('paragraph')).toHaveCount(0);
|
|
|
|
|
|
|
|
frame = (await initialize({page, description: 'Ignored'})).frame;
|
|
|
|
|
|
|
|
// Check no title or description present
|
|
|
|
await expect(frame.getByRole('heading')).toHaveCount(0);
|
|
|
|
await expect(frame.getByRole('paragraph')).toHaveCount(0);
|
2023-05-24 16:05:45 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-05-30 12:57:56 +03:00
|
|
|
test.describe('Submitting', () => {
|
|
|
|
test('Can submit the form', async ({page}) => {
|
|
|
|
const {frame, lastApiRequest} = await initialize({page, title: 'Sign up'});
|
|
|
|
|
|
|
|
// Fill out the form
|
|
|
|
const emailInput = frame.getByTestId('input');
|
|
|
|
await emailInput.fill('jamie@example.com');
|
|
|
|
|
|
|
|
// Click the submit button
|
|
|
|
const submitButton = frame.getByTestId('button');
|
|
|
|
await submitButton.click();
|
|
|
|
|
|
|
|
// Check input and button are gone
|
|
|
|
await expect(frame.getByTestId('input')).toHaveCount(0);
|
|
|
|
await expect(frame.getByTestId('button')).toHaveCount(0);
|
|
|
|
|
|
|
|
// Showing the success page
|
|
|
|
await expect(frame.getByTestId('success-page')).toHaveCount(1);
|
|
|
|
|
|
|
|
// Check the request body
|
|
|
|
expect(lastApiRequest.body).not.toBeNull();
|
|
|
|
expect(lastApiRequest.body).toHaveProperty('labels', []);
|
|
|
|
expect(lastApiRequest.body).toHaveProperty('email', 'jamie@example.com');
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Send a label when submitting the form', async ({page}) => {
|
2023-05-30 15:05:13 +03:00
|
|
|
const {frame, lastApiRequest} = await initialize({page, title: 'Sign up', 'label-1': 'Hello world'});
|
2023-05-30 12:57:56 +03:00
|
|
|
|
|
|
|
// Fill out the form
|
|
|
|
const emailInput = frame.getByTestId('input');
|
|
|
|
await emailInput.fill('jamie@example.com');
|
|
|
|
|
|
|
|
// Click the submit button
|
|
|
|
const submitButton = frame.getByTestId('button');
|
|
|
|
await submitButton.click();
|
|
|
|
|
|
|
|
// Showing the success page
|
|
|
|
await expect(frame.getByTestId('success-page')).toHaveCount(1);
|
|
|
|
|
|
|
|
// Check the request body
|
|
|
|
expect(lastApiRequest.body).not.toBeNull();
|
|
|
|
expect(lastApiRequest.body).toHaveProperty('labels', ['Hello world']);
|
|
|
|
expect(lastApiRequest.body).toHaveProperty('email', 'jamie@example.com');
|
|
|
|
});
|
|
|
|
|
2023-05-30 15:05:13 +03:00
|
|
|
test('Sends multiple labels when submitting the form', async ({page}) => {
|
|
|
|
const {frame, lastApiRequest} = await initialize({page, title: 'Sign up', 'label-1': 'Hello world', 'label-2': 'and another one'});
|
2023-05-30 12:57:56 +03:00
|
|
|
|
|
|
|
// Fill out the form
|
|
|
|
const emailInput = frame.getByTestId('input');
|
|
|
|
await emailInput.fill('hey@example.com');
|
|
|
|
|
|
|
|
// Click the submit button
|
|
|
|
const submitButton = frame.getByTestId('button');
|
|
|
|
await submitButton.click();
|
|
|
|
|
|
|
|
// Showing the success page
|
|
|
|
await expect(frame.getByTestId('success-page')).toHaveCount(1);
|
|
|
|
|
|
|
|
// Check the request body
|
|
|
|
expect(lastApiRequest.body).not.toBeNull();
|
|
|
|
expect(lastApiRequest.body).toHaveProperty('labels', ['Hello world', 'and another one']);
|
|
|
|
expect(lastApiRequest.body).toHaveProperty('email', 'hey@example.com');
|
|
|
|
});
|
|
|
|
|
2023-05-30 15:05:13 +03:00
|
|
|
test('Does not send labels when not the right numbering is used', async ({page}) => {
|
|
|
|
// Skip setting label-1, so label-2 is ignored
|
|
|
|
const {frame, lastApiRequest} = await initialize({page, title: 'Sign up', 'label-2': 'and another one'});
|
|
|
|
|
|
|
|
// Fill out the form
|
|
|
|
const emailInput = frame.getByTestId('input');
|
|
|
|
await emailInput.fill('hey@example.com');
|
|
|
|
|
|
|
|
// Click the submit button
|
|
|
|
const submitButton = frame.getByTestId('button');
|
|
|
|
await submitButton.click();
|
|
|
|
|
|
|
|
// Showing the success page
|
|
|
|
await expect(frame.getByTestId('success-page')).toHaveCount(1);
|
|
|
|
|
|
|
|
// Check the request body
|
|
|
|
expect(lastApiRequest.body).not.toBeNull();
|
|
|
|
expect(lastApiRequest.body).toHaveProperty('labels', []);
|
|
|
|
expect(lastApiRequest.body).toHaveProperty('email', 'hey@example.com');
|
|
|
|
});
|
|
|
|
|
2023-05-30 12:57:56 +03:00
|
|
|
test('Cannot submit the form with invalid email address', async ({page}) => {
|
|
|
|
const {frame, lastApiRequest} = await initialize({page, title: 'Sign up'});
|
|
|
|
|
|
|
|
// Fill out the form
|
|
|
|
const emailInput = frame.getByTestId('input');
|
|
|
|
await emailInput.fill('invalid');
|
|
|
|
|
|
|
|
// Click the submit button
|
|
|
|
const submitButton = frame.getByTestId('button');
|
|
|
|
await submitButton.click();
|
|
|
|
|
|
|
|
// Check input and button are not gone
|
|
|
|
await expect(frame.getByTestId('input')).toHaveCount(1);
|
|
|
|
await expect(frame.getByTestId('button')).toHaveCount(1);
|
|
|
|
|
|
|
|
// Not showing the success page
|
|
|
|
await expect(frame.getByTestId('success-page')).toHaveCount(0);
|
2023-05-24 16:05:45 +03:00
|
|
|
|
2023-05-30 12:57:56 +03:00
|
|
|
// Check error message is visible on the page
|
|
|
|
const errorMessage = frame.getByTestId('error-message');
|
|
|
|
await expect(errorMessage).toHaveCount(1);
|
|
|
|
expect(await errorMessage.innerText()).toBe('Please enter a valid email address');
|
2023-05-24 16:05:45 +03:00
|
|
|
|
2023-05-30 12:57:56 +03:00
|
|
|
expect(lastApiRequest.body).toBeNull();
|
2023-05-24 16:05:45 +03:00
|
|
|
|
2023-05-30 12:57:56 +03:00
|
|
|
// Try again
|
|
|
|
await emailInput.fill('valid@example.com');
|
|
|
|
await submitButton.click();
|
|
|
|
|
|
|
|
// Check input and button are gone
|
|
|
|
await expect(frame.getByTestId('input')).toHaveCount(0);
|
|
|
|
await expect(frame.getByTestId('button')).toHaveCount(0);
|
|
|
|
|
|
|
|
// Showing the success page
|
|
|
|
await expect(frame.getByTestId('success-page')).toHaveCount(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Shows error message on network issues', async ({page}) => {
|
2023-06-01 11:18:11 +03:00
|
|
|
const {frame} = await initialize({page, title: 'Sign up', site: 'http://localhost:1234/invalid'});
|
2023-05-30 12:57:56 +03:00
|
|
|
|
|
|
|
// Fill out the form
|
|
|
|
const emailInput = frame.getByTestId('input');
|
|
|
|
await emailInput.fill('valid@example.com');
|
|
|
|
|
|
|
|
// Click the submit button
|
|
|
|
const submitButton = frame.getByTestId('button');
|
|
|
|
await submitButton.click();
|
|
|
|
|
|
|
|
// Check input and button are not gone
|
|
|
|
await expect(frame.getByTestId('input')).toHaveCount(1);
|
|
|
|
await expect(frame.getByTestId('button')).toHaveCount(1);
|
|
|
|
|
|
|
|
// Not showing the success page
|
|
|
|
await expect(frame.getByTestId('success-page')).toHaveCount(0);
|
|
|
|
|
|
|
|
// Check error message is visible on the page
|
|
|
|
const errorMessage = frame.getByTestId('error-message');
|
|
|
|
await expect(errorMessage).toHaveCount(1);
|
|
|
|
expect(await errorMessage.innerText()).toBe('Something went wrong, please try again.');
|
|
|
|
});
|
2023-06-09 15:10:26 +03:00
|
|
|
|
|
|
|
test('Shows error message on 4xx status codes', async ({page}) => {
|
|
|
|
const {frame} = await initialize({page, title: 'Sign up', apiStatus: 400});
|
|
|
|
|
|
|
|
// Fill out the form
|
|
|
|
const emailInput = frame.getByTestId('input');
|
|
|
|
await emailInput.fill('valid@example.com');
|
|
|
|
|
|
|
|
// Click the submit button
|
|
|
|
const submitButton = frame.getByTestId('button');
|
|
|
|
await submitButton.click();
|
|
|
|
|
|
|
|
// Check input and button are not gone
|
|
|
|
await expect(frame.getByTestId('input')).toHaveCount(1);
|
|
|
|
await expect(frame.getByTestId('button')).toHaveCount(1);
|
|
|
|
|
|
|
|
// Not showing the success page
|
|
|
|
await expect(frame.getByTestId('success-page')).toHaveCount(0);
|
|
|
|
|
|
|
|
// Check error message is visible on the page
|
|
|
|
const errorMessage = frame.getByTestId('error-message');
|
|
|
|
await expect(errorMessage).toHaveCount(1);
|
|
|
|
expect(await errorMessage.innerText()).toBe('Something went wrong, please try again.');
|
|
|
|
});
|
2023-05-24 16:05:45 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|