Passed
Push — 6.5.0.0 ( 78fa09...9b9d4d )
by Christian
14:01 queued 12s
created

src/Administration/Resources/app/administration/src/app/component/structure/sw-admin/index.spec.ts   A

Complexity

Total Complexity 2
Complexity/F 2

Size

Lines of Code 149
Function Count 1

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 125
dl 0
loc 149
rs 10
c 0
b 0
f 0
wmc 2
mnd 1
bc 1
fnc 1
bpm 1
cpm 2
noi 0

1 Function

Rating   Name   Duplication   Size   Complexity  
A index.spec.ts ➔ createWrapper 0 34 1
1
import 'src/app/component/structure/sw-admin';
2
import 'src/app/component/utils/sw-notifications';
3
import 'src/app/component/utils/sw-duplicated-media-v2';
4
import swSettingsCacheModal from 'src/module/sw-settings-cache/component/sw-settings-cache-modal';
5
import 'src/app/component/utils/sw-license-violation';
6
import 'src/app/component/structure/sw-hidden-iframes';
7
import 'src/app/component/structure/sw-modals-renderer';
8
import 'src/app/component/app/sw-app-wrong-app-url-modal';
9
import { shallowMount } from '@vue/test-utils';
10
import { BroadcastChannel } from 'worker_threads';
11
import type { Wrapper } from '@vue/test-utils';
12
import type Vue from 'vue';
13
14
Shopware.Component.register('sw-settings-cache-modal', swSettingsCacheModal);
15
async function createWrapper(isLoggedIn: boolean, forwardLogout: () => void = () => {}, route: string = 'sw.wofoo.index'): Promise<Wrapper<Vue>> {
16
    return shallowMount(await Shopware.Component.build('sw-admin'), {
17
        stubs: {
18
            'sw-notifications': await Shopware.Component.build('sw-notifications'),
19
            'sw-duplicated-media-v2': await Shopware.Component.build('sw-duplicated-media-v2'),
20
            'sw-settings-cache-modal': await Shopware.Component.build('sw-settings-cache-modal'),
21
            'sw-license-violation': await Shopware.Component.build('sw-license-violation'),
22
            'sw-hidden-iframes': await Shopware.Component.build('sw-hidden-iframes'),
23
            'sw-modals-renderer': await Shopware.Component.build('sw-modals-renderer'),
24
            'sw-app-wrong-app-url-modal': await Shopware.Component.build('sw-app-wrong-app-url-modal'),
25
            'router-view': true,
26
        },
27
        mocks: {
28
            $router: {
29
                currentRoute: {
30
                    name: route,
31
                }
32
            }
33
        },
34
        provide: {
35
            cacheApiService: {},
36
            extensionStoreActionService: {},
37
            licenseViolationService: {},
38
            userActivityService: {
39
                updateLastUserActivity: () => {
40
                    localStorage.setItem('lastActivity', 'foo');
41
                }
42
            },
43
            loginService: {
44
                isLoggedIn: () => isLoggedIn,
45
                forwardLogout
46
            }
47
        },
48
        attachTo: document.body,
49
    });
50
}
51
52
describe('src/app/component/structure/sw-admin/index.ts', () => {
53
    let wrapper: Wrapper<Vue>;
54
55
    beforeEach(() => {
56
        global.BroadcastChannel = BroadcastChannel;
57
    });
58
59
    afterEach(async () => {
60
        if (wrapper) {
61
            await wrapper.destroy();
62
        }
63
64
        await flushPromises();
65
66
        localStorage.removeItem('lastActivity');
67
    });
68
69
    it('should be a Vue.js component', async () => {
70
        wrapper = await createWrapper(false);
71
72
        expect(wrapper.vm).toBeTruthy();
73
    });
74
75
    it('should update user activity on click', async () => {
76
        wrapper = await createWrapper(false);
77
78
        const lastActivity = localStorage.getItem('lastActivity');
79
80
        const app = wrapper.find('#app');
81
        await app.trigger('mousemove');
82
83
        const newLastActivity = localStorage.getItem('lastActivity');
84
85
        expect(lastActivity).not.toBe(newLastActivity);
86
        expect(newLastActivity).toEqual('foo');
87
    });
88
89
    it('should handle session_channel message', async () => {
90
        const forwardLogout = jest.fn();
91
        wrapper = await createWrapper(false, forwardLogout);
92
93
        const channel = new BroadcastChannel('session_channel');
94
        channel.postMessage({
95
            inactive: true,
96
        });
97
98
        await flushPromises();
99
100
        expect(forwardLogout).toBeCalledTimes(1);
101
        expect(forwardLogout).toBeCalledWith(true, true);
102
        channel.close();
103
    });
104
105
    it('should not handle session_channel message with improper event data', async () => {
106
        const forwardLogout = jest.fn();
107
        wrapper = await createWrapper(false, forwardLogout);
108
109
        const channel = new BroadcastChannel('session_channel');
110
        channel.postMessage(null);
111
        channel.postMessage({});
112
113
        await flushPromises();
114
115
        expect(forwardLogout).toBeCalledTimes(0);
116
        channel.close();
117
    });
118
119
    it('should not handle session_channel message on blocked route', async () => {
120
        const forwardLogout = jest.fn();
121
        wrapper = await createWrapper(false, forwardLogout, 'sw.login.index.login');
122
123
        const channel = new BroadcastChannel('session_channel');
124
        channel.postMessage({
125
            inactive: true
126
        });
127
128
        await flushPromises();
129
130
        expect(forwardLogout).toBeCalledTimes(0);
131
        channel.close();
132
    });
133
134
    it('should not handle session_channel message on active', async () => {
135
        const forwardLogout = jest.fn();
136
        wrapper = await createWrapper(false, forwardLogout);
137
138
        const channel = new BroadcastChannel('session_channel');
139
        channel.postMessage({
140
            inactive: false
141
        });
142
143
        await flushPromises();
144
145
        expect(forwardLogout).toBeCalledTimes(0);
146
        channel.close();
147
    });
148
});
149