|
| 1 | +import Big from "big.js"; |
| 2 | +import { EditableValue } from "mendix"; |
| 3 | +import { dynamic, EditableValueBuilder } from "@mendix/widget-plugin-test-utils"; |
| 4 | +import { getReadonly } from "../utils"; |
| 5 | + |
| 6 | +describe("getReadonly", () => { |
| 7 | + describe("when targetAttribute is readOnly", () => { |
| 8 | + it("is read-only regardless of customEditability 'default'", () => { |
| 9 | + const targetAttribute = new EditableValueBuilder<string>().withValue("value").isReadOnly().build(); |
| 10 | + |
| 11 | + const result = getReadonly( |
| 12 | + targetAttribute as unknown as EditableValue<string | Big>, |
| 13 | + "default", |
| 14 | + dynamic.available(true) |
| 15 | + ); |
| 16 | + |
| 17 | + expect(result).toBe(true); |
| 18 | + }); |
| 19 | + |
| 20 | + it("is read-only regardless of customEditability 'never'", () => { |
| 21 | + const targetAttribute = new EditableValueBuilder<string>().withValue("value").isReadOnly().build(); |
| 22 | + |
| 23 | + const result = getReadonly( |
| 24 | + targetAttribute as unknown as EditableValue<string | Big>, |
| 25 | + "never", |
| 26 | + dynamic.available(true) |
| 27 | + ); |
| 28 | + |
| 29 | + expect(result).toBe(true); |
| 30 | + }); |
| 31 | + |
| 32 | + it("is read-only regardless of customEditability 'conditionally' with expression true", () => { |
| 33 | + const targetAttribute = new EditableValueBuilder<string>().withValue("value").isReadOnly().build(); |
| 34 | + |
| 35 | + const result = getReadonly( |
| 36 | + targetAttribute as unknown as EditableValue<string | Big>, |
| 37 | + "conditionally", |
| 38 | + dynamic.available(true) |
| 39 | + ); |
| 40 | + |
| 41 | + expect(result).toBe(true); |
| 42 | + }); |
| 43 | + }); |
| 44 | + |
| 45 | + describe("when targetAttribute is editable", () => { |
| 46 | + it("is editable when customEditability is 'default'", () => { |
| 47 | + const targetAttribute = new EditableValueBuilder<string>().withValue("value").build(); |
| 48 | + |
| 49 | + const result = getReadonly( |
| 50 | + targetAttribute as unknown as EditableValue<string | Big>, |
| 51 | + "default", |
| 52 | + dynamic.available(true) |
| 53 | + ); |
| 54 | + |
| 55 | + expect(result).toBe(false); |
| 56 | + }); |
| 57 | + |
| 58 | + it("is read-only when customEditability is 'never'", () => { |
| 59 | + const targetAttribute = new EditableValueBuilder<string>().withValue("value").build(); |
| 60 | + |
| 61 | + const result = getReadonly( |
| 62 | + targetAttribute as unknown as EditableValue<string | Big>, |
| 63 | + "never", |
| 64 | + dynamic.available(true) |
| 65 | + ); |
| 66 | + |
| 67 | + expect(result).toBe(true); |
| 68 | + }); |
| 69 | + |
| 70 | + it("is read-only when customEditability is 'conditionally' and expression value is false", () => { |
| 71 | + const targetAttribute = new EditableValueBuilder<string>().withValue("value").build(); |
| 72 | + |
| 73 | + const result = getReadonly( |
| 74 | + targetAttribute as unknown as EditableValue<string | Big>, |
| 75 | + "conditionally", |
| 76 | + dynamic.available(false) |
| 77 | + ); |
| 78 | + |
| 79 | + expect(result).toBe(true); |
| 80 | + }); |
| 81 | + |
| 82 | + it("is editable when customEditability is 'conditionally' and expression value is true", () => { |
| 83 | + const targetAttribute = new EditableValueBuilder<string>().withValue("value").build(); |
| 84 | + |
| 85 | + const result = getReadonly( |
| 86 | + targetAttribute as unknown as EditableValue<string | Big>, |
| 87 | + "conditionally", |
| 88 | + dynamic.available(true) |
| 89 | + ); |
| 90 | + |
| 91 | + expect(result).toBe(false); |
| 92 | + }); |
| 93 | + |
| 94 | + it("is read-only when customEditability is 'conditionally' and expression is loading", () => { |
| 95 | + const targetAttribute = new EditableValueBuilder<string>().withValue("value").build(); |
| 96 | + |
| 97 | + const result = getReadonly( |
| 98 | + targetAttribute as unknown as EditableValue<string | Big>, |
| 99 | + "conditionally", |
| 100 | + dynamic.loading() |
| 101 | + ); |
| 102 | + |
| 103 | + expect(result).toBe(true); |
| 104 | + }); |
| 105 | + |
| 106 | + it("is read-only when customEditability is 'conditionally' and expression is unavailable", () => { |
| 107 | + const targetAttribute = new EditableValueBuilder<string>().withValue("value").build(); |
| 108 | + |
| 109 | + const result = getReadonly( |
| 110 | + targetAttribute as unknown as EditableValue<string | Big>, |
| 111 | + "conditionally", |
| 112 | + dynamic.unavailable() |
| 113 | + ); |
| 114 | + |
| 115 | + expect(result).toBe(true); |
| 116 | + }); |
| 117 | + }); |
| 118 | + |
| 119 | + describe("when targetAttribute is undefined", () => { |
| 120 | + it("is read-only when customEditability is 'never'", () => { |
| 121 | + const result = getReadonly(undefined, "never", dynamic.available(true)); |
| 122 | + |
| 123 | + expect(result).toBe(true); |
| 124 | + }); |
| 125 | + |
| 126 | + it("is editable when customEditability is 'default'", () => { |
| 127 | + const result = getReadonly(undefined, "default", dynamic.available(false)); |
| 128 | + |
| 129 | + expect(result).toBe(false); |
| 130 | + }); |
| 131 | + |
| 132 | + it("is read-only when customEditability is 'conditionally' and expression value is false", () => { |
| 133 | + const result = getReadonly(undefined, "conditionally", dynamic.available(false)); |
| 134 | + |
| 135 | + expect(result).toBe(true); |
| 136 | + }); |
| 137 | + |
| 138 | + it("is editable when customEditability is 'conditionally' and expression value is true", () => { |
| 139 | + const result = getReadonly(undefined, "conditionally", dynamic.available(true)); |
| 140 | + |
| 141 | + expect(result).toBe(false); |
| 142 | + }); |
| 143 | + |
| 144 | + it("is read-only when customEditability is 'conditionally' and expression is loading", () => { |
| 145 | + const result = getReadonly(undefined, "conditionally", dynamic.loading()); |
| 146 | + |
| 147 | + expect(result).toBe(true); |
| 148 | + }); |
| 149 | + |
| 150 | + it("is read-only when customEditability is 'conditionally' and expression is unavailable", () => { |
| 151 | + const result = getReadonly(undefined, "conditionally", dynamic.unavailable()); |
| 152 | + |
| 153 | + expect(result).toBe(true); |
| 154 | + }); |
| 155 | + }); |
| 156 | +}); |
0 commit comments