From ba1cf29b9499a7103ad21f9acfc9b25348e4f90d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9l=C3=A8ne=20Martin?= Date: Fri, 6 Jan 2023 14:42:47 -0800 Subject: [PATCH] Add test of relevance on calculates --- .../core/model/TriggerableDagTest.java | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/test/java/org/javarosa/core/model/TriggerableDagTest.java b/src/test/java/org/javarosa/core/model/TriggerableDagTest.java index 2d1361148..bcfc9638a 100644 --- a/src/test/java/org/javarosa/core/model/TriggerableDagTest.java +++ b/src/test/java/org/javarosa/core/model/TriggerableDagTest.java @@ -530,6 +530,33 @@ public void non_relevant_node_values_are_always_null_regardless_of_their_actual_ assertThat(scenario.answerOf("/data/some-field"), is(intAnswer(42))); } + // Users use relevance on calculates to ensure that calculations are only run when the values + // they need are populated. Using relevance is easier than adding a condition. + @Test + public void relevance_appliesToElementsWithoutControls() throws IOException, XFormParser.ParseException { + Scenario scenario = Scenario.init("Relevance on calculate", html( + head( + title("Relevance on calculate"), + model( + mainInstance(t("data id=\"relevance-calculate\"", + t("q1"), + t("c1"), + t("c2") + )), + bind("/data/q1").type("string"), + bind("/data/c1").calculate("2 * 2").relevant("/data/q1 = 'yes'"), + bind("/data/c2").calculate("/data/c1") + )), + body( + input("/data/q1") + ))); + + assertThat(scenario.answerOf("/data/c2"), is(nullValue())); + + scenario.answer("/data/q1", "yes"); + assertThat(scenario.answerOf("/data/c2"), is(intAnswer(4))); + } + /** * This test was inspired by the issue reported at https://code.google.com/archive/p/opendatakit/issues/888 *