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