1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
|
import { GlButton } from '@gitlab/ui';
import { shallowMount } from '@vue/test-utils';
import SettingsBlock from '~/vue_shared/components/settings/settings_block.vue';
describe('Settings Block', () => {
let wrapper;
const mountComponent = (propsData) => {
wrapper = shallowMount(SettingsBlock, {
propsData,
slots: {
title: '<div data-testid="title-slot"></div>',
description: '<div data-testid="description-slot"></div>',
default: '<div data-testid="default-slot"></div>',
},
});
};
afterEach(() => {
wrapper.destroy();
});
const findDefaultSlot = () => wrapper.find('[data-testid="default-slot"]');
const findTitleSlot = () => wrapper.find('[data-testid="title-slot"]');
const findDescriptionSlot = () => wrapper.find('[data-testid="description-slot"]');
const findExpandButton = () => wrapper.findComponent(GlButton);
const findSectionTitleButton = () => wrapper.find('[data-testid="section-title-button"]');
const expectExpandedState = ({ expanded = true } = {}) => {
const settingsExpandButton = findExpandButton();
expect(wrapper.classes('expanded')).toBe(expanded);
expect(settingsExpandButton.text()).toBe(
expanded ? SettingsBlock.i18n.collapseText : SettingsBlock.i18n.expandText,
);
expect(settingsExpandButton.attributes('aria-label')).toBe(
expanded ? SettingsBlock.i18n.collapseAriaLabel : SettingsBlock.i18n.expandAriaLabel,
);
};
it('renders the correct markup', () => {
mountComponent();
expect(wrapper.element).toMatchSnapshot();
});
it('has a default slot', () => {
mountComponent();
expect(findDefaultSlot().exists()).toBe(true);
});
it('has a title slot', () => {
mountComponent();
expect(findTitleSlot().exists()).toBe(true);
});
it('has a description slot', () => {
mountComponent();
expect(findDescriptionSlot().exists()).toBe(true);
});
describe('slide animation behaviour', () => {
it('is animated by default', () => {
mountComponent();
expect(wrapper.classes('no-animate')).toBe(false);
});
it.each`
slideAnimated | noAnimatedClass
${true} | ${false}
${false} | ${true}
`(
'sets the correct state when slideAnimated is $slideAnimated',
({ slideAnimated, noAnimatedClass }) => {
mountComponent({ slideAnimated });
expect(wrapper.classes('no-animate')).toBe(noAnimatedClass);
},
);
});
describe('expanded behaviour', () => {
it('is collapsed by default', () => {
mountComponent();
expectExpandedState({ expanded: false });
});
it('adds expanded class when the expand button is clicked', async () => {
mountComponent();
await findExpandButton().vm.$emit('click');
expectExpandedState({ expanded: true });
});
it('adds expanded class when the section title is clicked', async () => {
mountComponent();
await findSectionTitleButton().trigger('click');
expectExpandedState({ expanded: true });
});
describe('when `collapsible` is `false`', () => {
beforeEach(() => {
mountComponent({ collapsible: false });
});
it('does not render clickable section title', () => {
expect(findSectionTitleButton().exists()).toBe(false);
});
it('contains expanded class', () => {
expect(wrapper.classes('expanded')).toBe(true);
});
it('does not render expand toggle button', () => {
expect(findExpandButton().exists()).toBe(false);
});
});
});
});
|