|
1 | | - |
2 | 1 | import { describe, it } from "node:test"; |
3 | 2 | import assert from "node:assert"; |
4 | 3 |
|
@@ -183,5 +182,45 @@ describe("flattenContext", () => { |
183 | 182 | a_0__b_1__c_3: 25, |
184 | 183 | }, |
185 | 184 | ) |
186 | | - }) |
| 185 | + }); |
| 186 | +}); |
| 187 | + |
| 188 | +describe("badge", () => { |
| 189 | + it("should return the length of an array or string", () => { |
| 190 | + const expression = `BADGE($var)`; |
| 191 | + const context = {$var: [100]}; |
| 192 | + assert.equal(evaluate(expression, context), "1"); |
| 193 | + const context2 = {$var: [1,2,3]}; |
| 194 | + assert.equal(evaluate(expression, context2), "3"); |
| 195 | + const context3 = {$var: 'hello'}; |
| 196 | + assert.equal(evaluate(expression, context3), "5"); |
| 197 | + const context4 = {$var: {"a": "1", "b": "2"}}; |
| 198 | + assert.equal(evaluate(expression, context4), ""); |
| 199 | + }); |
| 200 | + it("should return 0 if the value is not an array or is empty or undefined", () => { |
| 201 | + const expression = `BADGE($var)`; |
| 202 | + const context = {$var: []}; |
| 203 | + assert.equal(evaluate(expression, context), ""); |
| 204 | + const context2 = {$var: undefined}; |
| 205 | + assert.equal(evaluate(expression, context2), ""); |
| 206 | + const context3 = {$var: 1}; |
| 207 | + assert.equal(evaluate(expression, context3), ""); |
| 208 | + }); |
| 209 | + it("should return the length of an array, which is populated with objects", () => { |
| 210 | + const expression = `BADGE($local.add_media__media)`; |
| 211 | + const context = { |
| 212 | + $local: { |
| 213 | + add_media__media: [ |
| 214 | + { id: 1, name: "a" }, |
| 215 | + { id: 2, name: { abc: 1 }}, |
| 216 | + ] |
| 217 | + } |
| 218 | + }; |
| 219 | + assert.equal(evaluate(expression, context), "2"); |
| 220 | + }); |
| 221 | + it("should include the given prefix/postfix when provided", () => { |
| 222 | + const expression = `BADGE($var,'count:[',']')`; |
| 223 | + const context = {$var: [100]}; |
| 224 | + assert.equal(evaluate(expression, context), "count:[1]"); |
| 225 | + }); |
187 | 226 | }); |
0 commit comments