From 3586da64b7ce64540977d781623d456456ecb8b8 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fran=C3=A7ois=20Grand?= <francois.grand@inrae.fr>
Date: Fri, 8 Apr 2022 11:34:16 +0200
Subject: [PATCH] test: check fields are empty on creation in structure
 calculator

refs #480
---
 e2e/calculator.po.ts                  |   4 +
 e2e/ouvrages-empty-fields.e2e-spec.ts | 115 ++++++++++++++++++++++++++
 e2e/preferences.po.ts                 |   8 ++
 jalhyd_branch                         |   2 +-
 4 files changed, 128 insertions(+), 1 deletion(-)
 create mode 100644 e2e/ouvrages-empty-fields.e2e-spec.ts

diff --git a/e2e/calculator.po.ts b/e2e/calculator.po.ts
index 1194295c9..4be637886 100644
--- a/e2e/calculator.po.ts
+++ b/e2e/calculator.po.ts
@@ -67,6 +67,10 @@ export class CalculatorPage {
         return element(by.css("fieldset-container .hyd-window-btns button.add-structure"));
     }
 
+    getCopyStructureButton() {
+        return element(by.css("fieldset-container .hyd-window-btns button.copy-structure"));
+    }
+
     getAllLinkButtons() {
         return element.all(by.css("mat-button-toggle.radio_link"));
     }
diff --git a/e2e/ouvrages-empty-fields.e2e-spec.ts b/e2e/ouvrages-empty-fields.e2e-spec.ts
new file mode 100644
index 000000000..9e8f1b576
--- /dev/null
+++ b/e2e/ouvrages-empty-fields.e2e-spec.ts
@@ -0,0 +1,115 @@
+import { ListPage } from "./list.po";
+import { browser, by, element } from "protractor";
+import { CalculatorPage } from "./calculator.po";
+import { AppPage } from "./app.po";
+
+/**
+ * Check that created/cloned structures have empty fields when
+ * "empty fields on calculator creation" is enabled
+ */
+describe("ngHyd - check that created/cloned structures have empty fields - ", () => {
+    //let prefPage: PreferencesPage;
+    let startPage: AppPage;
+    let listPage: ListPage;
+    let calcPage: CalculatorPage;
+
+    beforeAll(() => {
+        //prefPage = new PreferencesPage();
+        startPage = new AppPage();
+        listPage = new ListPage();
+        calcPage = new CalculatorPage();
+    });
+
+    beforeEach(async () => {
+        // enable evil option "empty fields on module creation"
+        // await prefPage.navigateTo();
+        // await prefPage.enableEvilEmptyFields(); // message "stale element reference: element is not attached to the page document"
+        // await browser.sleep(200);
+
+        // assume that "empty fields on module creation" is true by default
+
+        // start page
+        await startPage.navigateTo();
+        await browser.sleep(200);
+
+        // open structures calculator
+        await listPage.clickMenuEntryForCalcType(8);
+        await browser.sleep(200);
+    });
+
+    /**
+     * check that a input set is in a given (empty/filled) state
+     * @param inputIds id of the inputs to check
+     * @param emptys empty/not empty state array
+     */
+    async function checkFields(inputIds: string[], emptys: boolean[]) {
+        let n = 0;
+        for (const id of inputIds) {
+            const inp = await calcPage.getInputById(id);
+            const txt = await inp.getAttribute("value");
+            expect(txt === "").toEqual(emptys[n]);
+            n++;
+        }
+    }
+
+    it("when a structure calculator is created", async () => {
+        // check 1st structure empty fields
+        const inputIds = ["Q", "calc_Z1", "Z2", "0_ZDV", "0_L", "0_W", "0_CdGR"];
+        const emptys = [true, false, true, true, true, true, false];
+        await checkFields(inputIds, emptys);
+
+        // change 1st structure type to rectangular weir
+        const structSelect = calcPage.getSelectById("select_structure");
+        await calcPage.changeSelectValue(structSelect, 1);
+        await browser.sleep(200);
+
+        // check 1st structure empty fields
+        const inputIds2 = ["Q", "calc_Z1", "Z2", "0_ZDV", "0_L", "0_CdWR"];
+        const emptys2 = [true, false, true, true, true, false];
+        await checkFields(inputIds2, emptys2);
+    });
+
+    it("when a structure is added", async () => {
+        // add structure
+        const addStruct = calcPage.getAddStructureButton();
+        await addStruct.click();
+
+        // check 2nd structure empty fields
+        const inputIds = ["1_ZDV", "1_L", "1_W", "1_CdGR"];
+        const emptys = [true, true, true, false];
+        await checkFields(inputIds, emptys);
+    });
+
+    it("when a structure is copied", async () => {
+        // copy structure
+        const addStruct = calcPage.getCopyStructureButton();
+        await addStruct.click();
+
+        // check 2nd structure empty fields
+        const inputIds = ["1_ZDV", "1_L", "1_W", "1_CdGR"];
+        const emptys = [true, true, true, false];
+        await checkFields(inputIds, emptys);
+    });
+
+    it("when a modified structure is copied", async () => {
+        // change 1st structure type to rectangular weir
+        const structSelect = calcPage.getSelectById("select_structure");
+        await calcPage.changeSelectValue(structSelect, 1);
+        await browser.sleep(200);
+
+        // copy structure
+        const addStruct = calcPage.getCopyStructureButton();
+        await addStruct.click();
+        await browser.sleep(200);
+
+        // change 2nd structure type to rectangular gate
+        const structSelect2 = element(by.className("ng-tns-c3-47"));
+        await calcPage.changeSelectValue(structSelect2, 4);
+        await browser.sleep(200);
+
+        // check empty fields
+        const inputIds = ["1_ZDV", "1_L", "1_W", "1_CdGR"];
+        const emptys = [true, true, true, false];
+        await checkFields(inputIds, emptys);
+    });
+});
diff --git a/e2e/preferences.po.ts b/e2e/preferences.po.ts
index 016d2da96..ff9ff36ae 100644
--- a/e2e/preferences.po.ts
+++ b/e2e/preferences.po.ts
@@ -41,6 +41,14 @@ export class PreferencesPage {
         await option.click();
     }
 
+    async enableEvilEmptyFields() {
+        const cb = this.getEmptyFieldsCheckbox();
+        const underlyingCB = cb.element(by.css(`input.mat-checkbox-input`));
+        if (!underlyingCB.isSelected()) {
+            await cb.click();
+        }
+    }
+
     async disableEvilEmptyFields() {
         const cb = this.getEmptyFieldsCheckbox();
         const underlyingCB = cb.element(by.css(`input.mat-checkbox-input`));
diff --git a/jalhyd_branch b/jalhyd_branch
index 084f9effb..626e97d71 100644
--- a/jalhyd_branch
+++ b/jalhyd_branch
@@ -1 +1 @@
-300-supprimer-le-champ-_valuemode-dans-paramdefinition
\ No newline at end of file
+devel
\ No newline at end of file
-- 
GitLab