Improved Test Coverage in gui-editor-apex
[policy/gui.git] / gui-editors / gui-editor-apex / src / main / resources / webapp / js / __test__ / contextMenu.test.js
1 /*
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021 Nordix Foundation.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 const mod = require('../contextMenu');
22 const apexContextEdit = require('../ApexContextSchemaEditForm');
23 const apexEventEdit = require('../ApexEventEditForm');
24 const apexTaskEdit = require('../ApexTaskEditForm');
25 const apexPolicyEdit = require('../ApexPolicyEditForm');
26 const apexContextAlbumEdit = require('../ApexContextAlbumEditForm');
27
28 test('Test rightClickMenu', () => {
29    let documentSpy = jest.spyOn(document, 'getElementById');
30    let elementMock = document.createElement("rightClickMenu");
31    documentSpy.mockReturnValue(elementMock);
32    const event = new MouseEvent('click');
33    const mock_activate = jest.fn(mod.rightClickMenu);
34    mock_activate(event, 'type', 'name', 'version');
35    expect(mock_activate).toBeCalled();
36 });
37
38 test('Test rightClickMenuCreate when Type is CONTEXTSCHEMA', () => {
39    global.confirm = () => true
40    jest.spyOn(apexContextEdit, 'editContextSchemaForm_createContextSchema').mockReturnValueOnce(null);
41    const mock_activate = jest.fn(mod.rightClickMenuCreate);
42    mock_activate('parent', 'CONTEXTSCHEMA');
43    expect(mock_activate).toBeCalled();
44 });
45
46 test('Test rightClickMenuCreate when Type is EVENT or TASK or POLICY or CONTEXTALBUM or EMPTY', () => {
47    global.confirm = () => true
48    jest.spyOn(apexEventEdit, 'editEventForm_createEvent').mockReturnValueOnce(null);
49    const mock_activate = jest.fn(mod.rightClickMenuCreate);
50    mock_activate('parent', 'EVENT');
51    expect(mock_activate).toBeCalled();
52    mock_activate('parent', 'TASK');
53    expect(mock_activate).toBeCalled();
54    mock_activate('parent', 'POLICY');
55    expect(mock_activate).toBeCalled();
56    mock_activate('parent', 'CONTEXTALBUM');
57    expect(mock_activate).toBeCalled();
58    mock_activate('parent', '');
59    expect(mock_activate).toBeCalled();
60
61 });
62
63 test('Test rightClickMenuView when Type is CONTEXTSCHEMA or EVENT or TASK or POLICY or CONTEXTALBUM or EMPTY', () => {
64    global.confirm = () => true
65    jest.spyOn(apexContextEdit, 'editContextSchemaForm_viewContextSchema').mockReturnValueOnce(null);
66    const mock_activate = jest.fn(mod.rightClickMenuView);
67    mock_activate('parent', 'CONTEXTSCHEMA');
68    expect(mock_activate).toBeCalled();
69    mock_activate('parent', 'EVENT');
70    expect(mock_activate).toBeCalled();
71    mock_activate('parent', 'TASK');
72    expect(mock_activate).toBeCalled();
73    mock_activate('parent', 'POLICY');
74    expect(mock_activate).toBeCalled();
75    mock_activate('parent', 'CONTEXTALBUM');
76    expect(mock_activate).toBeCalled();
77    mock_activate('parent', '');
78    expect(mock_activate).toBeCalled();
79 });
80
81 test('Test rightClickMenuEdit when Type is CONTEXTSCHEMA or EVENT or TASK or POLICY or CONTEXTALBUM or EMPTY', () => {
82    global.confirm = () => true
83    jest.spyOn(apexContextEdit, 'editContextSchemaForm_viewContextSchema').mockReturnValueOnce(null);
84    const mock_activate = jest.fn(mod.rightClickMenuEdit);
85    mock_activate('parent', 'CONTEXTSCHEMA');
86    expect(mock_activate).toBeCalled();
87    mock_activate('parent', 'EVENT');
88    expect(mock_activate).toBeCalled();
89    mock_activate('parent', 'TASK');
90    expect(mock_activate).toBeCalled();
91    mock_activate('parent', 'POLICY');
92    expect(mock_activate).toBeCalled();
93    mock_activate('parent', 'CONTEXTALBUM');
94    expect(mock_activate).toBeCalled();
95    mock_activate('parent', '');
96    expect(mock_activate).toBeCalled();
97 });
98
99 test('Test rightClickMenuDelete when Type is CONTEXTSCHEMA or EVENT or TASK or POLICY or CONTEXTALBUM or EMPTY', () => {
100    jest.spyOn(apexContextEdit, 'editContextSchemaForm_deleteContextSchema').mockReturnValueOnce(null);
101    jest.spyOn(apexEventEdit, 'editEventForm_deleteEvent').mockReturnValueOnce(null);
102    jest.spyOn(apexTaskEdit, 'editTaskForm_deleteTask').mockReturnValueOnce(null);
103    jest.spyOn(apexPolicyEdit, 'editPolicyForm_deletePolicy').mockReturnValueOnce(null);
104    jest.spyOn(apexContextAlbumEdit, 'editContextAlbumForm_deleteContextAlbum').mockReturnValueOnce(null);
105    const mock_activate = jest.fn(mod.rightClickMenuDelete);
106    mock_activate('parent', 'CONTEXTSCHEMA');
107    expect(mock_activate).toBeCalled();
108    mock_activate('parent', 'EVENT');
109    expect(mock_activate).toBeCalled();
110    mock_activate('parent', 'TASK');
111    expect(mock_activate).toBeCalled();
112    mock_activate('parent', 'POLICY');
113    expect(mock_activate).toBeCalled();
114    mock_activate('parent', 'CONTEXTALBUM');
115    expect(mock_activate).toBeCalled();
116    mock_activate('parent', '');
117    expect(mock_activate).toBeCalled();
118 });