Skip to content
代码片段 群组 项目
未验证 提交 84140006 编辑于 作者: Frédéric Caplette's avatar Frédéric Caplette 提交者: GitLab
浏览文件

Merge branch '497993-remove-shared-survey-banner-component' into 'master'

No related branches found
No related tags found
无相关合并请求
import SurveyBanner from './survey_banner.vue';
export default {
component: SurveyBanner,
title: 'ee/vue_shared/survey_banner',
};
const Template = (args, { argTypes }) => ({
components: { SurveyBanner },
props: Object.keys(argTypes),
template: '<survey-banner v-bind="$props" />',
});
export const Default = Template.bind({});
Default.args = {
surveyLink: 'testlink.test',
daysToAskLater: 7,
title: 'Shared Survey Banner Test Title',
buttonText: 'Shared Survey Banner Button Text',
description: 'Shared Survey Banner Test Description',
toastMessage: 'Shared Survey Banner Test ToastMessage',
storageKey: 'testStorageKey',
bannerId: 'testbannerID',
svgPath: 'https://gitlab-org.gitlab.io/gitlab-svgs/dist/illustrations/chat-sm.svg',
};
<script>
import { GlButton, GlBanner, GlSprintf } from '@gitlab/ui';
import { __ } from '~/locale';
import LocalStorageSync from '~/vue_shared/components/local_storage_sync.vue';
import showToast from '~/vue_shared/plugins/global_toast';
export default {
components: { GlButton, GlBanner, GlSprintf, LocalStorageSync },
props: {
surveyLink: {
type: String,
required: true,
},
daysToAskLater: {
type: Number,
required: true,
},
title: {
type: String,
required: true,
},
buttonText: {
type: String,
required: true,
},
description: {
type: String,
required: true,
},
toastMessage: {
type: String,
required: true,
},
storageKey: {
type: String,
required: true,
},
bannerId: {
type: String,
required: true,
},
svgPath: {
type: String,
required: true,
},
},
data: () => ({
surveyShowDate: null,
}),
computed: {
shouldShowSurvey() {
const { surveyShowDate } = this;
const date = new Date(surveyShowDate);
// Survey is not enabled or user dismissed the survey by clicking the close icon.
if (surveyShowDate === this.$props.bannerId) {
return false;
}
// Date is invalid, we should show the survey.
if (Number.isNaN(date.getDate())) {
return true;
}
return date <= Date.now();
},
},
methods: {
hideSurvey() {
this.surveyShowDate = this.$props.bannerId;
},
askLater() {
const date = new Date();
date.setDate(date.getDate() + this.daysToAskLater);
this.surveyShowDate = date.toISOString();
showToast(this.$props.toastMessage);
},
},
i18n: {
askAgainLater: __('Ask again later'),
},
};
</script>
<template>
<local-storage-sync v-model="surveyShowDate" :storage-key="storageKey" as-string>
<gl-banner
v-if="shouldShowSurvey"
:title="title"
:button-text="buttonText"
:svg-path="svgPath"
:button-link="surveyLink"
@close="hideSurvey"
>
<p>
<gl-sprintf :message="description">
<template #bold="{ content }">
<span class="gl-font-bold">{{ content }}</span>
</template>
</gl-sprintf>
</p>
<template #actions>
<gl-button variant="link" class="gl-ml-5" data-testid="ask-later-button" @click="askLater">
{{ $options.i18n.askAgainLater }}
</gl-button>
</template>
</gl-banner>
</local-storage-sync>
</template>
import { GlBanner, GlButton } from '@gitlab/ui';
import { shallowMount } from '@vue/test-utils';
import { nextTick } from 'vue';
import SharedSurveyBanner from 'ee/vue_shared/survey_banner/survey_banner.vue';
import { extendedWrapper } from 'helpers/vue_test_utils_helper';
import LocalStorageSync from '~/vue_shared/components/local_storage_sync.vue';
import toast from '~/vue_shared/plugins/global_toast';
const TEST_LOCAL_STORAGE_KEY = 'testLocalStorageKey';
const TEST_BANNER_ID = 'testBannerId';
jest.mock('~/vue_shared/plugins/global_toast');
describe('Shared Survey Banner component', () => {
let wrapper;
const findGlBanner = () => wrapper.findComponent(GlBanner);
const findAskLaterButton = () => wrapper.findByTestId('ask-later-button');
const findLocalStorageSync = () => wrapper.findComponent(LocalStorageSync);
const getOffsetDateString = (days) => {
const date = new Date();
date.setDate(date.getDate() + days);
return date.toISOString();
};
const createWrapper = (props = {}) => {
wrapper = extendedWrapper(
shallowMount(SharedSurveyBanner, {
propsData: {
surveyLink: 'foo.bar',
daysToAskLater: 7,
title: 'testTitle',
buttonText: 'buttonText',
description: 'description',
toastMessage: 'toastMessage',
storageKey: TEST_LOCAL_STORAGE_KEY,
bannerId: TEST_BANNER_ID,
svgPath: '/foo.svg',
...props,
},
stubs: { GlBanner, GlButton, LocalStorageSync },
}),
);
};
beforeEach(() => {
gon.features = {};
});
afterEach(() => {
localStorage.removeItem(TEST_LOCAL_STORAGE_KEY);
});
beforeEach(() => {
createWrapper();
});
it('shows the banner with the correct components and props', () => {
const { title, buttonText, description, svgPath } = wrapper.props();
expect(findGlBanner().html()).toContain(description);
expect(findAskLaterButton().exists()).toBe(true);
expect(findLocalStorageSync().props('asString')).toBe(true);
expect(findGlBanner().props()).toMatchObject({
title,
buttonText,
svgPath,
});
});
it.each`
showOrHide | phrase | localStorageValue | isShown
${'hides'} | ${'a future date'} | ${getOffsetDateString(1)} | ${false}
${'shows'} | ${'a past date'} | ${getOffsetDateString(-1)} | ${true}
${'hides'} | ${'the current survey ID'} | ${TEST_BANNER_ID} | ${false}
${'shows'} | ${'a different survey ID'} | ${'SOME OTHER ID'} | ${true}
`(
'$showOrHide the banner if the localStorage value is $phrase',
async ({ localStorageValue, isShown }) => {
localStorage.setItem(TEST_LOCAL_STORAGE_KEY, localStorageValue);
createWrapper();
await nextTick();
expect(findGlBanner().exists()).toBe(isShown);
},
);
describe('closing the banner', () => {
beforeEach(() => {
createWrapper();
});
it('hides the banner and will set it to reshow later if the "Ask again later" button is clicked', async () => {
expect(findGlBanner().exists()).toBe(true);
findAskLaterButton().vm.$emit('click');
await nextTick();
const date = new Date(localStorage.getItem(TEST_LOCAL_STORAGE_KEY));
expect(findGlBanner().exists()).toBe(false);
expect(toast).toHaveBeenCalledTimes(1);
expect(date > new Date()).toBe(true);
});
it('hides the banner and sets it to never show again if the close button is clicked', async () => {
expect(findGlBanner().exists()).toBe(true);
findGlBanner().vm.$emit('close');
await nextTick();
expect(findGlBanner().exists()).toBe(false);
expect(localStorage.getItem(TEST_LOCAL_STORAGE_KEY)).toBe(TEST_BANNER_ID);
});
});
});
...@@ -7588,9 +7588,6 @@ msgstr "" ...@@ -7588,9 +7588,6 @@ msgstr ""
msgid "Ask a maintainer to check the import status for more details." msgid "Ask a maintainer to check the import status for more details."
msgstr "" msgstr ""
   
msgid "Ask again later"
msgstr ""
msgid "Ask someone with write access to resolve it." msgid "Ask someone with write access to resolve it."
msgstr "" msgstr ""
   
0% 加载中 .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册