Skip to content

Commit

Permalink
Treat if merge with false string as false (#4569)
Browse files Browse the repository at this point in the history
  • Loading branch information
humandad authored Feb 6, 2025
1 parent 56870fb commit 32ff5c0
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
53 changes: 53 additions & 0 deletions libs/ui/src/context/merge.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,51 @@ const mergeFormulaTestCases: MergeWithContextTestCase[] = [
},
]

const mergeIfTestCases: MergeWithContextTestCase[] = [
{
name: "simple if statement true",
context: new Context(),
input: "$If{[true][a][b]}",
expected: "a",
},
{
name: "simple if statement false",
context: new Context(),
input: "$If{[false][a][b]}",
expected: "b",
},
{
name: "if with context value present",
context: new Context().addRecordDataFrame({
foo: "A value",
}),
input: "$If{[${foo}][a][b]}",
expected: "a",
},
{
name: "if with context value empty string",
context: new Context().addRecordDataFrame({
foo: "",
}),
input: "$If{[${foo}][a][b]}",
expected: "b",
},
{
name: "if with context value missing",
context: new Context().addRecordDataFrame({}),
input: "$If{[${foo}][a][b]}",
expected: "b",
},
{
name: "if with context value false string",
context: new Context().addRecordDataFrame({
foo: "false",
}),
input: "$If{[${foo}][a][b]}",
expected: "b",
},
]

describe("merge", () => {
describe("$SignalOutput context", () => {
signalOutputMergeTestCases.forEach((tc) => {
Expand Down Expand Up @@ -679,4 +724,12 @@ describe("merge", () => {
})
})
})

describe("mergeIf", () => {
mergeIfTestCases.forEach((tc) => {
test(tc.name, () => {
expect(tc.context.merge(tc.input, tc.options)).toEqual(tc.expected)
})
})
})
})
3 changes: 2 additions & 1 deletion libs/ui/src/context/merge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,8 @@ const handlers: Record<MergeType, MergeHandler> = {
} catch {
throw InvalidIfMergeMsg
}
return context.merge(parts[0])
const conditionResult = context.merge(parts[0])
return conditionResult && conditionResult !== "false"
? context.merge(parts[1])
: context.merge(parts[2])
},
Expand Down

0 comments on commit 32ff5c0

Please sign in to comment.