| 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 App from '../App'; |
| b69ab31 | | | 10 | import platform from '../platform'; |
| b69ab31 | | | 11 | import {ignoreRTL} from '../testQueries'; |
| b69ab31 | | | 12 | import { |
| b69ab31 | | | 13 | closeCommitInfoSidebar, |
| b69ab31 | | | 14 | COMMIT, |
| b69ab31 | | | 15 | expectMessageSentToServer, |
| b69ab31 | | | 16 | openCommitInfoSidebar, |
| b69ab31 | | | 17 | simulateCommits, |
| b69ab31 | | | 18 | simulateMessageFromServer, |
| b69ab31 | | | 19 | simulateRepoConnected, |
| b69ab31 | | | 20 | simulateUncommittedChangedFiles, |
| b69ab31 | | | 21 | } from '../testUtils'; |
| b69ab31 | | | 22 | |
| b69ab31 | | | 23 | describe('Browse repo url', () => { |
| b69ab31 | | | 24 | beforeEach(() => { |
| b69ab31 | | | 25 | render(<App />); |
| b69ab31 | | | 26 | act(() => { |
| b69ab31 | | | 27 | simulateRepoConnected(); |
| b69ab31 | | | 28 | closeCommitInfoSidebar(); |
| b69ab31 | | | 29 | expectMessageSentToServer({ |
| b69ab31 | | | 30 | type: 'subscribe', |
| b69ab31 | | | 31 | kind: 'smartlogCommits', |
| b69ab31 | | | 32 | subscriptionID: expect.anything(), |
| b69ab31 | | | 33 | }); |
| b69ab31 | | | 34 | simulateCommits({ |
| b69ab31 | | | 35 | value: [ |
| b69ab31 | | | 36 | COMMIT('1', 'some public base', '0', {phase: 'public', remoteBookmarks: ['main']}), |
| b69ab31 | | | 37 | COMMIT('a', 'My Commit', '1', {isDot: true}), |
| b69ab31 | | | 38 | ], |
| b69ab31 | | | 39 | }); |
| b69ab31 | | | 40 | }); |
| b69ab31 | | | 41 | }); |
| b69ab31 | | | 42 | |
| b69ab31 | | | 43 | function setCodeBrowserConfig(value: string | undefined) { |
| b69ab31 | | | 44 | expectMessageSentToServer({type: 'getConfig', name: 'fbcodereview.code-browser-url'}); |
| b69ab31 | | | 45 | act(() => { |
| b69ab31 | | | 46 | simulateMessageFromServer({ |
| b69ab31 | | | 47 | type: 'gotConfig', |
| b69ab31 | | | 48 | name: 'fbcodereview.code-browser-url', |
| b69ab31 | | | 49 | value, |
| b69ab31 | | | 50 | }); |
| b69ab31 | | | 51 | }); |
| b69ab31 | | | 52 | } |
| b69ab31 | | | 53 | |
| b69ab31 | | | 54 | function clickBrowseRepo() { |
| b69ab31 | | | 55 | act(() => { |
| b69ab31 | | | 56 | fireEvent.contextMenu(screen.getByText('main')); |
| b69ab31 | | | 57 | }); |
| b69ab31 | | | 58 | expect(screen.getByText('Browse Repo At This Commit')).toBeInTheDocument(); |
| b69ab31 | | | 59 | fireEvent.click(screen.getByText('Browse Repo At This Commit')); |
| b69ab31 | | | 60 | } |
| b69ab31 | | | 61 | |
| b69ab31 | | | 62 | it('opens link to browse repo', () => { |
| b69ab31 | | | 63 | setCodeBrowserConfig(undefined); |
| b69ab31 | | | 64 | act(() => { |
| b69ab31 | | | 65 | fireEvent.contextMenu(screen.getByText('main')); |
| b69ab31 | | | 66 | }); |
| b69ab31 | | | 67 | expect(screen.queryByText('Browse Repo At This Commit')).not.toBeInTheDocument(); |
| b69ab31 | | | 68 | }); |
| b69ab31 | | | 69 | |
| b69ab31 | | | 70 | it('opens link to browse repo', async () => { |
| b69ab31 | | | 71 | setCodeBrowserConfig('https://www.example.com/repo/browse/%s'); |
| b69ab31 | | | 72 | clickBrowseRepo(); |
| b69ab31 | | | 73 | |
| b69ab31 | | | 74 | const openLinkSpy = jest.spyOn(platform, 'openExternalLink').mockImplementation(() => {}); |
| b69ab31 | | | 75 | expectMessageSentToServer({ |
| b69ab31 | | | 76 | type: 'getRepoUrlAtHash', |
| b69ab31 | | | 77 | revset: '1', |
| b69ab31 | | | 78 | path: undefined, |
| b69ab31 | | | 79 | }); |
| b69ab31 | | | 80 | act(() => { |
| b69ab31 | | | 81 | simulateMessageFromServer({ |
| b69ab31 | | | 82 | type: 'gotRepoUrlAtHash', |
| b69ab31 | | | 83 | url: {value: 'https://www.example.com/repo/browse/1/'}, |
| b69ab31 | | | 84 | }); |
| b69ab31 | | | 85 | }); |
| b69ab31 | | | 86 | |
| b69ab31 | | | 87 | await waitFor(() => |
| b69ab31 | | | 88 | expect(openLinkSpy).toHaveBeenCalledWith('https://www.example.com/repo/browse/1/'), |
| b69ab31 | | | 89 | ); |
| b69ab31 | | | 90 | }); |
| b69ab31 | | | 91 | |
| b69ab31 | | | 92 | it('surfaces errors', async () => { |
| b69ab31 | | | 93 | setCodeBrowserConfig('https://www.example.com/repo/browse/%s'); |
| b69ab31 | | | 94 | clickBrowseRepo(); |
| b69ab31 | | | 95 | |
| b69ab31 | | | 96 | const openLinkSpy = jest.spyOn(platform, 'openExternalLink').mockImplementation(() => {}); |
| b69ab31 | | | 97 | expectMessageSentToServer({ |
| b69ab31 | | | 98 | type: 'getRepoUrlAtHash', |
| b69ab31 | | | 99 | revset: '1', |
| b69ab31 | | | 100 | path: undefined, |
| b69ab31 | | | 101 | }); |
| b69ab31 | | | 102 | act(() => { |
| b69ab31 | | | 103 | simulateMessageFromServer({ |
| b69ab31 | | | 104 | type: 'gotRepoUrlAtHash', |
| b69ab31 | | | 105 | url: {error: new Error('failed')}, |
| b69ab31 | | | 106 | }); |
| b69ab31 | | | 107 | }); |
| b69ab31 | | | 108 | |
| b69ab31 | | | 109 | await waitFor(() => { |
| b69ab31 | | | 110 | expect(openLinkSpy).not.toHaveBeenCalled(); |
| b69ab31 | | | 111 | expect(screen.getByText('Failed to get repo URL to browse')).toBeInTheDocument(); |
| b69ab31 | | | 112 | }); |
| b69ab31 | | | 113 | }); |
| b69ab31 | | | 114 | |
| b69ab31 | | | 115 | describe('Copy file URL', () => { |
| b69ab31 | | | 116 | it('copies link to file repo', async () => { |
| b69ab31 | | | 117 | act(() => { |
| b69ab31 | | | 118 | simulateUncommittedChangedFiles({value: [{path: 'file1.txt', status: 'M'}]}); |
| b69ab31 | | | 119 | }); |
| b69ab31 | | | 120 | setCodeBrowserConfig('https://www.example.com/repo/browse/%s'); |
| b69ab31 | | | 121 | act(() => { |
| b69ab31 | | | 122 | fireEvent.contextMenu(screen.getByText(ignoreRTL('file1.txt'))); |
| b69ab31 | | | 123 | }); |
| b69ab31 | | | 124 | expect(screen.getByText('Copy file URL')).toBeInTheDocument(); |
| b69ab31 | | | 125 | fireEvent.click(screen.getByText('Copy file URL')); |
| b69ab31 | | | 126 | |
| b69ab31 | | | 127 | expectMessageSentToServer({ |
| b69ab31 | | | 128 | type: 'getRepoUrlAtHash', |
| b69ab31 | | | 129 | revset: '.', |
| b69ab31 | | | 130 | path: 'file1.txt', |
| b69ab31 | | | 131 | }); |
| b69ab31 | | | 132 | act(() => { |
| b69ab31 | | | 133 | simulateMessageFromServer({ |
| b69ab31 | | | 134 | type: 'gotRepoUrlAtHash', |
| b69ab31 | | | 135 | url: {value: 'https://www.example.com/repo/browse/a/file1.txt'}, |
| b69ab31 | | | 136 | }); |
| b69ab31 | | | 137 | }); |
| b69ab31 | | | 138 | |
| b69ab31 | | | 139 | await waitFor(() => { |
| b69ab31 | | | 140 | const copySpy = jest.spyOn(platform, 'clipboardCopy').mockImplementation(() => {}); |
| b69ab31 | | | 141 | expect(copySpy).toHaveBeenCalledWith( |
| b69ab31 | | | 142 | 'https://www.example.com/repo/browse/a/file1.txt', |
| b69ab31 | | | 143 | undefined, |
| b69ab31 | | | 144 | ); |
| b69ab31 | | | 145 | expect( |
| b69ab31 | | | 146 | screen.getByText('Copied https://www.example.com/repo/browse/a/file1.txt'), |
| b69ab31 | | | 147 | ).toBeInTheDocument(); |
| b69ab31 | | | 148 | }); |
| b69ab31 | | | 149 | }); |
| b69ab31 | | | 150 | |
| b69ab31 | | | 151 | it('uses appropricate commit revset', () => { |
| b69ab31 | | | 152 | act(() => { |
| b69ab31 | | | 153 | openCommitInfoSidebar(); |
| b69ab31 | | | 154 | }); |
| b69ab31 | | | 155 | setCodeBrowserConfig('https://www.example.com/repo/browse/%s'); |
| b69ab31 | | | 156 | act(() => { |
| b69ab31 | | | 157 | simulateCommits({ |
| b69ab31 | | | 158 | value: [ |
| b69ab31 | | | 159 | COMMIT('1', 'some public base', '0', {phase: 'public', remoteBookmarks: ['main']}), |
| b69ab31 | | | 160 | COMMIT('a', 'My Commit', '1', { |
| b69ab31 | | | 161 | isDot: true, |
| b69ab31 | | | 162 | totalFileCount: 1, |
| b69ab31 | | | 163 | filePathsSample: ['file2.txt'], |
| b69ab31 | | | 164 | }), |
| b69ab31 | | | 165 | ], |
| b69ab31 | | | 166 | }); |
| b69ab31 | | | 167 | }); |
| b69ab31 | | | 168 | |
| b69ab31 | | | 169 | act(() => { |
| b69ab31 | | | 170 | fireEvent.contextMenu(screen.getByText(ignoreRTL('file2.txt'))); |
| b69ab31 | | | 171 | }); |
| b69ab31 | | | 172 | expect(screen.getByText('Copy file URL')).toBeInTheDocument(); |
| b69ab31 | | | 173 | fireEvent.click(screen.getByText('Copy file URL')); |
| b69ab31 | | | 174 | |
| b69ab31 | | | 175 | expectMessageSentToServer({ |
| b69ab31 | | | 176 | type: 'getRepoUrlAtHash', |
| b69ab31 | | | 177 | revset: '.^', |
| b69ab31 | | | 178 | path: 'file2.txt', |
| b69ab31 | | | 179 | }); |
| b69ab31 | | | 180 | }); |
| b69ab31 | | | 181 | }); |
| b69ab31 | | | 182 | }); |