addons/isl/src/__tests__/operations/uncommitOperation.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 type {ChangedFile} from '../../types';
b69ab319
b69ab3110import {act, fireEvent, render, screen, waitFor} from '@testing-library/react';
b69ab3111import App from '../../App';
b69ab3112import platform from '../../platform';
b69ab3113import {CommitTreeListTestUtils, ignoreRTL} from '../../testQueries';
b69ab3114import {
b69ab3115 COMMIT,
b69ab3116 closeCommitInfoSidebar,
b69ab3117 expectMessageSentToServer,
b69ab3118 resetTestMessages,
b69ab3119 simulateCommits,
b69ab3120 simulateMessageFromServer,
b69ab3121} from '../../testUtils';
b69ab3122import {CommandRunner} from '../../types';
b69ab3123
b69ab3124const {withinCommitTree} = CommitTreeListTestUtils;
b69ab3125
b69ab3126const FILEPATH1 = 'file1.txt';
b69ab3127const FILEPATH2 = 'file2.txt';
b69ab3128const FILEPATH3 = 'file3.txt';
b69ab3129const FILE1 = {path: FILEPATH1, status: 'M'} as ChangedFile;
b69ab3130const FILE2 = {path: FILEPATH2, status: 'A'} as ChangedFile;
b69ab3131const FILE3 = {path: FILEPATH3, status: 'R'} as ChangedFile;
b69ab3132describe('UncommitOperation', () => {
b69ab3133 beforeEach(() => {
b69ab3134 resetTestMessages();
b69ab3135 render(<App />);
b69ab3136 act(() => {
b69ab3137 closeCommitInfoSidebar();
b69ab3138 expectMessageSentToServer({
b69ab3139 type: 'subscribe',
b69ab3140 kind: 'smartlogCommits',
b69ab3141 subscriptionID: expect.anything(),
b69ab3142 });
b69ab3143 simulateCommits({
b69ab3144 value: [
b69ab3145 COMMIT('1', 'Commit 1', '0', {phase: 'public'}),
b69ab3146 COMMIT('a', 'Commit A', '1', {filePathsSample: [FILEPATH1]}),
b69ab3147 COMMIT('b', 'Commit B', 'a', {filePathsSample: [FILEPATH1, FILEPATH2]}),
b69ab3148 COMMIT('c', 'Commit C', 'b', {
b69ab3149 isDot: true,
b69ab3150 filePathsSample: [FILEPATH1, FILEPATH2, FILEPATH3],
b69ab3151 }),
b69ab3152 ],
b69ab3153 });
b69ab3154 });
b69ab3155
b69ab3156 jest.spyOn(platform, 'confirm').mockImplementation(() => Promise.resolve(true));
b69ab3157 });
b69ab3158
b69ab3159 const clickUncommit = async (hash: string, filesSample: Array<ChangedFile>) => {
b69ab3160 const quickCommitButton = screen.queryByTestId('uncommit-button');
b69ab3161 act(() => {
b69ab3162 fireEvent.click(quickCommitButton as Element);
b69ab3163 });
b69ab3164 await waitFor(() => {
b69ab3165 expectMessageSentToServer({
b69ab3166 type: 'fetchCommitChangedFiles',
b69ab3167 hash,
b69ab3168 limit: undefined,
b69ab3169 });
b69ab3170 });
b69ab3171 act(() => {
b69ab3172 simulateMessageFromServer({
b69ab3173 type: 'fetchedCommitChangedFiles',
b69ab3174 hash,
b69ab3175 result: {
b69ab3176 value: {
b69ab3177 totalFileCount: 3,
b69ab3178 filesSample,
b69ab3179 },
b69ab3180 },
b69ab3181 });
b69ab3182 });
b69ab3183 await waitFor(() =>
b69ab3184 expectMessageSentToServer({
b69ab3185 type: 'runOperation',
b69ab3186 operation: {
b69ab3187 args: ['uncommit'],
b69ab3188 id: expect.anything(),
b69ab3189 runner: CommandRunner.Sapling,
b69ab3190 trackEventName: 'UncommitOperation',
b69ab3191 },
b69ab3192 }),
b69ab3193 );
b69ab3194 };
b69ab3195
b69ab3196 it('confirms before uncommitting', async () => {
b69ab3197 expect(withinCommitTree().queryByText(ignoreRTL('file1.txt'))).not.toBeInTheDocument();
b69ab3198 expect(withinCommitTree().queryByText(ignoreRTL('file2.txt'))).not.toBeInTheDocument();
b69ab3199 expect(withinCommitTree().queryByText(ignoreRTL('file3.txt'))).not.toBeInTheDocument();
b69ab31100
b69ab31101 const spy = jest.spyOn(platform, 'confirm').mockImplementation(() => Promise.resolve(true));
b69ab31102 await clickUncommit('c', [FILE1, FILE2, FILE3]);
b69ab31103 expect(spy).toHaveBeenCalledTimes(1);
b69ab31104
b69ab31105 expect(withinCommitTree().getByText(ignoreRTL('file1.txt'))).toBeInTheDocument();
b69ab31106 expect(withinCommitTree().getByText(ignoreRTL('file2.txt'))).toBeInTheDocument();
b69ab31107 expect(withinCommitTree().getByText(ignoreRTL('file3.txt'))).toBeInTheDocument();
b69ab31108 });
b69ab31109
b69ab31110 it('works on commit with children', async () => {
b69ab31111 act(() => {
b69ab31112 simulateCommits({
b69ab31113 value: [
b69ab31114 COMMIT('1', 'Commit 1', '0', {phase: 'public'}),
b69ab31115 COMMIT('a', 'Commit A', '1', {filePathsSample: [FILEPATH1]}),
b69ab31116 COMMIT('b', 'Commit B', 'a', {isDot: true, filePathsSample: [FILEPATH1, FILEPATH2]}),
b69ab31117 COMMIT('c', 'Commit C', 'b', {filePathsSample: [FILEPATH1, FILEPATH2, FILEPATH3]}),
b69ab31118 ],
b69ab31119 });
b69ab31120 });
b69ab31121
b69ab31122 expect(withinCommitTree().queryByText(ignoreRTL('file1.txt'))).not.toBeInTheDocument();
b69ab31123 expect(withinCommitTree().queryByText(ignoreRTL('file2.txt'))).not.toBeInTheDocument();
b69ab31124 expect(withinCommitTree().queryByText(ignoreRTL('file3.txt'))).not.toBeInTheDocument();
b69ab31125 await clickUncommit('b', [FILE1, FILE2]);
b69ab31126 expect(withinCommitTree().getByText(ignoreRTL('file1.txt'))).toBeInTheDocument();
b69ab31127 expect(withinCommitTree().getByText(ignoreRTL('file2.txt'))).toBeInTheDocument();
b69ab31128 expect(withinCommitTree().queryByText(ignoreRTL('file3.txt'))).not.toBeInTheDocument();
b69ab31129 });
b69ab31130});