Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
connorlbark committed Jan 7, 2025
1 parent d90dc27 commit a074a53
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,20 +59,19 @@ private static List<JsonNode> getSubQuestions(JsonNode parent) {
List<JsonNode> subQuestions = new ArrayList<>();

if (parent.get("type").asText().equals("paneldynamic") && parent.has("templateElements")) {
subQuestions = getPanelDynamicSubQuestions(parent);
// get subquestions and add parent stable id to each
subQuestions = getPanelDynamicSubQuestions(parent)
.stream()
.map(q -> (JsonNode) q.deepCopy())
.map(q -> {
((ObjectNode) q).put("parent", parent.get("name").asText());
return q;
})
.toList();
}

// keep track of the parent stableid
subQuestions = subQuestions
.stream()
.map(q -> (JsonNode) q.deepCopy())
.map(q -> {
((ObjectNode) q).put("parent", parent.get("name").asText());
return q;
})
.toList();

if (parent.get("type").asText().equals("checkbox") && parent.has("choices")) {
// not a derived value, so doesn't need parent
subQuestions = getCheckboxOtherSubquestions(parent);
}

Expand Down
113 changes: 102 additions & 11 deletions core/src/test/java/bio/terra/pearl/core/util/SurveyParseUtilsTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,19 @@
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasSize;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class SurveyParseUtilsTests extends BaseSpringBootTest {
@Autowired
Expand Down Expand Up @@ -54,7 +56,7 @@ public void testUnmarshalQuestionChoices() throws JsonProcessingException {
String expected = """
[{"stableId":"cardiacStentPlacement","text":"Cardiac stent placement"},{"stableId":"cardiacBypassSurgery","text":"Cardiac bypass surgery"},{"stableId":"noneOfThese","text":"None of these"}]""";

Assertions.assertEquals(expected, actual);
assertEquals(expected, actual);
}

@Test
Expand Down Expand Up @@ -101,7 +103,7 @@ public void testGetChildElements() throws JsonProcessingException {

List<JsonNode> actual = SurveyParseUtils.getAllQuestions(questionNode);

Assertions.assertEquals(2, actual.size());
assertEquals(2, actual.size());
}

@Test
Expand Down Expand Up @@ -134,16 +136,16 @@ public void testGetDynamicPanelElements() throws JsonProcessingException {

List<JsonNode> actual = SurveyParseUtils.getAllQuestions(questionNode);

Assertions.assertEquals(3, actual.size());
assertEquals(3, actual.size());

JsonNode panel = actual.get(0);
JsonNode firstName = actual.get(1);
JsonNode lastName = actual.get(2);


Assertions.assertEquals("examplePanel", panel.get("name").asText());
Assertions.assertEquals("firstName", firstName.get("name").asText());
Assertions.assertEquals("lastName", lastName.get("name").asText());
assertEquals("examplePanel", panel.get("name").asText());
assertEquals("firstName", firstName.get("name").asText());
assertEquals("lastName", lastName.get("name").asText());
}

@Test
Expand Down Expand Up @@ -176,7 +178,7 @@ public void testResolvingQuestionTemplate() throws JsonProcessingException {
String expected = """
[{"stableId":"cardiacStentPlacement","text":"Cardiac stent placement"},{"stableId":"cardiacBypassSurgery","text":"Cardiac bypass surgery"},{"stableId":"noneOfThese","text":"None of these"}]""";

Assertions.assertEquals(expected, actual);
assertEquals(expected, actual);
}

@Test
Expand Down Expand Up @@ -211,7 +213,7 @@ public void testParseQuestionChoicesTranslated(TestInfo info) throws JsonProcess
[{"stableId":"cardiacStentPlacement","text":"Cardiac stent placement"},{"stableId":"cardiacBypassSurgery","text":"Cardiac bypass surgery"},{"stableId":"noneOfThese","text":"None of these"}]
""";

Assertions.assertEquals(expected.strip(), actual.strip());
assertEquals(expected.strip(), actual.strip());
}

@Test
Expand All @@ -231,7 +233,7 @@ public void testResolvingQuestionDropdown() throws JsonProcessingException {
String expected = """
[{"stableId":"foo","text":"foo"},{"stableId":"bar","text":"bar"},{"stableId":"baz","text":"baz"}]""";

Assertions.assertEquals(expected, actual);
assertEquals(expected, actual);
}

@Test
Expand Down Expand Up @@ -363,10 +365,99 @@ public void testParseTitlesUnparseableForm() {
"title": {
"default": "The Basics",
"es"[]]]]]]
""";
""";

Map<String, String> parsedTitles = SurveyParseUtils.parseSurveyTitle(unparseableForm, "FallbackName");
assertThat(parsedTitles, equalTo(Map.of("en", "FallbackName")));
}

@Test
public void testParseCheckboxMultiOtherQuestions() throws JsonProcessingException {
String form = """
{
"elements": [
{
"name": "schools",
"type": "checkbox",
"title": "What schools did you attend?",
"renderAs": "checkbox-multiple-other",
"choices": [
{
"text": "MIT",
"value": "mit",
"otherStableId": "schoolsMitDetail",
"otherText": {
"en": "What did you study at MIT?",
"es": "¿Qué estudiaste en MIT?"
},
"otherPlaceHolder": {
"en": "Your major",
"es": "Tu especialidad"
}
},
{
"text": "Harvard",
"value": "harvard",
"otherStableId": "schoolsHarvardDetail",
"otherText": {
"en": "What did you study at Harvard?",
"es": "¿Qué estudiaste en Harvard?"
},
"otherPlaceHolder": {
"en": "Your major",
"es": "Tu especialidad"
}
},
{
"text": "Northeastern",
"value": "northeastern",
"otherStableId": "schoolsNortheasternDetail",
"otherText": {
"en": "What did you study at Northeastern?",
"es": "¿Qué estudiaste en Northeastern?"
},
"otherPlaceHolder": {
"en": "Your major",
"es": "Tu especialidad"
}
},
{
"text": "Boston University",
"value": "bu",
"otherStableId": "schoolsBuDetail",
"otherText": {
"en": "What did you study at Boston University?",
"es": "¿Qué estudiaste en Boston University?"
},
"otherPlaceHolder": {
"en": "Your major",
"es": "Tu especialidad"
}
},
{
"text": "Other",
"value": "other",
"otherStableId": "schoolsOtherDetail"
}
]
}
]
}
""";

List<JsonNode> questions = SurveyParseUtils.getAllQuestions(objectMapper.readTree(form));

assertThat(questions, hasSize(6));

Set<String> stableIds = questions.stream().map(q -> q.get("name").asText()).collect(Collectors.toSet());
assertEquals(Set.of(
"schools",
"schoolsMitDetail",
"schoolsHarvardDetail",
"schoolsNortheasternDetail",
"schoolsBuDetail",
"schoolsOtherDetail"),
stableIds);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@ import {

type LocalizedString = string | { [index: string]: string }

// I don't love that we have to do this manually, but
// using the "real" localization stuff via surveyjs
// makes this... a lot more complicated
const renderLocString = (locStr: LocalizedString, locale: string) => {
if (isString(locStr)) {
return locStr
}

console.log('locStr', locStr)
console.log('locale', locale)
if (isEmpty(locStr) || !locStr[locale]) {
return locStr['default'] || locStr['en'] || ''
}
Expand All @@ -47,25 +48,25 @@ type Choice = ItemValue & {
}


const OtherItem = ({ otherStableId, otherText, value, onChange } : {
otherStableId: string
otherText?: string
otherPlaceholder?: string
const OtherTextbox = ({ stableId, title, value, onChange }: {
stableId: string
title?: string
placeholder?: string
value: string
onChange: (value: string) => void
}) => {
const [otherValue, setOtherValue] = React.useState(value)

return <div className="my-1">
{!isEmpty(otherText) && <label htmlFor={otherStableId} className="h6 fw-semibold d-block">
{otherText}
{!isEmpty(title) && <label htmlFor={stableId} className="h6 fw-semibold d-block">
{title}
</label>}
<input
id={otherStableId}
id={stableId}
type='text'
className='sd-input sd-text'
value={otherValue}
aria-label={otherText}
aria-label={title}
onChange={e => {
onChange(e.target.value)
setOtherValue(e.target.value)
Expand Down Expand Up @@ -96,10 +97,10 @@ export class SurveyQuestionCheckboxMultipleOther extends SurveyQuestionCheckbox
const placeholder = otherPlaceholder && renderLocString(otherPlaceholder, survey.getLocale())

return <>
<OtherItem
otherStableId={otherStableId}
otherText={text}
otherPlaceholder={placeholder}
<OtherTextbox
stableId={otherStableId}
title={text}
placeholder={placeholder}
value={survey.getValue(otherStableId)}
onChange={(val: string) => survey.setValue(otherStableId, val)}
/>
Expand Down
2 changes: 1 addition & 1 deletion ui-core/src/surveyjs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import './autosized-signaturepad'
import './formatDate'
import './inputmask'
import './multiple-combobox-question'
import './multi-detail-checkbox'
import './checkbox-multiple-other'
import './address-validation-modal-question'
import './medications-question'

Expand Down

0 comments on commit a074a53

Please sign in to comment.