| b69ab31 | | | 1 | /** |
| b69ab31 | | | 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| b69ab31 | | | 3 | * |
| b69ab31 | | | 4 | * This source code is licensed under the MIT license found in the |
| b69ab31 | | | 5 | * LICENSE file in the root directory of this source tree. |
| b69ab31 | | | 6 | */ |
| b69ab31 | | | 7 | |
| b69ab31 | | | 8 | import type {RenderResult} from '@testing-library/react'; |
| b69ab31 | | | 9 | |
| b69ab31 | | | 10 | import {act, cleanup, fireEvent, render, screen, waitFor, within} from '@testing-library/react'; |
| b69ab31 | | | 11 | import fs from 'node:fs'; |
| b69ab31 | | | 12 | import path from 'node:path'; |
| b69ab31 | | | 13 | import {ComparisonType} from 'shared/Comparison'; |
| b69ab31 | | | 14 | import {nextTick} from 'shared/testUtils'; |
| b69ab31 | | | 15 | import {nullthrows} from 'shared/utils'; |
| b69ab31 | | | 16 | import App from '../App'; |
| b69ab31 | | | 17 | import {cancelAllHighlightingTasks} from '../ComparisonView/SplitDiffView/syntaxHighlighting'; |
| b69ab31 | | | 18 | import {parsePatchAndFilter, sortFilesByType} from '../ComparisonView/utils'; |
| b69ab31 | | | 19 | import { |
| b69ab31 | | | 20 | COMMIT, |
| b69ab31 | | | 21 | expectMessageSentToServer, |
| b69ab31 | | | 22 | openCommitInfoSidebar, |
| b69ab31 | | | 23 | resetTestMessages, |
| b69ab31 | | | 24 | simulateCommits, |
| b69ab31 | | | 25 | simulateMessageFromServer, |
| b69ab31 | | | 26 | simulateUncommittedChangedFiles, |
| b69ab31 | | | 27 | waitForWithTick, |
| b69ab31 | | | 28 | } from '../testUtils'; |
| b69ab31 | | | 29 | import {GeneratedStatus} from '../types'; |
| b69ab31 | | | 30 | |
| b69ab31 | | | 31 | afterEach(cleanup); |
| b69ab31 | | | 32 | |
| b69ab31 | | | 33 | const UNCOMMITTED_CHANGES_DIFF = `\ |
| b69ab31 | | | 34 | diff --git deletedFile.txt deletedFile.txt |
| b69ab31 | | | 35 | deleted file mode 100644 |
| b69ab31 | | | 36 | --- deletedFile.txt |
| b69ab31 | | | 37 | +++ /dev/null |
| b69ab31 | | | 38 | @@ -1,1 +0,0 @@ |
| b69ab31 | | | 39 | -Goodbye |
| b69ab31 | | | 40 | diff --git newFile.txt newFile.txt |
| b69ab31 | | | 41 | new file mode 100644 |
| b69ab31 | | | 42 | --- /dev/null |
| b69ab31 | | | 43 | +++ newFile.txt |
| b69ab31 | | | 44 | @@ -0,0 +1,1 @@ |
| b69ab31 | | | 45 | +hello |
| b69ab31 | | | 46 | diff --git someFile.txt someFile.txt |
| b69ab31 | | | 47 | --- someFile.txt |
| b69ab31 | | | 48 | +++ someFile.txt |
| b69ab31 | | | 49 | @@ -7,5 +7,5 @@ |
| b69ab31 | | | 50 | line 7 |
| b69ab31 | | | 51 | line 8 |
| b69ab31 | | | 52 | -line 9 |
| b69ab31 | | | 53 | +line 9 - modified |
| b69ab31 | | | 54 | line 10 |
| b69ab31 | | | 55 | line 11 |
| b69ab31 | | | 56 | diff --git -r a1b2c3d4e5f6 some/path/foo.go |
| b69ab31 | | | 57 | --- some/path/foo.go |
| b69ab31 | | | 58 | +++ some/path/foo.go |
| b69ab31 | | | 59 | @@ -0,1 +0,1 @@ |
| b69ab31 | | | 60 | -println("hi") |
| b69ab31 | | | 61 | +fmt.Println("hi") |
| b69ab31 | | | 62 | `; |
| b69ab31 | | | 63 | |
| b69ab31 | | | 64 | const DIFF_WITH_SYNTAX = `\ |
| b69ab31 | | | 65 | diff --git deletedFile.js deletedFile.js |
| b69ab31 | | | 66 | deleted file mode 100644 |
| b69ab31 | | | 67 | --- deletedFile.js |
| b69ab31 | | | 68 | +++ /dev/null |
| b69ab31 | | | 69 | @@ -1,1 +0,0 @@ |
| b69ab31 | | | 70 | -console.log('goodbye'); |
| b69ab31 | | | 71 | diff --git newFile.js newFile.js |
| b69ab31 | | | 72 | new file mode 100644 |
| b69ab31 | | | 73 | --- /dev/null |
| b69ab31 | | | 74 | +++ newFile.js |
| b69ab31 | | | 75 | @@ -0,0 +1,1 @@ |
| b69ab31 | | | 76 | +console.log('hello'); |
| b69ab31 | | | 77 | diff --git someFile.js someFile.js |
| b69ab31 | | | 78 | --- someFile.js |
| b69ab31 | | | 79 | +++ someFile.js |
| b69ab31 | | | 80 | @@ -2,5 +2,5 @@ |
| b69ab31 | | | 81 | function foo() { |
| b69ab31 | | | 82 | const variable_in_context_line = 0; |
| b69ab31 | | | 83 | - const variable_in_before = 1; |
| b69ab31 | | | 84 | + const variable_in_after = 1; |
| b69ab31 | | | 85 | console.log(variable_in_content_line); |
| b69ab31 | | | 86 | } |
| b69ab31 | | | 87 | `; |
| b69ab31 | | | 88 | |
| b69ab31 | | | 89 | Object.defineProperty(navigator, 'clipboard', { |
| b69ab31 | | | 90 | value: { |
| b69ab31 | | | 91 | writeText: jest.fn(() => Promise.resolve()), |
| b69ab31 | | | 92 | }, |
| b69ab31 | | | 93 | }); |
| b69ab31 | | | 94 | |
| b69ab31 | | | 95 | /* eslint-disable @typescript-eslint/no-non-null-assertion */ |
| b69ab31 | | | 96 | |
| b69ab31 | | | 97 | describe('ComparisonView', () => { |
| b69ab31 | | | 98 | let app: RenderResult | null = null; |
| b69ab31 | | | 99 | beforeEach(() => { |
| b69ab31 | | | 100 | mockFetchToSupportSyntaxHighlighting(); |
| b69ab31 | | | 101 | resetTestMessages(); |
| b69ab31 | | | 102 | app = render(<App />); |
| b69ab31 | | | 103 | act(() => { |
| b69ab31 | | | 104 | openCommitInfoSidebar(); |
| b69ab31 | | | 105 | simulateCommits({ |
| b69ab31 | | | 106 | value: [ |
| b69ab31 | | | 107 | COMMIT('1', 'some public base', '0', {phase: 'public'}), |
| b69ab31 | | | 108 | COMMIT('a', 'My Commit', '1'), |
| b69ab31 | | | 109 | COMMIT('b', 'Another Commit', 'a', {isDot: true}), |
| b69ab31 | | | 110 | ], |
| b69ab31 | | | 111 | }); |
| b69ab31 | | | 112 | simulateUncommittedChangedFiles({ |
| b69ab31 | | | 113 | value: [{path: 'src/file1.txt', status: 'M'}], |
| b69ab31 | | | 114 | }); |
| b69ab31 | | | 115 | }); |
| b69ab31 | | | 116 | }); |
| b69ab31 | | | 117 | |
| b69ab31 | | | 118 | // Not afterEach because afterEach runs in a separate tick and can be too |
| b69ab31 | | | 119 | // late to avoid act(..) warnings or timeout. |
| b69ab31 | | | 120 | function unmountNow() { |
| b69ab31 | | | 121 | cancelAllHighlightingTasks(); |
| b69ab31 | | | 122 | app?.unmount(); |
| b69ab31 | | | 123 | } |
| b69ab31 | | | 124 | |
| b69ab31 | | | 125 | afterEach(() => { |
| b69ab31 | | | 126 | jest.clearAllMocks(); |
| b69ab31 | | | 127 | }); |
| b69ab31 | | | 128 | |
| b69ab31 | | | 129 | async function clickComparisonViewButton() { |
| b69ab31 | | | 130 | await act(async () => { |
| b69ab31 | | | 131 | const button = screen.getByTestId('open-comparison-view-button-Uncommitted'); |
| b69ab31 | | | 132 | fireEvent.click(button); |
| b69ab31 | | | 133 | await nextTick(); |
| b69ab31 | | | 134 | }); |
| b69ab31 | | | 135 | } |
| b69ab31 | | | 136 | async function openUncommittedChangesComparison( |
| b69ab31 | | | 137 | diffContent?: string, |
| b69ab31 | | | 138 | generatedStatuses?: Record<string, GeneratedStatus>, |
| b69ab31 | | | 139 | ) { |
| b69ab31 | | | 140 | await clickComparisonViewButton(); |
| b69ab31 | | | 141 | await waitFor( |
| b69ab31 | | | 142 | () => |
| b69ab31 | | | 143 | expectMessageSentToServer({ |
| b69ab31 | | | 144 | type: 'requestComparison', |
| b69ab31 | | | 145 | comparison: {type: ComparisonType.UncommittedChanges}, |
| b69ab31 | | | 146 | }), |
| b69ab31 | | | 147 | // Since this dynamically imports the comparison view, it may take a while to load in resource-constrained CI, |
| b69ab31 | | | 148 | // so add a generous timeout to reducy flakiness. |
| b69ab31 | | | 149 | {timeout: 10_000}, |
| b69ab31 | | | 150 | ); |
| b69ab31 | | | 151 | act(() => { |
| b69ab31 | | | 152 | simulateMessageFromServer({ |
| b69ab31 | | | 153 | type: 'fetchedGeneratedStatuses', |
| b69ab31 | | | 154 | results: generatedStatuses ?? {}, |
| b69ab31 | | | 155 | }); |
| b69ab31 | | | 156 | }); |
| b69ab31 | | | 157 | await act(async () => { |
| b69ab31 | | | 158 | simulateMessageFromServer({ |
| b69ab31 | | | 159 | type: 'comparison', |
| b69ab31 | | | 160 | comparison: {type: ComparisonType.UncommittedChanges}, |
| b69ab31 | | | 161 | data: {diff: {value: diffContent ?? UNCOMMITTED_CHANGES_DIFF}}, |
| b69ab31 | | | 162 | }); |
| b69ab31 | | | 163 | await nextTick(); |
| b69ab31 | | | 164 | }); |
| b69ab31 | | | 165 | } |
| b69ab31 | | | 166 | function inComparisonView() { |
| b69ab31 | | | 167 | return within(screen.getByTestId('comparison-view')); |
| b69ab31 | | | 168 | } |
| b69ab31 | | | 169 | |
| b69ab31 | | | 170 | function closeComparisonView() { |
| b69ab31 | | | 171 | const closeButton = inComparisonView().getByTestId('close-comparison-view-button'); |
| b69ab31 | | | 172 | expect(closeButton).toBeInTheDocument(); |
| b69ab31 | | | 173 | act(() => { |
| b69ab31 | | | 174 | fireEvent.click(closeButton); |
| b69ab31 | | | 175 | }); |
| b69ab31 | | | 176 | } |
| b69ab31 | | | 177 | |
| b69ab31 | | | 178 | it('Loads comparison', async () => { |
| b69ab31 | | | 179 | await openUncommittedChangesComparison(); |
| b69ab31 | | | 180 | // Prevent act(..) warnings. This cannot be afterEach() which is too late. |
| b69ab31 | | | 181 | unmountNow(); |
| b69ab31 | | | 182 | }); |
| b69ab31 | | | 183 | |
| b69ab31 | | | 184 | it('parses files from comparison', async () => { |
| b69ab31 | | | 185 | await openUncommittedChangesComparison(); |
| b69ab31 | | | 186 | expect(inComparisonView().getByText('someFile.txt')).toBeInTheDocument(); |
| b69ab31 | | | 187 | expect(inComparisonView().getByText('newFile.txt')).toBeInTheDocument(); |
| b69ab31 | | | 188 | expect(inComparisonView().getByText('deletedFile.txt')).toBeInTheDocument(); |
| b69ab31 | | | 189 | unmountNow(); |
| b69ab31 | | | 190 | }); |
| b69ab31 | | | 191 | |
| b69ab31 | | | 192 | it('show file contents', async () => { |
| b69ab31 | | | 193 | await openUncommittedChangesComparison(); |
| b69ab31 | | | 194 | expect(inComparisonView().getByText('- modified')).toBeInTheDocument(); |
| b69ab31 | | | 195 | expect(inComparisonView().getAllByText('line 7')[0]).toBeInTheDocument(); |
| b69ab31 | | | 196 | expect(inComparisonView().getAllByText('line 8')[0]).toBeInTheDocument(); |
| b69ab31 | | | 197 | expect(inComparisonView().getAllByText('line 9')[0]).toBeInTheDocument(); |
| b69ab31 | | | 198 | expect(inComparisonView().getAllByText('line 10')[0]).toBeInTheDocument(); |
| b69ab31 | | | 199 | expect(inComparisonView().getAllByText('line 11')[0]).toBeInTheDocument(); |
| b69ab31 | | | 200 | unmountNow(); |
| b69ab31 | | | 201 | }); |
| b69ab31 | | | 202 | |
| b69ab31 | | | 203 | it('loads remaining lines', async () => { |
| b69ab31 | | | 204 | await openUncommittedChangesComparison(); |
| b69ab31 | | | 205 | const expandButton = inComparisonView().getByText('Expand 6 lines'); |
| b69ab31 | | | 206 | expect(expandButton).toBeInTheDocument(); |
| b69ab31 | | | 207 | act(() => { |
| b69ab31 | | | 208 | fireEvent.click(expandButton); |
| b69ab31 | | | 209 | }); |
| b69ab31 | | | 210 | await waitFor(() => { |
| b69ab31 | | | 211 | expectMessageSentToServer({ |
| b69ab31 | | | 212 | type: 'requestComparisonContextLines', |
| b69ab31 | | | 213 | id: {path: 'someFile.txt', comparison: {type: ComparisonType.UncommittedChanges}}, |
| b69ab31 | | | 214 | numLines: 6, |
| b69ab31 | | | 215 | start: 1, |
| b69ab31 | | | 216 | }); |
| b69ab31 | | | 217 | }); |
| b69ab31 | | | 218 | act(() => { |
| b69ab31 | | | 219 | simulateMessageFromServer({ |
| b69ab31 | | | 220 | type: 'comparisonContextLines', |
| b69ab31 | | | 221 | lines: {value: ['line 1', 'line 2', 'line 3', 'line 4', 'line 5', 'line 6']}, |
| b69ab31 | | | 222 | path: 'someFile.txt', |
| b69ab31 | | | 223 | }); |
| b69ab31 | | | 224 | }); |
| b69ab31 | | | 225 | await waitFor(() => { |
| b69ab31 | | | 226 | expect(inComparisonView().getAllByText('line 1')[0]).toBeInTheDocument(); |
| b69ab31 | | | 227 | expect(inComparisonView().getAllByText('line 2')[0]).toBeInTheDocument(); |
| b69ab31 | | | 228 | expect(inComparisonView().getAllByText('line 3')[0]).toBeInTheDocument(); |
| b69ab31 | | | 229 | expect(inComparisonView().getAllByText('line 4')[0]).toBeInTheDocument(); |
| b69ab31 | | | 230 | expect(inComparisonView().getAllByText('line 5')[0]).toBeInTheDocument(); |
| b69ab31 | | | 231 | expect(inComparisonView().getAllByText('line 6')[0]).toBeInTheDocument(); |
| b69ab31 | | | 232 | }); |
| b69ab31 | | | 233 | unmountNow(); |
| b69ab31 | | | 234 | }); |
| b69ab31 | | | 235 | |
| b69ab31 | | | 236 | it('can close comparison', async () => { |
| b69ab31 | | | 237 | await openUncommittedChangesComparison(); |
| b69ab31 | | | 238 | expect(inComparisonView().getByText('- modified')).toBeInTheDocument(); |
| b69ab31 | | | 239 | closeComparisonView(); |
| b69ab31 | | | 240 | expect(screen.queryByText('- modified')).not.toBeInTheDocument(); |
| b69ab31 | | | 241 | unmountNow(); |
| b69ab31 | | | 242 | }); |
| b69ab31 | | | 243 | |
| b69ab31 | | | 244 | it('invalidates cached remaining lines when the head commit changes', async () => { |
| b69ab31 | | | 245 | await openUncommittedChangesComparison(); |
| b69ab31 | | | 246 | const clickExpand = () => { |
| b69ab31 | | | 247 | const expandButton = inComparisonView().getByText('Expand 6 lines'); |
| b69ab31 | | | 248 | expect(expandButton).toBeInTheDocument(); |
| b69ab31 | | | 249 | act(() => { |
| b69ab31 | | | 250 | fireEvent.click(expandButton); |
| b69ab31 | | | 251 | }); |
| b69ab31 | | | 252 | }; |
| b69ab31 | | | 253 | clickExpand(); |
| b69ab31 | | | 254 | await waitFor(() => { |
| b69ab31 | | | 255 | expectMessageSentToServer({ |
| b69ab31 | | | 256 | type: 'requestComparisonContextLines', |
| b69ab31 | | | 257 | id: {path: 'someFile.txt', comparison: {type: ComparisonType.UncommittedChanges}}, |
| b69ab31 | | | 258 | numLines: 6, |
| b69ab31 | | | 259 | start: 1, |
| b69ab31 | | | 260 | }); |
| b69ab31 | | | 261 | }); |
| b69ab31 | | | 262 | act(() => { |
| b69ab31 | | | 263 | simulateMessageFromServer({ |
| b69ab31 | | | 264 | type: 'comparisonContextLines', |
| b69ab31 | | | 265 | lines: {value: ['line 1', 'line 2', 'line 3', 'line 4', 'line 5', 'line 6']}, |
| b69ab31 | | | 266 | path: 'someFile.txt', |
| b69ab31 | | | 267 | }); |
| b69ab31 | | | 268 | }); |
| b69ab31 | | | 269 | await waitForWithTick(() => { |
| b69ab31 | | | 270 | expect(inComparisonView().getAllByText('line 1')[0]).toBeInTheDocument(); |
| b69ab31 | | | 271 | expect(inComparisonView().getAllByText('line 6')[0]).toBeInTheDocument(); |
| b69ab31 | | | 272 | }); |
| b69ab31 | | | 273 | |
| b69ab31 | | | 274 | closeComparisonView(); |
| b69ab31 | | | 275 | resetTestMessages(); // make sure we don't find previous "requestComparisonContextLines" in later assertions |
| b69ab31 | | | 276 | |
| b69ab31 | | | 277 | // head commit changes |
| b69ab31 | | | 278 | |
| b69ab31 | | | 279 | act(() => { |
| b69ab31 | | | 280 | simulateCommits({ |
| b69ab31 | | | 281 | value: [ |
| b69ab31 | | | 282 | COMMIT('1', 'some public base', '0', {phase: 'public'}), |
| b69ab31 | | | 283 | COMMIT('a', 'My Commit', '1'), |
| b69ab31 | | | 284 | COMMIT('b', 'Another Commit', 'a'), |
| b69ab31 | | | 285 | COMMIT('c', 'New commit!', 'b', {isDot: true}), |
| b69ab31 | | | 286 | ], |
| b69ab31 | | | 287 | }); |
| b69ab31 | | | 288 | }); |
| b69ab31 | | | 289 | await openUncommittedChangesComparison(); |
| b69ab31 | | | 290 | expect(inComparisonView().getByText('- modified')).toBeInTheDocument(); |
| b69ab31 | | | 291 | |
| b69ab31 | | | 292 | clickExpand(); |
| b69ab31 | | | 293 | |
| b69ab31 | | | 294 | // previous context lines are no longer there |
| b69ab31 | | | 295 | expect(inComparisonView().queryByText('line 1')).not.toBeInTheDocument(); |
| b69ab31 | | | 296 | |
| b69ab31 | | | 297 | // it should ask for the line contents from the server again |
| b69ab31 | | | 298 | await waitFor(() => { |
| b69ab31 | | | 299 | expectMessageSentToServer({ |
| b69ab31 | | | 300 | type: 'requestComparisonContextLines', |
| b69ab31 | | | 301 | id: {path: 'someFile.txt', comparison: {type: ComparisonType.UncommittedChanges}}, |
| b69ab31 | | | 302 | numLines: 6, |
| b69ab31 | | | 303 | start: 1, |
| b69ab31 | | | 304 | }); |
| b69ab31 | | | 305 | }); |
| b69ab31 | | | 306 | act(() => { |
| b69ab31 | | | 307 | simulateMessageFromServer({ |
| b69ab31 | | | 308 | type: 'comparisonContextLines', |
| b69ab31 | | | 309 | lines: { |
| b69ab31 | | | 310 | value: [ |
| b69ab31 | | | 311 | 'different line 1', |
| b69ab31 | | | 312 | 'different line 2', |
| b69ab31 | | | 313 | 'different line 3', |
| b69ab31 | | | 314 | 'different line 4', |
| b69ab31 | | | 315 | 'different line 5', |
| b69ab31 | | | 316 | 'different line 6', |
| b69ab31 | | | 317 | ], |
| b69ab31 | | | 318 | }, |
| b69ab31 | | | 319 | path: 'someFile.txt', |
| b69ab31 | | | 320 | }); |
| b69ab31 | | | 321 | }); |
| b69ab31 | | | 322 | // new data is used |
| b69ab31 | | | 323 | await waitForWithTick(() => { |
| b69ab31 | | | 324 | expect(inComparisonView().getAllByText('different line 1')[0]).toBeInTheDocument(); |
| b69ab31 | | | 325 | expect(inComparisonView().getAllByText('different line 6')[0]).toBeInTheDocument(); |
| b69ab31 | | | 326 | }); |
| b69ab31 | | | 327 | unmountNow(); |
| b69ab31 | | | 328 | }); |
| b69ab31 | | | 329 | |
| b69ab31 | | | 330 | it('refresh button requests new data', async () => { |
| b69ab31 | | | 331 | await openUncommittedChangesComparison(); |
| b69ab31 | | | 332 | resetTestMessages(); |
| b69ab31 | | | 333 | |
| b69ab31 | | | 334 | act(() => { |
| b69ab31 | | | 335 | fireEvent.click(inComparisonView().getByTestId('comparison-refresh-button')); |
| b69ab31 | | | 336 | }); |
| b69ab31 | | | 337 | |
| b69ab31 | | | 338 | expectMessageSentToServer({ |
| b69ab31 | | | 339 | type: 'requestComparison', |
| b69ab31 | | | 340 | comparison: {type: ComparisonType.UncommittedChanges}, |
| b69ab31 | | | 341 | }); |
| b69ab31 | | | 342 | unmountNow(); |
| b69ab31 | | | 343 | }); |
| b69ab31 | | | 344 | |
| b69ab31 | | | 345 | it('changing comparison mode requests new data', async () => { |
| b69ab31 | | | 346 | await openUncommittedChangesComparison(); |
| b69ab31 | | | 347 | |
| b69ab31 | | | 348 | act(() => { |
| b69ab31 | | | 349 | fireEvent.change(inComparisonView().getByTestId('comparison-view-picker'), { |
| b69ab31 | | | 350 | target: {value: ComparisonType.StackChanges}, |
| b69ab31 | | | 351 | }); |
| b69ab31 | | | 352 | }); |
| b69ab31 | | | 353 | expectMessageSentToServer({ |
| b69ab31 | | | 354 | type: 'requestComparison', |
| b69ab31 | | | 355 | comparison: {type: ComparisonType.StackChanges}, |
| b69ab31 | | | 356 | }); |
| b69ab31 | | | 357 | unmountNow(); |
| b69ab31 | | | 358 | }); |
| b69ab31 | | | 359 | |
| b69ab31 | | | 360 | it('shows a spinner while a fetch is ongoing', async () => { |
| b69ab31 | | | 361 | await clickComparisonViewButton(); |
| b69ab31 | | | 362 | expect(inComparisonView().getByTestId('comparison-loading')).toBeInTheDocument(); |
| b69ab31 | | | 363 | |
| b69ab31 | | | 364 | await act(async () => { |
| b69ab31 | | | 365 | simulateMessageFromServer({ |
| b69ab31 | | | 366 | type: 'comparison', |
| b69ab31 | | | 367 | comparison: {type: ComparisonType.UncommittedChanges}, |
| b69ab31 | | | 368 | data: {diff: {value: UNCOMMITTED_CHANGES_DIFF}}, |
| b69ab31 | | | 369 | }); |
| b69ab31 | | | 370 | await nextTick(); |
| b69ab31 | | | 371 | }); |
| b69ab31 | | | 372 | expect(inComparisonView().queryByTestId('comparison-loading')).not.toBeInTheDocument(); |
| b69ab31 | | | 373 | unmountNow(); |
| b69ab31 | | | 374 | }); |
| b69ab31 | | | 375 | |
| b69ab31 | | | 376 | it('copies file path on click', async () => { |
| b69ab31 | | | 377 | await openUncommittedChangesComparison(); |
| b69ab31 | | | 378 | |
| b69ab31 | | | 379 | // Click on the "foo.go" of "some/path/foo.go". |
| b69ab31 | | | 380 | act(() => { |
| b69ab31 | | | 381 | fireEvent.click(inComparisonView().getByText('foo.go')); |
| b69ab31 | | | 382 | }); |
| b69ab31 | | | 383 | expect(navigator.clipboard.writeText).toHaveBeenCalledTimes(1); |
| b69ab31 | | | 384 | expect(navigator.clipboard.writeText).toHaveBeenCalledWith('foo.go'); |
| b69ab31 | | | 385 | |
| b69ab31 | | | 386 | // Click on the "some/" of "some/path/foo.go". |
| b69ab31 | | | 387 | act(() => { |
| b69ab31 | | | 388 | fireEvent.click(inComparisonView().getByText('some/')); |
| b69ab31 | | | 389 | }); |
| b69ab31 | | | 390 | expect(navigator.clipboard.writeText).toHaveBeenCalledTimes(2); |
| b69ab31 | | | 391 | expect(navigator.clipboard.writeText).toHaveBeenLastCalledWith('some/path/foo.go'); |
| b69ab31 | | | 392 | unmountNow(); |
| b69ab31 | | | 393 | }); |
| b69ab31 | | | 394 | |
| b69ab31 | | | 395 | describe('syntax highlighting', () => { |
| b69ab31 | | | 396 | it('renders syntax highlighting', async () => { |
| b69ab31 | | | 397 | await openUncommittedChangesComparison(DIFF_WITH_SYNTAX); |
| b69ab31 | | | 398 | await waitForSyntaxHighlightingToAppear(screen.getByTestId('comparison-view')); |
| b69ab31 | | | 399 | |
| b69ab31 | | | 400 | // console from console.log is highlighted as its own token |
| b69ab31 | | | 401 | const tokens = within(screen.getByTestId('comparison-view')).queryAllByText('console'); |
| b69ab31 | | | 402 | expect(tokens.length).toBeGreaterThan(0); |
| b69ab31 | | | 403 | // highlighted tokens have classes like mtk1, mtk2, etc. |
| b69ab31 | | | 404 | expect(tokens.some(token => /mtk\d+/.test(token.className))).toBe(true); |
| b69ab31 | | | 405 | unmountNow(); |
| b69ab31 | | | 406 | }); |
| b69ab31 | | | 407 | |
| b69ab31 | | | 408 | it('renders highlighting in context lines and diff lines', async () => { |
| b69ab31 | | | 409 | await openUncommittedChangesComparison(DIFF_WITH_SYNTAX); |
| b69ab31 | | | 410 | await waitForSyntaxHighlightingToAppear(screen.getByTestId('comparison-view')); |
| b69ab31 | | | 411 | |
| b69ab31 | | | 412 | await waitFor(() => { |
| b69ab31 | | | 413 | expect( |
| b69ab31 | | | 414 | within(screen.getByTestId('comparison-view')).queryAllByText('variable_in_context_line'), |
| b69ab31 | | | 415 | ).toHaveLength(2); |
| b69ab31 | | | 416 | expect( |
| b69ab31 | | | 417 | within(screen.getByTestId('comparison-view')).getByText('variable_in_before'), |
| b69ab31 | | | 418 | ).toBeInTheDocument(); |
| b69ab31 | | | 419 | expect( |
| b69ab31 | | | 420 | within(screen.getByTestId('comparison-view')).getByText('variable_in_after'), |
| b69ab31 | | | 421 | ).toBeInTheDocument(); |
| b69ab31 | | | 422 | }); |
| b69ab31 | | | 423 | unmountNow(); |
| b69ab31 | | | 424 | }); |
| b69ab31 | | | 425 | |
| b69ab31 | | | 426 | it('highlights expanded context lines', async () => { |
| b69ab31 | | | 427 | await openUncommittedChangesComparison(DIFF_WITH_SYNTAX); |
| b69ab31 | | | 428 | await waitForSyntaxHighlightingToAppear(screen.getByTestId('comparison-view')); |
| b69ab31 | | | 429 | |
| b69ab31 | | | 430 | const expandButton = inComparisonView().getByText('Expand 1 line'); |
| b69ab31 | | | 431 | expect(expandButton).toBeInTheDocument(); |
| b69ab31 | | | 432 | act(() => { |
| b69ab31 | | | 433 | fireEvent.click(expandButton); |
| b69ab31 | | | 434 | }); |
| b69ab31 | | | 435 | await waitFor(() => { |
| b69ab31 | | | 436 | expectMessageSentToServer({ |
| b69ab31 | | | 437 | type: 'requestComparisonContextLines', |
| b69ab31 | | | 438 | id: {path: 'someFile.js', comparison: {type: ComparisonType.UncommittedChanges}}, |
| b69ab31 | | | 439 | numLines: 1, |
| b69ab31 | | | 440 | start: 1, |
| b69ab31 | | | 441 | }); |
| b69ab31 | | | 442 | }); |
| b69ab31 | | | 443 | act(() => { |
| b69ab31 | | | 444 | simulateMessageFromServer({ |
| b69ab31 | | | 445 | type: 'comparisonContextLines', |
| b69ab31 | | | 446 | lines: {value: ['const loaded_additional_context_variable = 5;']}, |
| b69ab31 | | | 447 | path: 'someFile.js', |
| b69ab31 | | | 448 | }); |
| b69ab31 | | | 449 | }); |
| b69ab31 | | | 450 | await waitFor(() => { |
| b69ab31 | | | 451 | // highlighted token appears by itself |
| b69ab31 | | | 452 | expect( |
| b69ab31 | | | 453 | inComparisonView().queryAllByText('loaded_additional_context_variable'), |
| b69ab31 | | | 454 | ).toHaveLength(2); |
| b69ab31 | | | 455 | }); |
| b69ab31 | | | 456 | unmountNow(); |
| b69ab31 | | | 457 | }); |
| b69ab31 | | | 458 | }); |
| b69ab31 | | | 459 | |
| b69ab31 | | | 460 | const makeFileDiff = (name: string, content: string) => { |
| b69ab31 | | | 461 | return `diff --git file${name}.txt file${name}.txt |
| b69ab31 | | | 462 | --- file${name}.txt |
| b69ab31 | | | 463 | +++ file${name}.txt |
| b69ab31 | | | 464 | @@ -1,2 +1,2 @@ |
| b69ab31 | | | 465 | ${content} |
| b69ab31 | | | 466 | `; |
| b69ab31 | | | 467 | }; |
| b69ab31 | | | 468 | |
| b69ab31 | | | 469 | describe('collapsing files', () => { |
| b69ab31 | | | 470 | it('can click to collapse files', async () => { |
| b69ab31 | | | 471 | const SINGLE_CHANGE = makeFileDiff('1', '+const x = 1;'); |
| b69ab31 | | | 472 | await openUncommittedChangesComparison(SINGLE_CHANGE); |
| b69ab31 | | | 473 | |
| b69ab31 | | | 474 | const collapseButton = screen.getByTestId('split-diff-view-file-header-collapse-button'); |
| b69ab31 | | | 475 | expect(inComparisonView().getByText('const x = 1;')).toBeInTheDocument(); |
| b69ab31 | | | 476 | expect(inComparisonView().getByText('file1.txt')).toBeInTheDocument(); |
| b69ab31 | | | 477 | fireEvent.click(collapseButton); |
| b69ab31 | | | 478 | expect(inComparisonView().queryByText('const x = 1;')).not.toBeInTheDocument(); |
| b69ab31 | | | 479 | expect(inComparisonView().getByText('file1.txt')).toBeInTheDocument(); |
| b69ab31 | | | 480 | }); |
| b69ab31 | | | 481 | |
| b69ab31 | | | 482 | it('first files are expanded, later files are collapsed', async () => { |
| b69ab31 | | | 483 | // 10 files, 1000 added lines each |
| b69ab31 | | | 484 | const HUGE_DIFF = [ |
| b69ab31 | | | 485 | ...new Array(10) |
| b69ab31 | | | 486 | .fill(undefined) |
| b69ab31 | | | 487 | .map((_, index) => |
| b69ab31 | | | 488 | makeFileDiff(String(index), new Array(1001).fill("+console.log('hi');").join('\n')), |
| b69ab31 | | | 489 | ), |
| b69ab31 | | | 490 | ].join('\n'); |
| b69ab31 | | | 491 | await openUncommittedChangesComparison(HUGE_DIFF); |
| b69ab31 | | | 492 | |
| b69ab31 | | | 493 | const collapsedStates = inComparisonView().queryAllByTestId( |
| b69ab31 | | | 494 | /split-diff-view-file-header-(collapse|expand)-button/, |
| b69ab31 | | | 495 | ); |
| b69ab31 | | | 496 | const collapsedValues = collapsedStates.map(node => node.dataset.testid); |
| b69ab31 | | | 497 | expect(collapsedValues).toEqual([ |
| b69ab31 | | | 498 | 'split-diff-view-file-header-collapse-button', |
| b69ab31 | | | 499 | 'split-diff-view-file-header-collapse-button', |
| b69ab31 | | | 500 | 'split-diff-view-file-header-collapse-button', |
| b69ab31 | | | 501 | 'split-diff-view-file-header-expand-button', |
| b69ab31 | | | 502 | 'split-diff-view-file-header-expand-button', |
| b69ab31 | | | 503 | 'split-diff-view-file-header-expand-button', |
| b69ab31 | | | 504 | 'split-diff-view-file-header-expand-button', |
| b69ab31 | | | 505 | 'split-diff-view-file-header-expand-button', |
| b69ab31 | | | 506 | 'split-diff-view-file-header-expand-button', |
| b69ab31 | | | 507 | 'split-diff-view-file-header-expand-button', |
| b69ab31 | | | 508 | ]); |
| b69ab31 | | | 509 | unmountNow(); |
| b69ab31 | | | 510 | }, 20_000 /* potentially slow test */); |
| b69ab31 | | | 511 | |
| b69ab31 | | | 512 | it('a single large file is expanded so you always see something', async () => { |
| b69ab31 | | | 513 | const GIANT_FILE = makeFileDiff( |
| b69ab31 | | | 514 | 'bigChange.txt', |
| b69ab31 | | | 515 | new Array(5000).fill('+big_file_contents').join('\n'), |
| b69ab31 | | | 516 | ); |
| b69ab31 | | | 517 | const SMALL_FILE = makeFileDiff('smallChange.txt', '+small_file_contents'); |
| b69ab31 | | | 518 | const GIANT_AND_SMALL = [GIANT_FILE, SMALL_FILE].join('\n'); |
| b69ab31 | | | 519 | await openUncommittedChangesComparison(GIANT_AND_SMALL); |
| b69ab31 | | | 520 | |
| b69ab31 | | | 521 | // the large file starts expanded |
| b69ab31 | | | 522 | expect(inComparisonView().getAllByText('big_file_contents').length).toBeGreaterThan(0); |
| b69ab31 | | | 523 | // the small file starts collapsed |
| b69ab31 | | | 524 | expect(inComparisonView().queryByText('small_file_contents')).not.toBeInTheDocument(); |
| b69ab31 | | | 525 | unmountNow(); |
| b69ab31 | | | 526 | }); |
| b69ab31 | | | 527 | }); |
| b69ab31 | | | 528 | |
| b69ab31 | | | 529 | describe('generated files', () => { |
| b69ab31 | | | 530 | it('generated status is fetched for files being compared', async () => { |
| b69ab31 | | | 531 | const NORMAL_FILE = makeFileDiff('normal1', '+normal_contents'); |
| b69ab31 | | | 532 | const PARTIAL_FILE = makeFileDiff('partial1', '+partial_contents'); |
| b69ab31 | | | 533 | const GENERATED_FILE = makeFileDiff('generated1', '+generated_contents'); |
| b69ab31 | | | 534 | const ALL = [GENERATED_FILE, PARTIAL_FILE, NORMAL_FILE].join('\n'); |
| b69ab31 | | | 535 | |
| b69ab31 | | | 536 | await openUncommittedChangesComparison(ALL); |
| b69ab31 | | | 537 | await waitFor(() => { |
| b69ab31 | | | 538 | expectMessageSentToServer({ |
| b69ab31 | | | 539 | type: 'fetchGeneratedStatuses', |
| b69ab31 | | | 540 | paths: ['filegenerated1.txt', 'filepartial1.txt', 'filenormal1.txt'], |
| b69ab31 | | | 541 | }); |
| b69ab31 | | | 542 | }); |
| b69ab31 | | | 543 | unmountNow(); |
| b69ab31 | | | 544 | }); |
| b69ab31 | | | 545 | |
| b69ab31 | | | 546 | it('banner says that files are generated', async () => { |
| b69ab31 | | | 547 | const NORMAL_FILE = makeFileDiff('normal2', '+normal_contents'); |
| b69ab31 | | | 548 | const PARTIAL_FILE = makeFileDiff('partial2', '+partial_contents'); |
| b69ab31 | | | 549 | const GENERATED_FILE = makeFileDiff('generated2', '+generated_contents'); |
| b69ab31 | | | 550 | const ALL = [GENERATED_FILE, PARTIAL_FILE, NORMAL_FILE].join('\n'); |
| b69ab31 | | | 551 | |
| b69ab31 | | | 552 | await openUncommittedChangesComparison(ALL, { |
| b69ab31 | | | 553 | 'filenormal2.txt': GeneratedStatus.Manual, |
| b69ab31 | | | 554 | 'filegenerated2.txt': GeneratedStatus.Generated, |
| b69ab31 | | | 555 | 'filepartial2.txt': GeneratedStatus.PartiallyGenerated, |
| b69ab31 | | | 556 | }); |
| b69ab31 | | | 557 | expect(inComparisonView().getByText('This file is generated')).toBeInTheDocument(); |
| b69ab31 | | | 558 | expect(inComparisonView().getByText('This file is partially generated')).toBeInTheDocument(); |
| b69ab31 | | | 559 | unmountNow(); |
| b69ab31 | | | 560 | }); |
| b69ab31 | | | 561 | |
| b69ab31 | | | 562 | it('generated files are collapsed by default', async () => { |
| b69ab31 | | | 563 | const NORMAL_FILE = makeFileDiff('normal3', '+normal_contents'); |
| b69ab31 | | | 564 | const PARTIAL_FILE = makeFileDiff('partial3', '+partial_contents'); |
| b69ab31 | | | 565 | const GENERATED_FILE = makeFileDiff('generated3', '+generated_contents'); |
| b69ab31 | | | 566 | const ALL = [GENERATED_FILE, PARTIAL_FILE, NORMAL_FILE].join('\n'); |
| b69ab31 | | | 567 | |
| b69ab31 | | | 568 | await openUncommittedChangesComparison(ALL, { |
| b69ab31 | | | 569 | 'filenormal3.txt': GeneratedStatus.Manual, |
| b69ab31 | | | 570 | 'filegenerated3.txt': GeneratedStatus.Generated, |
| b69ab31 | | | 571 | 'filepartial3.txt': GeneratedStatus.PartiallyGenerated, |
| b69ab31 | | | 572 | }); |
| b69ab31 | | | 573 | |
| b69ab31 | | | 574 | // normal, partial start expanded |
| b69ab31 | | | 575 | expect(inComparisonView().getByText('normal_contents')).toBeInTheDocument(); |
| b69ab31 | | | 576 | expect(inComparisonView().getByText('partial_contents')).toBeInTheDocument(); |
| b69ab31 | | | 577 | await waitFor(() => { |
| b69ab31 | | | 578 | // generated starts collapsed |
| b69ab31 | | | 579 | expect(inComparisonView().queryByText('generated_contents')).not.toBeInTheDocument(); |
| b69ab31 | | | 580 | }); |
| b69ab31 | | | 581 | |
| b69ab31 | | | 582 | expect(inComparisonView().getByText('Show anyway')).toBeInTheDocument(); |
| b69ab31 | | | 583 | fireEvent.click(inComparisonView().getByText('Show anyway')); |
| b69ab31 | | | 584 | await waitFor(() => { |
| b69ab31 | | | 585 | // generated now expands |
| b69ab31 | | | 586 | expect(inComparisonView().getByText('generated_contents')).toBeInTheDocument(); |
| b69ab31 | | | 587 | }); |
| b69ab31 | | | 588 | unmountNow(); |
| b69ab31 | | | 589 | }); |
| b69ab31 | | | 590 | }); |
| b69ab31 | | | 591 | }); |
| b69ab31 | | | 592 | |
| b69ab31 | | | 593 | function waitForSyntaxHighlightingToAppear(inside: HTMLElement): Promise<void> { |
| b69ab31 | | | 594 | return waitFor(() => { |
| b69ab31 | | | 595 | const tokens = inside.querySelectorAll('.mtk1'); |
| b69ab31 | | | 596 | expect(tokens.length).toBeGreaterThan(0); |
| b69ab31 | | | 597 | }); |
| b69ab31 | | | 598 | } |
| b69ab31 | | | 599 | |
| b69ab31 | | | 600 | function mockFetchToSupportSyntaxHighlighting(): jest.SpyInstance { |
| b69ab31 | | | 601 | return jest.spyOn(global, 'fetch').mockImplementation( |
| b69ab31 | | | 602 | jest.fn(async (url: URL) => { |
| b69ab31 | | | 603 | if (url.pathname.includes('generated/textmate')) { |
| b69ab31 | | | 604 | const match = /.*generated\/textmate\/(.*)$/.exec(url.pathname); |
| b69ab31 | | | 605 | const filename = nullthrows(match)[1]; |
| b69ab31 | | | 606 | const toPublicDir = (filename: string) => |
| b69ab31 | | | 607 | path.normalize(path.join(__dirname, '../../public/generated/textmate', filename)); |
| b69ab31 | | | 608 | if (filename === 'onig.wasm') { |
| b69ab31 | | | 609 | const file = await fs.promises.readFile(toPublicDir(filename)); |
| b69ab31 | | | 610 | return { |
| b69ab31 | | | 611 | headers: new Map(), |
| b69ab31 | | | 612 | arrayBuffer: () => file.buffer, |
| b69ab31 | | | 613 | }; |
| b69ab31 | | | 614 | } else { |
| b69ab31 | | | 615 | const file = await fs.promises.readFile(toPublicDir(filename), 'utf-8'); |
| b69ab31 | | | 616 | return {text: () => file}; |
| b69ab31 | | | 617 | } |
| b69ab31 | | | 618 | } |
| b69ab31 | | | 619 | throw new Error(`${url} not found`); |
| b69ab31 | | | 620 | }) as jest.Mock, |
| b69ab31 | | | 621 | ); |
| b69ab31 | | | 622 | } |
| b69ab31 | | | 623 | |
| b69ab31 | | | 624 | describe('ComparisonView utils', () => { |
| b69ab31 | | | 625 | describe('sortFilesByType', () => { |
| b69ab31 | | | 626 | it('sorts by type', () => { |
| b69ab31 | | | 627 | const files = parsePatchAndFilter(UNCOMMITTED_CHANGES_DIFF); |
| b69ab31 | | | 628 | |
| b69ab31 | | | 629 | expect(files).toEqual([ |
| b69ab31 | | | 630 | expect.objectContaining({newFileName: 'deletedFile.txt', type: 'Removed'}), |
| b69ab31 | | | 631 | expect.objectContaining({newFileName: 'newFile.txt', type: 'Added'}), |
| b69ab31 | | | 632 | expect.objectContaining({newFileName: 'someFile.txt', type: 'Modified'}), |
| b69ab31 | | | 633 | expect.objectContaining({newFileName: 'some/path/foo.go', type: 'Modified'}), |
| b69ab31 | | | 634 | ]); |
| b69ab31 | | | 635 | |
| b69ab31 | | | 636 | sortFilesByType(files); |
| b69ab31 | | | 637 | |
| b69ab31 | | | 638 | expect(files).toEqual([ |
| b69ab31 | | | 639 | expect.objectContaining({newFileName: 'some/path/foo.go', type: 'Modified'}), |
| b69ab31 | | | 640 | expect.objectContaining({newFileName: 'someFile.txt', type: 'Modified'}), |
| b69ab31 | | | 641 | expect.objectContaining({newFileName: 'newFile.txt', type: 'Added'}), |
| b69ab31 | | | 642 | expect.objectContaining({newFileName: 'deletedFile.txt', type: 'Removed'}), |
| b69ab31 | | | 643 | ]); |
| b69ab31 | | | 644 | }); |
| b69ab31 | | | 645 | }); |
| b69ab31 | | | 646 | }); |