-
Notifications
You must be signed in to change notification settings - Fork 334
Expand file tree
/
Copy pathCard.test.ts
More file actions
104 lines (74 loc) · 3.53 KB
/
Card.test.ts
File metadata and controls
104 lines (74 loc) · 3.53 KB
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
import { shallowMount } from '@vue/test-utils';
import Card from '@shell/components/Resource/Detail/Card/index.vue';
const mockPush = jest.fn();
jest.mock('vue-router', () => ({ useRouter: () => ({ push: mockPush }) }));
describe('component: Card/index', () => {
it('should render title and default slot', () => {
const title = 'title';
const content = 'content';
const wrapper = shallowMount(Card, { props: { title }, slots: { default: content } });
expect(wrapper.find('.title').text()).toStrictEqual(title);
});
it('should allow you to override the heading with slot', () => {
const title = 'title';
const content = 'content';
const wrapper = shallowMount(Card, { props: { title }, slots: { heading: content } });
expect(wrapper.find('.title').exists()).toBeFalsy();
expect(wrapper.find('.heading').text()).toStrictEqual(content);
});
it('should allow you to override the title with slot', () => {
const title = 'title';
const content = 'content';
const wrapper = shallowMount(Card, { props: { title }, slots: { title: content } });
expect(wrapper.find('.title').text()).toStrictEqual(content);
});
it('should allow you to insert heading-action with slot', () => {
const content = '<div id="test">content</div>';
const wrapper = shallowMount(Card, { slots: { 'heading-action': content } });
expect(wrapper.find('.heading #test').exists()).toBeTruthy();
});
it('should hide heading when no title or heading slots are provided', () => {
const wrapper = shallowMount(Card, { slots: { default: 'body' } });
expect(wrapper.find('.heading').exists()).toBeFalsy();
});
describe('click navigation', () => {
const to = { name: 'test-route', params: { id: '1' } };
beforeEach(() => {
mockPush.mockClear();
});
it('should navigate when card body is clicked and to prop is set', async() => {
const wrapper = shallowMount(Card, { props: { to }, slots: { default: '<span>content</span>' } });
await wrapper.find('.body span').trigger('click');
expect(mockPush).toHaveBeenCalledWith(to);
});
it('should not navigate when no to prop is set', async() => {
const wrapper = shallowMount(Card, { props: { title: 'test' }, slots: { default: '<span>content</span>' } });
await wrapper.find('.body span').trigger('click');
expect(mockPush).not.toHaveBeenCalled();
});
it('should not navigate when clicking on a child anchor element', async() => {
const wrapper = shallowMount(Card, {
props: { to },
slots: { default: '<a href="#" class="inner-link">link</a>' },
});
await wrapper.find('.inner-link').trigger('click');
expect(mockPush).not.toHaveBeenCalled();
});
it('should not navigate when clicking on a child button element', async() => {
const wrapper = shallowMount(Card, {
props: { to },
slots: { default: '<button class="inner-btn">btn</button>' },
});
await wrapper.find('.inner-btn').trigger('click');
expect(mockPush).not.toHaveBeenCalled();
});
it('should add clickable class when to prop is set', () => {
const wrapper = shallowMount(Card, { props: { to }, slots: { default: 'body' } });
expect(wrapper.find('.detail-card').classes()).toContain('clickable');
});
it('should not add clickable class when to prop is not set', () => {
const wrapper = shallowMount(Card, { slots: { default: 'body' } });
expect(wrapper.find('.detail-card').classes()).not.toContain('clickable');
});
});
});