addons/isl/src/__tests__/BrowseRepoUrl.test.tsxblame
View source
b69ab311/**
b69ab312 * Copyright (c) Meta Platforms, Inc. and affiliates.
b69ab313 *
b69ab314 * This source code is licensed under the MIT license found in the
b69ab315 * LICENSE file in the root directory of this source tree.
b69ab316 */
b69ab317
b69ab318import {act, fireEvent, render, screen, waitFor} from '@testing-library/react';
b69ab319import App from '../App';
b69ab3110import platform from '../platform';
b69ab3111import {ignoreRTL} from '../testQueries';
b69ab3112import {
b69ab3113 closeCommitInfoSidebar,
b69ab3114 COMMIT,
b69ab3115 expectMessageSentToServer,
b69ab3116 openCommitInfoSidebar,
b69ab3117 simulateCommits,
b69ab3118 simulateMessageFromServer,
b69ab3119 simulateRepoConnected,
b69ab3120 simulateUncommittedChangedFiles,
b69ab3121} from '../testUtils';
b69ab3122
b69ab3123describe('Browse repo url', () => {
b69ab3124 beforeEach(() => {
b69ab3125 render(<App />);
b69ab3126 act(() => {
b69ab3127 simulateRepoConnected();
b69ab3128 closeCommitInfoSidebar();
b69ab3129 expectMessageSentToServer({
b69ab3130 type: 'subscribe',
b69ab3131 kind: 'smartlogCommits',
b69ab3132 subscriptionID: expect.anything(),
b69ab3133 });
b69ab3134 simulateCommits({
b69ab3135 value: [
b69ab3136 COMMIT('1', 'some public base', '0', {phase: 'public', remoteBookmarks: ['main']}),
b69ab3137 COMMIT('a', 'My Commit', '1', {isDot: true}),
b69ab3138 ],
b69ab3139 });
b69ab3140 });
b69ab3141 });
b69ab3142
b69ab3143 function setCodeBrowserConfig(value: string | undefined) {
b69ab3144 expectMessageSentToServer({type: 'getConfig', name: 'fbcodereview.code-browser-url'});
b69ab3145 act(() => {
b69ab3146 simulateMessageFromServer({
b69ab3147 type: 'gotConfig',
b69ab3148 name: 'fbcodereview.code-browser-url',
b69ab3149 value,
b69ab3150 });
b69ab3151 });
b69ab3152 }
b69ab3153
b69ab3154 function clickBrowseRepo() {
b69ab3155 act(() => {
b69ab3156 fireEvent.contextMenu(screen.getByText('main'));
b69ab3157 });
b69ab3158 expect(screen.getByText('Browse Repo At This Commit')).toBeInTheDocument();
b69ab3159 fireEvent.click(screen.getByText('Browse Repo At This Commit'));
b69ab3160 }
b69ab3161
b69ab3162 it('opens link to browse repo', () => {
b69ab3163 setCodeBrowserConfig(undefined);
b69ab3164 act(() => {
b69ab3165 fireEvent.contextMenu(screen.getByText('main'));
b69ab3166 });
b69ab3167 expect(screen.queryByText('Browse Repo At This Commit')).not.toBeInTheDocument();
b69ab3168 });
b69ab3169
b69ab3170 it('opens link to browse repo', async () => {
b69ab3171 setCodeBrowserConfig('https://www.example.com/repo/browse/%s');
b69ab3172 clickBrowseRepo();
b69ab3173
b69ab3174 const openLinkSpy = jest.spyOn(platform, 'openExternalLink').mockImplementation(() => {});
b69ab3175 expectMessageSentToServer({
b69ab3176 type: 'getRepoUrlAtHash',
b69ab3177 revset: '1',
b69ab3178 path: undefined,
b69ab3179 });
b69ab3180 act(() => {
b69ab3181 simulateMessageFromServer({
b69ab3182 type: 'gotRepoUrlAtHash',
b69ab3183 url: {value: 'https://www.example.com/repo/browse/1/'},
b69ab3184 });
b69ab3185 });
b69ab3186
b69ab3187 await waitFor(() =>
b69ab3188 expect(openLinkSpy).toHaveBeenCalledWith('https://www.example.com/repo/browse/1/'),
b69ab3189 );
b69ab3190 });
b69ab3191
b69ab3192 it('surfaces errors', async () => {
b69ab3193 setCodeBrowserConfig('https://www.example.com/repo/browse/%s');
b69ab3194 clickBrowseRepo();
b69ab3195
b69ab3196 const openLinkSpy = jest.spyOn(platform, 'openExternalLink').mockImplementation(() => {});
b69ab3197 expectMessageSentToServer({
b69ab3198 type: 'getRepoUrlAtHash',
b69ab3199 revset: '1',
b69ab31100 path: undefined,
b69ab31101 });
b69ab31102 act(() => {
b69ab31103 simulateMessageFromServer({
b69ab31104 type: 'gotRepoUrlAtHash',
b69ab31105 url: {error: new Error('failed')},
b69ab31106 });
b69ab31107 });
b69ab31108
b69ab31109 await waitFor(() => {
b69ab31110 expect(openLinkSpy).not.toHaveBeenCalled();
b69ab31111 expect(screen.getByText('Failed to get repo URL to browse')).toBeInTheDocument();
b69ab31112 });
b69ab31113 });
b69ab31114
b69ab31115 describe('Copy file URL', () => {
b69ab31116 it('copies link to file repo', async () => {
b69ab31117 act(() => {
b69ab31118 simulateUncommittedChangedFiles({value: [{path: 'file1.txt', status: 'M'}]});
b69ab31119 });
b69ab31120 setCodeBrowserConfig('https://www.example.com/repo/browse/%s');
b69ab31121 act(() => {
b69ab31122 fireEvent.contextMenu(screen.getByText(ignoreRTL('file1.txt')));
b69ab31123 });
b69ab31124 expect(screen.getByText('Copy file URL')).toBeInTheDocument();
b69ab31125 fireEvent.click(screen.getByText('Copy file URL'));
b69ab31126
b69ab31127 expectMessageSentToServer({
b69ab31128 type: 'getRepoUrlAtHash',
b69ab31129 revset: '.',
b69ab31130 path: 'file1.txt',
b69ab31131 });
b69ab31132 act(() => {
b69ab31133 simulateMessageFromServer({
b69ab31134 type: 'gotRepoUrlAtHash',
b69ab31135 url: {value: 'https://www.example.com/repo/browse/a/file1.txt'},
b69ab31136 });
b69ab31137 });
b69ab31138
b69ab31139 await waitFor(() => {
b69ab31140 const copySpy = jest.spyOn(platform, 'clipboardCopy').mockImplementation(() => {});
b69ab31141 expect(copySpy).toHaveBeenCalledWith(
b69ab31142 'https://www.example.com/repo/browse/a/file1.txt',
b69ab31143 undefined,
b69ab31144 );
b69ab31145 expect(
b69ab31146 screen.getByText('Copied https://www.example.com/repo/browse/a/file1.txt'),
b69ab31147 ).toBeInTheDocument();
b69ab31148 });
b69ab31149 });
b69ab31150
b69ab31151 it('uses appropricate commit revset', () => {
b69ab31152 act(() => {
b69ab31153 openCommitInfoSidebar();
b69ab31154 });
b69ab31155 setCodeBrowserConfig('https://www.example.com/repo/browse/%s');
b69ab31156 act(() => {
b69ab31157 simulateCommits({
b69ab31158 value: [
b69ab31159 COMMIT('1', 'some public base', '0', {phase: 'public', remoteBookmarks: ['main']}),
b69ab31160 COMMIT('a', 'My Commit', '1', {
b69ab31161 isDot: true,
b69ab31162 totalFileCount: 1,
b69ab31163 filePathsSample: ['file2.txt'],
b69ab31164 }),
b69ab31165 ],
b69ab31166 });
b69ab31167 });
b69ab31168
b69ab31169 act(() => {
b69ab31170 fireEvent.contextMenu(screen.getByText(ignoreRTL('file2.txt')));
b69ab31171 });
b69ab31172 expect(screen.getByText('Copy file URL')).toBeInTheDocument();
b69ab31173 fireEvent.click(screen.getByText('Copy file URL'));
b69ab31174
b69ab31175 expectMessageSentToServer({
b69ab31176 type: 'getRepoUrlAtHash',
b69ab31177 revset: '.^',
b69ab31178 path: 'file2.txt',
b69ab31179 });
b69ab31180 });
b69ab31181 });
b69ab31182});