addons/isl/src/testQueries.tsblame
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 type {Hash} from './types';
b69ab319
b69ab3110import {act, fireEvent, screen, waitFor, within} from '@testing-library/react';
b69ab3111import {nullthrows} from 'shared/utils';
b69ab3112import {commitMessageFieldsSchema} from './CommitInfoView/CommitMessageFields';
b69ab3113import {OSSCommitMessageFieldSchema} from './CommitInfoView/OSSCommitMessageFieldsSchema';
b69ab3114import {convertFieldNameToKey} from './CommitInfoView/utils';
b69ab3115import {readAtom} from './jotaiUtils';
b69ab3116import {individualToggleKey} from './selection';
b69ab3117import {expectMessageSentToServer} from './testUtils';
b69ab3118import {assert} from './utils';
b69ab3119
b69ab3120export const CommitTreeListTestUtils = {
b69ab3121 withinCommitTree() {
b69ab3122 return within(screen.getByTestId('commit-tree-root'));
b69ab3123 },
b69ab3124
b69ab3125 async clickGoto(commit: Hash) {
b69ab3126 const myCommit = screen.queryByTestId(`commit-${commit}`);
b69ab3127 const gotoButton = myCommit?.querySelector('.goto-button button');
b69ab3128 expect(gotoButton).toBeDefined();
b69ab3129 await act(async () => {
b69ab3130 await fireEvent.click(gotoButton as Element);
b69ab3131 });
b69ab3132 },
b69ab3133};
b69ab3134
b69ab3135export const CommitInfoTestUtils = {
b69ab3136 withinCommitInfo() {
b69ab3137 return within(screen.getByTestId('commit-info-view'));
b69ab3138 },
b69ab3139
b69ab3140 withinCommitActionBar() {
b69ab3141 return within(screen.getByTestId('commit-info-actions-bar'));
b69ab3142 },
b69ab3143
b69ab3144 openCommitInfoSidebar() {
b69ab3145 screen.queryAllByTestId('drawer-label').forEach(el => {
b69ab3146 const commitInfoTab = within(el).queryByText('Commit Info');
b69ab3147 commitInfoTab?.click();
b69ab3148 });
b69ab3149 },
b69ab3150
b69ab3151 clickToSelectCommit(hash: string, cmdClick?: boolean) {
b69ab3152 const commit = within(screen.getByTestId(`commit-${hash}`)).queryByTestId('draggable-commit');
b69ab3153 expect(commit).toBeInTheDocument();
b69ab3154 act(() => {
b69ab3155 fireEvent.click(nullthrows(commit), {[individualToggleKey]: cmdClick === true});
b69ab3156 });
b69ab3157 },
b69ab3158
b69ab3159 clickCommitMode() {
b69ab3160 const commitRadioChoice = within(screen.getByTestId('commit-info-toolbar-top')).getByText(
b69ab3161 'Commit',
b69ab3162 );
b69ab3163 act(() => {
b69ab3164 fireEvent.click(commitRadioChoice);
b69ab3165 });
b69ab3166 },
b69ab3167
b69ab3168 clickAmendMode() {
b69ab3169 const commitRadioChoice = within(screen.getByTestId('commit-info-toolbar-top')).getByText(
b69ab3170 'Amend',
b69ab3171 );
b69ab3172 act(() => {
b69ab3173 fireEvent.click(commitRadioChoice);
b69ab3174 });
b69ab3175 },
b69ab3176
b69ab3177 async clickAmendButton() {
b69ab3178 const amendButton: HTMLButtonElement | null = within(
b69ab3179 screen.getByTestId('commit-info-actions-bar'),
b69ab3180 ).queryByText('Amend');
b69ab3181 expect(amendButton).toBeInTheDocument();
b69ab3182 act(() => {
b69ab3183 fireEvent.click(nullthrows(amendButton));
b69ab3184 });
b69ab3185 await waitFor(() =>
b69ab3186 expectMessageSentToServer({
b69ab3187 type: 'runOperation',
b69ab3188 operation: expect.objectContaining({
b69ab3189 args: expect.arrayContaining(['amend']),
b69ab3190 }),
b69ab3191 }),
b69ab3192 );
b69ab3193 },
b69ab3194
b69ab3195 clickAmendMessageButton() {
b69ab3196 const amendMessageButton: HTMLButtonElement | null = within(
b69ab3197 screen.getByTestId('commit-info-actions-bar'),
b69ab3198 ).queryByText('Amend Message');
b69ab3199 expect(amendMessageButton).toBeInTheDocument();
b69ab31100 act(() => {
b69ab31101 fireEvent.click(nullthrows(amendMessageButton));
b69ab31102 });
b69ab31103 },
b69ab31104
b69ab31105 async clickCommitButton() {
b69ab31106 const commitButton: HTMLButtonElement | null = within(
b69ab31107 screen.getByTestId('commit-info-actions-bar'),
b69ab31108 ).queryByText('Commit');
b69ab31109 expect(commitButton).toBeInTheDocument();
b69ab31110 act(() => {
b69ab31111 fireEvent.click(nullthrows(commitButton));
b69ab31112 });
b69ab31113 await waitFor(() =>
b69ab31114 expectMessageSentToServer({
b69ab31115 type: 'runOperation',
b69ab31116 operation: expect.objectContaining({
b69ab31117 args: expect.arrayContaining(['commit']),
b69ab31118 }),
b69ab31119 }),
b69ab31120 );
b69ab31121 },
b69ab31122
b69ab31123 clickCancel() {
b69ab31124 const cancelButton: HTMLButtonElement | null =
b69ab31125 CommitInfoTestUtils.withinCommitInfo().queryByText('Cancel');
b69ab31126 expect(cancelButton).toBeInTheDocument();
b69ab31127
b69ab31128 act(() => {
b69ab31129 fireEvent.click(nullthrows(cancelButton));
b69ab31130 });
b69ab31131 },
b69ab31132
b69ab31133 /** Get the textarea for the title editor */
b69ab31134 getTitleEditor(): HTMLTextAreaElement {
b69ab31135 const title = screen.getByTestId('commit-info-title-field') as HTMLTextAreaElement;
b69ab31136 expect(title).toBeInTheDocument();
b69ab31137 return title;
b69ab31138 },
b69ab31139
b69ab31140 /** Get the textarea for the description editor
b69ab31141 * For internal builds, this points to the "summary" editor instead of the "description" editor
b69ab31142 */
b69ab31143 getDescriptionEditor(): HTMLTextAreaElement {
b69ab31144 const description = screen.getByTestId(
b69ab31145 isInternalMessageFields() ? 'commit-info-summary-field' : 'commit-info-description-field',
b69ab31146 ) as HTMLTextAreaElement;
b69ab31147 expect(description).toBeInTheDocument();
b69ab31148 return description;
b69ab31149 },
b69ab31150
b69ab31151 /** Get the textarea for the test plan editor. Unavailable in OSS tests (use internal-only tests). */
b69ab31152 getTestPlanEditor(): HTMLTextAreaElement {
b69ab31153 assert(isInternalMessageFields(), 'Cannot edit test plan in OSS');
b69ab31154 const testPlan = screen.getByTestId('commit-info-test-plan-field') as HTMLTextAreaElement;
b69ab31155 expect(testPlan).toBeInTheDocument();
b69ab31156 return testPlan;
b69ab31157 },
b69ab31158
b69ab31159 /** Get the input element for a given field's editor, according to the field key in the FieldConfig (actually just a div in tests) */
b69ab31160 getFieldEditor(key: string): HTMLDivElement {
b69ab31161 const renderKey = convertFieldNameToKey(key);
b69ab31162 const el = screen.getByTestId(`commit-info-${renderKey}-field`) as HTMLDivElement;
b69ab31163 expect(el).toBeInTheDocument();
b69ab31164 return el;
b69ab31165 },
b69ab31166
b69ab31167 descriptionTextContent() {
b69ab31168 return CommitInfoTestUtils.getDescriptionEditor().value;
b69ab31169 },
b69ab31170
b69ab31171 clickToEditTitle() {
b69ab31172 act(() => {
b69ab31173 const title = screen.getByTestId('commit-info-rendered-title');
b69ab31174 expect(title).toBeInTheDocument();
b69ab31175 fireEvent.click(title);
b69ab31176 });
b69ab31177 },
b69ab31178 clickToEditDescription() {
b69ab31179 act(() => {
b69ab31180 const description = screen.getByTestId(
b69ab31181 isInternalMessageFields()
b69ab31182 ? 'commit-info-rendered-summary'
b69ab31183 : 'commit-info-rendered-description',
b69ab31184 );
b69ab31185 expect(description).toBeInTheDocument();
b69ab31186 fireEvent.click(description);
b69ab31187 });
b69ab31188 },
b69ab31189 clickToEditTestPlan() {
b69ab31190 assert(isInternalMessageFields(), 'Cannot edit test plan in OSS');
b69ab31191 act(() => {
b69ab31192 const testPlan = screen.getByTestId('commit-info-rendered-test-plan');
b69ab31193 expect(testPlan).toBeInTheDocument();
b69ab31194 fireEvent.click(testPlan);
b69ab31195 });
b69ab31196 },
b69ab31197
b69ab31198 /** Internal tests only, since GitHub's message schema does not include this field */
b69ab31199 clickToEditReviewers() {
b69ab31200 act(() => {
b69ab31201 const title = screen.getByTestId('commit-info-rendered-reviewers');
b69ab31202 expect(title).toBeInTheDocument();
b69ab31203 fireEvent.click(title);
b69ab31204 });
b69ab31205 },
b69ab31206
b69ab31207 expectIsEditingTitle() {
b69ab31208 const titleEditor = screen.queryByTestId('commit-info-title-field') as HTMLInputElement;
b69ab31209 expect(titleEditor).toBeInTheDocument();
b69ab31210 },
b69ab31211 expectIsNOTEditingTitle() {
b69ab31212 const titleEditor = screen.queryByTestId('commit-info-title-field') as HTMLInputElement;
b69ab31213 expect(titleEditor).not.toBeInTheDocument();
b69ab31214 },
b69ab31215
b69ab31216 expectIsEditingDescription() {
b69ab31217 const descriptionEditor = screen.queryByTestId(
b69ab31218 isInternalMessageFields() ? 'commit-info-summary-field' : 'commit-info-description-field',
b69ab31219 ) as HTMLTextAreaElement;
b69ab31220 expect(descriptionEditor).toBeInTheDocument();
b69ab31221 },
b69ab31222 expectIsNOTEditingDescription() {
b69ab31223 const descriptionEditor = screen.queryByTestId(
b69ab31224 isInternalMessageFields() ? 'commit-info-summary-field' : 'commit-info-description-field',
b69ab31225 ) as HTMLTextAreaElement;
b69ab31226 expect(descriptionEditor).not.toBeInTheDocument();
b69ab31227 },
b69ab31228};
b69ab31229
b69ab31230export const MergeConflictTestUtils = {
b69ab31231 waitForContinueButtonNotDisabled: () =>
b69ab31232 waitFor(() =>
b69ab31233 expect(
b69ab31234 within(screen.getByTestId('commit-tree-root')).getByTestId(
b69ab31235 'conflict-continue-button',
b69ab31236 ) as HTMLButtonElement,
b69ab31237 ).not.toBeDisabled(),
b69ab31238 ),
b69ab31239 clickContinueConflicts: () =>
b69ab31240 act(() => {
b69ab31241 fireEvent.click(
b69ab31242 within(screen.getByTestId('commit-tree-root')).getByTestId('conflict-continue-button'),
b69ab31243 );
b69ab31244 }),
b69ab31245 expectInMergeConflicts: () =>
b69ab31246 expect(
b69ab31247 within(screen.getByTestId('commit-tree-root')).getByText('Unresolved Merge Conflicts'),
b69ab31248 ).toBeInTheDocument(),
b69ab31249 expectNotInMergeConflicts: () =>
b69ab31250 expect(
b69ab31251 within(screen.getByTestId('commit-tree-root')).queryByText('Unresolved Merge Conflicts'),
b69ab31252 ).not.toBeInTheDocument(),
b69ab31253};
b69ab31254
b69ab31255function isInternalMessageFields(): boolean {
b69ab31256 const schema = readAtom(commitMessageFieldsSchema);
b69ab31257 return schema !== OSSCommitMessageFieldSchema;
b69ab31258}
b69ab31259
b69ab31260/**
b69ab31261 * When querying changed files, there may be unicode left-to-right marks in the path,
b69ab31262 * which make the test hard to read. This util searches for a string, inserting optional
b69ab31263 * RTL marks at path boundaries.
b69ab31264 */
b69ab31265export function ignoreRTL(s: string): RegExp {
b69ab31266 return new RegExp(`^\u200E?${s}\u200E?$`);
b69ab31267}