// @ts-nocheck import _ from "lodash" import { assert } from "chai" import * as formUtils from "../src/formUtils" import * as simpleForm from "./simpleForm" import * as sectionedForm from "./sectionedForm" import * as propertyLinksFormDesign from "./propertyLinksFormDesign" import canonical from "canonical-json" function compare(actual: any, expected: any) { return assert.equal( canonical(actual), canonical(expected), "\n" + canonical(actual) + "\n" + canonical(expected) + "\n" ) } describe("FormUtils", function () { describe("priorQuestions", () => context("two prior questions, one following", function () { it("returns prior questions", function () { const priors = formUtils.priorQuestions(simpleForm, simpleForm.contents[2]) return assert.deepEqual(_.pluck(priors, "_id"), ["0001", "0002"]) }) it("correctly handles sections", function () { const priors = formUtils.priorQuestions(sectionedForm, sectionedForm.contents[0].contents[2]) return assert.deepEqual(_.pluck(priors, "_id"), ["0001", "0002"]) }) it("correctly handles sections as item", function () { const priors = formUtils.priorQuestions(sectionedForm, sectionedForm.contents[1]) assert.deepEqual(_.pluck(priors, "_id"), ["0001", "0002", "0003", "0004", "0010Number"]) }) it("includes group contents", function () { const form = { _type: "Form", contents: [{ _id: "groupid", _type: "Group", contents: simpleForm.contents }] } const priors = formUtils.priorQuestions(form) return assert.deepEqual(_.pluck(priors, "_id"), ["0001", "0002", "0003", "0004"]) }) it("doesn't include roster contents by default", function () { const form = { _type: "Form", contents: [{ _id: "groupid", _type: "RosterGroup", contents: simpleForm.contents }] } const priors = formUtils.priorQuestions(form) return assert.deepEqual(_.pluck(priors, "_id"), []) }) it("includes roster contents only if rosterId specified", function () { const form = { _type: "Form", contents: [{ _id: "groupid", _type: "RosterGroup", contents: simpleForm.contents }] } const priors = formUtils.priorQuestions(form, null, "groupid") return assert.deepEqual(_.pluck(priors, "_id"), ["0001", "0002", "0003", "0004"]) }) })) describe("getRosterIds", () => it("gets unique roster ids", function () { const form = _.cloneDeep(sectionedForm) form.contents[0].contents.push({ _id: "groupid", _type: "RosterGroup", contents: [] }) return assert.deepEqual(formUtils.getRosterIds(form), ["groupid"]) })) describe("findItem", function () { it("finds question", () => assert.equal(formUtils.findItem(simpleForm, "0002")._id, "0002")) it("finds section", () => assert.equal(formUtils.findItem(sectionedForm, "section2")._id, "section2")) it("correctly handles sections", () => assert.equal(formUtils.findItem(sectionedForm, "0002")._id, "0002")) }) describe("prepareQuestion", function () { context("blank TextQuestion that is prepared", function () { beforeEach(function () { this.question = { _type: "TextQuestion" } return formUtils.prepareQuestion(this.question) }) it("adds _id", function () { return assert.equal(this.question._id.length, 32) }) it("adds text", function () { return assert.deepEqual(this.question.text, {}) }) it("adds required:false", function () { return assert.equal(this.question.required, false) }) it("adds empty conditions", function () { return assert.deepEqual(this.question.conditions, []) }) it("adds empty validations", function () { return assert.deepEqual(this.question.validations, []) }) }) it("adds decimal:true to NumberQuestion and NumberColumnQuestion", function () { let question = formUtils.prepareQuestion({ _type: "NumberQuestion" }) assert.equal(question.decimal, true) question = formUtils.prepareQuestion({ _type: "NumberColumnQuestion" }) return assert.equal(question.decimal, true) }) it("removes choices for NumberQuestion", function () { const question = formUtils.prepareQuestion({ _type: "NumberQuestion", choices: [] }) return assert.isUndefined(question.choices) }) it("removes decimal for TextQuestion", function () { const question = formUtils.prepareQuestion({ _type: "TextQuestion", decimal: true }) return assert.isUndefined(question.decimal) }) it("adds choices to RadioQuestion and DropdownColumnQuestion", function () { let question = formUtils.prepareQuestion({ _type: "RadioQuestion" }) assert.deepEqual(question.choices, []) question = formUtils.prepareQuestion({ _type: "DropdownColumnQuestion" }) return assert.deepEqual(question.choices, []) }) // it "adds items? to MultipleTextQuestion" // it "adds items? to MultipleNumberQuestion" it("adds format to DateQuestion", function () { const question = formUtils.prepareQuestion({ _type: "DateQuestion" }) return assert.equal(question.format, "YYYY-MM-DD") }) it("adds format to TextQuestion", function () { const question = formUtils.prepareQuestion({ _type: "TextQuestion" }) return assert.equal(question.format, "singleline") }) }) //it "adds format to DateTimeQuestion" describe("changeQuestionType", function () { beforeEach(function () { this.question = { _type: "TextQuestion", validations: [ { op: "lengthRange", rhs: { literal: { min: 6, max: 8 } }, message: { _base: "es", es: "message" } } ] } return formUtils.prepareQuestion(this.question) }) it("removed validations", function () { formUtils.changeQuestionType(this.question, "DateQuestion") return assert.equal(this.question.validations.length, 0) }) it("removes format", function () { formUtils.changeQuestionType(this.question, "DateQuestion") return assert.equal(this.question.format, "YYYY-MM-DD") }) }) describe("duplicateItem", function () { describe("duplicate question", function () { before(function () { return (this.duplicate = formUtils.duplicateItem(simpleForm.contents[0])) }) it("sets new id", function () { return assert.notEqual(this.duplicate._id, simpleForm.contents[0]._id) }) it("sets _basedOn", function () { return assert.equal(this.duplicate._basedOn, simpleForm.contents[0]._id) }) }) describe("duplicate section", function () { before(function () { return (this.duplicate = formUtils.duplicateItem(sectionedForm.contents[0])) }) it("sets new id", function () { return assert.notEqual(this.duplicate._id, sectionedForm.contents[0]._id) }) it("sets _basedOn", function () { return assert.equal(this.duplicate._basedOn, sectionedForm.contents[0]._id) }) it("duplicates questions", function () { return assert.equal(this.duplicate.contents[0]._basedOn, sectionedForm.contents[0].contents[0]._id) }) it("maps references in conditions", function () { return assert.equal(this.duplicate.contents[2].conditions[0].lhs.question, this.duplicate.contents[1]._id) }) it("handles OR conditions") it("handles AND conditions") }) it("removes conditions which reference non-present questions", function () { this.duplicate = formUtils.duplicateItem(sectionedForm.contents[1]) return assert.equal(this.duplicate.contents[0].conditions.length, 0) }) describe("duplicates form calculations", function () { before(function () { const form = _.cloneDeep(sectionedForm) form["calculations"] = [ { _id: "__calc1", name: { en: "Calculation 1", _base: "en" }, expr: { type: "op", op: "/", table: "responses:f1", exprs: [ { column: "data:0010Number:value", table: "responses:f1", type: "field" }, { value: 10, type: "literal" } ] } }, { _id: "__calc2", name: { en: "Calculation 2", _base: "en" }, expr: { type: "op", op: "/", table: "responses:f1", exprs: [ { column: "calculation:__calc1", table: "responses:f1", type: "field" }, { value: 10, type: "literal" } ] } } ] this.original = form this.duplicate = formUtils.duplicateItem(form) }) it("regenerates calculation IDs", function () { assert.notEqual(this.duplicate.calculations[0]._id, this.original.calculations[0]._id) }) it("properly updates calculations references within calculations", function () { return assert.equal( this.duplicate.calculations[1].expr.exprs[0].column.split(":")[1], this.duplicate.calculations[0]._id ) }) it("calculation column references new IDs", function () { return assert.equal( this.duplicate.calculations[0].expr.exprs[0].column.split(":")[1], this.duplicate.contents[0].contents[4]._id ) }) }) describe("duplicate form", function () { before(function () { return (this.duplicate = formUtils.duplicateItem(sectionedForm)) }) it("sets _basedOn", function () { return assert.equal(this.duplicate.contents[0]._basedOn, sectionedForm.contents[0]._id) }) it("duplicates questions", function () { return assert.equal(this.duplicate.contents[0].contents[0]._basedOn, sectionedForm.contents[0].contents[0]._id) }) it("maps references across sections", function () { return assert.equal( this.duplicate.contents[1].contents[0].conditions[0].lhs.question, this.duplicate.contents[0].contents[2]._id ) }) it("duplicates locales", function () { return assert.equal(this.duplicate.locales[0].code, "en") }) }) }) describe("update localizations", () => it("adds form-level localizations", function () { const form = _.cloneDeep(simpleForm) formUtils.updateLocalizations(form) return assert(form.localizedStrings.length > 5) })) describe("hasLocalizations", function () { it("true if has one translation", function () { const obj = [{ _base: "en", en: "x", fr: "y" }] assert.isTrue(formUtils.hasLocalizations(obj, "fr")) return assert.isFalse(formUtils.hasLocalizations(obj, "de")) }) it("false if blank/non-existent", function () { const obj = [{ _base: "en", en: "x", fr: "" }] return assert.isFalse(formUtils.hasLocalizations(obj, "fr")) }) }) describe("extractLocalizedStrings", function () { it("gets all strings", function () { const obj = { a: [ { b: { _base: "en", en: "hello" } }, { c: { _base: "es", en: "hello2" } } ], d: "test", e: null } const strs = formUtils.extractLocalizedStrings(obj) return assert.deepEqual(strs, [ { _base: "en", en: "hello" }, { _base: "es", en: "hello2" } ]) }) it("gets localizedStrings strings", function () { const obj = { localizedStrings: [ { b: { _base: "en", en: "hello" } }, { c: { _base: "es", en: "hello2" } } ], d: "test", e: null } const strs = formUtils.extractLocalizedStrings(obj) return assert.deepEqual(strs, [ { _base: "en", en: "hello" }, { _base: "es", en: "hello2" } ]) }) }) describe("findEntityQuestion", () => it("can find entity questions from inside rosters", function () { const formDesign = { name: { en: "Roster matrix site survey", _base: "en" }, _type: "Form", _schema: 21, locales: [{ code: "en", name: "English" }], contents: [ { _id: "52a494e3623a43f4ab17d777512f9ab4", name: { en: "Untitled Section", _base: "en" }, _type: "Section", contents: [ { _id: "fde9c0d3a1834845884dcf3add4e3142", name: { en: "site details", _base: "en" }, _type: "RosterMatrix", allowAdd: true, contents: [ { _id: "8f2f5a526296469b8ea6daec6bfdce70", text: { en: "What site", _base: "en" }, _type: "SiteColumnQuestion", required: false, siteType: "water_point", validations: [] }, { _id: "15287c8b0c03427fa36052e432dcd10f", text: { en: "How long?", _base: "en" }, _type: "NumberColumnQuestion", decimal: true, required: false, validations: [] } ], conditions: [], allowRemove: true } ], conditions: [] } ] } // formDesign = JSON.parse(formDesign) const siteQuestions = formUtils.findEntityQuestions(formDesign, "water_point") assert.equal(siteQuestions.length, 1) })) describe("extractEntityReferences", function () { it("extracts entity questions", function () { const formDesign = { _type: "Form", contents: [ { _id: "0001", _type: "EntityQuestion", entityType: "community" } ] } const responseData = { "0001": { value: "123456" } } return compare(formUtils.extractEntityReferences(formDesign, responseData), [ { question: "0001", entityType: "community", property: "_id", value: "123456" } ]) }) it("extracts site questions", function () { const formDesign = { _type: "Form", contents: [ { _id: "0001", _type: "SiteQuestion", siteTypes: ["Health facility"] } ] } const responseData = { "0001": { value: { code: "10007" } } } return compare(formUtils.extractEntityReferences(formDesign, responseData), [ { question: "0001", entityType: "health_facility", property: "code", value: "10007" } ]) }) it("extracts site columns of a roster matrix with the column's site type", function () { const formDesign = { _type: "Form", contents: [ { _id: "matrixid", _type: "RosterMatrix", contents: [ { _id: "0001", _type: "SiteColumnQuestion", siteType: "Health facility" } ] } ] } const responseData = { matrixid: [{ _id: "firstrid", data: { "0001": { value: { code: "10007" } } } }] } return compare(formUtils.extractEntityReferences(formDesign, responseData), [ { question: "0001", roster: "firstrid", entityType: "health_facility", property: "code", value: "10007" } ]) }) it("extracts site questions in roster", function () { const formDesign = { _type: "Form", contents: [ { _id: "rosterid", _type: "RosterGroup", contents: [ { _id: "0001", _type: "SiteQuestion", siteTypes: ["Health facility"] } ] } ] } const responseData = { rosterid: [ { _id: "firstrid", data: { "0001": { value: { code: "10007" } } } }, { _id: "secondrid", data: { "0001": { value: { code: "10014" } } } } ] } return compare(formUtils.extractEntityReferences(formDesign, responseData), [ { question: "0001", roster: "firstrid", entityType: "health_facility", property: "code", value: "10007" }, { question: "0001", roster: "secondrid", entityType: "health_facility", property: "code", value: "10014" } ]) }) }) it("prepares admin region question", function () { const question = { _type: "AdminRegionQuestion", defaultValue: 123, extra: true } formUtils.prepareQuestion(question) assert.deepEqual(question.allowedLevels, []) assert.isUndefined(question.defaultValue) assert.isUndefined(question.extra) }) describe("getAllSiteEntityTypes", function () { it("includes site questions and roster matrix site columns once each", function () { const formDesign = { _type: "Form", contents: [ { _id: "q1", _type: "SiteQuestion", siteTypes: ["Water point"] }, { _id: "q2", _type: "SiteQuestion", siteTypes: ["health_facility"] }, { _id: "matrixid", _type: "RosterMatrix", contents: [ { _id: "c1", _type: "SiteColumnQuestion", siteType: "community" }, { _id: "c2", _type: "SiteColumnQuestion", siteType: "health_facility" }, { _id: "c3", _type: "SiteColumnQuestion" } ] } ] } assert.deepEqual(formUtils.getAllSiteEntityTypes(formDesign), ["water_point", "health_facility", "community"]) }) }) describe("normalizeMixedTopLevelSections", function () { // 32-char hex ids as produced by createUid const qid1 = "11112222333344445555666677778888" const qid2 = "aaaabbbbccccddddeeeeffff00001111" const section = (id: string) => ({ _id: "sec_" + id, _type: "Section", name: { _base: "en", en: "Page " + id }, conditions: [], contents: [{ _id: "q_" + id, _type: "TextQuestion", text: { _base: "en", en: id } }] }) const question = (id: string) => ({ _id: id, _type: "TextQuestion", text: { _base: "en", en: id } }) const design = (contents: any[]) => ({ _type: "Form", name: { _base: "en", en: "Test" }, contents }) as any it("wraps a stray run between sections in a new Untitled Page section", function () { const result = formUtils.normalizeMixedTopLevelSections(design([section("a"), question(qid1), section("b")]))! assert.deepEqual( result.contents.map((item: any) => item._type), ["Section", "Section", "Section"] ) const wrapper: any = result.contents[1] assert.equal(wrapper._type, "Section") assert.deepEqual(wrapper.name, { _base: "en", en: "Untitled Page" }) assert.deepEqual(wrapper.conditions, []) assert.equal(wrapper.contents.length, 1) assert.equal(wrapper.contents[0]._id, qid1) }) it("wraps consecutive strays together and separate runs separately", function () { const result = formUtils.normalizeMixedTopLevelSections( design([section("a"), question(qid1), question(qid2), section("b"), question("q3")]) )! assert.deepEqual( result.contents.map((item: any) => (item._type === "Section" ? _.pluck(item.contents, "_id") : item._id)), [["q_a"], [qid1, qid2], ["q_b"], ["q3"]] ) }) it("preserves question order and item references", function () { const stray = question(qid1) const input = design([section("a"), stray, section("b")]) const result = formUtils.normalizeMixedTopLevelSections(input)! // Item objects are reused, not cloned assert.strictEqual((result.contents[1] as any).contents[0], stray) assert.strictEqual(result.contents[0], input.contents[0]) // Flattened question order unchanged const flatten = (d: any) => _.pluck( formUtils.allItems(d).filter((item: any) => item._type === "TextQuestion"), "_id" ) assert.deepEqual(flatten(result), flatten(input)) }) it("does not mutate the input design", function () { const input = design([section("a"), question(qid1)]) const before = JSON.stringify(input) formUtils.normalizeMixedTopLevelSections(input) assert.equal(JSON.stringify(input), before) }) it("derives the wrapper section id from the first wrapped item deterministically", function () { const make = () => design([section("a"), question(qid1), question(qid2)]) const result1 = formUtils.normalizeMixedTopLevelSections(make())! const result2 = formUtils.normalizeMixedTopLevelSections(make())! assert.equal((result1.contents[1] as any)._id, qid1.split("").reverse().join("")) assert.equal(JSON.stringify(result1), JSON.stringify(result2)) }) it("falls back to a fresh id if the derived id collides with an existing item", function () { const collider = qid1.split("").reverse().join("") const result = formUtils.normalizeMixedTopLevelSections( design([section("a"), question(qid1), question(collider)]) )! const wrapper: any = result.contents[1] assert.notEqual(wrapper._id, collider) assert.equal(wrapper.contents.length, 2) }) it("is idempotent", function () { const result = formUtils.normalizeMixedTopLevelSections(design([section("a"), question(qid1)]))! assert.isNull(formUtils.normalizeMixedTopLevelSections(result)) }) it("returns null when no repair is needed", function () { // All sections assert.isNull(formUtils.normalizeMixedTopLevelSections(design([section("a"), section("b")]))) // No sections assert.isNull(formUtils.normalizeMixedTopLevelSections(design([question(qid1), question(qid2)]))) // Empty assert.isNull(formUtils.normalizeMixedTopLevelSections(design([]))) }) it("leaves single-page forms (first item not a Section) alone by default", function () { assert.isNull(formUtils.normalizeMixedTopLevelSections(design([question(qid1), section("a")]))) }) it("wraps leading strays when wrapLeading is true", function () { const result = formUtils.normalizeMixedTopLevelSections(design([question(qid1), section("a")]), true)! assert.deepEqual( result.contents.map((item: any) => item._type), ["Section", "Section"] ) assert.equal((result.contents[0] as any).contents[0]._id, qid1) }) }) it("gets custom table refs", function () { const tableIds = formUtils.getCustomTablesReferenced({ name: { en: "Cascading Ref Form", _base: "en" }, _type: "Form", _schema: 21, locales: [{ code: "en", name: "English" }], contents: [ { _type: "CascadingRefQuestion", _id: "aa331b86fb5d40ffbf6600e8357e2b0a", text: { en: "Cascade", _base: "en" }, tableId: "custom.ts.cities", dropdowns: [ { columnId: "c0", name: { en: "Province" } }, { columnId: "c1", name: { en: "City" } } ] } ] }) return assert.deepEqual(tableIds, ["custom.ts.cities"]) }) describe("getAnswerType for cascading refs", function () { it("is cascading_ref by default", function () { assert.equal(formUtils.getAnswerType({ _type: "CascadingRefQuestion", tableId: "custom.ts.cities", dropdowns: [] }), "cascading_ref") }) it("is cascading_refs when multiSelectLast", function () { assert.equal(formUtils.getAnswerType({ _type: "CascadingRefQuestion", tableId: "custom.ts.cities", dropdowns: [], multiSelectLast: true }), "cascading_refs") }) }) describe("normalizeCascadingRefsValue", function () { it("wraps a bare string", function () { assert.deepEqual(formUtils.normalizeCascadingRefsValue("abc"), ["abc"]) }) it("keeps arrays, dropping blanks", function () { assert.deepEqual(formUtils.normalizeCascadingRefsValue(["a", "", null, "b"]), ["a", "b"]) }) it("returns null for empty", function () { assert.isNull(formUtils.normalizeCascadingRefsValue(null)) assert.isNull(formUtils.normalizeCascadingRefsValue(undefined)) assert.isNull(formUtils.normalizeCascadingRefsValue("")) assert.isNull(formUtils.normalizeCascadingRefsValue([])) assert.isNull(formUtils.normalizeCascadingRefsValue([""])) }) }) })