Commit 35d7d3fa authored by Dallas Reedy's avatar Dallas Reedy

Reduce code duplication in the Jest specs

parent 65adfc5b
...@@ -10,6 +10,8 @@ describe('PaidFeatureCalloutPopover', () => { ...@@ -10,6 +10,8 @@ describe('PaidFeatureCalloutPopover', () => {
let trackingSpy; let trackingSpy;
let wrapper; let wrapper;
const trackingExperimentKey = 'experiment:highlight_paid_features_during_active_trial';
const findGlPopover = () => wrapper.findComponent(GlPopover); const findGlPopover = () => wrapper.findComponent(GlPopover);
const defaultProps = { const defaultProps = {
...@@ -22,50 +24,50 @@ describe('PaidFeatureCalloutPopover', () => { ...@@ -22,50 +24,50 @@ describe('PaidFeatureCalloutPopover', () => {
targetId: 'some-feature-callout-target', targetId: 'some-feature-callout-target',
}; };
const createComponent = (props = defaultProps) => { const createComponent = (extraProps = {}) => {
return extendedWrapper( return extendedWrapper(
shallowMount(PaidFeatureCalloutPopover, { shallowMount(PaidFeatureCalloutPopover, {
propsData: props, propsData: {
...defaultProps,
...extraProps,
},
}), }),
); );
}; };
beforeEach(() => {
wrapper = createComponent();
});
afterEach(() => { afterEach(() => {
wrapper.destroy(); wrapper.destroy();
}); });
describe('GlPopover attributes', () => { describe('GlPopover attributes', () => {
describe('with some default props', () => { const sharedAttrs = {
beforeEach(() => { boundary: 'viewport',
wrapper = createComponent(); placement: 'top',
}); target: 'some-feature-callout-target',
};
describe('with some default props', () => {
it('sets attributes on the GlPopover component', () => { it('sets attributes on the GlPopover component', () => {
const attributes = findGlPopover().attributes(); const attributes = findGlPopover().attributes();
expect(attributes).toMatchObject({ expect(attributes).toMatchObject(sharedAttrs);
boundary: 'viewport',
placement: 'top',
target: 'some-feature-callout-target',
});
expect(attributes.containerId).toBeUndefined(); expect(attributes.containerId).toBeUndefined();
}); });
}); });
describe('with additional, optional props', () => { describe('with additional, optional props', () => {
beforeEach(() => { beforeEach(() => {
wrapper = createComponent({ wrapper = createComponent({ containerId: 'some-container-id' });
...defaultProps,
containerId: 'some-container-id',
});
}); });
it('sets more attributes on the GlPopover component', () => { it('sets more attributes on the GlPopover component', () => {
expect(findGlPopover().attributes()).toMatchObject({ expect(findGlPopover().attributes()).toMatchObject({
boundary: 'viewport', ...sharedAttrs,
container: 'some-container-id', container: 'some-container-id',
placement: 'top',
target: 'some-feature-callout-target',
}); });
}); });
}); });
...@@ -73,16 +75,12 @@ describe('PaidFeatureCalloutPopover', () => { ...@@ -73,16 +75,12 @@ describe('PaidFeatureCalloutPopover', () => {
describe('popoverTitle', () => { describe('popoverTitle', () => {
it('renders the title text', () => { it('renders the title text', () => {
wrapper = createComponent();
expect(wrapper.vm.popoverTitle).toEqual('12 days remaining to enjoy some feature'); expect(wrapper.vm.popoverTitle).toEqual('12 days remaining to enjoy some feature');
}); });
}); });
describe('popoverContent', () => { describe('popoverContent', () => {
it('renders the content text', () => { it('renders the content text', () => {
wrapper = createComponent();
expect(wrapper.vm.popoverContent).toEqual( expect(wrapper.vm.popoverContent).toEqual(
'Enjoying your GitLab Awesomesauce trial? To continue using some feature after your trial ends, upgrade to ' + 'Enjoying your GitLab Awesomesauce trial? To continue using some feature after your trial ends, upgrade to ' +
'GitLab Amazing.', 'GitLab Amazing.',
...@@ -91,14 +89,13 @@ describe('PaidFeatureCalloutPopover', () => { ...@@ -91,14 +89,13 @@ describe('PaidFeatureCalloutPopover', () => {
}); });
describe('promo image', () => { describe('promo image', () => {
const promoImagePathForTest = 'path/to/some/image.svg';
const findPromoImage = () => wrapper.findByTestId('promo-img'); const findPromoImage = () => wrapper.findByTestId('promo-img');
describe('with the optional promoImagePath prop', () => { describe('with the optional promoImagePath prop', () => {
beforeEach(() => { beforeEach(() => {
wrapper = createComponent({ wrapper = createComponent({ promoImagePath: promoImagePathForTest });
...defaultProps,
promoImagePath: 'path/to/some/image.svg',
});
}); });
it('renders the promo image', () => { it('renders the promo image', () => {
...@@ -108,8 +105,7 @@ describe('PaidFeatureCalloutPopover', () => { ...@@ -108,8 +105,7 @@ describe('PaidFeatureCalloutPopover', () => {
describe('with the optional promoImageAltText prop', () => { describe('with the optional promoImageAltText prop', () => {
beforeEach(() => { beforeEach(() => {
wrapper = createComponent({ wrapper = createComponent({
...defaultProps, promoImagePath: promoImagePathForTest,
promoImagePath: 'path/to/some/image.svg',
promoImageAltText: 'My fancy alt text', promoImageAltText: 'My fancy alt text',
}); });
}); });
...@@ -120,13 +116,6 @@ describe('PaidFeatureCalloutPopover', () => { ...@@ -120,13 +116,6 @@ describe('PaidFeatureCalloutPopover', () => {
}); });
describe('without the optional promoImageAltText prop', () => { describe('without the optional promoImageAltText prop', () => {
beforeEach(() => {
wrapper = createComponent({
...defaultProps,
promoImagePath: 'path/to/some/image.svg',
});
});
it('renders the promo image with default alt text', () => { it('renders the promo image with default alt text', () => {
expect(findPromoImage().attributes('alt')).toBe('SVG illustration'); expect(findPromoImage().attributes('alt')).toBe('SVG illustration');
}); });
...@@ -134,10 +123,6 @@ describe('PaidFeatureCalloutPopover', () => { ...@@ -134,10 +123,6 @@ describe('PaidFeatureCalloutPopover', () => {
}); });
describe('without the optional promoImagePath prop', () => { describe('without the optional promoImagePath prop', () => {
beforeEach(() => {
wrapper = createComponent();
});
it('does not render a promo image', () => { it('does not render a promo image', () => {
expect(findPromoImage().exists()).toBe(false); expect(findPromoImage().exists()).toBe(false);
}); });
...@@ -145,27 +130,27 @@ describe('PaidFeatureCalloutPopover', () => { ...@@ -145,27 +130,27 @@ describe('PaidFeatureCalloutPopover', () => {
}); });
describe('call-to-action buttons', () => { describe('call-to-action buttons', () => {
const sharedAttrs = {
target: '_blank',
variant: 'confirm',
size: 'small',
block: '',
'data-track-action': 'click_button',
'data-track-property': trackingExperimentKey,
};
const findUpgradeBtn = () => wrapper.findByTestId('upgradeBtn'); const findUpgradeBtn = () => wrapper.findByTestId('upgradeBtn');
const findCompareBtn = () => wrapper.findByTestId('compareBtn'); const findCompareBtn = () => wrapper.findByTestId('compareBtn');
beforeEach(() => {
wrapper = createComponent();
});
it('correctly renders an Upgrade button', () => { it('correctly renders an Upgrade button', () => {
const upgradeBtn = findUpgradeBtn(); const upgradeBtn = findUpgradeBtn();
expect(upgradeBtn.text()).toEqual('Upgrade to GitLab Amazing'); expect(upgradeBtn.text()).toEqual('Upgrade to GitLab Amazing');
expect(upgradeBtn.attributes()).toMatchObject({ expect(upgradeBtn.attributes()).toMatchObject({
...sharedAttrs,
href: '/-/subscriptions/new?namespace_id=123&plan_id=abc456', href: '/-/subscriptions/new?namespace_id=123&plan_id=abc456',
target: '_blank',
category: 'primary', category: 'primary',
variant: 'confirm',
size: 'small',
block: '',
'data-track-action': 'click_button',
'data-track-label': 'upgrade_to_ultimate', 'data-track-label': 'upgrade_to_ultimate',
'data-track-property': 'experiment:highlight_paid_features_during_active_trial',
}); });
}); });
...@@ -174,15 +159,10 @@ describe('PaidFeatureCalloutPopover', () => { ...@@ -174,15 +159,10 @@ describe('PaidFeatureCalloutPopover', () => {
expect(compareBtn.text()).toEqual('Compare all plans'); expect(compareBtn.text()).toEqual('Compare all plans');
expect(compareBtn.attributes()).toMatchObject({ expect(compareBtn.attributes()).toMatchObject({
...sharedAttrs,
href: '/group/test-group/-/billings', href: '/group/test-group/-/billings',
target: '_blank',
category: 'secondary', category: 'secondary',
variant: 'confirm',
size: 'small',
block: '',
'data-track-action': 'click_button',
'data-track-label': 'compare_all_plans', 'data-track-label': 'compare_all_plans',
'data-track-property': 'experiment:highlight_paid_features_during_active_trial',
}); });
}); });
}); });
...@@ -197,16 +177,12 @@ describe('PaidFeatureCalloutPopover', () => { ...@@ -197,16 +177,12 @@ describe('PaidFeatureCalloutPopover', () => {
it('tracks that the popover has been shown', () => { it('tracks that the popover has been shown', () => {
expect(trackingSpy).toHaveBeenCalledWith(undefined, 'popover_shown', { expect(trackingSpy).toHaveBeenCalledWith(undefined, 'popover_shown', {
label: 'feature_highlight_popover:some feature', label: 'feature_highlight_popover:some feature',
property: 'experiment:highlight_paid_features_during_active_trial', property: trackingExperimentKey,
}); });
}); });
}); });
describe('onResize', () => { describe('onResize', () => {
beforeEach(() => {
wrapper = createComponent();
});
it.each` it.each`
bp | disabled bp | disabled
${'xs'} | ${'true'} ${'xs'} | ${'true'}
......
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