Skip to content

Commit 644344b

Browse files
authored
[ENG-12319] Registration schema responses empty in API (#1065)
- Ticket: https://openscience.atlassian.net/browse/ENG-12319 - Feature flag: n/a ## Summary of Changes 1. Fixed issue with no `page-heading`. 2. Added unit tests for mapper.
1 parent 3401534 commit 644344b

2 files changed

Lines changed: 169 additions & 0 deletions

File tree

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
import { BlockType } from '@osf/shared/enums/block-type.enum';
2+
import { FieldType } from '@osf/shared/enums/field-type.enum';
3+
import { SchemaBlocksResponseJsonApi } from '@osf/shared/models/registration/schema-blocks-json-api.model';
4+
5+
import { PageSchemaMapper } from './page-schema.mapper';
6+
7+
type SchemaBlock = SchemaBlocksResponseJsonApi['data'][number];
8+
9+
function block(id: string, blockType: BlockType, overrides: Partial<SchemaBlock['attributes']> = {}): SchemaBlock {
10+
return {
11+
id,
12+
type: 'schema-blocks',
13+
attributes: {
14+
block_type: blockType,
15+
display_text: '',
16+
example_text: '',
17+
help_text: '',
18+
index: 0,
19+
registration_response_key: null,
20+
required: false,
21+
schema_block_group_key: id,
22+
...overrides,
23+
},
24+
};
25+
}
26+
27+
function response(blocks: SchemaBlock[]): SchemaBlocksResponseJsonApi {
28+
return { data: blocks, meta: { total: blocks.length } };
29+
}
30+
31+
describe('PageSchemaMapper', () => {
32+
it('should return no pages when the response has no blocks', () => {
33+
expect(PageSchemaMapper.fromSchemaBlocksResponse(response([]))).toEqual([]);
34+
});
35+
36+
it('should map a page heading and attach its question', () => {
37+
const pages = PageSchemaMapper.fromSchemaBlocksResponse(
38+
response([
39+
block('page-1', BlockType.PageHeading, { display_text: 'Study &amp; design', help_text: 'Help' }),
40+
block('q-1', BlockType.QuestionLabel, {
41+
display_text: 'Research questions',
42+
schema_block_group_key: 'g1',
43+
}),
44+
block('input-1', BlockType.LongTextInput, {
45+
registration_response_key: 'q1',
46+
required: true,
47+
schema_block_group_key: 'g1',
48+
}),
49+
])
50+
);
51+
52+
expect(pages).toEqual([
53+
{
54+
id: 'page-1',
55+
title: 'Study & design',
56+
helpText: 'Help',
57+
questions: [
58+
{
59+
id: 'q-1',
60+
displayText: 'Research questions',
61+
helpText: '',
62+
exampleText: '',
63+
required: true,
64+
groupKey: 'g1',
65+
responseKey: 'q1',
66+
fieldType: FieldType.TextArea,
67+
},
68+
],
69+
},
70+
]);
71+
});
72+
73+
it('should attach section questions to the section', () => {
74+
const pages = PageSchemaMapper.fromSchemaBlocksResponse(
75+
response([
76+
block('page-1', BlockType.PageHeading, { display_text: 'Methods' }),
77+
block('section-1', BlockType.SectionHeading, { display_text: 'Analysis' }),
78+
block('q-1', BlockType.QuestionLabel, { display_text: 'Data processing', schema_block_group_key: 'g1' }),
79+
block('input-1', BlockType.ShortTextInput, {
80+
registration_response_key: 'q2',
81+
required: true,
82+
schema_block_group_key: 'g1',
83+
}),
84+
])
85+
);
86+
87+
expect(pages[0].questions).toEqual([]);
88+
expect(pages[0].sections).toEqual([
89+
{
90+
id: 'section-1',
91+
title: 'Analysis',
92+
helpText: '',
93+
questions: [
94+
{
95+
id: 'q-1',
96+
displayText: 'Data processing',
97+
helpText: '',
98+
exampleText: '',
99+
required: true,
100+
groupKey: 'g1',
101+
responseKey: 'q2',
102+
fieldType: FieldType.Text,
103+
},
104+
],
105+
},
106+
]);
107+
});
108+
109+
it('should wrap questions without a page heading into one page', () => {
110+
const pages = PageSchemaMapper.fromSchemaBlocksResponse(
111+
response([
112+
block('q-1', BlockType.QuestionLabel, { display_text: 'Research questions', schema_block_group_key: 'g1' }),
113+
block('input-1', BlockType.LongTextInput, {
114+
registration_response_key: 'q1',
115+
required: true,
116+
schema_block_group_key: 'g1',
117+
}),
118+
block('q-2', BlockType.QuestionLabel, { display_text: 'Upload files', schema_block_group_key: 'g2' }),
119+
block('input-2', BlockType.FileInput, {
120+
registration_response_key: '53-7',
121+
required: false,
122+
schema_block_group_key: 'g2',
123+
}),
124+
])
125+
);
126+
127+
expect(pages).toEqual([
128+
{
129+
id: 'q-1',
130+
title: '',
131+
questions: [
132+
{
133+
id: 'q-1',
134+
displayText: 'Research questions',
135+
helpText: '',
136+
exampleText: '',
137+
required: true,
138+
groupKey: 'g1',
139+
responseKey: 'q1',
140+
fieldType: FieldType.TextArea,
141+
},
142+
{
143+
id: 'q-2',
144+
displayText: 'Upload files',
145+
helpText: '',
146+
exampleText: '',
147+
required: false,
148+
groupKey: 'g2',
149+
responseKey: '53-7',
150+
fieldType: FieldType.File,
151+
},
152+
],
153+
},
154+
]);
155+
});
156+
});

‎src/app/shared/mappers/registration/page-schema.mapper.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { SchemaBlocksResponseJsonApi } from '@shared/models/registration/schema-
77
export class PageSchemaMapper {
88
static fromSchemaBlocksResponse(response: SchemaBlocksResponseJsonApi): PageSchema[] {
99
const pages: PageSchema[] = [];
10+
const unpagedQuestions: Question[] = [];
1011
let currentPage!: PageSchema;
1112
let currentQuestion: Question | null = null;
1213
let currentSection: Section | null = null;
@@ -75,6 +76,8 @@ export class PageSchemaMapper {
7576
} else if (currentPage) {
7677
currentPage.questions = currentPage.questions || [];
7778
currentPage.questions.push(currentQuestion);
79+
} else {
80+
unpagedQuestions.push(currentQuestion);
7881
}
7982
break;
8083

@@ -94,6 +97,8 @@ export class PageSchemaMapper {
9497
} else if (currentPage) {
9598
currentPage.questions = currentPage.questions || [];
9699
currentPage.questions.push(currentQuestion);
100+
} else {
101+
unpagedQuestions.push(currentQuestion);
97102
}
98103
break;
99104

@@ -151,6 +156,14 @@ export class PageSchemaMapper {
151156
}
152157
});
153158

159+
if (!pages.length && unpagedQuestions.length) {
160+
pages.push({
161+
id: unpagedQuestions[0].id,
162+
title: '',
163+
questions: unpagedQuestions,
164+
});
165+
}
166+
154167
return pages;
155168
}
156169
}

0 commit comments

Comments
 (0)