From 0f47b6cd44b11138d390fcc8c431738a5622a539 Mon Sep 17 00:00:00 2001
From: "arkadiusz.adamski" <aadamski@est.tech>
Date: Tue, 15 Jun 2021 11:53:55 +0100
Subject: Increase code coverage on gui

- increase code coverage on gui js

Issue-ID: POLICY-3351
Signed-off-by: arkadiusz.adamski <aadamski@est.tech>
Change-Id: I5c24e6f02c401fd1eaee819105673cfcf1c81e13
---
 .../resources/webapp/js/__test__/ApexAjax.test.js  | 141 +++++++++++++++------
 .../js/__test__/ApexKeyInformationTab.test.js      |  76 +++++++++++
 .../resources/webapp/js/__test__/ApexTable.test.js |  36 +++---
 .../resources/webapp/js/__test__/ApexUtils.test.js |  98 ++++++++++++++
 4 files changed, 295 insertions(+), 56 deletions(-)
 create mode 100644 gui-editors/gui-editor-apex/src/main/resources/webapp/js/__test__/ApexKeyInformationTab.test.js
 create mode 100644 gui-editors/gui-editor-apex/src/main/resources/webapp/js/__test__/ApexUtils.test.js

(limited to 'gui-editors/gui-editor-apex/src/main')

diff --git a/gui-editors/gui-editor-apex/src/main/resources/webapp/js/__test__/ApexAjax.test.js b/gui-editors/gui-editor-apex/src/main/resources/webapp/js/__test__/ApexAjax.test.js
index 34640c3..dadbaa0 100644
--- a/gui-editors/gui-editor-apex/src/main/resources/webapp/js/__test__/ApexAjax.test.js
+++ b/gui-editors/gui-editor-apex/src/main/resources/webapp/js/__test__/ApexAjax.test.js
@@ -1,6 +1,6 @@
 /*
  * ============LICENSE_START=======================================================
- *  Copyright (C) 2020 Nordix Foundation
+ *  Copyright (C) 2020-2021 Nordix Foundation
  *  ================================================================================
  *  Licensed under the Apache License, Version 2.0 (the "License");
  *  you may not use this file except in compliance with the License.
@@ -20,19 +20,23 @@
 const mod = require('../ApexAjax');
 
 const requestURL = "http://localhost:7979";
-const data = {
-    useHttps: 'useHttps',
-    hostname: 'hostname',
-    port: 'port',
-    username: 'username',
-    password: 'password',
-    messages: {
-        message: ''
-    },
-    content: ['01', '02'],
-    result: 'ok',
-    ok: true
-}
+let data = {};
+
+beforeEach(() => {
+    data = {
+        useHttps: 'useHttps',
+        hostname: 'hostname',
+        port: 'port',
+        username: 'username',
+        password: 'password',
+        messages: {
+            message: ''
+        },
+        content: ['01', '02'],
+        result: 'ok',
+        ok: true
+    };
+});
 
 test('Test ajax_get error', () => {
    const callback = jest.fn();
@@ -44,15 +48,68 @@ test('Test ajax_get error', () => {
    expect(mock_get_error).toHaveBeenCalled();
 });
 
-test('Test ajax_get success', () => {
-    const callback = jest.fn();
+test('Test ajax_get success', (done) => {
+    const callback = jest.fn((actualData) => {
+        expect(actualData).toEqual(data);
+        done();
+    });
     const jqXHR = { status: 200, responseText: "" };
     $.ajax = jest.fn().mockImplementation((args) => {
         args.success(data, null, jqXHR);
     });
-    const mock_get_success = jest.fn(mod.ajax_get(requestURL, callback));
-    mock_get_success();
-    expect(mock_get_success).toHaveBeenCalled();
+    mod.ajax_get(requestURL, callback);
+});
+
+test('Test ajax_getWithKeyInfo success', (done) => {
+    const myCallback = jest.fn((actual) => {
+        expect(actual).toEqual({
+            key: {
+                name: "name1",
+                version: "version1"
+            },
+            uuid: "UUID1",
+            description: "description1"
+        });
+        done();
+    });
+    data.messages = {
+        message: [
+            '{"apexKeyInfo": {"UUID": "UUID1", "description": "description1", "key":{"name": "name1", "version":' +
+            ' "version1"}}, "objectType": {"key": {"name": "name1", "version": "version1"}}}'
+        ]
+    };
+    const jqXHR = {status: 200, responseText: ""};
+
+    $.ajax = jest.fn().mockImplementation((args) => {
+        args.success(data, null, jqXHR);
+    });
+    mod.ajax_getWithKeyInfo("requestUrl", "objectType", myCallback, undefined);
+});
+
+test('Test ajax_getWithKeyInfo with custom key success', (done) => {
+    const myCallback = jest.fn((actual) => {
+        expect(actual).toEqual({
+            customKey: {
+                name: "name1",
+                version: "version1"
+            },
+            uuid: "UUID1",
+            description: "description1"
+        });
+        done();
+    });
+    data.messages = {
+        message: [
+            '{"apexKeyInfo": {"UUID": "UUID1", "description": "description1", "key":{"name": "name1",' +
+            ' "version": "version1"}}, "objectType": {"customKey": {"name": "name1", "version": "version1"}}}'
+        ]
+    };
+    const jqXHR = {status: 200, responseText: ""};
+
+    $.ajax = jest.fn().mockImplementation((args) => {
+        args.success(data, null, jqXHR);
+    });
+    mod.ajax_getWithKeyInfo("requestUrl", "objectType", myCallback, "customKey");
 });
 
 test('Test ajax_delete error', () => {
@@ -66,15 +123,16 @@ test('Test ajax_delete error', () => {
     expect(mock_delete_error).toHaveBeenCalled();
 });
 
-test('Test ajax_delete success', () => {
-    const callback = jest.fn();
+test('Test ajax_delete success', (done) => {
+    const callback = jest.fn((actualData) => {
+        expect(actualData).toEqual(data);
+        done();
+    });
     const jqXHR = { status: 200, responseText: "" };
     $.ajax = jest.fn().mockImplementation((args) => {
         args.success(data, null, jqXHR);
     });
-    const mock_delete_success = jest.fn(mod.ajax_delete(requestURL, callback));
-    mock_delete_success();
-    expect(mock_delete_success).toHaveBeenCalled();
+    mod.ajax_delete(requestURL, callback);
 });
 
 test('Test ajax_post error', () => {
@@ -88,15 +146,16 @@ test('Test ajax_post error', () => {
     expect(mock_post_error).toHaveBeenCalled();
 });
 
-test('Test ajax_post success', () => {
-    const callback = jest.fn();
+test('Test ajax_post success', (done) => {
+    const callback = jest.fn((actualData) => {
+        expect(actualData).toEqual(data);
+        done();
+    });
     const jqXHR = { status: 200, responseText: "" };
     $.ajax = jest.fn().mockImplementation((args) => {
         args.success(data, null, jqXHR);
     });
-    const mock_post_success = jest.fn(mod.ajax_post(requestURL, data, callback));
-    mock_post_success();
-    expect(mock_post_success).toHaveBeenCalled();
+    mod.ajax_post(requestURL, data, callback);
 });
 
 test('Test ajax_put error', () => {
@@ -110,16 +169,17 @@ test('Test ajax_put error', () => {
     expect(mock_put_error).toHaveBeenCalled();
 });
 
-test('Test ajax_put success', () => {
-    const callback = jest.fn();
+test('Test ajax_put success', (done) => {
+    const callback = jest.fn((actualData) => {
+        expect(actualData).toEqual(data);
+        done();
+    });
     const jqXHR = { status: 200, responseText: "" };
 
     $.ajax = jest.fn().mockImplementation((args) => {
         args.success(data, null, jqXHR);
     });
-    const mock_put_success = jest.fn(mod.ajax_put(requestURL, data, callback));
-    mock_put_success();
-    expect(mock_put_success).toHaveBeenCalled();
+    mod.ajax_put(requestURL, data, callback);
 });
 
 test('Test ajax_getOKOrFail error', () => {
@@ -133,16 +193,15 @@ test('Test ajax_getOKOrFail error', () => {
     expect(mock_getOKOrFail_error).toHaveBeenCalled();
 });
 
-test('Test ajax_getOKOrFail success', () => {
-    const callback = jest.fn();
+test('Test ajax_getOKOrFail success', (done) => {
+    const callback = jest.fn((actualData) => {
+        expect(actualData).toEqual(data);
+        done();
+    });
     const jqXHR = { status: 200, responseText: "" };
 
     $.ajax = jest.fn().mockImplementation((args) => {
         args.success(data, null, jqXHR);
     });
-    const mock_getOKOrFail_success = jest.fn(mod.ajax_getOKOrFail(requestURL, callback));
-    mock_getOKOrFail_success();
-    expect(mock_getOKOrFail_success).toHaveBeenCalled();
+    mod.ajax_getOKOrFail(requestURL, callback);
 });
-
-test.todo('Test ajax_getWithKeyInfo');
diff --git a/gui-editors/gui-editor-apex/src/main/resources/webapp/js/__test__/ApexKeyInformationTab.test.js b/gui-editors/gui-editor-apex/src/main/resources/webapp/js/__test__/ApexKeyInformationTab.test.js
new file mode 100644
index 0000000..2dcc7c2
--- /dev/null
+++ b/gui-editors/gui-editor-apex/src/main/resources/webapp/js/__test__/ApexKeyInformationTab.test.js
@@ -0,0 +1,76 @@
+/*
+ * ============LICENSE_START=======================================================
+ *  Copyright (C) 2021 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 ApexUtils = require("../ApexUtils");
+const ApexKeyInformationTab = require("../ApexKeyInformationTab");
+
+test("Test keyInformationTab_activate", () => {
+    document.body.innerHTML = '<div id ="keyInformationTab"></div>';
+    const data = {
+        useHttps: 'useHttps',
+        hostname: 'hostname',
+        port: 'port',
+        username: 'username',
+        password: 'password',
+        messages: {
+            message: [
+                '{"apexKeyInfo": {"UUID": "UUID1", "description": "description1", "key":{"name": "name1", "version":' +
+                ' "version1"}}, "objectType": {"key": {"name": "name1", "version": "version1"}}}'
+            ]
+        },
+        content: ['01', '02'],
+        result: 'ok',
+        ok: true
+    };
+    $.ajax = jest.fn().mockImplementation((args) => {
+        args.success(data, null, null);
+    });
+    ApexKeyInformationTab.keyInformationTab_activate();
+
+    const actual = document.getElementById("keyInformationTabContent");
+    const expected = /<td>name1:version1<\/td><td><uuid>UUID1<\/uuid><\/td><td><desc>description1<\/desc><\/td>/;
+    expect(actual.innerHTML).toMatch(expected);
+});
+
+test("Test keyInformationTab_deactivate", (done) => {
+    ApexUtils.apexUtils_removeElement = jest.fn((id) => {
+        expect(id).toBe("keyInformationTabContent");
+        done();
+    });
+    ApexKeyInformationTab.keyInformationTab_deactivate()
+})
+
+test("Test keyInformationTab_create, key information tab exists", () => {
+    document.body.innerHTML = '<div id ="keyInformationTabContent"></div>';
+
+    ApexKeyInformationTab.keyInformationTab_create();
+    const actual = document.getElementById("keyInformationTab");
+    expect(actual).toBeNull();
+});
+
+test("Test keyInformationTab_create, ", () => {
+    document.body.innerHTML = '<div id ="keyInformationTab"></div>';
+
+    ApexKeyInformationTab.keyInformationTab_create();
+    const actual = document.getElementById("keyInformationTabContent");
+    const expected = '<table id="keyInformationTableBody" class="apexTable ebTable elTablelib-Table-table ebTable_striped"><thead id="keyInformationTableHeader"><tr id="keyInformationTableHeaderRow"><th id="keyInformationTableKeyHeader">Key Information</th><th id="keyInformationTableUUIDHeader">UUID</th><th id="keyInformationTableDescriptionHeader">Description</th></tr></thead><tbody></tbody></table>'
+    expect(actual.innerHTML).toBe(expected);
+});
+
+
diff --git a/gui-editors/gui-editor-apex/src/main/resources/webapp/js/__test__/ApexTable.test.js b/gui-editors/gui-editor-apex/src/main/resources/webapp/js/__test__/ApexTable.test.js
index 504f84f..894d668 100644
--- a/gui-editors/gui-editor-apex/src/main/resources/webapp/js/__test__/ApexTable.test.js
+++ b/gui-editors/gui-editor-apex/src/main/resources/webapp/js/__test__/ApexTable.test.js
@@ -1,6 +1,6 @@
 /*
  * ============LICENSE_START=======================================================
- *  Copyright (C) 2020 Nordix Foundation
+ *  Copyright (C) 2020-2021 Nordix Foundation
  *  ================================================================================
  *  Licensed under the Apache License, Version 2.0 (the "License");
  *  you may not use this file except in compliance with the License.
@@ -17,21 +17,27 @@
  *  ============LICENSE_END=========================================================
  */
 
-const mod = require('../ApexTable');
+const ApexTable = require("../ApexTable")
 
-let wrapper = document.createElement("example");
-wrapper.setAttribute("id", "engineSummary_wrapper");
-wrapper.setAttribute("class", "wrapper_borderless");
+test("Test createTable", () => {
+    const expected = document.createElement("table");
+    expected.id = "my-id";
+    expected.className = "apexTable ebTable elTablelib-Table-table ebTable_striped";
 
-test('call createTable', () => {
-    const createTable = mod.createTable('01');
-    expect(createTable.getAttribute('id')).toBeDefined();
-    expect(createTable.getAttribute('class')).toBeDefined();
-    expect(createTable.getAttribute('id').valueOf()).toBe('01');
+    const actual = ApexTable.createTable(expected.id);
+    expect(actual).toEqual(actual);
 });
 
-test('test setRowHover', () => {
-    const mock = jest.fn(mod.setRowHover(wrapper));
-    mock();
-    expect(mock).toBeCalledTimes(1);
-})
\ No newline at end of file
+test("Test setRowHover", () => {
+    const element = {
+        className: null,
+        onmouseover: null,
+        onmouseout: null
+    };
+
+    ApexTable.setRowHover(element);
+
+    expect(element.className).toBe("ebTableRow");
+    expect(typeof element.onmouseover).toBe("function");
+    expect(typeof element.onmouseout).toBe("function");
+});
diff --git a/gui-editors/gui-editor-apex/src/main/resources/webapp/js/__test__/ApexUtils.test.js b/gui-editors/gui-editor-apex/src/main/resources/webapp/js/__test__/ApexUtils.test.js
new file mode 100644
index 0000000..bc331b6
--- /dev/null
+++ b/gui-editors/gui-editor-apex/src/main/resources/webapp/js/__test__/ApexUtils.test.js
@@ -0,0 +1,98 @@
+/*
+ * ============LICENSE_START=======================================================
+ *  Copyright (C) 2021 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 ApexUtils = require('../ApexUtils');
+
+afterEach(() => {
+    delete global.confirm;
+
+    document.body.innerHTML = '';
+});
+
+test('test apexUtils_areYouSure', () => {
+    const errorMsg = 'My message';
+    const returned = {};
+    global.confirm = jest
+        .fn()
+        .mockImplementation((message) => {
+            expect(message).toBe(errorMsg);
+            return returned;
+        });
+
+    const actual = ApexUtils.apexUtils_areYouSure(errorMsg);
+    expect(actual).toBe(returned);
+});
+
+test('apexUtils_emptyElement found', () => {
+    document.body.innerHTML =
+        '<div id="tested">' +
+        '  <span id="username"></span>' +
+        '  <button id="button"></button>' +
+        '</div>';
+
+    ApexUtils.apexUtils_emptyElement('tested');
+
+    expect(document.body.innerHTML).toBe('<div id="tested"></div>')
+});
+
+test('apexUtils_emptyElement found', () => {
+    const text =
+        '<div id="other">' +
+        '  <span id="username"></span>' +
+        '  <button id="button"></button>' +
+        '</div>';
+    document.body.innerHTML = text;
+    ApexUtils.apexUtils_emptyElement('tested');
+
+    expect(document.body.innerHTML).toBe(text)
+});
+
+test('apexUtils_removeElement not found', () => {
+    const expected = /<div>\s*<button id="button"><\/button>\s*<\/div>/;
+
+    document.body.innerHTML =
+        '<div>' +
+        '  <span id="tested"></span>' +
+        '  <button id="button"></button>' +
+        '</div>';
+
+    ApexUtils.apexUtils_removeElement('tested');
+    expect(document.body.innerHTML).toMatch(expected);
+});
+
+test('apexUtils_escapeHtml', () => {
+    const actual = ApexUtils.apexUtils_escapeHtml('&<ab>"\'/`=\n\t d');
+    expect(actual).toBe('&amp;&lt;ab&gt;&quot;&#39;&#x2F;`=<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;d')
+});
+
+test('createAddFormButton no text', () => {
+    const expected = document.createElement('div');
+    expected.setAttribute('class','add-field');
+    expected.innerHTML = '<i class="form-add-icon ebIcon ebIcon_add"></i><span class="form-add-text">Add</span>';
+    const actual = ApexUtils.createAddFormButton();
+    expect(actual).toEqual(expected);
+});
+
+test('createAddFormButton with text', () => {
+    const expected = document.createElement('div');
+    expected.setAttribute('class','add-field');
+    expected.innerHTML = '<i class="form-add-icon ebIcon ebIcon_add"></i><span class="form-add-text">My_text</span>';
+    const actual = ApexUtils.createAddFormButton('My_text');
+    expect(actual).toEqual(expected);
+});
-- 
cgit 1.2.3-korg