|
1 |
| -import { listAttr, obj, ref, refSet, list, listAction, listRef, listRefSet } from "../src/main"; |
| 1 | +import { listAttr, obj, ref, refSet, list, listAction, listRef, listRefSet, objArray, cases } from "../src/main"; |
2 | 2 |
|
3 | 3 | describe("prop mocking functions", () => {
|
4 | 4 | describe("list", () => {
|
@@ -146,4 +146,41 @@ describe("prop mocking functions", () => {
|
146 | 146 | });
|
147 | 147 | });
|
148 | 148 | });
|
| 149 | + |
| 150 | + describe("cases", () => { |
| 151 | + it("throw if there is no default case", () => { |
| 152 | + expect(() => { |
| 153 | + cases([obj(), ""]); |
| 154 | + }).toThrow(); |
| 155 | + }); |
| 156 | + |
| 157 | + it("returns mapper that takes obj and returns value from the case", () => { |
| 158 | + const item = obj(); |
| 159 | + const mapFn = cases([item, "item value"], [undefined, "None"]); |
| 160 | + expect(mapFn(item)).toBe("item value"); |
| 161 | + }); |
| 162 | + |
| 163 | + it("use default case if item not found", () => { |
| 164 | + const mapFn = cases([obj(), "item value"], [undefined, "None"]); |
| 165 | + expect(mapFn(obj())).toBe("None"); |
| 166 | + }); |
| 167 | + |
| 168 | + it("can work with listAttr", () => { |
| 169 | + const items = objArray(5); |
| 170 | + const [a, b, c] = items; |
| 171 | + const mapFn = cases([a, "Alice"], [b, "Bob"], [c, "Chuck"], [undefined, "None"]); |
| 172 | + const props = { |
| 173 | + datasource: list(items), |
| 174 | + name: listAttr(mapFn) |
| 175 | + }; |
| 176 | + |
| 177 | + expect(props.datasource.items?.map(item => props.name.get(item).value)).toEqual([ |
| 178 | + "Alice", |
| 179 | + "Bob", |
| 180 | + "Chuck", |
| 181 | + "None", |
| 182 | + "None" |
| 183 | + ]); |
| 184 | + }); |
| 185 | + }); |
149 | 186 | });
|
0 commit comments