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