6.5 KB188 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} from '@testing-library/react';
9import userEvent from '@testing-library/user-event';
10import App from '../App';
11import {CommitInfoTestUtils} from '../testQueries';
12import {
13 closeCommitInfoSidebar,
14 expectMessageNOTSentToServer,
15 expectMessageSentToServer,
16 openCommitInfoSidebar,
17 resetTestMessages,
18 simulateCommits,
19 TEST_COMMIT_HISTORY,
20} from '../testUtils';
21import {CommandRunner} from '../types';
22
23/*eslint-disable @typescript-eslint/no-non-null-assertion */
24
25describe('combine', () => {
26 beforeEach(() => {
27 resetTestMessages();
28 render(<App />);
29 act(() => {
30 closeCommitInfoSidebar();
31 expectMessageSentToServer({
32 type: 'subscribe',
33 kind: 'smartlogCommits',
34 subscriptionID: expect.anything(),
35 });
36 simulateCommits({
37 value: TEST_COMMIT_HISTORY,
38 });
39 });
40 });
41
42 describe('shows preview for contiguous commits', () => {
43 it('shows preview button for contiguous commits', () => {
44 CommitInfoTestUtils.clickToSelectCommit('b', /* cmd click */ true);
45 CommitInfoTestUtils.clickToSelectCommit('c', /* cmd click */ true);
46 CommitInfoTestUtils.clickToSelectCommit('d', /* cmd click */ true);
47
48 expect(screen.getByText('Combine 3 commits')).toBeInTheDocument();
49 });
50
51 it('gaps prevents button', () => {
52 CommitInfoTestUtils.clickToSelectCommit('b', /* cmd click */ true);
53 CommitInfoTestUtils.clickToSelectCommit('d', /* cmd click */ true);
54
55 expect(screen.queryByText(/Combine \d+ commits/)).not.toBeInTheDocument();
56 });
57 });
58
59 it('allows cancelling', () => {
60 CommitInfoTestUtils.clickToSelectCommit('b', /* cmd click */ true);
61 CommitInfoTestUtils.clickToSelectCommit('c', /* cmd click */ true);
62
63 fireEvent.click(screen.getByText('Combine 2 commits'));
64 fireEvent.click(screen.getByText('Cancel'));
65
66 expectMessageNOTSentToServer({
67 type: 'runOperation',
68 operation: expect.anything(),
69 });
70 });
71
72 it('runs combine operation', () => {
73 CommitInfoTestUtils.clickToSelectCommit('b', /* cmd click */ true);
74 CommitInfoTestUtils.clickToSelectCommit('c', /* cmd click */ true);
75 CommitInfoTestUtils.clickToSelectCommit('d', /* cmd click */ true);
76
77 fireEvent.click(screen.getByText('Combine 3 commits'));
78 const runCombineButton = screen.getByText('Run Combine');
79 expect(runCombineButton).toBeInTheDocument();
80 fireEvent.click(runCombineButton);
81
82 expectMessageSentToServer({
83 type: 'runOperation',
84 operation: {
85 args: [
86 'fold',
87 '--exact',
88 {type: 'exact-revset', revset: 'b::d'},
89 '--message',
90 'Commit B, Commit C, Commit D\n',
91 ],
92 id: expect.anything(),
93 runner: CommandRunner.Sapling,
94 trackEventName: 'FoldOperation',
95 },
96 });
97 });
98
99 it('shows preview of combined message', () => {
100 act(() => openCommitInfoSidebar());
101
102 CommitInfoTestUtils.clickToSelectCommit('b', /* cmd click */ true);
103 CommitInfoTestUtils.clickToSelectCommit('c', /* cmd click */ true);
104 CommitInfoTestUtils.clickToSelectCommit('d', /* cmd click */ true);
105
106 fireEvent.click(CommitInfoTestUtils.withinCommitInfo().getByText('Combine 3 commits'));
107
108 expect(screen.getByText('Previewing result of combined commits')).toBeInTheDocument();
109
110 expect(
111 CommitInfoTestUtils.withinCommitInfo().getByText('Commit B, Commit C, Commit D'),
112 ).toBeInTheDocument();
113 });
114
115 it('allows editing combined message', () => {
116 act(() => openCommitInfoSidebar());
117
118 CommitInfoTestUtils.clickToSelectCommit('b', /* cmd click */ true);
119 CommitInfoTestUtils.clickToSelectCommit('c', /* cmd click */ true);
120 CommitInfoTestUtils.clickToSelectCommit('d', /* cmd click */ true);
121
122 fireEvent.click(CommitInfoTestUtils.withinCommitInfo().getByText('Combine 3 commits'));
123
124 CommitInfoTestUtils.clickToEditDescription();
125 act(() => {
126 userEvent.type(CommitInfoTestUtils.getDescriptionEditor(), 'new description');
127 });
128
129 fireEvent.click(CommitInfoTestUtils.withinCommitInfo().getByText('Run Combine'));
130
131 expectMessageSentToServer({
132 type: 'runOperation',
133 operation: expect.objectContaining({
134 args: [
135 'fold',
136 '--exact',
137 {type: 'exact-revset', revset: 'b::d'},
138 '--message',
139 expect.stringContaining('new description'),
140 ],
141 }),
142 });
143 });
144
145 describe('optimistic state', () => {
146 it('shows preview before running', () => {
147 CommitInfoTestUtils.clickToSelectCommit('b', /* cmd click */ true);
148 CommitInfoTestUtils.clickToSelectCommit('c', /* cmd click */ true);
149 CommitInfoTestUtils.clickToSelectCommit('d', /* cmd click */ true);
150
151 fireEvent.click(screen.getByText('Combine 3 commits'));
152
153 act(() => closeCommitInfoSidebar());
154
155 // combined commit is there
156 expect(screen.getByText('Commit B, Commit C, Commit D')).toBeInTheDocument();
157 // original commits are gone
158 expect(screen.queryByText('Commit B')).not.toBeInTheDocument();
159 expect(screen.queryByText('Commit C')).not.toBeInTheDocument();
160 expect(screen.queryByText('Commit D')).not.toBeInTheDocument();
161 // parent and children are still there
162 expect(screen.getByText('Commit A')).toBeInTheDocument();
163 expect(screen.getByText('Commit E')).toBeInTheDocument();
164 });
165
166 it('shows optimistic state when running', () => {
167 CommitInfoTestUtils.clickToSelectCommit('b', /* cmd click */ true);
168 CommitInfoTestUtils.clickToSelectCommit('c', /* cmd click */ true);
169 CommitInfoTestUtils.clickToSelectCommit('d', /* cmd click */ true);
170
171 fireEvent.click(screen.getByText('Combine 3 commits'));
172 fireEvent.click(screen.getByText('Run Combine'));
173
174 act(() => closeCommitInfoSidebar());
175
176 // combined commit is there
177 expect(screen.getByText('Commit B, Commit C, Commit D')).toBeInTheDocument();
178 // original commits are gone
179 expect(screen.queryByText('Commit B')).not.toBeInTheDocument();
180 expect(screen.queryByText('Commit C')).not.toBeInTheDocument();
181 expect(screen.queryByText('Commit D')).not.toBeInTheDocument();
182 // parent and children are still there
183 expect(screen.getByText('Commit A')).toBeInTheDocument();
184 expect(screen.getByText('Commit E')).toBeInTheDocument();
185 });
186 });
187});
188