| 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 | |
| 8 | import {act, fireEvent, render, screen, waitFor, within} from '@testing-library/react'; |
| 9 | import userEvent from '@testing-library/user-event'; |
| 10 | import App from '../App'; |
| 11 | import {CommitInfoTestUtils} from '../testQueries'; |
| 12 | import { |
| 13 | closeCommitInfoSidebar, |
| 14 | COMMIT, |
| 15 | expectMessageSentToServer, |
| 16 | resetTestMessages, |
| 17 | simulateCommits, |
| 18 | simulateUncommittedChangedFiles, |
| 19 | } from '../testUtils'; |
| 20 | |
| 21 | describe('Optimistic Revset', () => { |
| 22 | beforeEach(() => { |
| 23 | resetTestMessages(); |
| 24 | render(<App />); |
| 25 | act(() => { |
| 26 | expectMessageSentToServer({ |
| 27 | type: 'subscribe', |
| 28 | kind: 'smartlogCommits', |
| 29 | subscriptionID: expect.anything(), |
| 30 | }); |
| 31 | simulateUncommittedChangedFiles({ |
| 32 | value: [ |
| 33 | {path: 'file1.txt', status: 'M'}, |
| 34 | {path: 'file2.txt', status: 'A'}, |
| 35 | {path: 'file3.txt', status: 'R'}, |
| 36 | ], |
| 37 | }); |
| 38 | simulateCommits({ |
| 39 | value: [ |
| 40 | COMMIT('2', 'master', '00', {phase: 'public', remoteBookmarks: ['remote/master']}), |
| 41 | COMMIT('1', 'Commit 1', '0', {phase: 'public'}), |
| 42 | COMMIT('a', 'Commit A', '1'), |
| 43 | COMMIT('b', 'Commit B', 'a', {isDot: true}), |
| 44 | ], |
| 45 | }); |
| 46 | }); |
| 47 | }); |
| 48 | |
| 49 | const clickQuickCommit = async () => { |
| 50 | const quickCommitButton = screen.getByTestId('quick-commit-button'); |
| 51 | act(() => { |
| 52 | fireEvent.click(quickCommitButton); |
| 53 | }); |
| 54 | await waitFor(() => |
| 55 | expectMessageSentToServer({ |
| 56 | type: 'runOperation', |
| 57 | operation: expect.objectContaining({ |
| 58 | args: expect.arrayContaining(['commit']), |
| 59 | }), |
| 60 | }), |
| 61 | ); |
| 62 | }; |
| 63 | |
| 64 | function rightClickAndChooseFromContextMenu(element: Element, choiceMatcher: string) { |
| 65 | act(() => { |
| 66 | fireEvent.contextMenu(element); |
| 67 | }); |
| 68 | const choice = within(screen.getByTestId('context-menu-container')).getByText(choiceMatcher); |
| 69 | expect(choice).not.toEqual(null); |
| 70 | act(() => { |
| 71 | fireEvent.click(choice); |
| 72 | }); |
| 73 | } |
| 74 | |
| 75 | it('after commit, uses revset to act on optimistic commit', async () => { |
| 76 | act(() => { |
| 77 | CommitInfoTestUtils.clickAmendMode(); |
| 78 | closeCommitInfoSidebar(); |
| 79 | }); |
| 80 | |
| 81 | const mockDate = new Date('2024-01-01T00:00:00.000Z'); |
| 82 | jest.spyOn(Date, 'now').mockImplementation(() => { |
| 83 | return mockDate.valueOf(); |
| 84 | }); |
| 85 | |
| 86 | const quickInput = screen.getByTestId('quick-commit-title'); |
| 87 | act(() => { |
| 88 | userEvent.type(quickInput, 'My Commit'); |
| 89 | }); |
| 90 | await clickQuickCommit(); |
| 91 | expectMessageSentToServer({ |
| 92 | type: 'runOperation', |
| 93 | operation: expect.objectContaining({ |
| 94 | args: ['commit', '--addremove', '--message', 'My Commit'], |
| 95 | }), |
| 96 | }); |
| 97 | |
| 98 | await waitFor(() => { |
| 99 | expect(screen.getByText('My Commit')).toBeInTheDocument(); |
| 100 | }); |
| 101 | |
| 102 | rightClickAndChooseFromContextMenu(screen.getByText('My Commit'), 'Hide Commit'); |
| 103 | fireEvent.click(screen.getByText('Hide')); |
| 104 | |
| 105 | expectMessageSentToServer({ |
| 106 | type: 'runOperation', |
| 107 | operation: expect.objectContaining({ |
| 108 | args: [ |
| 109 | 'hide', |
| 110 | '--rev', |
| 111 | { |
| 112 | type: 'optimistic-revset', |
| 113 | revset: 'first(sort((children(b)-b) & date(">Mon, 01 Jan 2024 00:00:00 GMT"),date))', |
| 114 | fake: 'OPTIMISTIC_COMMIT_b', |
| 115 | }, |
| 116 | ], |
| 117 | }), |
| 118 | }); |
| 119 | }); |
| 120 | }); |
| 121 | |