/// <reference types="cypress" />

const path = require('path')

describe('transport driver', () => {
  beforeEach(() => {
    cy.fixture('globals').as('globals')
    cy.fixture('master-data/transport-driver').as('transportDriver')
    cy.visit('/domain/el-cold-chain-warehouse/transport_driver')
    cy.waitGristLoad()
  })

  it('filters the grist by name with the same letter case', function () {
    const data = this.transportDriver.nameLetterCase
    cy.get('input[name="name"]').type(`${data['name']}{enter}`)
    cy.waitOneRecord()
    cy.recordExists(data)
  })

  it('filters the grist by name with a different letter case', function () {
    const data = this.transportDriver.nameLetterCase
    cy.get('input[name="name"]').type(`${data['name'].toLowerCase()}{enter}`)
    cy.waitOneRecord()
    cy.recordExists(data)
  })

  it('filters the grist by driver code the same letter case', function () {
    const data = this.transportDriver.driverLetterCase
    cy.get('input[name="driverCode"]').type(`${data['driver code']}{enter}`)
    cy.waitOneRecord()
    cy.recordExists(data)
  })

  it('filters the grist by driver code with a different letter case', function () {
    const data = this.transportDriver.driverLetterCase
    cy.get('input[name="driverCode"]').type(`${data['driver code'].toLowerCase()}{enter}`)
    cy.waitOneRecord()
    cy.recordExists(data)
  })

  it('downloads the json, csv, xlsx, xls files', function () {
    const fileTypes = this.globals.fileTypes
    cy.exportGrist(path, fileTypes, 'transport driver')
  })

  it('imports new drivers from an xlsx file', function () {
    const data = this.transportDriver.importNew
    cy.importGrist('master-data/transportDriverImport.xlsx')
    cy.importAllPopup()
    for (const i of data) {
      cy.recordExists(i)
    }
  })

  it('imports deleted drivers from an xlsx file', function () {
    const data = this.transportDriver.importDeleted
    cy.importGrist('master-data/transportDriverDeletedImport.xlsx')
    cy.importAllPopup()
    cy.log('false positive')
    // pending fix for import function
    // code for next assertion steps goes here
    // will return false positive!
  })

  it('adds transport driver', function () {
    const data = this.transportDriver.add1
    cy.addTransportDriver(data)
    cy.clickSaveButton()
    cy.contains('span', 'data updated successfully').should('be.visible')
    cy.recordExists(data)
  })

  it('adds duplicate [name, driver code], different letter case', function () {
    const data = this.transportDriver.add2
    cy.addTransportDriver(data)
    cy.clickSaveButton()
    cy.contains('span', 'data updated successfully').should('be.visible')
    cy.recordExists(data)
  })

  it('adds duplicate [name, driver code], same letter case', function () {
    const data = this.transportDriver.add3
    cy.addTransportDriver(data)
    cy.clickSaveButton()
    cy.contains('span', 'duplicate key value violates unique constraint "ix_transport-driver_0"').should('be.visible')
  })

  it('adds duplicate name, different driver code', function () {
    const data = this.transportDriver.add4
    cy.addTransportDriver(data)
    cy.clickSaveButton()
    cy.contains('span', 'data updated successfully').should('be.visible')
    cy.recordExists(data)
  })

  it('adds different name, duplicate driver code', function () {
    const data = this.transportDriver.add5
    cy.addTransportDriver(data)
    cy.clickSaveButton()
    cy.contains('span', 'data updated successfully').should('be.visible')
    cy.recordExists(data)
  })

  it('deletes a transport driver', function () {
    const data = this.transportDriver.delete1
    cy.checkGristRecords([data.name])
    cy.clickDeleteButton()
    cy.clickPopupButton('delete')
    cy.contains('span', 'data deleted successfully').should('be.visible')
    cy.contains('span', data.name).should('not.exist')
  })

  it('deletes 3 transport drivers', function () {
    const names = this.transportDriver.deleteMultiple
    cy.checkGristRecords(names)
    cy.clickDeleteButton()
    cy.clickPopupButton('delete')
    cy.contains('span', 'data deleted successfully').should('be.visible')
    for (const name of names) {
      cy.contains('span', name).should('not.exist')
    }
  })

  it('edits a transport driver', function () {
    const data = this.transportDriver.edit
    cy.editField(data.oldVal, data.newVal)
    cy.clickSaveButton()
    cy.contains('span', 'data updated successfully').should('be.visible')
    cy.contains('span', data.newName).should('exist')
  })
})
