All files / Scripts/rules/types HeaderSectionMissingRule.ts

100% Statements 16/16
100% Branches 6/6
100% Functions 2/2
100% Lines 16/16

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61              6x     6x   6x                             25x 25x     25x 5x   20x 19x   1x       25x 25x 25x 25x                       15x   15x    
// A validation to verify that a header section exists.  The rule is of the format:
//
//  IF section is missing
//    then display error message on UI for section
//
 
import { HeaderSection, IComplexValidationRule } from "./interfaces";
import { findSectionSubSection } from "../engine/HeaderValidationRules";
 
// rule counter to assign unique rule numbers to each rule (internal number only)
let uniqueRuleNumber = 0;
 
export class HeaderSectionMissingRule implements IComplexValidationRule {
    public checkSection: string;
    public errorMessage: string;
    public errorReportingSection: string[];
    public ruleNumber: number;
    public primaryRule: boolean;
    public severity: "error" | "warning" | "info";
    public errorPattern: string;
 
    constructor(
        checkSection: string,
        errorMessage: string,
        reportSection: string | string[],
        severity: "error" | "warning" | "info"
    ) {
        this.checkSection = checkSection;
        this.errorMessage = errorMessage || "";
 
        // Make sure reporting section is an array
        if (Array.isArray(reportSection)) {
            this.errorReportingSection = reportSection;
        } else {
            if (reportSection) {
                this.errorReportingSection = [reportSection];
            } else {
                this.errorReportingSection = [];
            }
        }
 
        this.ruleNumber = ++uniqueRuleNumber;
        this.primaryRule = true;
        this.severity = severity;
        this.errorPattern = "";
    }
 
    /**
     * Determine if the rule is violated by the header sections passed in.
     * @param setOfSections - set of sections being displayed. An array of sections that are displayed on the UI,
     * where each entry in the array is an array of the portions of the header that are displayed in on that
     * section within the UI.
     * @returns true if the rule is violated (section is missing)
     */
    public violatesComplexRule(setOfSections: HeaderSection[][]): boolean {
        // FOREACH section find instance of section to look for in that group of sections
        const sectionDefinition = findSectionSubSection(setOfSections, this.checkSection);
 
        return sectionDefinition.length === 0;
    }
}