// @ts-nocheck import _ from "lodash" import { assert } from "chai" import React from "react" import TestComponent from "@mwater/react-library/lib/TestComponent" import MatrixAnswerComponent from "../src/answers/MatrixAnswerComponent" import MatrixColumnCellComponent from "../src/MatrixColumnCellComponent" import * as formUtils from "../src/formUtils" import ResponseDataValidator from "../src/ResponseDataValidator" import VisibilityCalculator from "../src/VisibilityCalculator" /** Rows of the fixed matrix used by most tests */ const PE_PIPES = "rowPePipes" const UV = "rowUv" const TANK = "rowTank" function makeItems() { return [ { id: PE_PIPES, label: { _base: "en", en: "PE Pipes" } }, { id: UV, label: { _base: "en", en: "UV treatment" } }, { id: TANK, label: { _base: "en", en: "Storage tank" } } ] } /** Dropdown column with a mix of unrestricted and row-restricted choices */ function makeStandardsColumn(overrides = {}) { return _.extend( { _id: "standard", _type: "DropdownColumnQuestion", text: { _base: "en", en: "Standard" }, validations: [], choices: [ // Unrestricted: available everywhere { id: "iso9001", label: { _base: "en", en: "ISO 9001" } }, // Restricted to a single row { id: "iso4427", label: { _base: "en", en: "ISO 4427" }, matrixRowIds: [PE_PIPES] }, { id: "nsf55", label: { _base: "en", en: "NSF/ANSI 55" }, matrixRowIds: [UV] }, // Restricted to several rows { id: "nsf61", label: { _base: "en", en: "NSF/ANSI 61" }, matrixRowIds: [PE_PIPES, TANK] } ] }, overrides ) } /** Form context fixture. InnerFormContext requires a T localizer, which the cell component uses * for the marker on a stored answer its row no longer allows. */ const formCtx = { locale: "en", T: (str: string) => str } /** Lets the async choice visibility calculation settle and React re-render */ async function settle() { for (let i = 0; i < 5; i++) { await Promise.resolve() } await new Promise((resolve) => setTimeout(resolve, 0)) } describe("Matrix row-specific choices", function () { describe("formUtils.isChoiceAllowedInMatrixRow", function () { it("allows a choice with no restriction in every row", function () { const choice = { id: "c", label: {} } assert.isTrue(formUtils.isChoiceAllowedInMatrixRow(choice, PE_PIPES)) assert.isTrue(formUtils.isChoiceAllowedInMatrixRow(choice, UV)) }) it("allows a restricted choice only in the listed rows", function () { const choice = { id: "c", label: {}, matrixRowIds: [PE_PIPES, TANK] } assert.isTrue(formUtils.isChoiceAllowedInMatrixRow(choice, PE_PIPES)) assert.isTrue(formUtils.isChoiceAllowedInMatrixRow(choice, TANK)) assert.isFalse(formUtils.isChoiceAllowedInMatrixRow(choice, UV)) }) it("allows an explicitly empty restriction in no row", function () { const choice = { id: "c", label: {}, matrixRowIds: [] } assert.isFalse(formUtils.isChoiceAllowedInMatrixRow(choice, PE_PIPES)) assert.isFalse(formUtils.isChoiceAllowedInMatrixRow(choice, UV)) }) it("does not become unrestricted when every referenced row is deleted", function () { const choice = { id: "c", label: {}, matrixRowIds: ["rowThatWasDeleted"] } assert.isFalse(formUtils.isChoiceAllowedInMatrixRow(choice, PE_PIPES)) assert.isFalse(formUtils.isChoiceAllowedInMatrixRow(choice, UV)) }) it("ignores the restriction when there is no fixed row (roster matrix)", function () { const choice = { id: "c", label: {}, matrixRowIds: [PE_PIPES] } assert.isTrue(formUtils.isChoiceAllowedInMatrixRow(choice, undefined)) assert.isTrue(formUtils.isChoiceAllowedInMatrixRow(choice, null)) }) }) describe("formUtils.isMatrixCellAnswerIncompatibleWithRow", function () { const column = makeStandardsColumn() it("is false for an allowed choice", function () { assert.isFalse(formUtils.isMatrixCellAnswerIncompatibleWithRow(column, PE_PIPES, "iso4427")) assert.isFalse(formUtils.isMatrixCellAnswerIncompatibleWithRow(column, TANK, "nsf61")) }) it("is false for an unrestricted choice", function () { assert.isFalse(formUtils.isMatrixCellAnswerIncompatibleWithRow(column, UV, "iso9001")) }) it("is true for a choice not allowed in the row", function () { assert.isTrue(formUtils.isMatrixCellAnswerIncompatibleWithRow(column, UV, "iso4427")) assert.isTrue(formUtils.isMatrixCellAnswerIncompatibleWithRow(column, UV, "nsf61")) }) it("is false for blank answers", function () { assert.isFalse(formUtils.isMatrixCellAnswerIncompatibleWithRow(column, UV, null)) assert.isFalse(formUtils.isMatrixCellAnswerIncompatibleWithRow(column, UV, "")) }) it("is false for a value referencing a choice that no longer exists", function () { assert.isFalse(formUtils.isMatrixCellAnswerIncompatibleWithRow(column, UV, "removedChoice")) }) it("is false for non-dropdown columns", function () { const textColumn = { _id: "t", _type: "TextColumnQuestion", text: {}, validations: [] } assert.isFalse(formUtils.isMatrixCellAnswerIncompatibleWithRow(textColumn, UV, "iso4427")) }) it("is false when there is no row (roster matrix)", function () { assert.isFalse(formUtils.isMatrixCellAnswerIncompatibleWithRow(column, null, "iso4427")) }) }) describe("MatrixAnswerComponent rendering", function () { beforeEach(function () { this.toDestroy = [] this.render = (options = {}) => { const matrixRef = React.createRef() const props = _.extend( { ref: matrixRef, items: makeItems(), columns: [makeStandardsColumn()], value: null, onValueChange() { return null }, data: {}, responseRow: null, schema: null, formCtx }, options ) const comp = new TestComponent() comp.matrixRef = matrixRef this.toDestroy.push(comp) return comp } /** Returns [{ row label, [option labels] }] for each rendered row */ this.getRowOptions = (comp: any) => { const rows = Array.from(comp.getComponent().querySelectorAll("tbody tr")) return rows.map((row: any) => ({ label: row.querySelector("td label").textContent, options: Array.from(row.querySelectorAll("select option")) .map((o: any) => o.textContent) .filter((t: string) => t !== "") })) } }) afterEach(function () { for (let comp of this.toDestroy) { comp.destroy() } this.toDestroy = [] }) it("shows unrestricted choices in all rows and restricted ones only in their rows", async function () { const comp = this.render() await settle() assert.deepEqual(this.getRowOptions(comp), [ { label: "PE Pipes", options: ["ISO 9001", "ISO 4427", "NSF/ANSI 61"] }, { label: "UV treatment", options: ["ISO 9001", "NSF/ANSI 55"] }, { label: "Storage tank", options: ["ISO 9001", "NSF/ANSI 61"] } ]) }) it("gives every row the same dropdown width, however many choices it shows", async function () { const comp = this.render() await settle() // The three rows show 3, 2 and 2 choices. A width:auto select sizes itself to its widest // visible option, which made the column render ragged once choices became row-specific. const widths = Array.from(comp.getComponent().querySelectorAll("tbody tr select")).map( (sel: any) => sel.style.width ) assert.deepEqual(widths, ["100%", "100%", "100%"]) assert.notInclude(widths, "auto") }) it("hides a choice restricted to no rows everywhere", async function () { const column = makeStandardsColumn() column.choices = [ { id: "iso9001", label: { _base: "en", en: "ISO 9001" } }, { id: "nowhere", label: { _base: "en", en: "Nowhere" }, matrixRowIds: [] } ] const comp = this.render({ columns: [column] }) await settle() for (let row of this.getRowOptions(comp)) { assert.deepEqual(row.options, ["ISO 9001"], `Wrong options for ${row.label}`) } }) it("hides a choice whose rows have all been deleted", async function () { const column = makeStandardsColumn() column.choices = [ { id: "iso9001", label: { _base: "en", en: "ISO 9001" } }, { id: "orphan", label: { _base: "en", en: "Orphan" }, matrixRowIds: ["deletedRow"] } ] const comp = this.render({ columns: [column] }) await settle() for (let row of this.getRowOptions(comp)) { assert.deepEqual(row.options, ["ISO 9001"], `Wrong options for ${row.label}`) } }) it("keeps the mapping when rows are renamed", async function () { const items = makeItems() items[0].label = { _base: "en", en: "Polyethylene Pipes" } const comp = this.render({ items }) await settle() const rows = this.getRowOptions(comp) assert.equal(rows[0].label, "Polyethylene Pipes") assert.deepEqual(rows[0].options, ["ISO 9001", "ISO 4427", "NSF/ANSI 61"]) }) it("keeps the mapping when rows are reordered", async function () { const items = makeItems().reverse() const comp = this.render({ items }) await settle() assert.deepEqual(this.getRowOptions(comp), [ { label: "Storage tank", options: ["ISO 9001", "NSF/ANSI 61"] }, { label: "UV treatment", options: ["ISO 9001", "NSF/ANSI 55"] }, { label: "PE Pipes", options: ["ISO 9001", "ISO 4427", "NSF/ANSI 61"] } ]) }) it("applies row restrictions and choice conditions together", async function () { const column = makeStandardsColumn() // ISO 4427 is restricted to PE Pipes AND conditional on q1 being answered "yes" column.choices[1].conditions = [{ lhs: { question: "q1" }, op: "is", rhs: { literal: "yes" } }] // Condition not met: hidden even in its own row let comp = this.render({ columns: [column], data: { q1: { value: "no" } } }) await settle() assert.deepEqual(this.getRowOptions(comp)[0].options, ["ISO 9001", "NSF/ANSI 61"]) // Condition met: shown in its own row only comp = this.render({ columns: [column], data: { q1: { value: "yes" } } }) await settle() assert.deepEqual(this.getRowOptions(comp)[0].options, ["ISO 9001", "ISO 4427", "NSF/ANSI 61"]) assert.deepEqual(this.getRowOptions(comp)[1].options, ["ISO 9001", "NSF/ANSI 55"]) }) it("behaves unchanged when no choice has a restriction (backward compatibility)", async function () { const column = makeStandardsColumn() column.choices = column.choices.map((c: any) => _.omit(c, "matrixRowIds")) const comp = this.render({ columns: [column] }) await settle() for (let row of this.getRowOptions(comp)) { assert.deepEqual(row.options, ["ISO 9001", "ISO 4427", "NSF/ANSI 55", "NSF/ANSI 61"]) } }) it("still shows an incompatible stored answer rather than blanking it", async function () { const comp = this.render({ value: { [UV]: { standard: { value: "iso4427" } } } }) await settle() const uvRow = this.getRowOptions(comp)[1] assert.include(uvRow.options.join("|"), "ISO 4427", "Stored answer should remain visible") const uvSelect = comp.getComponent().querySelectorAll("tbody tr")[1].querySelector("select") assert.equal(uvSelect.value, "iso4427", "Stored answer should remain selected") }) }) describe("MatrixAnswerComponent.validate", function () { beforeEach(function () { this.toDestroy = [] this.renderAndValidate = (value: any) => { const matrixRef = React.createRef() const comp = new TestComponent( null} data={{}} responseRow={null} schema={null} formCtx={formCtx} /> ) this.toDestroy.push(comp) return { comp, invalid: matrixRef.current.validate() } } }) afterEach(function () { for (let comp of this.toDestroy) { comp.destroy() } this.toDestroy = [] }) it("accepts an answer allowed in its row", function () { const { invalid } = this.renderAndValidate({ [PE_PIPES]: { standard: { value: "iso4427" } } }) assert.isFalse(invalid) }) it("accepts an unrestricted answer in any row", function () { const { invalid } = this.renderAndValidate({ [UV]: { standard: { value: "iso9001" } } }) assert.isFalse(invalid) }) it("rejects an answer that is not allowed in its row", function () { const { invalid } = this.renderAndValidate({ [UV]: { standard: { value: "iso4427" } } }) assert.isTrue(invalid) }) it("does not delete the incompatible stored answer", function () { const value = { [UV]: { standard: { value: "iso4427" } } } this.renderAndValidate(value) assert.deepEqual(value, { [UV]: { standard: { value: "iso4427" } } }) }) it("accepts every answer when no restrictions exist (backward compatibility)", function () { const { invalid } = this.renderAndValidate({ [PE_PIPES]: { standard: { value: "iso9001" } }, [UV]: { standard: { value: "iso9001" } }, [TANK]: { standard: { value: "iso9001" } } }) assert.isFalse(invalid) }) }) describe("ResponseDataValidator", function () { const design = { _type: "Form", _id: "form123", _schema: 11, name: { _base: "en", en: "Sample Form" }, contents: [ { _id: "matrix01", _type: "MatrixQuestion", text: { _base: "en", en: "Matrix" }, conditions: [], validations: [], items: makeItems(), columns: [makeStandardsColumn()] } ] } const responseRow = { getField() { return Promise.resolve(null) }, followJoin() { return Promise.resolve(null) } } async function validate(value: any) { const data = { matrix01: { value } } const visibilityStructure = await new VisibilityCalculator(design as any).createVisibilityStructure( data, responseRow as any ) return new ResponseDataValidator().validate(design as any, visibilityStructure, data, null, responseRow as any) } it("accepts an allowed answer", async function () { assert.isNull(await validate({ [PE_PIPES]: { standard: { value: "iso4427" } } })) }) it("reports an answer that is not allowed in its row", async function () { const result = await validate({ [UV]: { standard: { value: "nsf61" } } }) assert.equal(result.questionId, "matrix01.rowUv.standard") assert.equal(result.error, true) }) it("accepts answers when no restrictions exist (backward compatibility)", async function () { const plainDesign = _.cloneDeep(design) plainDesign.contents[0].columns[0].choices = plainDesign.contents[0].columns[0].choices.map((c: any) => _.omit(c, "matrixRowIds") ) const data = { matrix01: { value: { [UV]: { standard: { value: "iso4427" } } } } } const visibilityStructure = await new VisibilityCalculator(plainDesign as any).createVisibilityStructure( data, responseRow as any ) const result = await new ResponseDataValidator().validate( plainDesign as any, visibilityStructure, data, null, responseRow as any ) assert.isNull(result) }) }) describe("roster matrix behavior is preserved", function () { it("shows row-restricted choices when no matrixRowId is given", async function () { const comp = new TestComponent( null} schema={null} formCtx={formCtx} />
) await settle() const options = Array.from(comp.getComponent().querySelectorAll("option")) .map((o: any) => o.textContent) .filter((t: string) => t !== "") assert.deepEqual(options, ["ISO 9001", "ISO 4427", "NSF/ANSI 55", "NSF/ANSI 61"]) comp.destroy() }) }) })