Commit e0e14dff authored by Alexander Turinske's avatar Alexander Turinske

Simplify alert messaging

- decouple messaging from error
- update tests
parent 24f161ed
<script>
import { GlAlert, GlLink, GlSprintf } from '@gitlab/ui';
import { ERRORS } from '../constants';
export default {
ERRORS,
components: {
GlAlert,
GlLink,
......@@ -16,18 +14,7 @@ export default {
},
wikiPagePath: {
type: String,
required: false,
default: '',
},
},
computed: {
errorMessage() {
if (this.error === this.$options.ERRORS.PAGE_CHANGE.ERROR) {
return this.$options.ERRORS.PAGE_CHANGE.MESSAGE;
} else if (this.error === this.$options.ERRORS.PAGE_RENAME.ERROR) {
return this.$options.ERRORS.PAGE_RENAME.MESSAGE;
}
return this.error;
required: true,
},
},
};
......@@ -35,7 +22,7 @@ export default {
<template>
<gl-alert variant="danger" :dismissible="false">
<gl-sprintf :message="errorMessage">
<gl-sprintf :message="error">
<template #wikiLink="{ content }">
<gl-link :href="wikiPagePath" target="_blank">{{ content }}</gl-link>
</template>
......
import { s__ } from '~/locale';
export const ERRORS = {
PAGE_CHANGE: {
ERROR: 'WikiPage::PageChangedError',
MESSAGE: s__(
'WikiPageConflictMessage|Someone edited the page the same time you did. Please check out %{wikiLinkStart}the page%{wikiLinkEnd} and make sure your changes will not unintentionally remove theirs.',
),
},
PAGE_RENAME: {
ERROR: 'WikiPage::PageRenameError',
MESSAGE: s__('WikiEdit|There is already a page with the same title in that path.'),
},
};
......@@ -116,7 +116,7 @@ module WikiActions
render 'shared/wikis/edit'
end
rescue WikiPage::PageChangedError, WikiPage::PageRenameError => e
@error = e
@error = e.message
render 'shared/wikis/edit'
end
# rubocop:enable Gitlab/ModuleWithInstanceVariables
......
......@@ -205,14 +205,15 @@ class WikiPage
last_commit_sha = attrs.delete(:last_commit_sha)
if last_commit_sha && last_commit_sha != self.last_commit_sha
raise PageChangedError
raise PageChangedError, s_(
'WikiPageConflictMessage|Someone edited the page the same time you did. Please check out %{wikiLinkStart}the page%{wikiLinkEnd} and make sure your changes will not unintentionally remove theirs.')
end
update_attributes(attrs)
if title.present? && title_changed? && wiki.find_page(title).present?
attributes[:title] = page.title
raise PageRenameError
raise PageRenameError, s_('WikiEdit|There is already a page with the same title in that path.')
end
save do
......
- wiki_page_title @page, @page.persisted? ? _('Edit') : _('New')
- add_page_specific_style 'page_bundles/wiki'
- errorMessage = @error || ''
- if @error
#js-wiki-error{ data: { error: errorMessage, wiki_page_path: wiki_page_path(@wiki, @page) } }
#js-wiki-error{ data: { error: @error, wiki_page_path: wiki_page_path(@wiki, @page) } }
.wiki-page-header.top-area.has-sidebar-toggle.flex-column.flex-lg-row
= wiki_sidebar_toggle_button
......
import { GlAlert, GlLink, GlSprintf } from '@gitlab/ui';
import { shallowMount } from '@vue/test-utils';
import WikiAlert from '~/pages/shared/wikis/components/wiki_alert.vue';
import { ERRORS } from '~/pages/shared/wikis/constants';
describe('WikiAlert', () => {
let wrapper;
const ERROR = 'There is already a page with the same title in that path.';
const ERROR_WITH_LINK =
'Someone edited the page the same time you did. Please check out %{wikiLinkStart}the page%{wikiLinkEnd} and make sure your changes will not unintentionally remove theirs.';
const PATH = '/test';
function createWrapper(propsData = {}, stubs = {}) {
wrapper = shallowMount(WikiAlert, {
propsData,
propsData: { wikiPagePath: PATH, ...propsData },
stubs,
});
}
......@@ -24,32 +27,15 @@ describe('WikiAlert', () => {
describe('Wiki Alert', () => {
it('does show an alert when there is an error', () => {
createWrapper({ error: ERRORS.PAGE_RENAME.ERROR });
createWrapper({ error: ERROR });
expect(findGlAlert().exists()).toBe(true);
});
it('does show the page change message text', () => {
createWrapper({ error: ERRORS.PAGE_CHANGE.ERROR });
expect(findGlSprintf().exists()).toBe(true);
const text = findGlSprintf();
expect(text.attributes('message')).toBe(ERRORS.PAGE_CHANGE.MESSAGE);
});
it('does show the page rename message text', () => {
createWrapper({ error: ERRORS.PAGE_RENAME.ERROR });
expect(findGlSprintf().attributes('message')).toBe(ERRORS.PAGE_RENAME.MESSAGE);
});
it('does show the error message', () => {
const error = 'test message';
createWrapper({ error });
expect(findGlSprintf().attributes('message')).toBe(error);
expect(findGlSprintf().attributes('message')).toBe(ERROR);
});
it('does show the link to the help path', () => {
const wikiPagePath = '/help';
createWrapper({ error: ERRORS.PAGE_CHANGE.ERROR, wikiPagePath }, { GlAlert, GlSprintf });
expect(findGlLink().attributes('href')).toBe(wikiPagePath);
createWrapper({ error: ERROR_WITH_LINK }, { GlAlert, GlSprintf });
expect(findGlLink().attributes('href')).toBe(PATH);
});
});
});
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