addons/isl/src/__tests__/FetchAdditionalCommitsButton.test.tsxblame
View source
b69ab311/**
b69ab312 * Copyright (c) Meta Platforms, Inc. and affiliates.
b69ab313 *
b69ab314 * This source code is licensed under the MIT license found in the
b69ab315 * LICENSE file in the root directory of this source tree.
b69ab316 */
b69ab317
b69ab318import {act, fireEvent, render, screen, waitFor} from '@testing-library/react';
b69ab319import * as utils from 'shared/utils';
b69ab3110import App from '../App';
b69ab3111import {
b69ab3112 COMMIT,
b69ab3113 closeCommitInfoSidebar,
b69ab3114 expectMessageSentToServer,
b69ab3115 getLastMessageOfTypeSentToServer,
b69ab3116 simulateCommits,
b69ab3117 simulateMessageFromServer,
b69ab3118 simulateRepoConnected,
b69ab3119} from '../testUtils';
b69ab3120import {CommandRunner} from '../types';
b69ab3121
b69ab3122const allCommits = [
b69ab3123 COMMIT('1', 'some public base', '0', {phase: 'public'}),
b69ab3124 COMMIT('a', 'My Commit', '1'),
b69ab3125 COMMIT('b', 'Another Commit', 'a', {isDot: true}),
b69ab3126];
b69ab3127
b69ab3128describe('CommitTreeList', () => {
b69ab3129 beforeEach(() => {
b69ab3130 render(<App />);
b69ab3131 act(() => {
b69ab3132 simulateRepoConnected();
b69ab3133 closeCommitInfoSidebar();
b69ab3134 expectMessageSentToServer({
b69ab3135 type: 'subscribe',
b69ab3136 kind: 'smartlogCommits',
b69ab3137 subscriptionID: expect.anything(),
b69ab3138 });
b69ab3139 simulateCommits({value: allCommits});
b69ab3140 });
b69ab3141 });
b69ab3142
b69ab3143 it('load more button works', () => {
b69ab3144 fireEvent.click(screen.getByText('Load more commits'));
b69ab3145 expectMessageSentToServer({type: 'loadMoreCommits'});
b69ab3146 act(() => simulateMessageFromServer({type: 'commitsShownRange', rangeInDays: 60}));
b69ab3147 act(() => simulateMessageFromServer({type: 'beganLoadingMoreCommits'}));
b69ab3148 act(() => simulateCommits({value: allCommits}));
b69ab3149 });
b69ab3150
b69ab3151 it('disables while running', () => {
b69ab3152 fireEvent.click(screen.getByText('Load more commits'));
b69ab3153 expectMessageSentToServer({type: 'loadMoreCommits'});
b69ab3154 act(() => simulateMessageFromServer({type: 'commitsShownRange', rangeInDays: 60}));
b69ab3155 act(() => simulateMessageFromServer({type: 'beganLoadingMoreCommits'}));
b69ab3156 expect(screen.getByText('Load more commits')).toBeDisabled();
b69ab3157
b69ab3158 act(() => simulateCommits({value: allCommits}));
b69ab3159 expect(screen.getByText('Load more commits')).not.toBeDisabled();
b69ab3160 });
b69ab3161
b69ab3162 it('uses cloud sync after loading all commits', async () => {
b69ab3163 fireEvent.click(screen.getByText('Load more commits'));
b69ab3164 expectMessageSentToServer({type: 'loadMoreCommits'});
b69ab3165 act(() => simulateMessageFromServer({type: 'commitsShownRange', rangeInDays: 60}));
b69ab3166 act(() => simulateMessageFromServer({type: 'beganLoadingMoreCommits'}));
b69ab3167 act(() => simulateCommits({value: allCommits}));
b69ab3168
b69ab3169 fireEvent.click(screen.getByText('Load more commits'));
b69ab3170 expectMessageSentToServer({type: 'loadMoreCommits'});
b69ab3171 act(() => simulateMessageFromServer({type: 'commitsShownRange', rangeInDays: undefined}));
b69ab3172 act(() => simulateMessageFromServer({type: 'beganLoadingMoreCommits'}));
b69ab3173 act(() => simulateCommits({value: allCommits}));
b69ab3174
b69ab3175 expectMessageSentToServer({type: 'getConfig', name: 'extensions.commitcloud'});
b69ab3176 act(() =>
b69ab3177 simulateMessageFromServer({type: 'gotConfig', name: 'extensions.commitcloud', value: ''}),
b69ab3178 );
b69ab3179
b69ab3180 await waitFor(() => expect(screen.getByText('Fetch all cloud commits')));
b69ab3181 fireEvent.click(screen.getByText('Fetch all cloud commits'));
b69ab3182
b69ab3183 const message = await waitFor(() =>
b69ab3184 utils.nullthrows(getLastMessageOfTypeSentToServer('runOperation')),
b69ab3185 );
b69ab3186 const id = message.operation.id;
b69ab3187
b69ab3188 expectMessageSentToServer({
b69ab3189 type: 'runOperation',
b69ab3190 operation: {
b69ab3191 args: ['cloud', 'sync', '--full'],
b69ab3192 id,
b69ab3193 runner: CommandRunner.Sapling,
b69ab3194 trackEventName: 'CommitCloudSyncOperation',
b69ab3195 },
b69ab3196 });
b69ab3197
b69ab3198 act(() =>
b69ab3199 simulateMessageFromServer({
b69ab31100 type: 'operationProgress',
b69ab31101 id,
b69ab31102 kind: 'spawn',
b69ab31103 queue: [],
b69ab31104 }),
b69ab31105 );
b69ab31106 act(() =>
b69ab31107 simulateMessageFromServer({
b69ab31108 type: 'operationProgress',
b69ab31109 id,
b69ab31110 kind: 'exit',
b69ab31111 exitCode: 0,
b69ab31112 timestamp: 1234,
b69ab31113 }),
b69ab31114 );
b69ab31115
b69ab31116 // buttons are gone now that we synced from cloud
b69ab31117 await waitFor(() => {
b69ab31118 expect(screen.queryByText('Load more commits')).not.toBeInTheDocument();
b69ab31119 expect(screen.queryByText('Fetch all cloud commits')).not.toBeInTheDocument();
b69ab31120 });
b69ab31121 });
b69ab31122
b69ab31123 it('does not show cloud sync button if commit cloud not enabled', async () => {
b69ab31124 fireEvent.click(screen.getByText('Load more commits'));
b69ab31125 expectMessageSentToServer({type: 'loadMoreCommits'});
b69ab31126 act(() => simulateMessageFromServer({type: 'commitsShownRange', rangeInDays: 60}));
b69ab31127 act(() => simulateMessageFromServer({type: 'beganLoadingMoreCommits'}));
b69ab31128 act(() => simulateCommits({value: allCommits}));
b69ab31129
b69ab31130 fireEvent.click(screen.getByText('Load more commits'));
b69ab31131 expectMessageSentToServer({type: 'loadMoreCommits'});
b69ab31132 act(() => simulateMessageFromServer({type: 'commitsShownRange', rangeInDays: undefined}));
b69ab31133 act(() => simulateMessageFromServer({type: 'beganLoadingMoreCommits'}));
b69ab31134 act(() => simulateCommits({value: allCommits}));
b69ab31135
b69ab31136 expectMessageSentToServer({type: 'getConfig', name: 'extensions.commitcloud'});
b69ab31137 // eslint-disable-next-line require-await
b69ab31138 await act(async () =>
b69ab31139 simulateMessageFromServer({type: 'gotConfig', name: 'extensions.commitcloud', value: '!'}),
b69ab31140 );
b69ab31141
b69ab31142 expect(screen.queryByText('Fetch all cloud commits')).not.toBeInTheDocument();
b69ab31143 });
b69ab31144});