| 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 {ChangedFile, ChangedFileStatus, RepoRelativePath} from '../types'; |
| b69ab31 | | | 9 | |
| b69ab31 | | | 10 | import {act, fireEvent, render, screen, waitFor} from '@testing-library/react'; |
| b69ab31 | | | 11 | import {nextTick} from 'shared/testUtils'; |
| b69ab31 | | | 12 | import App from '../App'; |
| b69ab31 | | | 13 | import {__TEST__} from '../ChangedFilesWithFetching'; |
| b69ab31 | | | 14 | import {CommitInfoTestUtils, ignoreRTL} from '../testQueries'; |
| b69ab31 | | | 15 | import { |
| b69ab31 | | | 16 | COMMIT, |
| b69ab31 | | | 17 | expectMessageNOTSentToServer, |
| b69ab31 | | | 18 | expectMessageSentToServer, |
| b69ab31 | | | 19 | resetTestMessages, |
| b69ab31 | | | 20 | simulateCommits, |
| b69ab31 | | | 21 | simulateMessageFromServer, |
| b69ab31 | | | 22 | simulateRepoConnected, |
| b69ab31 | | | 23 | } from '../testUtils'; |
| b69ab31 | | | 24 | import {leftPad} from '../utils'; |
| b69ab31 | | | 25 | |
| b69ab31 | | | 26 | function makeFiles(n: number): Array<RepoRelativePath> { |
| b69ab31 | | | 27 | return new Array(n).fill(null).map((_, i) => `file${leftPad(i, 3, '0')}.txt`); |
| b69ab31 | | | 28 | } |
| b69ab31 | | | 29 | function withStatus(files: Array<RepoRelativePath>, status: ChangedFileStatus): Array<ChangedFile> { |
| b69ab31 | | | 30 | return files.map(path => ({path, status})); |
| b69ab31 | | | 31 | } |
| b69ab31 | | | 32 | |
| b69ab31 | | | 33 | describe('ChangedFilesWithFetching', () => { |
| b69ab31 | | | 34 | beforeEach(() => { |
| b69ab31 | | | 35 | resetTestMessages(); |
| b69ab31 | | | 36 | render(<App />); |
| b69ab31 | | | 37 | act(() => { |
| b69ab31 | | | 38 | simulateRepoConnected(); |
| b69ab31 | | | 39 | expectMessageSentToServer({ |
| b69ab31 | | | 40 | type: 'subscribe', |
| b69ab31 | | | 41 | kind: 'smartlogCommits', |
| b69ab31 | | | 42 | subscriptionID: expect.anything(), |
| b69ab31 | | | 43 | }); |
| b69ab31 | | | 44 | simulateCommits({ |
| b69ab31 | | | 45 | value: [ |
| b69ab31 | | | 46 | COMMIT('1', 'some public base', '0', {phase: 'public', isDot: true}), |
| b69ab31 | | | 47 | COMMIT('a', 'My Commit', '1', { |
| b69ab31 | | | 48 | totalFileCount: 2, |
| b69ab31 | | | 49 | filePathsSample: ['file1.js', 'file2.js'], |
| b69ab31 | | | 50 | }), |
| b69ab31 | | | 51 | COMMIT('b', 'Another Commit', 'a', { |
| b69ab31 | | | 52 | totalFileCount: 700, |
| b69ab31 | | | 53 | filePathsSample: makeFiles(500), |
| b69ab31 | | | 54 | }), |
| b69ab31 | | | 55 | COMMIT('c', 'Another Commit 2', 'a', { |
| b69ab31 | | | 56 | totalFileCount: 700, |
| b69ab31 | | | 57 | filePathsSample: makeFiles(500), |
| b69ab31 | | | 58 | }), |
| b69ab31 | | | 59 | ], |
| b69ab31 | | | 60 | }); |
| b69ab31 | | | 61 | }); |
| b69ab31 | | | 62 | }); |
| b69ab31 | | | 63 | |
| b69ab31 | | | 64 | afterEach(() => { |
| b69ab31 | | | 65 | // reset cache between tests |
| b69ab31 | | | 66 | __TEST__.commitFilesCache.clear(); |
| b69ab31 | | | 67 | }); |
| b69ab31 | | | 68 | |
| b69ab31 | | | 69 | async function waitForNextPageToLoad() { |
| b69ab31 | | | 70 | await waitFor(() => { |
| b69ab31 | | | 71 | expect(screen.getByTestId('changed-files-next-page')).toBeInTheDocument(); |
| b69ab31 | | | 72 | }); |
| b69ab31 | | | 73 | } |
| b69ab31 | | | 74 | |
| b69ab31 | | | 75 | it("Does not fetch files if they're already all fetched", () => { |
| b69ab31 | | | 76 | CommitInfoTestUtils.clickToSelectCommit('a'); |
| b69ab31 | | | 77 | |
| b69ab31 | | | 78 | expectMessageNOTSentToServer({ |
| b69ab31 | | | 79 | type: 'fetchCommitChangedFiles', |
| b69ab31 | | | 80 | hash: 'a', |
| b69ab31 | | | 81 | limit: expect.anything(), |
| b69ab31 | | | 82 | }); |
| b69ab31 | | | 83 | }); |
| b69ab31 | | | 84 | |
| b69ab31 | | | 85 | it('Fetches files and shows additional pages', async () => { |
| b69ab31 | | | 86 | CommitInfoTestUtils.clickToSelectCommit('b'); |
| b69ab31 | | | 87 | |
| b69ab31 | | | 88 | expectMessageSentToServer({type: 'fetchCommitChangedFiles', hash: 'b', limit: undefined}); |
| b69ab31 | | | 89 | act(() => { |
| b69ab31 | | | 90 | simulateMessageFromServer({ |
| b69ab31 | | | 91 | type: 'fetchedCommitChangedFiles', |
| b69ab31 | | | 92 | hash: 'b', |
| b69ab31 | | | 93 | result: {value: {filesSample: withStatus(makeFiles(510), 'M'), totalFileCount: 510}}, |
| b69ab31 | | | 94 | }); |
| b69ab31 | | | 95 | }); |
| b69ab31 | | | 96 | |
| b69ab31 | | | 97 | await waitForNextPageToLoad(); |
| b69ab31 | | | 98 | |
| b69ab31 | | | 99 | expect(screen.getByText(ignoreRTL('file000.txt'))).toBeInTheDocument(); |
| b69ab31 | | | 100 | expect(screen.getByText(ignoreRTL('file499.txt'))).toBeInTheDocument(); |
| b69ab31 | | | 101 | expect(screen.queryByText(ignoreRTL('file500.txt'))).not.toBeInTheDocument(); |
| b69ab31 | | | 102 | fireEvent.click(screen.getByTestId('changed-files-next-page')); |
| b69ab31 | | | 103 | expect(screen.queryByText(ignoreRTL('file499.txt'))).not.toBeInTheDocument(); |
| b69ab31 | | | 104 | expect(screen.getByText(ignoreRTL('file500.txt'))).toBeInTheDocument(); |
| b69ab31 | | | 105 | expect(screen.getByText(ignoreRTL('file509.txt'))).toBeInTheDocument(); |
| b69ab31 | | | 106 | }); |
| b69ab31 | | | 107 | |
| b69ab31 | | | 108 | it('Caches files', () => { |
| b69ab31 | | | 109 | CommitInfoTestUtils.clickToSelectCommit('c'); |
| b69ab31 | | | 110 | CommitInfoTestUtils.clickToSelectCommit('a'); |
| b69ab31 | | | 111 | resetTestMessages(); |
| b69ab31 | | | 112 | |
| b69ab31 | | | 113 | CommitInfoTestUtils.clickToSelectCommit('c'); |
| b69ab31 | | | 114 | expectMessageNOTSentToServer({type: 'fetchCommitChangedFiles', hash: expect.anything()}); |
| b69ab31 | | | 115 | }); |
| b69ab31 | | | 116 | |
| b69ab31 | | | 117 | it('Handles race condition when switching commits and responses come in wrong order', async () => { |
| b69ab31 | | | 118 | // Select commit 'b' - this will start fetching files |
| b69ab31 | | | 119 | CommitInfoTestUtils.clickToSelectCommit('b'); |
| b69ab31 | | | 120 | expectMessageSentToServer({type: 'fetchCommitChangedFiles', hash: 'b', limit: undefined}); |
| b69ab31 | | | 121 | |
| b69ab31 | | | 122 | // Before 'b' response comes back, switch to commit 'c' |
| b69ab31 | | | 123 | CommitInfoTestUtils.clickToSelectCommit('c'); |
| b69ab31 | | | 124 | expectMessageSentToServer({type: 'fetchCommitChangedFiles', hash: 'c', limit: undefined}); |
| b69ab31 | | | 125 | |
| b69ab31 | | | 126 | // Simulate 'c' response coming back first (fast) |
| b69ab31 | | | 127 | act(() => { |
| b69ab31 | | | 128 | simulateMessageFromServer({ |
| b69ab31 | | | 129 | type: 'fetchedCommitChangedFiles', |
| b69ab31 | | | 130 | hash: 'c', |
| b69ab31 | | | 131 | result: { |
| b69ab31 | | | 132 | value: { |
| b69ab31 | | | 133 | filesSample: withStatus(['fileC1.txt', 'fileC2.txt', 'fileC3.txt'], 'M'), |
| b69ab31 | | | 134 | totalFileCount: 3, |
| b69ab31 | | | 135 | }, |
| b69ab31 | | | 136 | }, |
| b69ab31 | | | 137 | }); |
| b69ab31 | | | 138 | }); |
| b69ab31 | | | 139 | |
| b69ab31 | | | 140 | // Verify we're showing commit 'c' files |
| b69ab31 | | | 141 | await waitFor(() => { |
| b69ab31 | | | 142 | expect(screen.getByText(ignoreRTL('fileC1.txt'))).toBeInTheDocument(); |
| b69ab31 | | | 143 | expect(screen.getByText(ignoreRTL('fileC2.txt'))).toBeInTheDocument(); |
| b69ab31 | | | 144 | }); |
| b69ab31 | | | 145 | |
| b69ab31 | | | 146 | // Now simulate 'b' response coming back late (slow) |
| b69ab31 | | | 147 | act(() => { |
| b69ab31 | | | 148 | simulateMessageFromServer({ |
| b69ab31 | | | 149 | type: 'fetchedCommitChangedFiles', |
| b69ab31 | | | 150 | hash: 'b', |
| b69ab31 | | | 151 | result: { |
| b69ab31 | | | 152 | value: { |
| b69ab31 | | | 153 | filesSample: withStatus(['fileB1.txt', 'fileB2.txt', 'fileB3.txt'], 'M'), |
| b69ab31 | | | 154 | totalFileCount: 3, |
| b69ab31 | | | 155 | }, |
| b69ab31 | | | 156 | }, |
| b69ab31 | | | 157 | }); |
| b69ab31 | | | 158 | }); |
| b69ab31 | | | 159 | |
| b69ab31 | | | 160 | // Wait a bit to ensure any state updates have completed |
| b69ab31 | | | 161 | await nextTick(); |
| b69ab31 | | | 162 | |
| b69ab31 | | | 163 | await waitFor(() => { |
| b69ab31 | | | 164 | // The files should STILL be from commit 'c', not 'b' |
| b69ab31 | | | 165 | // With the fix enabled, the cancellation token prevents 'b' from overwriting 'c' |
| b69ab31 | | | 166 | expect(screen.getByText(ignoreRTL('fileC1.txt'))).toBeInTheDocument(); |
| b69ab31 | | | 167 | expect(screen.getByText(ignoreRTL('fileC2.txt'))).toBeInTheDocument(); |
| b69ab31 | | | 168 | }); |
| b69ab31 | | | 169 | |
| b69ab31 | | | 170 | // Verify we're NOT showing commit 'b' files |
| b69ab31 | | | 171 | expect(screen.queryByText(ignoreRTL('fileB1.txt'))).not.toBeInTheDocument(); |
| b69ab31 | | | 172 | expect(screen.queryByText(ignoreRTL('fileB2.txt'))).not.toBeInTheDocument(); |
| b69ab31 | | | 173 | }); |
| b69ab31 | | | 174 | }); |