0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-01-27 22:49:56 -05:00
ghost/apps/comments-ui/test/e2e/autoclose-forms.test.ts

66 lines
2.2 KiB
TypeScript
Raw Normal View History

Refactored comments-ui form management (#21621) ref https://linear.app/ghost/issue/PLG-230 closes https://linear.app/ghost/issue/PLG-256 Adding an in-reply-to reference link/snippet to reply forms was proving difficult with the previous setup due the amount of data that needed to be passed up and down a deeply nested component tree. This refactor lays the groundwork for making that easier and aims to make form autoclose behaviour more centralised by keeping the open form state in app context and the opening/closing of forms in actions so there's less need for messy local state and to drill functions down the component tree. - replaces `openFormCount` context state with an `openCommentForms` array - keeping detailed open form references in the application state means the display of forms is centrally managed rather than managed via local state inside components - it simplifies some of the problems faced with the `<PublishedComment>` component that previously managed form display. That component is re-used for both the top-level comment as well as replies even though replying on a reply puts the top-level comment into reply mode meaning we had a mess of local state and passed-through functions to work around the component having varying behaviour depending on nesting level - `openFormCount` is still available on the application state via `useMemo` on the provider meaning the implementation of `openCommentForms` is hidden from app code that just needs to know if forms are open - removes `<AutocloseForm>` as the autoclose behaviour is now controlled via the `openCommentForm` action - updated `<Form>` so it manages the "has unsaved changes" properties on `openFormComments` ready for use by `openCommentForm`'s autoclosing behaviour
2024-11-14 18:26:23 +00:00
import {FrameLocator, expect, test} from '@playwright/test';
import {MockedApi, initialize, waitEditorFocused} from '../utils/e2e';
import {buildReply} from '../utils/fixtures';
Refactored comments-ui form management (#21621) ref https://linear.app/ghost/issue/PLG-230 closes https://linear.app/ghost/issue/PLG-256 Adding an in-reply-to reference link/snippet to reply forms was proving difficult with the previous setup due the amount of data that needed to be passed up and down a deeply nested component tree. This refactor lays the groundwork for making that easier and aims to make form autoclose behaviour more centralised by keeping the open form state in app context and the opening/closing of forms in actions so there's less need for messy local state and to drill functions down the component tree. - replaces `openFormCount` context state with an `openCommentForms` array - keeping detailed open form references in the application state means the display of forms is centrally managed rather than managed via local state inside components - it simplifies some of the problems faced with the `<PublishedComment>` component that previously managed form display. That component is re-used for both the top-level comment as well as replies even though replying on a reply puts the top-level comment into reply mode meaning we had a mess of local state and passed-through functions to work around the component having varying behaviour depending on nesting level - `openFormCount` is still available on the application state via `useMemo` on the provider meaning the implementation of `openCommentForms` is hidden from app code that just needs to know if forms are open - removes `<AutocloseForm>` as the autoclose behaviour is now controlled via the `openCommentForm` action - updated `<Form>` so it manages the "has unsaved changes" properties on `openFormComments` ready for use by `openCommentForm`'s autoclosing behaviour
2024-11-14 18:26:23 +00:00
test.describe('Autoclose forms', async () => {
let mockedApi: MockedApi;
let frame: FrameLocator;
test.beforeEach(async ({page}) => {
mockedApi = new MockedApi({});
mockedApi.setMember({});
mockedApi.addComment({
html: '<p>Comment 1</p>',
replies: [{
html: '<p>Reply 1.1</p>'
}, {
html: '<p>Reply 1.2</p>'
}].map(buildReply)
Refactored comments-ui form management (#21621) ref https://linear.app/ghost/issue/PLG-230 closes https://linear.app/ghost/issue/PLG-256 Adding an in-reply-to reference link/snippet to reply forms was proving difficult with the previous setup due the amount of data that needed to be passed up and down a deeply nested component tree. This refactor lays the groundwork for making that easier and aims to make form autoclose behaviour more centralised by keeping the open form state in app context and the opening/closing of forms in actions so there's less need for messy local state and to drill functions down the component tree. - replaces `openFormCount` context state with an `openCommentForms` array - keeping detailed open form references in the application state means the display of forms is centrally managed rather than managed via local state inside components - it simplifies some of the problems faced with the `<PublishedComment>` component that previously managed form display. That component is re-used for both the top-level comment as well as replies even though replying on a reply puts the top-level comment into reply mode meaning we had a mess of local state and passed-through functions to work around the component having varying behaviour depending on nesting level - `openFormCount` is still available on the application state via `useMemo` on the provider meaning the implementation of `openCommentForms` is hidden from app code that just needs to know if forms are open - removes `<AutocloseForm>` as the autoclose behaviour is now controlled via the `openCommentForm` action - updated `<Form>` so it manages the "has unsaved changes" properties on `openFormComments` ready for use by `openCommentForm`'s autoclosing behaviour
2024-11-14 18:26:23 +00:00
});
mockedApi.addComment({
html: '<p>Comment 2</p>'
});
({frame} = await initialize({
mockedApi,
page,
publication: 'Publisher weekly',
labs: {
commentImprovements: true
}
}));
});
// NOTE: form counts are replies + 1 for the main form that is now always shown
// at the top of the comments list
test('autocloses open reply forms when opening another', async ({}) => {
const comment1 = await frame.getByTestId('comment-component').nth(0);
await comment1.getByTestId('reply-button').nth(1).click();
Refactored comments-ui form management (#21621) ref https://linear.app/ghost/issue/PLG-230 closes https://linear.app/ghost/issue/PLG-256 Adding an in-reply-to reference link/snippet to reply forms was proving difficult with the previous setup due the amount of data that needed to be passed up and down a deeply nested component tree. This refactor lays the groundwork for making that easier and aims to make form autoclose behaviour more centralised by keeping the open form state in app context and the opening/closing of forms in actions so there's less need for messy local state and to drill functions down the component tree. - replaces `openFormCount` context state with an `openCommentForms` array - keeping detailed open form references in the application state means the display of forms is centrally managed rather than managed via local state inside components - it simplifies some of the problems faced with the `<PublishedComment>` component that previously managed form display. That component is re-used for both the top-level comment as well as replies even though replying on a reply puts the top-level comment into reply mode meaning we had a mess of local state and passed-through functions to work around the component having varying behaviour depending on nesting level - `openFormCount` is still available on the application state via `useMemo` on the provider meaning the implementation of `openCommentForms` is hidden from app code that just needs to know if forms are open - removes `<AutocloseForm>` as the autoclose behaviour is now controlled via the `openCommentForm` action - updated `<Form>` so it manages the "has unsaved changes" properties on `openFormComments` ready for use by `openCommentForm`'s autoclosing behaviour
2024-11-14 18:26:23 +00:00
await expect(frame.getByTestId('form')).toHaveCount(2);
const comment2 = await frame.getByTestId('comment-component').nth(3);
await comment2.getByTestId('reply-button').click();
await expect(frame.getByTestId('form')).toHaveCount(2);
});
test('does not autoclose open reply form with unsaved changes', async ({}) => {
const comment1 = await frame.getByTestId('comment-component').nth(0);
await comment1.getByTestId('reply-button').nth(1).click();
Refactored comments-ui form management (#21621) ref https://linear.app/ghost/issue/PLG-230 closes https://linear.app/ghost/issue/PLG-256 Adding an in-reply-to reference link/snippet to reply forms was proving difficult with the previous setup due the amount of data that needed to be passed up and down a deeply nested component tree. This refactor lays the groundwork for making that easier and aims to make form autoclose behaviour more centralised by keeping the open form state in app context and the opening/closing of forms in actions so there's less need for messy local state and to drill functions down the component tree. - replaces `openFormCount` context state with an `openCommentForms` array - keeping detailed open form references in the application state means the display of forms is centrally managed rather than managed via local state inside components - it simplifies some of the problems faced with the `<PublishedComment>` component that previously managed form display. That component is re-used for both the top-level comment as well as replies even though replying on a reply puts the top-level comment into reply mode meaning we had a mess of local state and passed-through functions to work around the component having varying behaviour depending on nesting level - `openFormCount` is still available on the application state via `useMemo` on the provider meaning the implementation of `openCommentForms` is hidden from app code that just needs to know if forms are open - removes `<AutocloseForm>` as the autoclose behaviour is now controlled via the `openCommentForm` action - updated `<Form>` so it manages the "has unsaved changes" properties on `openFormComments` ready for use by `openCommentForm`'s autoclosing behaviour
2024-11-14 18:26:23 +00:00
await expect(frame.getByTestId('form')).toHaveCount(2);
const editor = frame.getByTestId('form-editor').nth(1);
await waitEditorFocused(editor);
await editor.type('Replying to comment 1');
const comment2 = await frame.getByTestId('comment-component').nth(3);
await comment2.getByTestId('reply-button').click();
await expect(frame.getByTestId('form')).toHaveCount(3);
});
});