Commit eb1ea420 authored by Paul Slaughter's avatar Paul Slaughter

Remove joinSentences in favor of HTML <br>

- joinSentences has a strong leaning towards English
and wouldn't work simply in other languages.
- Also clean up a few specs
parent fbd8cf89
<script> <script>
import { mapState, mapActions, mapGetters } from 'vuex'; import { mapState, mapActions, mapGetters } from 'vuex';
import { GlModal } from '@gitlab/ui'; import { GlModal, GlSafeHtmlDirective } from '@gitlab/ui';
import { n__, __ } from '~/locale'; import { n__, __ } from '~/locale';
import LoadingButton from '~/vue_shared/components/loading_button.vue'; import LoadingButton from '~/vue_shared/components/loading_button.vue';
import CommitMessageField from './message_field.vue'; import CommitMessageField from './message_field.vue';
...@@ -18,6 +18,9 @@ export default { ...@@ -18,6 +18,9 @@ export default {
SuccessMessage, SuccessMessage,
GlModal, GlModal,
}, },
directives: {
SafeHtml: GlSafeHtmlDirective,
},
data() { data() {
return { return {
isCompact: true, isCompact: true,
...@@ -189,7 +192,7 @@ export default { ...@@ -189,7 +192,7 @@ export default {
:action-cancel="{ text: __('Cancel') }" :action-cancel="{ text: __('Cancel') }"
@ok="forceCreateNewBranch" @ok="forceCreateNewBranch"
> >
{{ lastCommitError.message }} <div v-safe-html="lastCommitError.messageHTML"></div>
</gl-modal> </gl-modal>
</form> </form>
</transition> </transition>
......
import { escape } from 'lodash';
import { __ } from '~/locale'; import { __ } from '~/locale';
import { joinSentences } from '~/lib/utils/text_utility';
const CODEOWNERS_REGEX = /Push.*protected branches.*CODEOWNERS/; const CODEOWNERS_REGEX = /Push.*protected branches.*CODEOWNERS/;
const BRANCH_CHANGED_REGEX = /changed.*since.*start.*edit/; const BRANCH_CHANGED_REGEX = /changed.*since.*start.*edit/;
export const createUnexpectedCommitError = () => ({ export const createUnexpectedCommitError = () => ({
title: __('Unexpected error'), title: __('Unexpected error'),
message: __('Could not commit. An unexpected error occurred.'), messageHTML: __('Could not commit. An unexpected error occurred.'),
canCreateBranch: false, canCreateBranch: false,
}); });
export const createCodeownersCommitError = message => ({ export const createCodeownersCommitError = message => ({
title: __('CODEOWNERS rule violation'), title: __('CODEOWNERS rule violation'),
message, messageHTML: escape(message),
canCreateBranch: true, canCreateBranch: true,
}); });
export const createBranchChangedCommitError = message => ({ export const createBranchChangedCommitError = message => ({
title: __('Branch changed'), title: __('Branch changed'),
message: joinSentences(message, __('Would you like to create a new branch?')), messageHTML: `${escape(message)}<br/><br/>${__('Would you like to create a new branch?')}`,
canCreateBranch: true, canCreateBranch: true,
}); });
......
...@@ -399,24 +399,3 @@ export const truncateNamespace = (string = '') => { ...@@ -399,24 +399,3 @@ export const truncateNamespace = (string = '') => {
* @returns {Boolean} * @returns {Boolean}
*/ */
export const hasContent = obj => isString(obj) && obj.trim() !== ''; export const hasContent = obj => isString(obj) && obj.trim() !== '';
/**
* Joins the given sentences by adding periods if necessary.
*
* @param {...string} sentences
*/
export const joinSentences = (...sentences) =>
sentences.reduce((acc, sentence) => {
if (!sentence?.trim()) {
return acc;
} else if (!acc) {
return sentence;
} else if (/[.!?]\s*$/.test(acc)) {
const endsWithSpace = /\s$/.test(acc);
const sep = endsWithSpace ? '' : ' ';
return `${acc}${sep}${sentence}`;
}
return `${acc}. ${sentence}`;
}, '');
...@@ -270,7 +270,7 @@ describe('IDE commit form', () => { ...@@ -270,7 +270,7 @@ describe('IDE commit form', () => {
jest.spyOn(vm.$refs.commitErrorModal, 'show'); jest.spyOn(vm.$refs.commitErrorModal, 'show');
const error = createError(); const error = createError();
vm.$store.state.commit.commitError = error; store.state.commit.commitError = error;
await vm.$nextTick(); await vm.$nextTick();
...@@ -279,7 +279,9 @@ describe('IDE commit form', () => { ...@@ -279,7 +279,9 @@ describe('IDE commit form', () => {
actionCancel: { text: 'Cancel' }, actionCancel: { text: 'Cancel' },
...props, ...props,
}); });
expect(document.body).toHaveText(error.message); // Because of the legacy 'mountComponent' approach here, the only way to
// test the text of the modal is by viewing the content of the modal added to the document.
expect(document.body).toHaveText(error.messageHTML);
}); });
}); });
...@@ -289,7 +291,7 @@ describe('IDE commit form', () => { ...@@ -289,7 +291,7 @@ describe('IDE commit form', () => {
}); });
it('updates commit action and commits', async () => { it('updates commit action and commits', async () => {
vm.$store.state.commit.commitError = createCodeownersCommitError('test message'); store.state.commit.commitError = createCodeownersCommitError('test message');
await vm.$nextTick(); await vm.$nextTick();
......
...@@ -5,9 +5,9 @@ import { ...@@ -5,9 +5,9 @@ import {
parseCommitError, parseCommitError,
} from '~/ide/lib/errors'; } from '~/ide/lib/errors';
const TEST_MESSAGE = 'Test message'; const TEST_SPECIAL = '&special<';
const TEST_MESSAGE_WITH_SENTENCE = 'Test message.'; const TEST_SPECIAL_ESCAPED = '&amp;special&lt;';
const TEST_MESSAGE_WITH_SENTENCE_AND_SPACE = 'Test message. '; const TEST_MESSAGE = 'Test message.';
const CODEOWNERS_MESSAGE = const CODEOWNERS_MESSAGE =
'Push to protected branches that contain changes to files matching CODEOWNERS is not allowed'; 'Push to protected branches that contain changes to files matching CODEOWNERS is not allowed';
const CHANGED_MESSAGE = 'Things changed since you started editing'; const CHANGED_MESSAGE = 'Things changed since you started editing';
...@@ -25,7 +25,15 @@ describe('~/ide/lib/errors', () => { ...@@ -25,7 +25,15 @@ describe('~/ide/lib/errors', () => {
it('uses given message', () => { it('uses given message', () => {
expect(createCodeownersCommitError(TEST_MESSAGE)).toEqual({ expect(createCodeownersCommitError(TEST_MESSAGE)).toEqual({
title: 'CODEOWNERS rule violation', title: 'CODEOWNERS rule violation',
message: TEST_MESSAGE, messageHTML: TEST_MESSAGE,
canCreateBranch: true,
});
});
it('escapes special chars', () => {
expect(createCodeownersCommitError(TEST_SPECIAL)).toEqual({
title: 'CODEOWNERS rule violation',
messageHTML: TEST_SPECIAL_ESCAPED,
canCreateBranch: true, canCreateBranch: true,
}); });
}); });
...@@ -33,14 +41,13 @@ describe('~/ide/lib/errors', () => { ...@@ -33,14 +41,13 @@ describe('~/ide/lib/errors', () => {
describe('createBranchChangedCommitError', () => { describe('createBranchChangedCommitError', () => {
it.each` it.each`
message | expectedMessage message | expectedMessage
${TEST_MESSAGE} | ${`${TEST_MESSAGE}. Would you like to create a new branch?`} ${TEST_MESSAGE} | ${`${TEST_MESSAGE}<br/><br/>Would you like to create a new branch?`}
${TEST_MESSAGE_WITH_SENTENCE} | ${`${TEST_MESSAGE}. Would you like to create a new branch?`} ${TEST_SPECIAL} | ${`${TEST_SPECIAL_ESCAPED}<br/><br/>Would you like to create a new branch?`}
${TEST_MESSAGE_WITH_SENTENCE_AND_SPACE} | ${`${TEST_MESSAGE}. Would you like to create a new branch?`}
`('uses given message="$message"', ({ message, expectedMessage }) => { `('uses given message="$message"', ({ message, expectedMessage }) => {
expect(createBranchChangedCommitError(message)).toEqual({ expect(createBranchChangedCommitError(message)).toEqual({
title: 'Branch changed', title: 'Branch changed',
message: expectedMessage, messageHTML: expectedMessage,
canCreateBranch: true, canCreateBranch: true,
}); });
}); });
......
...@@ -545,10 +545,10 @@ describe('IDE commit module actions', () => { ...@@ -545,10 +545,10 @@ describe('IDE commit module actions', () => {
await store.dispatch('commit/commitChanges').catch(() => {}); await store.dispatch('commit/commitChanges').catch(() => {});
expect(store.commit.mock.calls).toEqual([ expect(store.commit.mock.calls).toEqual([
expect.arrayContaining(['commit/CLEAR_ERROR']), ['commit/CLEAR_ERROR', undefined, undefined],
expect.arrayContaining(['commit/UPDATE_LOADING', true]), ['commit/UPDATE_LOADING', true, undefined],
expect.arrayContaining(['commit/UPDATE_LOADING', false]), ['commit/UPDATE_LOADING', false, undefined],
expect.arrayContaining(['commit/SET_ERROR', createUnexpectedCommitError()]), ['commit/SET_ERROR', createUnexpectedCommitError(), undefined],
]); ]);
}); });
}); });
......
...@@ -325,17 +325,4 @@ describe('text_utility', () => { ...@@ -325,17 +325,4 @@ describe('text_utility', () => {
expect(textUtils.hasContent(txt)).toEqual(result); expect(textUtils.hasContent(txt)).toEqual(result);
}); });
}); });
describe('joinSentences', () => {
it.each`
input | output
${[]} | ${''}
${['Lorem ipsum']} | ${'Lorem ipsum'}
${['Lorem ipsum', null, 'Dolar sit']} | ${'Lorem ipsum. Dolar sit'}
${['Lorem ipsum!', 'Dolar sit']} | ${'Lorem ipsum! Dolar sit'}
${['Lorem ipsum? ', 'Dolar sit']} | ${'Lorem ipsum? Dolar sit'}
`('joins the sentences with periods ($input)', ({ input, output }) => {
expect(textUtils.joinSentences(...input)).toBe(output);
});
});
}); });
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment