/** 
 | 
 * @file        S&OP Chart component to wrap common methods the team encounter during development 
 | 
 * @description Chart class extending e2e's Chart. 
 | 
 * All S&OP page objects inherit from our own class (inheriting e2e/libappbase), but we can propose common methods to them. 
 | 
 * @author      Clarence (clarence.chan@3ds.com) 
 | 
 * @copyright   Dassault Systèmes 
 | 
 */ 
 | 
import { ElementFinder } from '../e2elib/node_modules/protractor/built'; 
 | 
import { Chart } from '../e2elib/lib/src/pageobjects/chart/chart.component'; 
 | 
import { ChartData } from '../e2elib/lib/src/pageobjects/chart/chartdata.component'; 
 | 
import { ChartSeriesSOP } from './chartseriessop'; 
 | 
  
 | 
export class ChartSOP extends Chart { 
 | 
  public constructor(componentPath: string, isCustomPath?: boolean, elementObj?: ElementFinder) { 
 | 
    super(componentPath, isCustomPath, elementObj); 
 | 
  } 
 | 
  /** 
 | 
   * Verify the chart title 
 | 
   * 
 | 
   * @param chartTitle Chart Title 
 | 
   */ 
 | 
  public async verifyChartTitle(chartTitle: string): Promise<void> { 
 | 
    try { 
 | 
      const chartTitleActual = await this.getChartTitle(); 
 | 
      expect(chartTitleActual).toBe(chartTitle, stepChart.incorrectChartTitle(chartTitle, chartTitleActual)); 
 | 
    } catch { 
 | 
      Error('Failed to verify chart title'); 
 | 
    } 
 | 
  } 
 | 
  
 | 
  /** 
 | 
   * Verify chart data point exists. Fails expect if data point value differs and throws error if no data point found. 
 | 
   * 
 | 
   * @param legendGroupName Legend group name. 
 | 
   * @param legendGroupItem Legend group item that forms part of the return value. 
 | 
   * @param label Label that forms part of the return value. 
 | 
   * @param expectedValue Expected y-axis value to verify. 
 | 
   */ 
 | 
  public async verifyHasDataPoint(legendGroupName: string, legendGroupItem: string, label: string, expectedValue: string, tolerance: number = 0): Promise<void> { 
 | 
    if (tolerance < 0) { 
 | 
      throw Error(`Tolerance (if defined) must be positive number. Current defined tolerance ${tolerance} when verifying data point for group = ${legendGroupName}, item = ${legendGroupItem}, label = ${label}.`); 
 | 
    } 
 | 
  
 | 
    try { 
 | 
      const chartValues = await this.getChartDataValue(legendGroupName, [legendGroupItem], [label]); 
 | 
      const actualValue = chartValues[0].values[0]; 
 | 
      const valueWithinTolerance = Math.abs(Number(actualValue) - Number(expectedValue)) <= tolerance; 
 | 
  
 | 
      const toleranceMsg = tolerance > 0 ? ` (with tolerance +/- ${tolerance})` : ''; // If tolerance defined, output as part of failure message 
 | 
      expect(valueWithinTolerance).toBe( 
 | 
        true, 
 | 
        `Verify data point fail for group = ${legendGroupName}, item = ${legendGroupItem}, label = ${label}. Expected data point = ${expectedValue}, actual = ${actualValue}${toleranceMsg}.`); 
 | 
    } catch { 
 | 
      throw Error(`Cannot find data point for group = ${legendGroupName}, item = ${legendGroupItem}, label = ${label}.`); 
 | 
    } 
 | 
  } 
 | 
  
 | 
  /** 
 | 
   * Verify chart data point does not exist. Throws error if data point found. 
 | 
   * 
 | 
   * @param legendGroupName Legend group name. 
 | 
   * @param legendGroupItem Legend group item that forms part of the return value. 
 | 
   * @param label Label that forms part of the return value. 
 | 
   */ 
 | 
  public async verifyHasNoDataPoint(legendGroupName: string, legendGroupItem: string, label: string): Promise<void> { 
 | 
    let chartValues: ChartData[]; 
 | 
    try { 
 | 
      chartValues = await this.getChartDataValue(legendGroupName, [legendGroupItem], [label]); 
 | 
    } catch { 
 | 
      return; // Catch error and return since we are expecting no data point 
 | 
    } 
 | 
  
 | 
    throw Error(`Expected no data point for group = ${legendGroupName}, item = ${legendGroupItem}, label = ${label}, but found y-axis value = ${chartValues[0].values[0]}.`); 
 | 
  } 
 | 
  
 | 
  /** 
 | 
   * Verify the number of series under the legend 
 | 
   * 
 | 
   * @param nrOfLegend Number of series under the legend 
 | 
   */ 
 | 
  public async verifyNrOfSeries(nrOfLegend: Number): Promise<void> { 
 | 
    try { 
 | 
      const actual = (await this.getChartLegend().getLegendGroupListItems(0)).length; 
 | 
      expect (actual).toBe(nrOfLegend, stepChart.incorrectNrOfLegend(nrOfLegend, actual)); 
 | 
    } catch { 
 | 
      Error('Failed to verify Nr of Series'); 
 | 
    } 
 | 
  } 
 | 
  
 | 
  /** 
 | 
   * Verify the number of X axis label 
 | 
   * 
 | 
   * @param nrOfLabel Number of x axis labels 
 | 
   */ 
 | 
  public async verifyXAxisLabelCount(nrOfLabel: number): Promise<void> { 
 | 
    const actual = await this.getXAxisLabelsCount(); 
 | 
    expect(actual).toBe(nrOfLabel, stepChart.incorrectXAxisLabelCount(nrOfLabel, actual)); 
 | 
  } 
 | 
  
 | 
  /** 
 | 
   * Verify if the series exist, and if the series exist, check if the criteria of the series if fullfilled 
 | 
   * 
 | 
   * @param seriesName Name of the series 
 | 
   * @param verifyCriteria Extra criteria to check against the series if it exist 
 | 
   */ 
 | 
  public async verifySeries(seriesName: string, verifyCriteria?: ChartSeriesCriteria): Promise<void> { 
 | 
    const series = await ChartSeriesSOP.getChartSeries(this, seriesName); 
 | 
    expect(series).toBeDefined(stepChart.seriesNotFound); 
 | 
    if (series && verifyCriteria?.Type) { 
 | 
      const actualChartType = await this.getSeriesChartType(series.seriesIndex); 
 | 
      expect(actualChartType).toBe(verifyCriteria.Type, stepChart.incorrectSeriesChartType(seriesName, verifyCriteria.Type, actualChartType)); 
 | 
    } 
 | 
  } 
 | 
  
 | 
  /** 
 | 
   * Verify the y axis label title 
 | 
   * 
 | 
   * @param labelTitle Provided Y axis label title 
 | 
   */ 
 | 
  public async verifyYAxisLabelTitle(labelTitle: String): Promise<void> { 
 | 
    try { 
 | 
      const yAxisLabelTitle = await this.getYAxisTitle(); 
 | 
      expect(yAxisLabelTitle).toBe(labelTitle, stepChart.incorrectYAxisLabelTitle(labelTitle, yAxisLabelTitle)); 
 | 
    } catch { 
 | 
      Error('Failed to verify Y Axis Label title'); 
 | 
    } 
 | 
  } 
 | 
  
 | 
  /** 
 | 
   * Verify the x axis label title(s) 
 | 
   * 
 | 
   * @param expectedLabels Provided X axis label title(s) 
 | 
   */ 
 | 
  public async verifyXAxisLabelTitle(expectedLabels: string[]): Promise<void> { 
 | 
    try { 
 | 
      const xAxisLabelTitle = await this.getXAxisLabels(); 
 | 
      expect(xAxisLabelTitle).toEqual(expectedLabels, stepChart.incorrectXAxisLabelTitle(expectedLabels, xAxisLabelTitle)); 
 | 
    } catch { 
 | 
      Error('Failed to verify X Axis Label title'); 
 | 
    } 
 | 
  } 
 | 
  
 | 
} 
 | 
  
 | 
export interface ChartSeriesCriteria { 
 | 
  Type?: BarChartSeriesType; 
 | 
} 
 | 
  
 | 
export enum BarChartSeriesType { 
 | 
  Stacked_bar = 'stacked_bar', 
 | 
} 
 | 
  
 | 
// Step description to re-use in spec file to prevent scriptor re-write each time 
 | 
export const stepChart = { 
 | 
  getChartValues: (chartName: string, legendGroupItem: string, dataToLookup: string[]): string => `In chart "${chartName}", get chart values for "${legendGroupItem}" at "${dataToLookup.join(', ')}".`, 
 | 
  getXAxisLabelCount: (chartName: string): string => `In chart "${chartName}", get the number of X Axis label.`, 
 | 
  incorrectChartTitle: (chartTitle: any, actual: any): string => `Chart title Expected "${chartTitle}", Actual "${actual}".`, 
 | 
  incorrectNrOfLegend: (legend: Number, actual: Number): string => `Number of legend Expected "${legend}", Actual "${actual}".`, 
 | 
  incorrectSeriesChartType: (seriesName: any, chartType: any, actual: any): string => `Series "${seriesName}" Expected Chart Type "${chartType}", Actual Chart Type "${actual}".`, 
 | 
  incorrectXAxisLabelCount : (nrOfLabel: number, actual: number): string => `Expected Number of X axis Label is : "${nrOfLabel}", actual Number of X axis Label is : ${actual}.`, 
 | 
  incorrectXAxisLabelTitle: (labelTitle: any, actual: any): string => `X axis label Expected "${labelTitle}", Actual "${actual}".`, 
 | 
  incorrectYAxisLabelTitle: (labelTitle: any, actual: any): string => `Y axis label Expected "${labelTitle}", Actual "${actual}".`, 
 | 
  seriesNotFound : (seriesName: any): string => `Series "${seriesName}" is not found.`, 
 | 
  verifyChartTitle: (chartTitle: any): string => `Verify that the chart title is "${chartTitle}".`, 
 | 
  verifyHasDataPoint: (chartName: string, legendGroupItem: string, label: string, expectedValue: string): string => { 
 | 
    return `In chart "${chartName}", verify chart value for "${legendGroupItem}" at "${label}" equals "${expectedValue}".`; 
 | 
  }, 
 | 
  verifyNrOfSeries: (nrOfSeries: Number): string => `Verify the number of series in legend is "${nrOfSeries}".`, 
 | 
  verifySeries: (seriesName: any, seriestype?: any): string => `Verify Series "${seriesName}" exist with the correct series type : "${seriestype}".`, 
 | 
  verifyXAxisLabelCount: (nrOfLabel: any): string => `Verify that there are "${nrOfLabel}" X Axis Label.`, 
 | 
  verifyXAxisLabelTitle: (labelTitle: any): string => `Verify that the X Axis Label is "${labelTitle}"`, 
 | 
  verifyYAxisLabelTitle: (labelTitle: any): string => `Verify that the Y Axis Label is "${labelTitle}".`, 
 | 
}; 
 | 
  
 | 
export { stepChart as StepChart }; 
 |