import { expect } from "chai"
import Home from "../pages/home.page"
import MyOrg from "../pages/myOrg.page"

describe("My organization page", () => {
  beforeEach("Open the my organization page", () => {
    Home.openAsAdminUser()
    const myOrgUrl = Home.myOrgLink.getAttribute("href")
    MyOrg.openAsAdminUser(myOrgUrl)
  })

  describe("When checking the report statistics part of the page", () => {
    it("Should see different types of fields statistics", () => {
      MyOrg.engagementDateStatistics.waitForDisplayed()
      const daysWithEvent = MyOrg.engagementDateStatistics.$$(
        ".fc-event-container"
      )
      expect(daysWithEvent.length).to.equal(1)

      MyOrg.locationStatistics.waitForDisplayed()

      MyOrg.engagementStatus.waitForDisplayed()
      const engagementStatusLegend = MyOrg.engagementStatus
        .$(".pieLegend")
        .$$("span")
      expect(engagementStatusLegend.length).to.equal(4)

      MyOrg.tasks.waitForDisplayed()
      const tasksBars = MyOrg.tasks.$("svg g").$$(".bars-group")
      expect(tasksBars.length).to.equal(4)
      let countTasksBars = 0
      tasksBars.forEach(bar => {
        if (bar.$("rect").getAttribute("data-tip").includes("<p>1</p>")) {
          countTasksBars++
        }
      })
      expect(countTasksBars).to.equal(3)

      MyOrg.trainingEvent.waitForDisplayed()
      const trainingEventLegend = MyOrg.trainingEvent.$(".pieLegend").$$("span")
      expect(trainingEventLegend.length).to.equal(3)
      const trainignEventTotal = MyOrg.trainingEvent.$("svg g text")
      expect(trainignEventTotal.getText()).to.equal("1")

      MyOrg.numberTrained.waitForDisplayed()
      const numberTrained = MyOrg.numberTrained.$("div em")
      expect(numberTrained.getText()).to.equal("Not specified")
    })
  })
})

@@ -474,206 +474,554 @@
     
-MyOrg.engagementDateStatistics.waitForDisplayed()%0A      const daysWithEvent = MyOrg.engagementDateStatistics.$$(%0A        %22.fc-event-container%22%0A      )%0A      expect(daysWithEvent.length).to.equal(1)%0A
+// Note: checks like length.above are being made because the wdio tests%0A      // might also be run after the jacocoTestReport, without resetting the%0A      // database, and in that case we have more reports in the statistics%0A      MyOrg.engagementDateStatistics.waitForDisplayed()%0A      const daysWithEvent = MyOrg.engagementDateStatistics.$$(%0A        %22.fc-event-container%22%0A      )%0A      // There is at least one date with events in the calendar%0A      expect(daysWithEvent).to.have.length.above(0)%0A%0A      // Location statistics map is being loaded
 %0A   
@@ -1108,32 +1108,79 @@
 tForDisplayed()%0A
+      // There are 4 engagement status options%0A
       const enga
@@ -1303,33 +1303,32 @@
 usLegend
-.length).to.equal
+).to.have.length
 (4)%0A%0A   
@@ -1353,32 +1353,73 @@
 tForDisplayed()%0A
+      // There are 4 tasks on the x-axis%0A
       const task
@@ -1419,20 +1419,16 @@
 st tasks
-Bars
  = MyOrg
@@ -1485,29 +1485,24 @@
 asks
-Bars.length).to.equal
+).to.have.length
 (4)%0A
@@ -1541,20 +1541,16 @@
    tasks
-Bars
 .forEach
@@ -1571,16 +1571,17 @@
     if (
++
 bar.$(%22r
@@ -1604,39 +1604,20 @@
 te(%22
-data-tip%22).includes(%22%3Cp%3E1%3C/p%3E%22)
+height%22) %3E 0
 ) %7B%0A
@@ -1662,16 +1662,51 @@
      %7D)%0A
+      // There is at least one bar%0A
       ex
@@ -1729,23 +1729,26 @@
 ars).to.
-equal(3
+be.above(0
 )%0A%0A     
@@ -1779,32 +1779,76 @@
 tForDisplayed()%0A
+      // There are 3 training event options%0A
       const trai
@@ -1948,28 +1948,77 @@
 gend
-.length).to.equal(3)
+).to.have.length(3)%0A      // Total number of reports is bigger than 0
 %0A   
@@ -2092,24 +2092,25 @@
      expect(
++
 trainignEven
@@ -2134,17 +2134,18 @@
 .to.
-equal(%221%22
+be.above(0
 )%0A%0A 

Fix myOrg wdio tests