/*
* ============LICENSE_START=======================================================
* Copyright (C) 2020-2022 Nordix Foundation
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
* ============LICENSE_END=========================================================
*/
const mod = require('../ApexPolicyEditForm_State');
const contextAlbum = {
name: 'testName',
version: '0.0.1',
scope: 'test',
uuid: 'testUUID',
description: 'testDesc',
writeable: true
};
const contextSchema = {
name: 'testName',
version: '0.0.1',
schemaFlavour: 'testFlav',
schemaDefinition: 'testDef',
uuid: 'testUUID',
description: 'testDesc'
}
const task = {
key: {
name: 'testName',
version: 'testVersion'
},
uuid: 'testUUID'
};
const event = {
key: {
name: 'testName',
version: '0.0.1',
},
nameSpace: 'test',
source: 'test',
target: 'test',
uuid: 'test',
description: 'test',
}
const policy = {
policyKey: {
name: 'testName',
version: 'testVersion',
uuid: 'testUUID'
},
uuid: 'testUUID',
state: [{ key: 'testName1', value: '1'}, { key: 'testName2',value: '2'}]
}
const state = {
trigger: {
name: 'testName',
version: '0.0.1',
},
defaultTask: {
name: 'deftestName',
version: '0.0.2',
},
taskReferences: [{key: { name: 'testName1',version: '1'}, key: { name: 'testName2',version: '2'}}],
stateOutputs : [{key: { name: 'testName1',version: '1'}, key: { name: 'testName2',version: '2'}}],
stateFinalizerLogicMap : [{key: {value: { logic: 'testName1', logicFlavour : '1'}}, key: {value: { logic: 'testName1', logicFlavour : '1'}}}]
}
const parentTBody = document.createElement('table');
test('Test editPolicyForm_State_generateStateDiv CREATE', () => {
const mock_editPolicyForm_State_generateStateDiv = jest.fn(mod.editPolicyForm_State_generateStateDiv);
mock_editPolicyForm_State_generateStateDiv('CREATE', policy, 'stateName', state, task, event, contextAlbum, contextSchema);
expect(mock_editPolicyForm_State_generateStateDiv).toBeCalledWith('CREATE', policy, 'stateName', state, task, event, contextAlbum, contextSchema);
});
test('Test editPolicyForm_State_generateStateDiv VIEW', () => {
const mock_editPolicyForm_State_generateStateDiv = jest.fn(mod.editPolicyForm_State_generateStateDiv);
mock_editPolicyForm_State_generateStateDiv('VIEW', policy, 'stateName', state, task, event, contextAlbum, contextSchema);
expect(mock_editPolicyForm_State_generateStateDiv).toBeCalledWith('VIEW', policy, 'stateName', state, task, event, contextAlbum, contextSchema);
});
test('Test editPolicyForm_State_generateStateDiv EDIT', () => {
const mock_editPolicyForm_State_generateStateDiv = jest.fn(mod.editPolicyForm_State_generateStateDiv);
mock_editPolicyForm_State_generateStateDiv('EDIT', policy, 'stateName', state, task, event, contextAlbum, contextSchema);
expect(mock_editPolicyForm_State_generateStateDiv).toBeCalledWith('EDIT', policy, 'stateName', state, task, event, contextAlbum, contextSchema);
});
test('Test editPolicyForm_State_addStateLogicOutput', () => {
const mock_editPolicyForm_State_addStateLogicOutput = jest.fn(mod.editPolicyForm_State_addStateLogicOutput);
mock_editPolicyForm_State_addStateLogicOutput(parentTBody, false, 'stateName', state, 'outputName', 'logic', 'flavour');
expect(mock_editPolicyForm_State_addStateLogicOutput).toBeCalledWith(parentTBody, false, 'stateName', state, 'outputName', 'logic', 'flavour');
});
test('Test editPolicyForm_State_addStateDirectOutput', () => {
const mock_editPolicyForm_State_addStateDirectOutput = jest.fn(mod.editPolicyForm_State_addStateDirectOutput);
mock_editPolicyForm_State_addStateDirectOutput(parentTBody, false, 'stateName', state, 'outputName', state, event, 'options', event);
expect(mock_editPolicyForm_State_addStateDirectOutput).toBeCalledWith(parentTBody, false, 'stateName', state, 'outputName', state, event, 'options', event);
});
test('Test editPolicyForm_State_addPolicyContext', () => {
const mock_editPolicyForm_State_addPolicyContext = jest.fn(mod.editPolicyForm_State_addPolicyContext);
mock_editPolicyForm_State_addPolicyContext(parentTBody, false, 'stateName', 'contextName', 'ref', contextAlbum);
expect(mock_editPolicyForm_State_addPolicyContext).toBeCalledWith(parentTBody, false, 'stateName', 'contextName', 'ref', contextAlbum);
});
test('Test editPolicyForm_State_addPolicyTask', () => {
const mock_editPolicyForm_State_addPolicyTask = jest.fn(mod.editPolicyForm_State_addPolicyTask);
mock_editPolicyForm_State_addPolicyTask(parentTBody, false, false, state, 'stateName', 'ref', task, 'options');
expect(mock_editPolicyForm_State_addPolicyTask).toBeCalledWith(parentTBody, false, false, state, 'stateName', 'ref', task, 'options');
});
test('Test editPolicyForm_State_getStateBean StateName is Null', () => {
jest.spyOn(global.console, 'error').mockImplementation(() => {});
mod.editPolicyForm_State_getStateBean(null);
expect(console.error).toHaveBeenCalledTimes(2);
mod.editPolicyForm_State_getStateBean('FakeState');
expect(console.error).toHaveBeenCalledTimes(4);
global.console.error.mockRestore();
});
test('Test editPolicyForm_State_getLogicOutputMappingOptions with page', () => {
document.documentElement.innerHTML = '
' +
'' +
'';
let documentSpy = jest.spyOn(document, 'getElementById');
let elementMock = document.createElement("editPolicyFormLogicOutputsTable_stateName");
elementMock.value = "localName";
elementMock.rows = document.getElementById("editPolicyFormLogicOutputsTable_stateName").rows;
documentSpy.mockReturnValue(elementMock);
const mock_activate = jest.fn(mod.editPolicyForm_State_getLogicOutputMappingOptions);
mock_activate('stateName');
expect(mock_activate).toBeCalledWith('stateName');
});
test('Test editPolicyForm_State_getStateBean with page', () => {
document.documentElement.innerHTML = '' +
'' +
'' +
'' +
'cell 1 | cell 2 |
' +
'cell 3 | cell4 |
' +
'cell 5 | cell6 |
' +
'
' +
'' +
'';
let documentSpy = jest.spyOn(document, 'getElementById');
let elementMock = document.createElement("editPolicyFormDirOutputsTable_stateName");
elementMock.setAttribute("context_id", "a0");
elementMock.setAttribute("task_id", "d0");
elementMock.rows = document.getElementById("editPolicyFormContextsTable_stateName").rows;
elementMock.key = {"name": "name1", "version": "version1"};
elementMock.selectedOption =
{"name": "name1", "version": "version1",
"album": { "key": { "name": 'testAlbumName', "version": '0.0.1'}},
"event": { "key": { "name": 'testEventName', "version": '0.1.1'}},
"task": { "key": { "name": 'tname', "version": '1.0.0'}}};
elementMock.value = "localName";
documentSpy.mockReturnValue(elementMock);
const mock_activate = jest.fn(mod.editPolicyForm_State_getStateBean);
mock_activate('stateName');
expect(mock_activate).toBeCalledWith('stateName');
});