|
| 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 & 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 | +}); |
0 commit comments