| 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} from '@testing-library/react'; |
| 9 | import {nextTick} from 'shared/testUtils'; |
| 10 | import App from '../App'; |
| 11 | import { |
| 12 | closeCommitInfoSidebar, |
| 13 | COMMIT, |
| 14 | expectMessageNOTSentToServer, |
| 15 | expectMessageSentToServer, |
| 16 | resetTestMessages, |
| 17 | simulateCommits, |
| 18 | simulateMessageFromServer, |
| 19 | simulateRepoConnected, |
| 20 | simulateUncommittedChangedFiles, |
| 21 | } from '../testUtils'; |
| 22 | |
| 23 | describe('UnsavedFiles', () => { |
| 24 | beforeEach(() => { |
| 25 | resetTestMessages(); |
| 26 | render(<App />); |
| 27 | act(() => { |
| 28 | simulateRepoConnected(); |
| 29 | closeCommitInfoSidebar(); |
| 30 | expectMessageSentToServer({ |
| 31 | type: 'subscribe', |
| 32 | kind: 'smartlogCommits', |
| 33 | subscriptionID: expect.anything(), |
| 34 | }); |
| 35 | simulateCommits({ |
| 36 | value: [ |
| 37 | COMMIT('1', 'some public base', '0', {phase: 'public'}), |
| 38 | COMMIT('a', 'My Commit', '1'), |
| 39 | COMMIT('b', 'Another Commit', 'a', {isDot: true}), |
| 40 | ], |
| 41 | }); |
| 42 | simulateUncommittedChangedFiles({value: [{path: 'foo.txt', status: 'M'}]}); |
| 43 | }); |
| 44 | }); |
| 45 | |
| 46 | it('subscribes to changes to unsaved files', () => { |
| 47 | expectMessageSentToServer({ |
| 48 | type: 'platform/subscribeToUnsavedFiles', |
| 49 | }); |
| 50 | }); |
| 51 | |
| 52 | it('shows badge with unsaved files', () => { |
| 53 | act(() => |
| 54 | simulateMessageFromServer({ |
| 55 | type: 'platform/unsavedFiles', |
| 56 | unsaved: [{path: 'foo.txt', uri: 'file:///foo.txt'}], |
| 57 | }), |
| 58 | ); |
| 59 | |
| 60 | expect(screen.getByText('1 unsaved file')).toBeInTheDocument(); |
| 61 | |
| 62 | act(() => |
| 63 | simulateMessageFromServer({ |
| 64 | type: 'platform/unsavedFiles', |
| 65 | unsaved: [ |
| 66 | {path: 'foo.txt', uri: 'file:///foo.txt'}, |
| 67 | {path: 'bar.txt', uri: 'file:///bar.txt'}, |
| 68 | ], |
| 69 | }), |
| 70 | ); |
| 71 | |
| 72 | expect(screen.getByText('2 unsaved files')).toBeInTheDocument(); |
| 73 | }); |
| 74 | |
| 75 | describe('confirms before commit/amend', () => { |
| 76 | beforeEach(() => { |
| 77 | act(() => |
| 78 | simulateMessageFromServer({ |
| 79 | type: 'platform/unsavedFiles', |
| 80 | unsaved: [{path: 'foo.txt', uri: 'file:///foo.txt'}], |
| 81 | }), |
| 82 | ); |
| 83 | }); |
| 84 | |
| 85 | it('allows saving all files', async () => { |
| 86 | fireEvent.click(screen.getByText('Commit')); |
| 87 | expect(screen.getByText('You have 1 unsaved file')).toBeInTheDocument(); |
| 88 | fireEvent.click(screen.getByText('Save All and Continue')); |
| 89 | await waitFor(() => { |
| 90 | expectMessageSentToServer({ |
| 91 | type: 'platform/saveAllUnsavedFiles', |
| 92 | }); |
| 93 | }); |
| 94 | act(() => { |
| 95 | simulateMessageFromServer({ |
| 96 | type: 'platform/savedAllUnsavedFiles', |
| 97 | success: true, |
| 98 | }); |
| 99 | }); |
| 100 | await waitFor(() => |
| 101 | expectMessageSentToServer({ |
| 102 | type: 'runOperation', |
| 103 | operation: expect.objectContaining({ |
| 104 | args: expect.arrayContaining(['commit']), |
| 105 | }), |
| 106 | }), |
| 107 | ); |
| 108 | }); |
| 109 | |
| 110 | it('allows continuing without saving', async () => { |
| 111 | fireEvent.click(screen.getByText('Commit')); |
| 112 | expect(screen.getByText('You have 1 unsaved file')).toBeInTheDocument(); |
| 113 | fireEvent.click(screen.getByText('Continue Without Saving')); |
| 114 | expectMessageNOTSentToServer({ |
| 115 | type: 'platform/saveAllUnsavedFiles', |
| 116 | }); |
| 117 | await waitFor(() => |
| 118 | expectMessageSentToServer({ |
| 119 | type: 'runOperation', |
| 120 | operation: expect.objectContaining({ |
| 121 | args: expect.arrayContaining(['commit']), |
| 122 | }), |
| 123 | }), |
| 124 | ); |
| 125 | }); |
| 126 | |
| 127 | it('allows cancelling commit', async () => { |
| 128 | fireEvent.click(screen.getByText('Commit')); |
| 129 | expect(screen.getByText('You have 1 unsaved file')).toBeInTheDocument(); |
| 130 | fireEvent.click(screen.getByText('Cancel')); |
| 131 | await nextTick(); |
| 132 | expectMessageNOTSentToServer({ |
| 133 | type: 'platform/saveAllUnsavedFiles', |
| 134 | }); |
| 135 | expectMessageNOTSentToServer({ |
| 136 | type: 'runOperation', |
| 137 | operation: expect.anything(), |
| 138 | }); |
| 139 | }); |
| 140 | }); |
| 141 | }); |
| 142 | |