|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import { parseExpression } from "vega-expression"; |
| 3 | +import { sanitizeFieldName } from "./util"; |
| 4 | + |
| 5 | +describe("sanitizeFieldName", () => { |
| 6 | + it("keeps simple field names readable", () => { |
| 7 | + expect(sanitizeFieldName("total_sales")).toBe("rill_total_sales"); |
| 8 | + }); |
| 9 | + |
| 10 | + it("returns a valid Vega expression function name for measure names with operators", () => { |
| 11 | + const measureNames: [string, string][] = [ |
| 12 | + ["Total Sample Revenue", "rill_Total_u20_Sample_u20_Revenue"], |
| 13 | + ["Sample Rate* Lift", "rill_Sample_u20_Rate_u2a__u20_Lift"], |
| 14 | + [ |
| 15 | + "Share(%) | Variant A", |
| 16 | + "rill_Share_u28__u25__u29__u20__u7c__u20_Variant_u20_A", |
| 17 | + ], |
| 18 | + [ |
| 19 | + "Share(%) | Baseline", |
| 20 | + "rill_Share_u28__u25__u29__u20__u7c__u20_Baseline", |
| 21 | + ], |
| 22 | + [ |
| 23 | + "Avg Sample Value | Variant A", |
| 24 | + "rill_Avg_u20_Sample_u20_Value_u20__u7c__u20_Variant_u20_A", |
| 25 | + ], |
| 26 | + [ |
| 27 | + "Avg Sample Value | Baseline", |
| 28 | + "rill_Avg_u20_Sample_u20_Value_u20__u7c__u20_Baseline", |
| 29 | + ], |
| 30 | + [ |
| 31 | + "Success Rate | Variant A", |
| 32 | + "rill_Success_u20_Rate_u20__u7c__u20_Variant_u20_A", |
| 33 | + ], |
| 34 | + [ |
| 35 | + "Success Rate | Baseline", |
| 36 | + "rill_Success_u20_Rate_u20__u7c__u20_Baseline", |
| 37 | + ], |
| 38 | + ["Success Rate | Delta", "rill_Success_u20_Rate_u20__u7c__u20_Delta"], |
| 39 | + ]; |
| 40 | + |
| 41 | + for (const [measureName, expectedFormatType] of measureNames) { |
| 42 | + const formatType = sanitizeFieldName(measureName); |
| 43 | + |
| 44 | + expect(formatType).toBe(expectedFormatType); |
| 45 | + expect(() => parseExpression(`${formatType}(datum.value)`)).not.toThrow(); |
| 46 | + } |
| 47 | + }); |
| 48 | +}); |
0 commit comments