import React, { useState } from "react" import { assert } from "chai" import { render, fireEvent } from "@testing-library/react" import { CascadingRefAnswerComponent } from "../../src/answers/CascadingRefAnswerComponent" import { defaultT } from "ez-localize" import { spy } from "sinon" import { CascadingRefQuestion } from "../../src/formDesign" import { CustomTablesetSchemaBuilder } from "../../lib/CustomTablesetSchemaBuilder" import { Schema, Row } from "@mwater/expressions" describe("CascadingRefAnswer", () => { const rows: Row[] = [ { _id: "wpg", c0: "manitoba", c1: "winnipeg" }, { _id: "wloo", c0: "ontario", c1: "waterloo" }, { _id: "tor", c0: "ontario", c1: "toronto" } ] const tableSet = { _id: "1234", code: "ts", design: { name: { _base: "en", en: "TS" }, tables: [ { id: "cities", name: { _base: "en", en: "Cities" }, properties: [ { id: "c0", name: { en: "Province" }, type: "enum", enumValues: [ { id: "manitoba", name: { en: "Manitoba" } }, { id: "ontario", name: { en: "Ontario" } } ] }, { id: "c1", name: { en: "City" }, type: "enum", enumValues: [ { id: "winnipeg", name: { en: "Winnipeg" } }, { id: "toronto", name: { en: "Toronto" } }, { id: "waterloo", name: { en: "Waterloo" } } ] } ] } ] } } const schema = new CustomTablesetSchemaBuilder().addTableset(new Schema(), tableSet) const getCustomTableRows = (tableId: string) => Promise.resolve(rows) const getCustomTableRow = (tableId: string, rowId: string) => Promise.resolve(rows.find(r => r._id === rowId) ?? null) const getEntityById = (entityType: string, entityId: string, callback: (entity: any) => void) => { callback(null) } const question: CascadingRefQuestion = { _id: "123", _type: "CascadingRefQuestion", tableId: "custom.ts.cities", text: { _base: "en", en: "Question" }, conditions: [], validations: [], dropdowns: [ { columnId: "c0", name: { _base: "en", en: "Province" } }, { columnId: "c1", name: { _base: "en", en: "City" } } ] } it("selects simple item", async () => { const onValueChange = spy() const { findByDisplayValue } = render( ) const level0 = (await findByDisplayValue("Select...")) as HTMLSelectElement fireEvent.change(level0, { target: { value: '"manitoba"' } }) assert.equal(onValueChange.firstCall.args[0], "wpg") }) it("selects cascading item", async () => { const onValueChange = spy() const { findByDisplayValue, findAllByDisplayValue } = render( ) const level0 = (await findByDisplayValue("Select...")) as HTMLSelectElement fireEvent.change(level0, { target: { value: '"ontario"' } }) assert.isTrue(onValueChange.notCalled) // Wait for the second select to appear after the first selection const selects = await findAllByDisplayValue("Select...") const level1 = selects[selects.length - 1] as HTMLSelectElement fireEvent.change(level1, { target: { value: '"toronto"' } }) assert.equal(onValueChange.firstCall.args[0], "tor") }) it("resets item", async () => { const onValueChange = spy() const { findByDisplayValue } = render( ) const level0 = (await findByDisplayValue("Select...")) as HTMLSelectElement fireEvent.change(level0, { target: { value: '"manitoba"' } }) fireEvent.change(level0, { target: { value: "null" } }) assert.equal(onValueChange.secondCall.args[0], null) }) describe("multiSelectLast", () => { const multiQuestion: CascadingRefQuestion = { ...question, multiSelectLast: true } /** Controlled wrapper so that the value round-trips like it does in a real form */ function Harness(props: { initialValue?: string[] | string; onValueChange: (value?: any) => void; question?: CascadingRefQuestion }) { const [value, setValue] = useState(props.initialValue) return ( { setValue(v) props.onValueChange(v) }} schema={schema} getCustomTableRows={getCustomTableRows} getCustomTableRow={getCustomTableRow} getEntityById={getEntityById} /> ) } /** Finds the LargeCheckbox row for a label. The label text is in a span inside the clickable row */ async function findCheck(findByText: (text: string) => Promise, label: string) { return (await findByText(label)).parentElement as HTMLElement } function isChecked(row: HTMLElement) { return row.querySelector("i")!.className.includes("fa-check-square") } it("shows checklist for last level and toggles ids", async () => { const onValueChange = spy() const { findByDisplayValue, findByText, queryByText } = render() const level0 = (await findByDisplayValue("Select...")) as HTMLSelectElement fireEvent.change(level0, { target: { value: '"ontario"' } }) // Only Ontario cities are listed const toronto = await findCheck(findByText, "Toronto") assert.isNotNull(queryByText("Waterloo")) assert.isNull(queryByText("Winnipeg")) assert.isFalse(isChecked(toronto)) fireEvent.click(toronto) assert.deepEqual(onValueChange.lastCall.args[0], ["tor"]) assert.isTrue(isChecked(await findCheck(findByText, "Toronto"))) fireEvent.click(await findCheck(findByText, "Waterloo")) // Order follows the checklist (sorted by label) assert.deepEqual(onValueChange.lastCall.args[0], ["tor", "wloo"]) // Unchecking the last one clears the value entirely fireEvent.click(await findCheck(findByText, "Toronto")) assert.deepEqual(onValueChange.lastCall.args[0], ["wloo"]) fireEvent.click(await findCheck(findByText, "Waterloo")) assert.isUndefined(onValueChange.lastCall.args[0]) }) it("selects all and clears", async () => { const onValueChange = spy() const { findByDisplayValue, findByText } = render() const level0 = (await findByDisplayValue("Select...")) as HTMLSelectElement fireEvent.change(level0, { target: { value: '"ontario"' } }) fireEvent.click(await findByText("Select All")) assert.deepEqual(onValueChange.lastCall.args[0], ["tor", "wloo"]) fireEvent.click(await findByText("Clear")) assert.isUndefined(onValueChange.lastCall.args[0]) }) it("hydrates upper levels from existing value", async () => { const onValueChange = spy() const { findByText, findByDisplayValue } = render() // Province is pre-selected and the checklist shows Waterloo checked assert.isTrue(isChecked(await findCheck(findByText, "Waterloo"))) assert.isFalse(isChecked(await findCheck(findByText, "Toronto"))) const level0 = (await findByDisplayValue("Ontario")) as HTMLSelectElement assert.isNotNull(level0) assert.isTrue(onValueChange.notCalled) }) it("clears selection when an upper level changes", async () => { const onValueChange = spy() const { findByDisplayValue, findByText, queryByText } = render() const level0 = (await findByDisplayValue("Ontario")) as HTMLSelectElement fireEvent.change(level0, { target: { value: '"manitoba"' } }) assert.isTrue(onValueChange.calledOnce) assert.isUndefined(onValueChange.lastCall.args[0]) // Manitoba's checklist is shown, nothing selected assert.isFalse(isChecked(await findCheck(findByText, "Winnipeg"))) assert.isNull(queryByText("Waterloo")) }) it("drops stale ids on next change", async () => { const onValueChange = spy() const { findByText } = render() fireEvent.click(await findCheck(findByText, "Toronto")) assert.deepEqual(onValueChange.lastCall.args[0], ["tor", "wloo"]) }) it("shows whole table as checklist with a single dropdown", async () => { const onValueChange = spy() const single: CascadingRefQuestion = { ...multiQuestion, dropdowns: [multiQuestion.dropdowns[1]] } const { findByText } = render() assert.isNotNull(await findCheck(findByText, "Toronto")) assert.isNotNull(await findCheck(findByText, "Winnipeg")) fireEvent.click(await findCheck(findByText, "Winnipeg")) assert.deepEqual(onValueChange.lastCall.args[0], ["wpg"]) }) it("uses a searchable multi-select instead of checkboxes for large lists", async () => { const onValueChange = spy() const manyRows: Row[] = Array.from({ length: 25 }, (_, i) => ({ _id: `r${i}`, c0: "ontario", c1: "toronto" })) const single: CascadingRefQuestion = { ...multiQuestion, dropdowns: [multiQuestion.dropdowns[1]] } const { findByText, container } = render( Promise.resolve(manyRows)} getCustomTableRow={getCustomTableRow} getEntityById={getEntityById} /> ) assert.isNotNull(await findByText("0 of 25 selected")) assert.equal(container.querySelectorAll('[id^="cascading-ref-"]').length, 0, "no checkboxes") assert.isNotNull(container.querySelector("input[role=combobox]"), "react-select rendered") fireEvent.click(await findByText("Select All")) assert.equal(onValueChange.lastCall.args[0].length, 25) }) it("hides Select All above 100 rows", async () => { const manyRows: Row[] = Array.from({ length: 101 }, (_, i) => ({ _id: `r${i}`, c0: "ontario", c1: "toronto" })) const single: CascadingRefQuestion = { ...multiQuestion, dropdowns: [multiQuestion.dropdowns[1]] } const { findByText, queryByText } = render( Promise.resolve(manyRows)} getCustomTableRow={getCustomTableRow} getEntityById={getEntityById} /> ) assert.isNotNull(await findByText("0 of 101 selected")) assert.isNull(queryByText("Select All")) assert.isNotNull(queryByText("Clear")) }) it("leaves out rows with a blank last level", async () => { const onValueChange = spy() const withBlank: Row[] = [...rows, { _id: "blank", c0: "ontario", c1: null }] const { findByDisplayValue, findByText, queryByText } = render( Promise.resolve(withBlank)} getCustomTableRow={getCustomTableRow} getEntityById={getEntityById} /> ) const level0 = (await findByDisplayValue("Select...")) as HTMLSelectElement fireEvent.change(level0, { target: { value: '"ontario"' } }) assert.isNotNull(await findByText("0 of 2 selected")) assert.isNull(queryByText("—")) assert.isNull(queryByText("None")) fireEvent.click(await findByText("Select All")) assert.deepEqual(onValueChange.lastCall.args[0], ["tor", "wloo"]) }) }) })