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
|
import { nextTick } from 'vue';
import { mountExtended } from 'helpers/vue_test_utils_helper';
import KeepAliveSlots from '~/vue_shared/components/keep_alive_slots.vue';
const SLOT_1 = {
slotKey: 'slot-1',
title: 'Hello 1',
};
const SLOT_2 = {
slotKey: 'slot-2',
title: 'Hello 2',
};
describe('~/vue_shared/components/keep_alive_slots.vue', () => {
let wrapper;
const createSlotContent = ({ slotKey, title }) => `
<div data-testid="slot-child" data-slot-id="${slotKey}">
<h1>${title}</h1>
<input type="text" />
</div>
`;
const createComponent = (props = {}) => {
wrapper = mountExtended(KeepAliveSlots, {
propsData: props,
slots: {
[SLOT_1.slotKey]: createSlotContent(SLOT_1),
[SLOT_2.slotKey]: createSlotContent(SLOT_2),
},
});
};
const findRenderedSlots = () =>
wrapper.findAllByTestId('slot-child').wrappers.map((x) => ({
title: x.find('h1').text(),
inputValue: x.find('input').element.value,
isVisible: x.isVisible(),
}));
describe('default', () => {
beforeEach(() => {
createComponent();
});
it('doesnt show anything', () => {
expect(findRenderedSlots()).toEqual([]);
});
describe('when slotKey is changed', () => {
beforeEach(async () => {
wrapper.setProps({ slotKey: SLOT_1.slotKey });
await nextTick();
});
it('shows slot', () => {
expect(findRenderedSlots()).toEqual([
{
title: SLOT_1.title,
isVisible: true,
inputValue: '',
},
]);
});
it('hides everything when slotKey cannot be found', async () => {
wrapper.setProps({ slotKey: '' });
await nextTick();
expect(findRenderedSlots()).toEqual([
{
title: SLOT_1.title,
isVisible: false,
inputValue: '',
},
]);
});
describe('when user intreracts then slotKey changes again', () => {
beforeEach(async () => {
wrapper.find('input').setValue('TEST');
wrapper.setProps({ slotKey: SLOT_2.slotKey });
await nextTick();
});
it('keeps first slot alive but hidden', () => {
expect(findRenderedSlots()).toEqual([
{
title: SLOT_1.title,
isVisible: false,
inputValue: 'TEST',
},
{
title: SLOT_2.title,
isVisible: true,
inputValue: '',
},
]);
});
});
});
});
describe('initialized with slotKey', () => {
beforeEach(() => {
createComponent({ slotKey: SLOT_2.slotKey });
});
it('shows slot', () => {
expect(findRenderedSlots()).toEqual([
{
title: SLOT_2.title,
isVisible: true,
inputValue: '',
},
]);
});
});
});
|