addons/isl/src/__tests__/FillCommitMessage.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, within} from '@testing-library/react';
b69ab319import userEvent from '@testing-library/user-event';
b69ab3110import App from '../App';
b69ab3111import platform from '../platform';
b69ab3112import {CommitInfoTestUtils} from '../testQueries';
b69ab3113import {
b69ab3114 COMMIT,
b69ab3115 expectMessageSentToServer,
b69ab3116 openCommitInfoSidebar,
b69ab3117 simulateCommits,
b69ab3118 simulateMessageFromServer,
b69ab3119} from '../testUtils';
b69ab3120
b69ab3121/* eslint-disable @typescript-eslint/no-non-null-assertion */
b69ab3122
b69ab3123const {
b69ab3124 withinCommitInfo,
b69ab3125 clickAmendMode,
b69ab3126 clickCommitMode,
b69ab3127 clickToSelectCommit,
b69ab3128 getTitleEditor,
b69ab3129 getDescriptionEditor,
b69ab3130} = CommitInfoTestUtils;
b69ab3131
b69ab3132describe('FillCommitMessage', () => {
b69ab3133 beforeEach(() => {
b69ab3134 render(<App />);
b69ab3135 act(() => {
b69ab3136 openCommitInfoSidebar();
b69ab3137 expectMessageSentToServer({
b69ab3138 type: 'subscribe',
b69ab3139 kind: 'smartlogCommits',
b69ab3140 subscriptionID: expect.anything(),
b69ab3141 });
b69ab3142 simulateCommits({
b69ab3143 value: [
b69ab3144 COMMIT('1', 'some public base', '0', {phase: 'public'}),
b69ab3145 COMMIT('a', 'My Commit', '1'),
b69ab3146 COMMIT('b', 'Head Commit', 'a', {
b69ab3147 isDot: true,
b69ab3148 description: 'Summary: This is my commit message\n',
b69ab3149 }),
b69ab3150 ],
b69ab3151 });
b69ab3152 });
b69ab3153 });
b69ab3154
b69ab3155 it('Shows fill message buttons in commit mode', () => {
b69ab3156 clickCommitMode();
b69ab3157 expect(screen.getByText('Fill commit message from', {exact: false})).toBeInTheDocument();
b69ab3158 });
b69ab3159
b69ab3160 it('does not show fill buttons in amend mode', () => {
b69ab3161 clickCommitMode();
b69ab3162 clickToSelectCommit('a');
b69ab3163 expect(screen.queryByText('Fill commit message from', {exact: false})).not.toBeInTheDocument();
b69ab3164 clickToSelectCommit('b');
b69ab3165 clickAmendMode();
b69ab3166 expect(screen.queryByText('Fill commit message from', {exact: false})).not.toBeInTheDocument();
b69ab3167 });
b69ab3168
b69ab3169 it('Load from last commit', async () => {
b69ab3170 clickCommitMode();
b69ab3171
b69ab3172 expect(getTitleEditor()).toHaveValue('');
b69ab3173 expect(getDescriptionEditor()).toHaveValue('');
b69ab3174
b69ab3175 const loadFromLastCommit = withinCommitInfo().getByText('last commit');
b69ab3176 expect(loadFromLastCommit).toBeInTheDocument();
b69ab3177 fireEvent.click(loadFromLastCommit);
b69ab3178 await waitFor(() => {
b69ab3179 expect(getTitleEditor().value).toMatch('Head Commit');
b69ab3180 expect(getDescriptionEditor().value).toMatch(/This is my commit message/);
b69ab3181 });
b69ab3182 });
b69ab3183
b69ab3184 it('Load from commit template', async () => {
b69ab3185 expectMessageSentToServer({type: 'fetchCommitMessageTemplate'});
b69ab3186 act(() => {
b69ab3187 simulateMessageFromServer({
b69ab3188 type: 'fetchedCommitMessageTemplate',
b69ab3189 template: 'template title\nSummary: template summary',
b69ab3190 });
b69ab3191 });
b69ab3192 clickCommitMode();
b69ab3193
b69ab3194 // the template is used automatically, so let's clear it out
b69ab3195 act(() => {
b69ab3196 userEvent.clear(getTitleEditor());
b69ab3197 userEvent.clear(getDescriptionEditor());
b69ab3198 });
b69ab3199
b69ab31100 expect(getTitleEditor()).toHaveValue('');
b69ab31101 expect(getDescriptionEditor()).toHaveValue('');
b69ab31102
b69ab31103 const loadFromLastCommit = withinCommitInfo().getByText('template file');
b69ab31104 expect(loadFromLastCommit).toBeInTheDocument();
b69ab31105 fireEvent.click(loadFromLastCommit);
b69ab31106 await waitFor(() => {
b69ab31107 expect(getTitleEditor().value).toMatch('template title');
b69ab31108 expect(getDescriptionEditor().value).toMatch(/template summary/);
b69ab31109 });
b69ab31110 });
b69ab31111
b69ab31112 describe('conflicts in message to fill', () => {
b69ab31113 async function triggerConflict() {
b69ab31114 clickCommitMode();
b69ab31115
b69ab31116 act(() => {
b69ab31117 userEvent.type(getTitleEditor(), 'existing title');
b69ab31118 userEvent.type(getDescriptionEditor(), 'Summary: existing description');
b69ab31119 });
b69ab31120
b69ab31121 const loadFromLastCommit = withinCommitInfo().getByText('last commit');
b69ab31122 expect(loadFromLastCommit).toBeInTheDocument();
b69ab31123 fireEvent.click(loadFromLastCommit);
b69ab31124
b69ab31125 await waitFor(() => {
b69ab31126 expect(screen.getByText('Commit Messages Conflict')).toBeInTheDocument();
b69ab31127 });
b69ab31128 }
b69ab31129
b69ab31130 function withinConflictWarning() {
b69ab31131 return within(screen.getByTestId('fill-message-conflict-warning'));
b69ab31132 }
b69ab31133
b69ab31134 it('shows warning and differences', async () => {
b69ab31135 await triggerConflict();
b69ab31136
b69ab31137 expect(withinConflictWarning().getByText('Title')).toBeInTheDocument();
b69ab31138 expect(withinConflictWarning().getByText('existing title')).toBeInTheDocument();
b69ab31139 expect(withinConflictWarning().getByText('Head Commit')).toBeInTheDocument();
b69ab31140 expect(
b69ab31141 withinConflictWarning().getByText('existing description', {exact: false}),
b69ab31142 ).toBeInTheDocument();
b69ab31143 expect(
b69ab31144 withinConflictWarning().getByText('This is my commit message', {exact: false}),
b69ab31145 ).toBeInTheDocument();
b69ab31146 });
b69ab31147
b69ab31148 it('allows merging', async () => {
b69ab31149 await triggerConflict();
b69ab31150 const mergeButton = screen.getByText('Merge');
b69ab31151 expect(mergeButton).toBeInTheDocument();
b69ab31152 fireEvent.click(mergeButton);
b69ab31153
b69ab31154 await waitFor(() => {
b69ab31155 expect(getTitleEditor().value).toMatch('existing title, Head Commit');
b69ab31156 expect(getDescriptionEditor().value).toMatch(/existing description/);
b69ab31157 expect(getDescriptionEditor().value).toMatch(/This is my commit message/);
b69ab31158 });
b69ab31159 });
b69ab31160
b69ab31161 it('allows merging non-empty', async () => {
b69ab31162 await triggerConflict();
b69ab31163 const mergeButton = screen.getByText('Only Fill Empty');
b69ab31164 expect(mergeButton).toBeInTheDocument();
b69ab31165 fireEvent.click(mergeButton);
b69ab31166
b69ab31167 await waitFor(() => {
b69ab31168 expect(getTitleEditor().value).toMatch('existing title');
b69ab31169 expect(getDescriptionEditor().value).toMatch(/existing description/);
b69ab31170 expect(getDescriptionEditor().value).not.toMatch(/This is my commit message/);
b69ab31171 });
b69ab31172 });
b69ab31173
b69ab31174 it('allows cancelling', async () => {
b69ab31175 await triggerConflict();
b69ab31176 const cancelButton = screen.getByText('Cancel');
b69ab31177 expect(cancelButton).toBeInTheDocument();
b69ab31178 fireEvent.click(cancelButton);
b69ab31179
b69ab31180 expect(screen.queryByText('Commit Messages Conflict')).not.toBeInTheDocument();
b69ab31181 await waitFor(() => {
b69ab31182 expect(getTitleEditor().value).toMatch('existing title');
b69ab31183 expect(getDescriptionEditor().value).toMatch(/existing description/);
b69ab31184 });
b69ab31185 });
b69ab31186
b69ab31187 it('allows overwriting', async () => {
b69ab31188 await triggerConflict();
b69ab31189 const overwrite = screen.getByText('Overwrite');
b69ab31190 expect(overwrite).toBeInTheDocument();
b69ab31191 fireEvent.click(overwrite);
b69ab31192
b69ab31193 await waitFor(() => {
b69ab31194 expect(getTitleEditor().value).toMatch('Head Commit');
b69ab31195 expect(getDescriptionEditor().value).toMatch(/This is my commit message/);
b69ab31196 });
b69ab31197 });
b69ab31198 });
b69ab31199
b69ab31200 it('Clears commit message', async () => {
b69ab31201 clickCommitMode();
b69ab31202
b69ab31203 expect(getTitleEditor()).toHaveValue('');
b69ab31204 expect(getDescriptionEditor()).toHaveValue('');
b69ab31205
b69ab31206 const confirmSpy = jest
b69ab31207 .spyOn(platform, 'confirm')
b69ab31208 .mockImplementation(() => Promise.resolve(true));
b69ab31209
b69ab31210 fireEvent.click(screen.getByTestId('fill-commit-message-more-options'));
b69ab31211 fireEvent.click(screen.getByText('Clear commit message'));
b69ab31212
b69ab31213 await waitFor(() => expect(confirmSpy).toHaveBeenCalled());
b69ab31214 });
b69ab31215});