4.5 KB131 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 type {ChangedFile} from '../../types';
9
10import {act, fireEvent, render, screen, waitFor} from '@testing-library/react';
11import App from '../../App';
12import platform from '../../platform';
13import {CommitTreeListTestUtils, ignoreRTL} from '../../testQueries';
14import {
15 COMMIT,
16 closeCommitInfoSidebar,
17 expectMessageSentToServer,
18 resetTestMessages,
19 simulateCommits,
20 simulateMessageFromServer,
21} from '../../testUtils';
22import {CommandRunner} from '../../types';
23
24const {withinCommitTree} = CommitTreeListTestUtils;
25
26const FILEPATH1 = 'file1.txt';
27const FILEPATH2 = 'file2.txt';
28const FILEPATH3 = 'file3.txt';
29const FILE1 = {path: FILEPATH1, status: 'M'} as ChangedFile;
30const FILE2 = {path: FILEPATH2, status: 'A'} as ChangedFile;
31const FILE3 = {path: FILEPATH3, status: 'R'} as ChangedFile;
32describe('UncommitOperation', () => {
33 beforeEach(() => {
34 resetTestMessages();
35 render(<App />);
36 act(() => {
37 closeCommitInfoSidebar();
38 expectMessageSentToServer({
39 type: 'subscribe',
40 kind: 'smartlogCommits',
41 subscriptionID: expect.anything(),
42 });
43 simulateCommits({
44 value: [
45 COMMIT('1', 'Commit 1', '0', {phase: 'public'}),
46 COMMIT('a', 'Commit A', '1', {filePathsSample: [FILEPATH1]}),
47 COMMIT('b', 'Commit B', 'a', {filePathsSample: [FILEPATH1, FILEPATH2]}),
48 COMMIT('c', 'Commit C', 'b', {
49 isDot: true,
50 filePathsSample: [FILEPATH1, FILEPATH2, FILEPATH3],
51 }),
52 ],
53 });
54 });
55
56 jest.spyOn(platform, 'confirm').mockImplementation(() => Promise.resolve(true));
57 });
58
59 const clickUncommit = async (hash: string, filesSample: Array<ChangedFile>) => {
60 const quickCommitButton = screen.queryByTestId('uncommit-button');
61 act(() => {
62 fireEvent.click(quickCommitButton as Element);
63 });
64 await waitFor(() => {
65 expectMessageSentToServer({
66 type: 'fetchCommitChangedFiles',
67 hash,
68 limit: undefined,
69 });
70 });
71 act(() => {
72 simulateMessageFromServer({
73 type: 'fetchedCommitChangedFiles',
74 hash,
75 result: {
76 value: {
77 totalFileCount: 3,
78 filesSample,
79 },
80 },
81 });
82 });
83 await waitFor(() =>
84 expectMessageSentToServer({
85 type: 'runOperation',
86 operation: {
87 args: ['uncommit'],
88 id: expect.anything(),
89 runner: CommandRunner.Sapling,
90 trackEventName: 'UncommitOperation',
91 },
92 }),
93 );
94 };
95
96 it('confirms before uncommitting', async () => {
97 expect(withinCommitTree().queryByText(ignoreRTL('file1.txt'))).not.toBeInTheDocument();
98 expect(withinCommitTree().queryByText(ignoreRTL('file2.txt'))).not.toBeInTheDocument();
99 expect(withinCommitTree().queryByText(ignoreRTL('file3.txt'))).not.toBeInTheDocument();
100
101 const spy = jest.spyOn(platform, 'confirm').mockImplementation(() => Promise.resolve(true));
102 await clickUncommit('c', [FILE1, FILE2, FILE3]);
103 expect(spy).toHaveBeenCalledTimes(1);
104
105 expect(withinCommitTree().getByText(ignoreRTL('file1.txt'))).toBeInTheDocument();
106 expect(withinCommitTree().getByText(ignoreRTL('file2.txt'))).toBeInTheDocument();
107 expect(withinCommitTree().getByText(ignoreRTL('file3.txt'))).toBeInTheDocument();
108 });
109
110 it('works on commit with children', async () => {
111 act(() => {
112 simulateCommits({
113 value: [
114 COMMIT('1', 'Commit 1', '0', {phase: 'public'}),
115 COMMIT('a', 'Commit A', '1', {filePathsSample: [FILEPATH1]}),
116 COMMIT('b', 'Commit B', 'a', {isDot: true, filePathsSample: [FILEPATH1, FILEPATH2]}),
117 COMMIT('c', 'Commit C', 'b', {filePathsSample: [FILEPATH1, FILEPATH2, FILEPATH3]}),
118 ],
119 });
120 });
121
122 expect(withinCommitTree().queryByText(ignoreRTL('file1.txt'))).not.toBeInTheDocument();
123 expect(withinCommitTree().queryByText(ignoreRTL('file2.txt'))).not.toBeInTheDocument();
124 expect(withinCommitTree().queryByText(ignoreRTL('file3.txt'))).not.toBeInTheDocument();
125 await clickUncommit('b', [FILE1, FILE2]);
126 expect(withinCommitTree().getByText(ignoreRTL('file1.txt'))).toBeInTheDocument();
127 expect(withinCommitTree().getByText(ignoreRTL('file2.txt'))).toBeInTheDocument();
128 expect(withinCommitTree().queryByText(ignoreRTL('file3.txt'))).not.toBeInTheDocument();
129 });
130});
131