Passed
Push — trunk ( 37af04...c71cc1 )
by Christian
13:15 queued 12s
created

sw-modal.spec.ts ➔ createWrapper   A

Complexity

Conditions 1

Size

Total Lines 15
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 12
dl 0
loc 15
rs 9.8
c 0
b 0
f 0
1
import 'src/app/component/base/sw-modal';
2
import { shallowMount } from '@vue/test-utils';
3
import type { Wrapper } from '@vue/test-utils';
4
import type Vue from 'vue';
5
6
async function createWrapper(): Promise<Wrapper<Vue>> {
7
    return shallowMount(await Shopware.Component.build('sw-modal'), {
8
        stubs: {
9
            'sw-icon': true
10
        },
11
        provide: {
12
            shortcutService: {
13
                startEventListener: () => {},
14
                stopEventListener: () => {}
15
            },
16
        },
17
        attachTo: document.body,
18
        slots: {
19
            default: `
20
                <div class="modal-content-stuff">
21
                    Some content inside modal body
22
                    <input name="test" class="test-input">
23
                </div>
24
            `
25
        }
26
    });
27
}
28
29
describe('src/app/component/base/sw-modal/index.js', () => {
30
    let wrapper: Wrapper<Vue>;
31
32
    beforeEach(async () => {
33
        wrapper = await createWrapper();
34
35
        await flushPromises();
36
    });
37
38
    afterEach(async () => {
39
        if (wrapper) {
40
            await wrapper.destroy();
41
        }
42
43
        await flushPromises();
44
    });
45
46
    it('should be a Vue.js component', () => {
47
        expect(wrapper.vm).toBeTruthy();
48
    });
49
50
    it('should render modal with body', async () => {
51
        await wrapper.setProps({
52
            title: 'Cool modal'
53
        });
54
55
        expect(wrapper.get('.sw-modal__body').text()).toBe('Some content inside modal body');
56
        expect(wrapper.get('.sw-modal__title').text()).toBe('Cool modal');
57
    });
58
59
    it('should show console error when using invalid variant', async () => {
60
        const consoleSpy = jest.spyOn(console, 'error').mockImplementation();
61
62
        await wrapper.setProps({
63
            variant: 'not-existing' // Make some trouble
64
        });
65
66
        expect(consoleSpy).toHaveBeenCalledTimes(1);
67
    });
68
69
    it.each([
70
        'default',
71
        'small',
72
        'large',
73
        'full'
74
    ])('should set correct variant class for %s', async (variant) => {
75
        await wrapper.setProps({
76
            variant: variant
77
        });
78
79
        expect(wrapper.get('.sw-modal').classes(`sw-modal--${variant}`)).toBe(true);
80
    });
81
82
    it('should fire the close event when clicking the close button', async () => {
83
        await wrapper.get('.sw-modal__close').trigger('click');
84
85
        expect(wrapper.emitted('modal-close').length).toBe(1);
86
    });
87
88
    it('should close the modal when clicking outside modal', async () => {
89
        await wrapper.get('.sw-modal').trigger('mousedown');
90
91
        expect(wrapper.emitted('modal-close').length).toBe(1);
92
    });
93
94
    it('should not close the modal when clicking outside modal and closeable option is false', async () => {
95
        await wrapper.setProps({
96
            closable: false
97
        });
98
99
        await wrapper.get('.sw-modal').trigger('mousedown');
100
101
        expect(wrapper.emitted('modal-close')).toBeUndefined();
102
    });
103
104
    it('should close the modal when using ESC key', async () => {
105
        await wrapper.get('.sw-modal__dialog').trigger('keyup.esc');
106
107
        expect(wrapper.emitted('modal-close').length).toBe(1);
108
    });
109
110
    it('should not close the modal when using ESC key when the event does not come from the modal dialog', async () => {
111
        await wrapper.get('.test-input').trigger('keyup.esc');
112
113
        expect(wrapper.emitted('modal-close')).toBeUndefined();
114
    });
115
});
116