|
| 1 | +import type { EndUserCondition, Field } from "./data-types"; |
| 2 | +import { getVisibleFields } from "./getVisibleFields"; |
| 3 | + |
| 4 | +const dropdownField: Field = { |
| 5 | + id: 123, |
| 6 | + name: "request[custom_fields][123]", |
| 7 | + required: false, |
| 8 | + error: null, |
| 9 | + label: "Dropdown Field", |
| 10 | + description: "", |
| 11 | + type: "tagger", |
| 12 | + options: [ |
| 13 | + { name: "One", value: "one" }, |
| 14 | + { name: "Two", value: "two" }, |
| 15 | + ], |
| 16 | +}; |
| 17 | + |
| 18 | +const secondDropdownField: Field = { |
| 19 | + id: 456, |
| 20 | + name: "request[custom_fields][456]", |
| 21 | + required: false, |
| 22 | + error: null, |
| 23 | + label: "Second Dropdown Field", |
| 24 | + description: "", |
| 25 | + type: "tagger", |
| 26 | + options: [ |
| 27 | + { name: "Three", value: "three" }, |
| 28 | + { name: "Four", value: "four" }, |
| 29 | + ], |
| 30 | +}; |
| 31 | + |
| 32 | +const textField: Field = { |
| 33 | + id: 789, |
| 34 | + name: "request[custom_fields][789]", |
| 35 | + required: false, |
| 36 | + error: null, |
| 37 | + label: "Text Field", |
| 38 | + description: "", |
| 39 | + type: "text", |
| 40 | + options: [], |
| 41 | +}; |
| 42 | + |
| 43 | +const integerField: Field = { |
| 44 | + id: 101, |
| 45 | + name: "request[custom_fields][101]", |
| 46 | + required: false, |
| 47 | + error: null, |
| 48 | + label: "Number Field", |
| 49 | + description: "", |
| 50 | + type: "number", |
| 51 | + options: [], |
| 52 | +}; |
| 53 | + |
| 54 | +describe("getVisibleFields", () => { |
| 55 | + it("should return the same fields if no end user conditions are provided", () => { |
| 56 | + const fields = [ |
| 57 | + dropdownField, |
| 58 | + secondDropdownField, |
| 59 | + textField, |
| 60 | + integerField, |
| 61 | + ]; |
| 62 | + const endUserConditions: EndUserCondition[] = []; |
| 63 | + |
| 64 | + const result = getVisibleFields(fields, endUserConditions); |
| 65 | + |
| 66 | + expect(result).toEqual(fields); |
| 67 | + }); |
| 68 | + |
| 69 | + it("should show fields and set required if the value matches", () => { |
| 70 | + const fields = [ |
| 71 | + { ...dropdownField, value: "one" }, |
| 72 | + { ...secondDropdownField, value: "three" }, |
| 73 | + { ...textField, required: true }, |
| 74 | + integerField, |
| 75 | + ]; |
| 76 | + const endUserConditions: EndUserCondition[] = [ |
| 77 | + { |
| 78 | + parent_field_id: 123, |
| 79 | + parent_field_type: "tagger", |
| 80 | + value: "one", |
| 81 | + child_fields: [{ id: 456, is_required: true }], |
| 82 | + }, |
| 83 | + { |
| 84 | + parent_field_id: 456, |
| 85 | + parent_field_type: "tagger", |
| 86 | + value: "three", |
| 87 | + child_fields: [{ id: 789, is_required: false }], |
| 88 | + }, |
| 89 | + ]; |
| 90 | + |
| 91 | + const result = getVisibleFields(fields, endUserConditions); |
| 92 | + |
| 93 | + expect(result).toEqual([ |
| 94 | + { ...dropdownField, value: "one" }, |
| 95 | + { ...secondDropdownField, value: "three", required: true }, |
| 96 | + textField, |
| 97 | + integerField, |
| 98 | + ]); |
| 99 | + }); |
| 100 | + |
| 101 | + it("should hide fields if the value does not match", () => { |
| 102 | + const fields = [ |
| 103 | + { ...dropdownField, value: "one" }, |
| 104 | + { ...secondDropdownField, value: "three" }, |
| 105 | + textField, |
| 106 | + integerField, |
| 107 | + ]; |
| 108 | + const endUserConditions: EndUserCondition[] = [ |
| 109 | + { |
| 110 | + parent_field_id: 123, |
| 111 | + parent_field_type: "tagger", |
| 112 | + value: "two", |
| 113 | + child_fields: [{ id: 456, is_required: true }], |
| 114 | + }, |
| 115 | + { |
| 116 | + parent_field_id: 456, |
| 117 | + parent_field_type: "tagger", |
| 118 | + value: "four", |
| 119 | + child_fields: [{ id: 789, is_required: false }], |
| 120 | + }, |
| 121 | + ]; |
| 122 | + |
| 123 | + const result = getVisibleFields(fields, endUserConditions); |
| 124 | + |
| 125 | + expect(result).toEqual([{ ...dropdownField, value: "one" }, integerField]); |
| 126 | + }); |
| 127 | + |
| 128 | + it("should not change required if the field is not part of some condition", () => { |
| 129 | + const fields = [ |
| 130 | + { ...dropdownField, value: "one", required: true }, |
| 131 | + { ...secondDropdownField, value: "three" }, |
| 132 | + { ...textField, required: true }, |
| 133 | + { ...integerField, required: false }, |
| 134 | + ]; |
| 135 | + const endUserConditions: EndUserCondition[] = [ |
| 136 | + { |
| 137 | + parent_field_id: 123, |
| 138 | + parent_field_type: "tagger", |
| 139 | + value: "one", |
| 140 | + child_fields: [{ id: 456, is_required: true }], |
| 141 | + }, |
| 142 | + ]; |
| 143 | + |
| 144 | + const result = getVisibleFields(fields, endUserConditions); |
| 145 | + |
| 146 | + expect(result).toEqual([ |
| 147 | + { ...dropdownField, value: "one", required: true }, |
| 148 | + { ...secondDropdownField, value: "three", required: true }, |
| 149 | + { ...textField, required: true }, |
| 150 | + { ...integerField, required: false }, |
| 151 | + ]); |
| 152 | + }); |
| 153 | + |
| 154 | + it("should hide fields with multi level conditions when the value on parent doesn't match", () => { |
| 155 | + const fields = [ |
| 156 | + { ...dropdownField, value: "one" }, |
| 157 | + { ...secondDropdownField, value: "three" }, |
| 158 | + { ...textField, value: "text" }, |
| 159 | + integerField, |
| 160 | + ]; |
| 161 | + const endUserConditions: EndUserCondition[] = [ |
| 162 | + { |
| 163 | + parent_field_id: 123, |
| 164 | + parent_field_type: "tagger", |
| 165 | + value: "two", |
| 166 | + child_fields: [ |
| 167 | + { |
| 168 | + id: 456, |
| 169 | + is_required: true, |
| 170 | + }, |
| 171 | + ], |
| 172 | + }, |
| 173 | + { |
| 174 | + parent_field_id: 456, |
| 175 | + parent_field_type: "tagger", |
| 176 | + value: "three", |
| 177 | + child_fields: [ |
| 178 | + { |
| 179 | + id: 789, |
| 180 | + is_required: false, |
| 181 | + }, |
| 182 | + ], |
| 183 | + }, |
| 184 | + { |
| 185 | + parent_field_id: 789, |
| 186 | + parent_field_type: "text", |
| 187 | + value: "text", |
| 188 | + child_fields: [ |
| 189 | + { |
| 190 | + id: 101, |
| 191 | + is_required: true, |
| 192 | + }, |
| 193 | + ], |
| 194 | + }, |
| 195 | + ]; |
| 196 | + |
| 197 | + const result = getVisibleFields(fields, endUserConditions); |
| 198 | + |
| 199 | + expect(result).toEqual([{ ...dropdownField, value: "one" }]); |
| 200 | + }); |
| 201 | + |
| 202 | + it("should not hide fields with multi level conditions when a second condition is met", () => { |
| 203 | + const fields = [ |
| 204 | + { ...dropdownField, value: "one" }, |
| 205 | + { ...secondDropdownField, value: "three" }, |
| 206 | + { ...textField, value: "text" }, |
| 207 | + integerField, |
| 208 | + ]; |
| 209 | + |
| 210 | + const endUserConditions: EndUserCondition[] = [ |
| 211 | + { |
| 212 | + parent_field_id: 123, |
| 213 | + parent_field_type: "tagger", |
| 214 | + value: "two", |
| 215 | + child_fields: [ |
| 216 | + { |
| 217 | + id: 456, |
| 218 | + is_required: true, |
| 219 | + }, |
| 220 | + ], |
| 221 | + }, |
| 222 | + { |
| 223 | + parent_field_id: 456, |
| 224 | + parent_field_type: "tagger", |
| 225 | + value: "three", |
| 226 | + child_fields: [ |
| 227 | + { |
| 228 | + id: 789, |
| 229 | + is_required: false, |
| 230 | + }, |
| 231 | + ], |
| 232 | + }, |
| 233 | + { |
| 234 | + parent_field_id: 789, |
| 235 | + parent_field_type: "text", |
| 236 | + value: "text", |
| 237 | + child_fields: [ |
| 238 | + { |
| 239 | + id: 101, |
| 240 | + is_required: true, |
| 241 | + }, |
| 242 | + ], |
| 243 | + }, |
| 244 | + { |
| 245 | + parent_field_id: 123, |
| 246 | + parent_field_type: "tagger", |
| 247 | + value: "one", |
| 248 | + child_fields: [ |
| 249 | + { |
| 250 | + id: 789, |
| 251 | + is_required: false, |
| 252 | + }, |
| 253 | + { |
| 254 | + id: 101, |
| 255 | + is_required: true, |
| 256 | + }, |
| 257 | + ], |
| 258 | + }, |
| 259 | + ]; |
| 260 | + |
| 261 | + const result = getVisibleFields(fields, endUserConditions); |
| 262 | + |
| 263 | + expect(result).toEqual([ |
| 264 | + { ...dropdownField, value: "one" }, |
| 265 | + { ...textField, value: "text" }, |
| 266 | + { ...integerField, required: true }, |
| 267 | + ]); |
| 268 | + }); |
| 269 | +}); |
0 commit comments