|
| 1 | +import React from 'react'; |
| 2 | +import '@testing-library/jest-dom/extend-expect'; |
| 3 | +import userEvent from '@testing-library/user-event'; |
| 4 | +import {useFakeTranslations} from 'pageflow/testHelpers'; |
| 5 | + |
| 6 | +import {EditableText} from 'frontend/commenting/EditableText'; |
| 7 | +import {renderWithCommenting} from 'testHelpers/renderWithCommenting'; |
| 8 | +import {slateSelection} from 'frontend/commenting/slateSelection'; |
| 9 | + |
| 10 | +import {commentHighlightStyles as highlightStyles} from 'pageflow-scrolled/review'; |
| 11 | +import commentingStyles from 'frontend/commenting/EditableTextHighlight.module.css'; |
| 12 | + |
| 13 | +jest.mock('frontend/commenting/slateSelection'); |
| 14 | + |
| 15 | +describe('commenting EditableText', () => { |
| 16 | + useFakeTranslations({ |
| 17 | + 'pageflow_scrolled.review.select_text_to_comment': 'Select text to comment', |
| 18 | + 'pageflow_scrolled.review.add_comment_placeholder': 'Add a comment...', |
| 19 | + 'pageflow_scrolled.review.new_topic': 'New topic', |
| 20 | + 'pageflow_scrolled.review.send': 'Send', |
| 21 | + 'pageflow_scrolled.review.cancel': 'Cancel' |
| 22 | + }); |
| 23 | + |
| 24 | + const value = [{type: 'paragraph', children: [{text: 'Some text to comment on'}]}]; |
| 25 | + |
| 26 | + it('renders plain text without commenting UI when inlineComments is not set', () => { |
| 27 | + const {getByText, queryByText, toggleAddCommentMode} = renderWithCommenting( |
| 28 | + <EditableText value={value} />, |
| 29 | + {inlineComments: false} |
| 30 | + ); |
| 31 | + |
| 32 | + toggleAddCommentMode(); |
| 33 | + |
| 34 | + expect(getByText('Some text to comment on')).toBeInTheDocument(); |
| 35 | + expect(queryByText('Select text to comment')).not.toBeInTheDocument(); |
| 36 | + }); |
| 37 | + |
| 38 | + it('shows hint in add comment mode', () => { |
| 39 | + const {getByText, toggleAddCommentMode} = renderWithCommenting( |
| 40 | + <EditableText value={value} /> |
| 41 | + ); |
| 42 | + |
| 43 | + toggleAddCommentMode(); |
| 44 | + |
| 45 | + expect(getByText('Select text to comment')).toBeInTheDocument(); |
| 46 | + }); |
| 47 | + |
| 48 | + it('highlights selected range on mouseup in add comment mode', async () => { |
| 49 | + const user = userEvent.setup(); |
| 50 | + const slateRange = { |
| 51 | + anchor: {path: [0, 0], offset: 5}, |
| 52 | + focus: {path: [0, 0], offset: 9} |
| 53 | + }; |
| 54 | + |
| 55 | + const {getByText, toggleAddCommentMode} = renderWithCommenting( |
| 56 | + <EditableText value={value} /> |
| 57 | + ); |
| 58 | + |
| 59 | + toggleAddCommentMode(); |
| 60 | + await slateSelection.simulateChange(user, getByText('Some text to comment on'), slateRange); |
| 61 | + |
| 62 | + const highlight = document.querySelector(`.${highlightStyles.highlight}`); |
| 63 | + expect(highlight).toBeInTheDocument(); |
| 64 | + expect(highlight).toHaveTextContent('text'); |
| 65 | + }); |
| 66 | + |
| 67 | + it('hides hint after selection is made', async () => { |
| 68 | + const user = userEvent.setup(); |
| 69 | + const slateRange = { |
| 70 | + anchor: {path: [0, 0], offset: 5}, |
| 71 | + focus: {path: [0, 0], offset: 9} |
| 72 | + }; |
| 73 | + |
| 74 | + const {getByText, queryByText, toggleAddCommentMode} = renderWithCommenting( |
| 75 | + <EditableText value={value} /> |
| 76 | + ); |
| 77 | + |
| 78 | + toggleAddCommentMode(); |
| 79 | + await slateSelection.simulateChange(user, getByText('Some text to comment on'), slateRange); |
| 80 | + |
| 81 | + expect(queryByText('Select text to comment')).not.toBeInTheDocument(); |
| 82 | + }); |
| 83 | + |
| 84 | + it('shows new thread form after selecting text in add comment mode', async () => { |
| 85 | + const user = userEvent.setup(); |
| 86 | + const slateRange = { |
| 87 | + anchor: {path: [0, 0], offset: 5}, |
| 88 | + focus: {path: [0, 0], offset: 9} |
| 89 | + }; |
| 90 | + |
| 91 | + const {getByText, getByPlaceholderText, toggleAddCommentMode} = renderWithCommenting( |
| 92 | + <EditableText value={value} /> |
| 93 | + ); |
| 94 | + |
| 95 | + toggleAddCommentMode(); |
| 96 | + await slateSelection.simulateChange(user, getByText('Some text to comment on'), slateRange); |
| 97 | + |
| 98 | + expect(getByPlaceholderText('Add a comment...')).toBeInTheDocument(); |
| 99 | + }); |
| 100 | + |
| 101 | + it('skips hint and highlights immediately when text is already selected', async () => { |
| 102 | + const user = userEvent.setup(); |
| 103 | + const slateRange = { |
| 104 | + anchor: {path: [0, 0], offset: 5}, |
| 105 | + focus: {path: [0, 0], offset: 9} |
| 106 | + }; |
| 107 | + |
| 108 | + const {getByText, queryByText, toggleAddCommentMode} = renderWithCommenting( |
| 109 | + <EditableText value={value} /> |
| 110 | + ); |
| 111 | + |
| 112 | + await slateSelection.simulateChange(user, getByText('Some text to comment on'), slateRange); |
| 113 | + toggleAddCommentMode(); |
| 114 | + |
| 115 | + expect(queryByText('Select text to comment')).not.toBeInTheDocument(); |
| 116 | + |
| 117 | + const highlight = document.querySelector(`.${highlightStyles.highlight}`); |
| 118 | + expect(highlight).toBeInTheDocument(); |
| 119 | + expect(highlight).toHaveTextContent('text'); |
| 120 | + }); |
| 121 | + |
| 122 | + it('highlights thread ranges as clickable', () => { |
| 123 | + const subjectRange = { |
| 124 | + anchor: {path: [0, 0], offset: 5}, |
| 125 | + focus: {path: [0, 0], offset: 9} |
| 126 | + }; |
| 127 | + |
| 128 | + const {container} = renderWithCommenting( |
| 129 | + <EditableText value={value} />, |
| 130 | + { |
| 131 | + commentThreads: [{ |
| 132 | + id: 1, |
| 133 | + subjectType: 'ContentElement', |
| 134 | + subjectId: 10, |
| 135 | + subjectRange, |
| 136 | + comments: [{id: 1, body: 'A comment', creatorName: 'Alice', creatorId: 1}] |
| 137 | + }] |
| 138 | + } |
| 139 | + ); |
| 140 | + |
| 141 | + const highlight = container.querySelector(`.${highlightStyles.highlight}`); |
| 142 | + expect(highlight).toBeInTheDocument(); |
| 143 | + expect(highlight).toHaveTextContent('text'); |
| 144 | + expect(highlight).toHaveClass(commentingStyles.clickable); |
| 145 | + }); |
| 146 | + |
| 147 | + it('shows badge for thread with subjectRange', () => { |
| 148 | + const subjectRange = { |
| 149 | + anchor: {path: [0, 0], offset: 5}, |
| 150 | + focus: {path: [0, 0], offset: 9} |
| 151 | + }; |
| 152 | + |
| 153 | + const {getByRole} = renderWithCommenting( |
| 154 | + <EditableText value={value} />, |
| 155 | + { |
| 156 | + commentThreads: [{ |
| 157 | + id: 1, |
| 158 | + subjectType: 'ContentElement', |
| 159 | + subjectId: 10, |
| 160 | + subjectRange, |
| 161 | + comments: [{id: 1, body: 'A comment', creatorName: 'Alice', creatorId: 1}] |
| 162 | + }] |
| 163 | + } |
| 164 | + ); |
| 165 | + |
| 166 | + expect(getByRole('status')).toBeInTheDocument(); |
| 167 | + }); |
| 168 | + |
| 169 | + it('does not show new form when selecting range of existing thread', async () => { |
| 170 | + const user = userEvent.setup(); |
| 171 | + const subjectRange = { |
| 172 | + anchor: {path: [0, 0], offset: 5}, |
| 173 | + focus: {path: [0, 0], offset: 9} |
| 174 | + }; |
| 175 | + |
| 176 | + const {container, queryByPlaceholderText, toggleAddCommentMode} = renderWithCommenting( |
| 177 | + <EditableText value={value} />, |
| 178 | + { |
| 179 | + commentThreads: [{ |
| 180 | + id: 1, |
| 181 | + subjectType: 'ContentElement', |
| 182 | + subjectId: 10, |
| 183 | + subjectRange, |
| 184 | + comments: [{id: 1, body: 'A comment', creatorName: 'Alice', creatorId: 1}] |
| 185 | + }] |
| 186 | + } |
| 187 | + ); |
| 188 | + |
| 189 | + toggleAddCommentMode(); |
| 190 | + await slateSelection.simulateChange( |
| 191 | + user, |
| 192 | + container.querySelector('[data-slate-editor]'), |
| 193 | + subjectRange |
| 194 | + ); |
| 195 | + |
| 196 | + expect(queryByPlaceholderText('Add a comment...')).not.toBeInTheDocument(); |
| 197 | + }); |
| 198 | +}); |
0 commit comments