addons/isl/src/__tests__/cwd.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, within} from '@testing-library/react';
b69ab319import App from '../App';
b69ab3110import {__TEST__} from '../repositoryData';
b69ab3111import {CommitTreeListTestUtils} from '../testQueries';
b69ab3112import {
b69ab3113 closeCommitInfoSidebar,
b69ab3114 COMMIT,
b69ab3115 expectMessageSentToServer,
b69ab3116 resetTestMessages,
b69ab3117 simulateCommits,
b69ab3118 simulateMessageFromServer,
b69ab3119 simulateRepoConnected,
b69ab3120} from '../testUtils';
b69ab3121const {isIrrelevantToCwd} = __TEST__;
b69ab3122
b69ab3123const {clickGoto} = CommitTreeListTestUtils;
b69ab3124
b69ab3125describe('cwd', () => {
b69ab3126 beforeEach(() => {
b69ab3127 render(<App />);
b69ab3128 act(() => {
b69ab3129 simulateRepoConnected();
b69ab3130 closeCommitInfoSidebar();
b69ab3131 expectMessageSentToServer({
b69ab3132 type: 'subscribe',
b69ab3133 kind: 'smartlogCommits',
b69ab3134 subscriptionID: expect.anything(),
b69ab3135 });
b69ab3136 simulateCommits({
b69ab3137 value: [
b69ab3138 COMMIT('1', 'some public base', '0', {phase: 'public'}),
b69ab3139 COMMIT('a', 'My Commit', '1'),
b69ab3140 COMMIT('b', 'Another Commit', 'a', {isDot: true}),
b69ab3141 ],
b69ab3142 });
b69ab3143 });
b69ab3144 });
b69ab3145
b69ab3146 const openCwdDropdown = () => {
b69ab3147 const cwdDropdown = screen.getByTestId('cwd-dropdown-button');
b69ab3148 act(() => {
b69ab3149 fireEvent.click(cwdDropdown);
b69ab3150 });
b69ab3151 expectMessageSentToServer({
b69ab3152 type: 'platform/subscribeToAvailableCwds',
b69ab3153 });
b69ab3154 act(() => {
b69ab3155 simulateMessageFromServer({
b69ab3156 type: 'platform/availableCwds',
b69ab3157 options: [
b69ab3158 {
b69ab3159 cwd: '/path/to/repo1',
b69ab3160 repoRoot: '/path/to/repo1',
b69ab3161 repoRelativeCwdLabel: 'repo1',
b69ab3162 },
b69ab3163 {
b69ab3164 cwd: '/path/to/repo2',
b69ab3165 repoRoot: '/path/to/repo2',
b69ab3166 repoRelativeCwdLabel: 'repo2',
b69ab3167 },
b69ab3168 {
b69ab3169 cwd: '/path/to/repo2/some/subdir',
b69ab3170 repoRoot: '/path/to/repo2',
b69ab3171 repoRelativeCwdLabel: 'repo2/some/subdir',
b69ab3172 },
b69ab3173 ],
b69ab3174 });
b69ab3175 });
b69ab3176 };
b69ab3177
b69ab3178 it('shows repo+relative cwd in the cwd button', () => {
b69ab3179 act(() => {
b69ab3180 simulateRepoConnected('/path/to/repo', '/path/to/repo/some/subdir');
b69ab3181 });
b69ab3182 expect(screen.getByText('repo/some/subdir')).toBeInTheDocument();
b69ab3183
b69ab3184 act(() => {
b69ab3185 simulateRepoConnected('C:\\path\\to\\repo', 'C:\\path\\to\\repo\\some\\subdir');
b69ab3186 });
b69ab3187 expect(screen.getByText('repo\\some\\subdir')).toBeInTheDocument();
b69ab3188 });
b69ab3189
b69ab3190 it('shows cwd options from the platform', () => {
b69ab3191 openCwdDropdown();
b69ab3192
b69ab3193 const dropdown = screen.getByTestId('cwd-details-dropdown');
b69ab3194
b69ab3195 expect(within(dropdown).getByText('repo1')).toBeInTheDocument();
b69ab3196 expect(within(dropdown).getByText('repo2')).toBeInTheDocument();
b69ab3197 });
b69ab3198
b69ab3199 it('shows cwd options from the platform with repo relative cwd paths', () => {
b69ab31100 openCwdDropdown();
b69ab31101
b69ab31102 const dropdown = screen.getByTestId('cwd-details-dropdown');
b69ab31103
b69ab31104 expect(within(dropdown).getByText('repo1')).toBeInTheDocument();
b69ab31105 expect(within(dropdown).getByText('repo2')).toBeInTheDocument();
b69ab31106 expect(within(dropdown).getByText('repo2/some/subdir')).toBeInTheDocument();
b69ab31107 });
b69ab31108
b69ab31109 it('requests new data for subscriptions after changing cwd', () => {
b69ab31110 openCwdDropdown();
b69ab31111
b69ab31112 resetTestMessages();
b69ab31113
b69ab31114 const dropdown = screen.getByTestId('cwd-details-dropdown');
b69ab31115 act(() => {
b69ab31116 fireEvent.click(within(dropdown).getByText('repo2'));
b69ab31117 });
b69ab31118
b69ab31119 expectMessageSentToServer({type: 'changeCwd', cwd: '/path/to/repo2'});
b69ab31120 expectMessageSentToServer({type: 'requestRepoInfo'});
b69ab31121 expectMessageSentToServer({
b69ab31122 type: 'subscribe',
b69ab31123 kind: 'smartlogCommits',
b69ab31124 subscriptionID: expect.anything(),
b69ab31125 });
b69ab31126 expectMessageSentToServer({
b69ab31127 type: 'subscribe',
b69ab31128 kind: 'uncommittedChanges',
b69ab31129 subscriptionID: expect.anything(),
b69ab31130 });
b69ab31131 });
b69ab31132
b69ab31133 it('clears out saved state when changing repos', async () => {
b69ab31134 await clickGoto('a');
b69ab31135
b69ab31136 expect(screen.getByText('sl goto --rev a')).toBeInTheDocument();
b69ab31137
b69ab31138 openCwdDropdown();
b69ab31139
b69ab31140 resetTestMessages();
b69ab31141
b69ab31142 const dropdown = screen.getByTestId('cwd-details-dropdown');
b69ab31143 act(() => {
b69ab31144 fireEvent.click(within(dropdown).getByText('repo2'));
b69ab31145 simulateRepoConnected('/path/to/repo2');
b69ab31146 });
b69ab31147
b69ab31148 expect(screen.queryByText('sl goto --rev a')).not.toBeInTheDocument();
b69ab31149 });
b69ab31150
b69ab31151 it('dismisses dropdown when changing cwd', () => {
b69ab31152 openCwdDropdown();
b69ab31153
b69ab31154 const dropdown = screen.getByTestId('cwd-details-dropdown');
b69ab31155 act(() => {
b69ab31156 fireEvent.click(within(dropdown).getByText('repo2'));
b69ab31157 });
b69ab31158
b69ab31159 expect(screen.queryByTestId('cwd-details-dropdown')).not.toBeInTheDocument();
b69ab31160 });
b69ab31161});
b69ab31162
b69ab31163describe('isIrrelevantToCwd', () => {
b69ab31164 const C = (maxPrefix: string) => COMMIT('1', 'title', '0', {maxCommonPathPrefix: maxPrefix});
b69ab31165 it('handles files outside cwd', () => {
b69ab31166 expect(isIrrelevantToCwd(C('www/'), 'fbcode/')).toBe(true);
b69ab31167 expect(isIrrelevantToCwd(C('www/subdir/'), 'fbcode/')).toBe(true);
b69ab31168 });
b69ab31169 it('handles files inside cwd', () => {
b69ab31170 expect(isIrrelevantToCwd(C('www/'), 'www/')).toBe(false);
b69ab31171 expect(isIrrelevantToCwd(C('www/subdir/'), 'www/')).toBe(false);
b69ab31172 });
b69ab31173 it('handles files above cwd', () => {
b69ab31174 expect(isIrrelevantToCwd(C('addons/isl/'), 'addons/isl')).toBe(false);
b69ab31175 expect(isIrrelevantToCwd(C('addons/'), 'addons/isl')).toBe(false);
b69ab31176 expect(isIrrelevantToCwd(C(''), 'addons/isl')).toBe(false);
b69ab31177 });
b69ab31178 it('handles cwd is root', () => {
b69ab31179 expect(isIrrelevantToCwd(C('addons/isl/'), '')).toBe(false);
b69ab31180 expect(isIrrelevantToCwd(C('addons/'), '')).toBe(false);
b69ab31181 expect(isIrrelevantToCwd(C(''), '')).toBe(false);
b69ab31182 expect(isIrrelevantToCwd(C('addons/isl/'), '/')).toBe(false);
b69ab31183 expect(isIrrelevantToCwd(C(''), '/')).toBe(false);
b69ab31184 expect(isIrrelevantToCwd(C('/'), '/')).toBe(false);
b69ab31185 });
b69ab31186});