// @ts-nocheck
import _ from "lodash"
import compare from "./compare"
import { assert } from "chai"
import React from "react"
import ReactDOM from "react-dom"
import TestComponent from "@mwater/react-library/lib/TestComponent"
import ReactTestUtils from "react-dom/test-utils"
import RosterMatrixComponent from "../src/RosterMatrixComponent"
import MockTContextWrapper from "./MockTContextWrapper"
import { default as ResponseRow } from "../src/ResponseRow"
import { TestBackend } from "react-dnd-test-backend"
import { DndProvider } from "react-dnd"
class Wrapper extends React.Component {
render() {
return {this.props.children}
}
}
describe("RosterMatrixComponent", function () {
beforeEach(function () {
this.toDestroy = []
this.render = (options: any) => {
// Capture a ref to the RosterMatrixComponent class instance so tests can
// call methods on it (e.g. .validate()) instead of relying on the removed
// ReactTestUtils.findRenderedComponentWithType helper.
const matrixRef = React.createRef()
const elem = (
str
}
})}
/>
)
const comp = new TestComponent({elem})
;(comp as any).matrixRef = matrixRef
this.toDestroy.push(comp)
return comp
}
// Create sample rosterMatrix with each type of column
return (this.rosterMatrix = {
_id: "a",
name: { en: "Name" },
contents: [
{ _id: "text", _type: "TextColumnQuestion", text: { en: "Text" } },
{ _id: "number", _type: "NumberColumnQuestion", text: { en: "Number" }, decimal: false },
{ _id: "check", _type: "CheckColumnQuestion", text: { en: "Check" } },
{
_id: "dropdown",
_type: "DropdownColumnQuestion",
text: { en: "Dropdown" },
choices: [
{ id: "x", label: { en: "X" } },
{ id: "y", label: { en: "Y" } }
]
},
{
_id: "units",
_type: "UnitsColumnQuestion",
text: { en: "Units" },
units: [
{ id: "cm", label: { en: "CM" } },
{ id: "inch", label: { en: "INCH" } }
]
},
{ _id: "textColumn", _type: "TextColumn", text: { en: "TextColumn" } }
]
})
})
afterEach(function () {
return this.toDestroy.map((comp: any) => comp.destroy())
})
it("adds entry when add is clicked", function (done) {
const onDataChange = (val: any) => {
assert.equal(val.a.length, 1)
assert(val.a[0]._id)
assert.deepEqual(val.a[0].data, {})
return done()
}
this.rosterMatrix.allowAdd = true
const comp = this.render({ rosterMatrix: this.rosterMatrix, data: {}, onDataChange })
const buttons = ReactTestUtils.scryRenderedDOMComponentsWithTag(comp.getComponent(), "button")
return TestComponent.click(buttons[0])
})
it("pre-fills sticky columns from previous row when adding", function (done) {
const onDataChange = (val: any) => {
assert.equal(val.a.length, 2)
assert.equal(val.a[1].data.text.value, "hello")
assert.equal(val.a[1].data.dropdown.value, "x")
assert(!val.a[1].data.number, "Non-sticky column should not be copied")
return done()
}
this.rosterMatrix.contents[0].sticky = true // text column
this.rosterMatrix.contents[3].sticky = true // dropdown column
this.rosterMatrix.allowAdd = true
const comp = this.render({
rosterMatrix: this.rosterMatrix,
data: {
a: [
{
_id: "1",
data: {
text: { value: "hello" },
number: { value: 42 },
dropdown: { value: "x" }
}
}
]
},
onDataChange
})
const buttons = ReactTestUtils.scryRenderedDOMComponentsWithTag(comp.getComponent(), "button")
return TestComponent.click(buttons[0])
})
it("does not pre-fill sticky columns when no previous row exists", function (done) {
const onDataChange = (val: any) => {
assert.equal(val.a.length, 1)
assert.deepEqual(val.a[0].data, {})
return done()
}
this.rosterMatrix.contents[0].sticky = true
this.rosterMatrix.allowAdd = true
const comp = this.render({ rosterMatrix: this.rosterMatrix, data: {}, onDataChange })
const buttons = ReactTestUtils.scryRenderedDOMComponentsWithTag(comp.getComponent(), "button")
return TestComponent.click(buttons[0])
})
it("does not pre-fill sticky columns when previous row value is null", function (done) {
const onDataChange = (val: any) => {
assert.equal(val.a.length, 2)
assert.deepEqual(val.a[1].data, {})
return done()
}
this.rosterMatrix.contents[0].sticky = true
this.rosterMatrix.allowAdd = true
const comp = this.render({
rosterMatrix: this.rosterMatrix,
data: {
a: [{ _id: "1", data: { text: { value: null } } }]
},
onDataChange
})
const buttons = ReactTestUtils.scryRenderedDOMComponentsWithTag(comp.getComponent(), "button")
return TestComponent.click(buttons[0])
})
it("does not show add if add is disabled", function () {
const comp = this.render({ rosterMatrix: this.rosterMatrix, data: {} })
return assert(!comp.findDOMNodeByText(/add/i))
})
it("removes entry when remove is clicked", function (done) {
const onDataChange = (val: any) => {
// Removes first one
compare(val, { a: [{ _id: "2", data: { x: { value: 2 } } }] })
return done()
}
this.rosterMatrix.allowRemove = true
const comp = this.render({
rosterMatrix: this.rosterMatrix,
data: {
a: [
{ _id: "1", data: { x: { value: 1 } } },
{ _id: "2", data: { x: { value: 2 } } }
]
},
onDataChange
})
return TestComponent.click(ReactTestUtils.scryRenderedDOMComponentsWithClass(comp.getComponent(), "fa-times")[0])
})
it("does not show remove if remove is disabled", function () {
const comp = this.render({ rosterMatrix: this.rosterMatrix, data: {} })
return assert.equal(ReactTestUtils.scryRenderedDOMComponentsWithClass(comp.getComponent(), "fa-times").length, 0)
})
it("puts answers from column components in correct position in array", function (done) {
const onDataChange = (val: any) => {
compare(val, {
a: [
{ _id: "1", data: {} },
{ _id: "2", data: { text: { value: "x" } } }
]
})
return done()
}
const comp = this.render({
rosterMatrix: this.rosterMatrix,
data: {
a: [
{ _id: "1", data: {} },
{ _id: "2", data: {} }
]
},
onDataChange
})
// Set 3rd input (second row text)
const inputs = ReactTestUtils.scryRenderedDOMComponentsWithTag(comp.getComponent(), "input")
inputs[3].value = "x"
return ReactTestUtils.Simulate.change(inputs[3])
})
it("uses alternate rosterId if specified", function (done) {
const onDataChange = (val: any) => {
assert.equal(val.b.length, 1)
assert(val.b[0]._id)
assert.deepEqual(val.b[0].data, {})
return done()
}
this.rosterMatrix.allowAdd = true
this.rosterMatrix.rosterId = "b"
const comp = this.render({ rosterMatrix: this.rosterMatrix, data: {}, onDataChange })
return TestComponent.click(ReactTestUtils.findRenderedDOMComponentWithTag(comp.getComponent(), "button"))
})
it("displays prompt", function () {
const comp = this.render({ rosterMatrix: this.rosterMatrix, data: {} })
return assert(comp.findDOMNodeByText(/Name/))
})
it("records text", function (done) {
const onDataChange = (val: any) => {
compare(val.a[0].data, { text: { value: "x" } })
return done()
}
const comp = this.render({ rosterMatrix: this.rosterMatrix, data: { a: [{}] }, onDataChange })
const inputs = ReactTestUtils.scryRenderedDOMComponentsWithTag(comp.getComponent(), "input")
inputs[0].value = "x"
return ReactTestUtils.Simulate.change(inputs[0])
})
// Disabled because it's not working because of react testing library
// it("records number", function (done) {
// const onDataChange = (val: any) => {
// compare(val.a[0].data, { number: { value: 1 } })
// return done()
// }
// const comp = this.render({ rosterMatrix: this.rosterMatrix, data: { a: [{}] }, onDataChange })
// const inputs = ReactTestUtils.scryRenderedDOMComponentsWithTag(comp.getComponent(), "input")
// inputs[1].value = "1"
// ReactTestUtils.Simulate.change(inputs[1])
// return ReactTestUtils.Simulate.blur(inputs[1])
// }) // Have to leave to set it
it("records check", function (done) {
const onDataChange = (val: any) => {
compare(val.a[0].data, { check: { value: true } })
return done()
}
const comp = this.render({ rosterMatrix: this.rosterMatrix, data: { a: [{}] }, onDataChange })
// Find the checkbox by looking for the unchecked square icon and clicking its parent div
const icons = ReactTestUtils.scryRenderedDOMComponentsWithClass(comp.getComponent(), "fa-square")
return ReactTestUtils.Simulate.click(icons[0].parentElement)
})
it("records dropdown", function (done) {
const onDataChange = (val: any) => {
compare(val.a[0].data, { dropdown: { value: "y" } })
return done()
}
const comp = this.render({ rosterMatrix: this.rosterMatrix, data: { a: [{}] }, onDataChange })
const inputs = ReactTestUtils.scryRenderedDOMComponentsWithTag(comp.getComponent(), "select")
inputs[0].value = "y"
return ReactTestUtils.Simulate.change(inputs[0], { target: { value: "y" } })
})
it("requires required columns fail", async function () {
this.rosterMatrix.contents[0].required = true
const comp = this.render({ rosterMatrix: this.rosterMatrix, data: { a: [{ _id: "1", data: {} }] } })
assert.isTrue(
await (comp as any).matrixRef.current.validate(false),
"Should fail validation"
)
})
it("requires required columns pass", async function () {
this.rosterMatrix.contents[0].required = true
const comp = this.render({
rosterMatrix: this.rosterMatrix,
data: { a: [{ _id: "1", data: { text: { value: "x" } } }] }
})
assert.isFalse(
await (comp as any).matrixRef.current.validate(false),
"Should pass validation"
)
})
it("validates columns fail", async function () {
this.rosterMatrix.contents[0].validations = [
{
op: "lengthRange",
rhs: { literal: { min: 4, max: 6 } },
message: { en: "message" }
}
]
const comp = this.render({
rosterMatrix: this.rosterMatrix,
data: { a: [{ _id: "1", data: { text: { value: "x" } } }] }
})
assert.isTrue(
await (comp as any).matrixRef.current.validate(false),
"Should fail validation"
)
})
it("validates columns pass", async function () {
this.rosterMatrix.contents[0].validations = [
{
op: "lengthRange",
rhs: { literal: { min: 4, max: 6 } },
message: { en: "message" }
}
]
const comp = this.render({
rosterMatrix: this.rosterMatrix,
data: { a: [{ _id: "1", data: { text: { value: "12345" } } }] }
})
assert.isFalse(
await (comp as any).matrixRef.current.validate(false),
"Should pass validation"
)
})
})