0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-01-06 22:40:14 -05:00

Fixed showing errors when non 2XX response in signup form

no issue
This commit is contained in:
Simon Backx 2023-06-09 14:10:26 +02:00
parent c26f77586a
commit 6d6da2492c
5 changed files with 35 additions and 9 deletions

View file

@ -180,4 +180,4 @@
"{{memberEmail}} will no longer receive this newsletter.": "A message shown when a user unsubscribes from a newsletter",
"{{memberEmail}} will no longer receive {{newsletterName}} newsletter.": "A message shown when a user unsubscribes from a newsletter",
"{{trialDays}} days free": "A label for free trial days"
}
}

View file

@ -38,7 +38,11 @@ const Preview: React.FC<SignupFormOptions & {
setTimeout(resolve, 2000);
});
return simulateApiError ? false : true;
if (simulateApiError) {
throw new Error('API Error');
}
return;
}
},
t: i18n.t,

View file

@ -33,10 +33,8 @@ export const setupGhostApi = ({siteUrl}: {siteUrl: string}) => {
});
if (response.status < 200 || response.status >= 300) {
return false;
throw new Error(response.statusText);
}
return true;
}
};
};

View file

@ -206,6 +206,30 @@ test.describe('Form', async () => {
await expect(errorMessage).toHaveCount(1);
expect(await errorMessage.innerText()).toBe('Something went wrong, please try again.');
});
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.');
});
});
});

View file

@ -7,7 +7,7 @@ type LastApiRequest = {
body: null | any
};
export async function initialize({page, path, ...options}: {page: Page, path?: string; title?: string, description?: string, icon?: string, backgroundColor?: string, buttonColor?: string, site?: string, 'label-1'?: string, 'label-2'?: string}) {
export async function initialize({page, path, apiStatus, ...options}: {page: Page, path?: string; title?: string, description?: string, icon?: string, backgroundColor?: string, buttonColor?: string, site?: string, 'label-1'?: string, 'label-2'?: string, apiStatus?: number}) {
const sitePath = `${MOCKED_SITE_URL}${path ?? ''}`;
await page.route(sitePath, async (route) => {
await route.fulfill({
@ -20,7 +20,7 @@ export async function initialize({page, path, ...options}: {page: Page, path?: s
await page.setViewportSize({width: 1000, height: 1000});
await page.goto(sitePath);
const lastApiRequest = await mockApi({page});
const lastApiRequest = await mockApi({page, status: apiStatus});
if (!options.site) {
options.site = MOCKED_SITE_URL;
@ -44,7 +44,7 @@ export async function initialize({page, path, ...options}: {page: Page, path?: s
};
}
export async function mockApi({page}: {page: any}) {
export async function mockApi({page, status = 200}: {page: any, status?: number}) {
const lastApiRequest: LastApiRequest = {
body: null
};
@ -54,7 +54,7 @@ export async function mockApi({page}: {page: any}) {
lastApiRequest.body = requestBody;
await route.fulfill({
status: 200,
status,
body: 'ok'
});
});