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