| 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 {act, fireEvent, render, screen, waitFor} from '@testing-library/react'; |
| b69ab31 | | | 9 | import * as utils from 'shared/utils'; |
| b69ab31 | | | 10 | import App from '../App'; |
| b69ab31 | | | 11 | import { |
| b69ab31 | | | 12 | COMMIT, |
| b69ab31 | | | 13 | closeCommitInfoSidebar, |
| b69ab31 | | | 14 | expectMessageSentToServer, |
| b69ab31 | | | 15 | getLastMessageOfTypeSentToServer, |
| b69ab31 | | | 16 | simulateCommits, |
| b69ab31 | | | 17 | simulateMessageFromServer, |
| b69ab31 | | | 18 | simulateRepoConnected, |
| b69ab31 | | | 19 | } from '../testUtils'; |
| b69ab31 | | | 20 | import {CommandRunner} from '../types'; |
| b69ab31 | | | 21 | |
| b69ab31 | | | 22 | const allCommits = [ |
| b69ab31 | | | 23 | COMMIT('1', 'some public base', '0', {phase: 'public'}), |
| b69ab31 | | | 24 | COMMIT('a', 'My Commit', '1'), |
| b69ab31 | | | 25 | COMMIT('b', 'Another Commit', 'a', {isDot: true}), |
| b69ab31 | | | 26 | ]; |
| b69ab31 | | | 27 | |
| b69ab31 | | | 28 | describe('CommitTreeList', () => { |
| b69ab31 | | | 29 | beforeEach(() => { |
| b69ab31 | | | 30 | render(<App />); |
| b69ab31 | | | 31 | act(() => { |
| b69ab31 | | | 32 | simulateRepoConnected(); |
| b69ab31 | | | 33 | closeCommitInfoSidebar(); |
| b69ab31 | | | 34 | expectMessageSentToServer({ |
| b69ab31 | | | 35 | type: 'subscribe', |
| b69ab31 | | | 36 | kind: 'smartlogCommits', |
| b69ab31 | | | 37 | subscriptionID: expect.anything(), |
| b69ab31 | | | 38 | }); |
| b69ab31 | | | 39 | simulateCommits({value: allCommits}); |
| b69ab31 | | | 40 | }); |
| b69ab31 | | | 41 | }); |
| b69ab31 | | | 42 | |
| b69ab31 | | | 43 | it('load more button works', () => { |
| b69ab31 | | | 44 | fireEvent.click(screen.getByText('Load more commits')); |
| b69ab31 | | | 45 | expectMessageSentToServer({type: 'loadMoreCommits'}); |
| b69ab31 | | | 46 | act(() => simulateMessageFromServer({type: 'commitsShownRange', rangeInDays: 60})); |
| b69ab31 | | | 47 | act(() => simulateMessageFromServer({type: 'beganLoadingMoreCommits'})); |
| b69ab31 | | | 48 | act(() => simulateCommits({value: allCommits})); |
| b69ab31 | | | 49 | }); |
| b69ab31 | | | 50 | |
| b69ab31 | | | 51 | it('disables while running', () => { |
| b69ab31 | | | 52 | fireEvent.click(screen.getByText('Load more commits')); |
| b69ab31 | | | 53 | expectMessageSentToServer({type: 'loadMoreCommits'}); |
| b69ab31 | | | 54 | act(() => simulateMessageFromServer({type: 'commitsShownRange', rangeInDays: 60})); |
| b69ab31 | | | 55 | act(() => simulateMessageFromServer({type: 'beganLoadingMoreCommits'})); |
| b69ab31 | | | 56 | expect(screen.getByText('Load more commits')).toBeDisabled(); |
| b69ab31 | | | 57 | |
| b69ab31 | | | 58 | act(() => simulateCommits({value: allCommits})); |
| b69ab31 | | | 59 | expect(screen.getByText('Load more commits')).not.toBeDisabled(); |
| b69ab31 | | | 60 | }); |
| b69ab31 | | | 61 | |
| b69ab31 | | | 62 | it('uses cloud sync after loading all commits', async () => { |
| b69ab31 | | | 63 | fireEvent.click(screen.getByText('Load more commits')); |
| b69ab31 | | | 64 | expectMessageSentToServer({type: 'loadMoreCommits'}); |
| b69ab31 | | | 65 | act(() => simulateMessageFromServer({type: 'commitsShownRange', rangeInDays: 60})); |
| b69ab31 | | | 66 | act(() => simulateMessageFromServer({type: 'beganLoadingMoreCommits'})); |
| b69ab31 | | | 67 | act(() => simulateCommits({value: allCommits})); |
| b69ab31 | | | 68 | |
| b69ab31 | | | 69 | fireEvent.click(screen.getByText('Load more commits')); |
| b69ab31 | | | 70 | expectMessageSentToServer({type: 'loadMoreCommits'}); |
| b69ab31 | | | 71 | act(() => simulateMessageFromServer({type: 'commitsShownRange', rangeInDays: undefined})); |
| b69ab31 | | | 72 | act(() => simulateMessageFromServer({type: 'beganLoadingMoreCommits'})); |
| b69ab31 | | | 73 | act(() => simulateCommits({value: allCommits})); |
| b69ab31 | | | 74 | |
| b69ab31 | | | 75 | expectMessageSentToServer({type: 'getConfig', name: 'extensions.commitcloud'}); |
| b69ab31 | | | 76 | act(() => |
| b69ab31 | | | 77 | simulateMessageFromServer({type: 'gotConfig', name: 'extensions.commitcloud', value: ''}), |
| b69ab31 | | | 78 | ); |
| b69ab31 | | | 79 | |
| b69ab31 | | | 80 | await waitFor(() => expect(screen.getByText('Fetch all cloud commits'))); |
| b69ab31 | | | 81 | fireEvent.click(screen.getByText('Fetch all cloud commits')); |
| b69ab31 | | | 82 | |
| b69ab31 | | | 83 | const message = await waitFor(() => |
| b69ab31 | | | 84 | utils.nullthrows(getLastMessageOfTypeSentToServer('runOperation')), |
| b69ab31 | | | 85 | ); |
| b69ab31 | | | 86 | const id = message.operation.id; |
| b69ab31 | | | 87 | |
| b69ab31 | | | 88 | expectMessageSentToServer({ |
| b69ab31 | | | 89 | type: 'runOperation', |
| b69ab31 | | | 90 | operation: { |
| b69ab31 | | | 91 | args: ['cloud', 'sync', '--full'], |
| b69ab31 | | | 92 | id, |
| b69ab31 | | | 93 | runner: CommandRunner.Sapling, |
| b69ab31 | | | 94 | trackEventName: 'CommitCloudSyncOperation', |
| b69ab31 | | | 95 | }, |
| b69ab31 | | | 96 | }); |
| b69ab31 | | | 97 | |
| b69ab31 | | | 98 | act(() => |
| b69ab31 | | | 99 | simulateMessageFromServer({ |
| b69ab31 | | | 100 | type: 'operationProgress', |
| b69ab31 | | | 101 | id, |
| b69ab31 | | | 102 | kind: 'spawn', |
| b69ab31 | | | 103 | queue: [], |
| b69ab31 | | | 104 | }), |
| b69ab31 | | | 105 | ); |
| b69ab31 | | | 106 | act(() => |
| b69ab31 | | | 107 | simulateMessageFromServer({ |
| b69ab31 | | | 108 | type: 'operationProgress', |
| b69ab31 | | | 109 | id, |
| b69ab31 | | | 110 | kind: 'exit', |
| b69ab31 | | | 111 | exitCode: 0, |
| b69ab31 | | | 112 | timestamp: 1234, |
| b69ab31 | | | 113 | }), |
| b69ab31 | | | 114 | ); |
| b69ab31 | | | 115 | |
| b69ab31 | | | 116 | // buttons are gone now that we synced from cloud |
| b69ab31 | | | 117 | await waitFor(() => { |
| b69ab31 | | | 118 | expect(screen.queryByText('Load more commits')).not.toBeInTheDocument(); |
| b69ab31 | | | 119 | expect(screen.queryByText('Fetch all cloud commits')).not.toBeInTheDocument(); |
| b69ab31 | | | 120 | }); |
| b69ab31 | | | 121 | }); |
| b69ab31 | | | 122 | |
| b69ab31 | | | 123 | it('does not show cloud sync button if commit cloud not enabled', async () => { |
| b69ab31 | | | 124 | fireEvent.click(screen.getByText('Load more commits')); |
| b69ab31 | | | 125 | expectMessageSentToServer({type: 'loadMoreCommits'}); |
| b69ab31 | | | 126 | act(() => simulateMessageFromServer({type: 'commitsShownRange', rangeInDays: 60})); |
| b69ab31 | | | 127 | act(() => simulateMessageFromServer({type: 'beganLoadingMoreCommits'})); |
| b69ab31 | | | 128 | act(() => simulateCommits({value: allCommits})); |
| b69ab31 | | | 129 | |
| b69ab31 | | | 130 | fireEvent.click(screen.getByText('Load more commits')); |
| b69ab31 | | | 131 | expectMessageSentToServer({type: 'loadMoreCommits'}); |
| b69ab31 | | | 132 | act(() => simulateMessageFromServer({type: 'commitsShownRange', rangeInDays: undefined})); |
| b69ab31 | | | 133 | act(() => simulateMessageFromServer({type: 'beganLoadingMoreCommits'})); |
| b69ab31 | | | 134 | act(() => simulateCommits({value: allCommits})); |
| b69ab31 | | | 135 | |
| b69ab31 | | | 136 | expectMessageSentToServer({type: 'getConfig', name: 'extensions.commitcloud'}); |
| b69ab31 | | | 137 | // eslint-disable-next-line require-await |
| b69ab31 | | | 138 | await act(async () => |
| b69ab31 | | | 139 | simulateMessageFromServer({type: 'gotConfig', name: 'extensions.commitcloud', value: '!'}), |
| b69ab31 | | | 140 | ); |
| b69ab31 | | | 141 | |
| b69ab31 | | | 142 | expect(screen.queryByText('Fetch all cloud commits')).not.toBeInTheDocument(); |
| b69ab31 | | | 143 | }); |
| b69ab31 | | | 144 | }); |