/** 
 | 
 * @file         ADSO-10111 
 | 
 * @description  Edit single unit cost, not unique cost encountered 
 | 
 * @testcategory Web - Financials - Units 
 | 
 * @author       Gay Er Xuan (erxuan.gay@3ds.com) 
 | 
 * @copyright    Dassault Systemes 
 | 
 */ 
 | 
import { qCustomMatcher } from '../../../e2elib/lib/src/main/qmatchers.const'; 
 | 
import { ListRow } from '../../../e2elib/lib/src/pageobjects/list/listrow.component'; 
 | 
import { asyncEvery } from '../../../libappbase/utils'; 
 | 
import { AppMP, Demo, Scenario } from '../../../libmp/appmp'; 
 | 
import { DataFoodBaseEntity, DataFoodBaseEntityName } from '../../../libmp/data/data.navigation'; 
 | 
import { startOfPlanningYear } from '../../../libmp/data/data.period'; 
 | 
import { DataFoodBaseUnitCost } from '../../../libmp/data/data.unitcost'; 
 | 
import { ListUnitCostColumn } from '../../../libmp/forms/form.unitcost'; 
 | 
  
 | 
describe('ADSO-10111 - Edit single unit cost, not unique cost encountered', () => { 
 | 
  const appMP = AppMP.getInstance(); 
 | 
  const listUnitCost = appMP.viewUnitCost.frmUnitCost.listUnitCost; 
 | 
  let dlgUnitCost = listUnitCost.dlgUnitCost; 
 | 
  let sopYear: string; 
 | 
  let unitCostRow1: ListRow | undefined; 
 | 
  let unitCostRow2: ListRow | undefined; 
 | 
  const account = DataFoodBaseUnitCost.Account; 
 | 
  const costDriver = DataFoodBaseUnitCost.CostDriver; 
 | 
  const unit = DataFoodBaseEntityName.DEFermenter; 
 | 
  const unitCostStart1String = `1-Jan-${startOfPlanningYear - 1}`; 
 | 
  let unitCostStart2: Date; 
 | 
  let unitCostStart2String = ''; 
 | 
  let editUnitCostStart = ''; 
 | 
  
 | 
  beforeAll(async () => { 
 | 
    jasmine.addMatchers(qCustomMatcher); 
 | 
    await appMP.login(); 
 | 
  }); 
 | 
  
 | 
  afterAll(async () => { 
 | 
    // Clear demo data 
 | 
    await appMP.formNavigation.reset(); 
 | 
    await appMP.cleanupAndLogout(); 
 | 
  }); 
 | 
  
 | 
  afterEach(async () => { 
 | 
    await appMP.checkToastMessage(); 
 | 
  }); 
 | 
  
 | 
  it(`Setup - ${AppMP.getDemoDataPath(Demo.Food, Scenario.Base)}`, async () => { 
 | 
    await appMP.createDemoDataset(Demo.Food, Scenario.Base, false); 
 | 
  }); 
 | 
  
 | 
  it(`Setup - Open view ${appMP.viewPeriod.viewPath}. Get sopYear and unit costs start date`, async () => { 
 | 
    // Open Periods view 
 | 
    await appMP.viewPeriod.switchTo(); 
 | 
    const formPeriod = appMP.viewPeriod.frmPeriod; 
 | 
    ({ sopYear } = await formPeriod.getStartAndEndOfPlanning()); 
 | 
    unitCostStart2 = new Date(Number(sopYear), 1, 1); 
 | 
    unitCostStart2String = `${DataFoodBaseUnitCost.Start2}-${sopYear}`; 
 | 
    editUnitCostStart = `${DataFoodBaseUnitCost.EditStart}-${sopYear}`; 
 | 
  }); 
 | 
  
 | 
  it(`Setup - Open view ${appMP.viewUnitCost.viewPath}. Verify 1 ${unit} exist with ${unitCostStart1String} as start date`, async () => { 
 | 
    // Open unit cost view 
 | 
    await appMP.viewUnitCost.switchTo(); 
 | 
    // Filter by DE Fermenter 
 | 
    await appMP.formNavigation.filterByEntity(unit, DataFoodBaseEntity.deUnitsParents); 
 | 
    // Verify 1 DE Fermenter Unit Cost exist in the list 
 | 
    await listUnitCost.waitForScreenUpdate(); 
 | 
    expect(await listUnitCost.getRowCount()).toBe(1, `Exactly one ${unit} Unit Cost should exist in the list`); 
 | 
    const unitCost = await listUnitCost.getRowByIndex(0); 
 | 
    const unitCostStart = await listUnitCost.getCellValueFromRow(ListUnitCostColumn.Start, unitCost); 
 | 
    expect(unitCostStart).toBe(unitCostStart1String, `Start of the ${unit} unit cost should be ${unitCostStart1String}`); 
 | 
  }); 
 | 
  
 | 
  it(`Setup - Create a new ${unit} unit cost with start date ${DataFoodBaseUnitCost.Start2}-(SOP Year)`, async () => { 
 | 
    // Create new DE Fermenter unit cost 
 | 
    dlgUnitCost = await listUnitCost.openEditDialog(); 
 | 
    await dlgUnitCost.updateUnitCostValue(account, costDriver, unit, unitCostStart2); 
 | 
    let btnDisabledTooltip = ''; 
 | 
    const canClickOk = await dlgUnitCost.clickOK(1000); 
 | 
    if (!canClickOk) { 
 | 
      btnDisabledTooltip = await dlgUnitCost.getOKButtonTooltip(); 
 | 
    } 
 | 
    expect(canClickOk).toBe(true, `OK button should be clickable. Ok button disabled with tooltip: ${btnDisabledTooltip}`); 
 | 
    unitCostRow2 = await listUnitCost.getUnitCostRowByValues(unit, account, costDriver, unitCostStart2String); 
 | 
    expect(unitCostRow2).toBeDefined(`New created ${unit} Unit Cost row with start date ${unitCostStart2String} should be found`); 
 | 
  }); 
 | 
  
 | 
  it(`Step 1 - Edit UnitCostRow with ${unitCostStart1String} start value to ${DataFoodBaseUnitCost.Start2}-SOPYear and verify it is blocked with expected tooltip`, async () => { 
 | 
    // Open edit dialog and update it 
 | 
    unitCostRow1 = await listUnitCost.getUnitCostRowByValues(unit, account, costDriver, unitCostStart1String); 
 | 
    expect(unitCostRow1).toBeDefined(`${unit} UnitCost with start ${unitCostStart1String} should be found`); 
 | 
    dlgUnitCost = await listUnitCost.openEditDialog(unitCostRow1); 
 | 
    await dlgUnitCost.dtsStart.setDate(unitCostStart2); 
 | 
    let btnDisabledTooltip = ''; 
 | 
    const canClickOk = await dlgUnitCost.clickOK(1000); 
 | 
    if (!canClickOk) { 
 | 
      btnDisabledTooltip = await dlgUnitCost.getOKButtonTooltip(); 
 | 
    } 
 | 
    expect(canClickOk).toBe(false, 'Ok button should be disabled as start date of UnitCost should be unique'); 
 | 
    expect(btnDisabledTooltip).toBe('Identical cost definition exists. Unit cost must be unique by Account, Cost driver and Start date.', `Feedback text should match. Current tooltip value: ${btnDisabledTooltip}`); 
 | 
  }); 
 | 
  
 | 
  it(`Step 2 - Edit UnitCostRow with ${unitCostStart1String} start value to ${DataFoodBaseUnitCost.EditStart}-SOPYear and verify UnitCost can be edited`, async () => { 
 | 
    // Update dialog 
 | 
    await dlgUnitCost.dtsStart.setDate(new Date(editUnitCostStart)); 
 | 
    let btnDisabledTooltip = ''; 
 | 
    const canClickOk = await dlgUnitCost.clickOK(1000); 
 | 
    if (!canClickOk) { 
 | 
      btnDisabledTooltip = await dlgUnitCost.getOKButtonTooltip(); 
 | 
    } 
 | 
    expect(canClickOk).toBe(true, `OK button should be clickable. Ok button disabled with tooltip: ${btnDisabledTooltip}`); 
 | 
  }); 
 | 
  
 | 
  it(`Step 3 - Verify ${unit} UnitCost(s) start date value is either ${DataFoodBaseUnitCost.EditStart}-SOPYear and ${DataFoodBaseUnitCost.Start2}-SOPYear`, async () => { 
 | 
    const dateArray = [editUnitCostStart, unitCostStart2String]; 
 | 
    const allUnitCostRows = await listUnitCost.getAllRows(); 
 | 
    const isStartDateValueCorrect = await asyncEvery(allUnitCostRows, async (row: ListRow) => { 
 | 
      const value = await listUnitCost.getCellValueFromRow(ListUnitCostColumn.Start, row); 
 | 
      return dateArray.indexOf(value) > -1; 
 | 
    }); 
 | 
    expect(isStartDateValueCorrect).toBe(true, `Start date of ${unit} UnitCost should be either ${editUnitCostStart} or ${unitCostStart2String}`); 
 | 
  }); 
 | 
}); 
 |