|
| 1 | +import { CEDAR_TEMPLATE_FIELD_TYPE, CedarTemplate } from '@osf/features/metadata/models'; |
| 2 | +import { FilterOperatorOption } from '@osf/shared/models/search/discoverable-filter.model'; |
| 3 | + |
| 4 | +import { CedarTemplateFilterMapper } from './cedar-template-filter.mapper'; |
| 5 | + |
| 6 | +const CEDAR_BASE = 'https://schema.metadatacenter.org/properties/'; |
| 7 | + |
| 8 | +function makeTemplate(overrides: Partial<CedarTemplate> = {}): CedarTemplate { |
| 9 | + return { |
| 10 | + '@id': 'https://repo.metadatacenter.org/templates/test', |
| 11 | + '@type': 'https://schema.metadatacenter.org/core/Template', |
| 12 | + type: 'object', |
| 13 | + title: 'Test', |
| 14 | + description: '', |
| 15 | + $schema: 'http://json-schema.org/draft-04/schema', |
| 16 | + '@context': {} as never, |
| 17 | + required: [], |
| 18 | + properties: { |
| 19 | + '@context': { |
| 20 | + properties: { |
| 21 | + 'School Type': { enum: [`${CEDAR_BASE}uuid-school-type`] }, |
| 22 | + 'Study Design': { enum: [`${CEDAR_BASE}uuid-study-design`] }, |
| 23 | + About: { enum: [`${CEDAR_BASE}uuid-about`] }, |
| 24 | + }, |
| 25 | + }, |
| 26 | + 'School Type': { |
| 27 | + '@type': CEDAR_TEMPLATE_FIELD_TYPE, |
| 28 | + _valueConstraints: { |
| 29 | + literals: [{ label: 'High School' }, { label: 'Middle School' }], |
| 30 | + }, |
| 31 | + }, |
| 32 | + 'Study Design': { |
| 33 | + '@type': CEDAR_TEMPLATE_FIELD_TYPE, |
| 34 | + _valueConstraints: { |
| 35 | + literals: [{ label: 'Intervention' }, { label: 'Correlational' }], |
| 36 | + }, |
| 37 | + }, |
| 38 | + About: { |
| 39 | + '@type': 'https://schema.metadatacenter.org/core/StaticTemplateField', |
| 40 | + _ui: { inputType: 'richtext' }, |
| 41 | + }, |
| 42 | + }, |
| 43 | + _ui: { |
| 44 | + order: ['School Type', 'Study Design', 'About'], |
| 45 | + propertyLabels: { 'School Type': 'School Type', 'Study Design': 'Study Design', About: 'About' }, |
| 46 | + propertyDescriptions: {}, |
| 47 | + }, |
| 48 | + ...overrides, |
| 49 | + }; |
| 50 | +} |
| 51 | + |
| 52 | +describe('CedarTemplateFilterMapper', () => { |
| 53 | + describe('fromTemplate', () => { |
| 54 | + it('should only include TemplateField entries with literals', () => { |
| 55 | + const filters = CedarTemplateFilterMapper.fromTemplate(makeTemplate()); |
| 56 | + |
| 57 | + expect(filters).toHaveLength(2); |
| 58 | + expect(filters.map((f) => f.key)).toEqual(['School Type', 'Study Design']); |
| 59 | + }); |
| 60 | + |
| 61 | + it('should skip StaticTemplateField entries', () => { |
| 62 | + const filters = CedarTemplateFilterMapper.fromTemplate(makeTemplate()); |
| 63 | + |
| 64 | + expect(filters.some((f) => f.key === 'About')).toBe(false); |
| 65 | + }); |
| 66 | + |
| 67 | + it('should pre-populate options from _valueConstraints.literals', () => { |
| 68 | + const filters = CedarTemplateFilterMapper.fromTemplate(makeTemplate()); |
| 69 | + const schoolType = filters.find((f) => f.key === 'School Type')!; |
| 70 | + |
| 71 | + expect(schoolType.options).toHaveLength(2); |
| 72 | + expect(schoolType.options![0]).toEqual({ |
| 73 | + label: 'High School', |
| 74 | + value: 'High School', |
| 75 | + cardSearchResultCount: null, |
| 76 | + }); |
| 77 | + expect(schoolType.options![1]).toEqual({ |
| 78 | + label: 'Middle School', |
| 79 | + value: 'Middle School', |
| 80 | + cardSearchResultCount: null, |
| 81 | + }); |
| 82 | + }); |
| 83 | + |
| 84 | + it('should set cardSearchResultCount to null for all options', () => { |
| 85 | + const filters = CedarTemplateFilterMapper.fromTemplate(makeTemplate()); |
| 86 | + |
| 87 | + filters.forEach((f) => { |
| 88 | + f.options?.forEach((opt) => { |
| 89 | + expect(opt.cardSearchResultCount).toBeNull(); |
| 90 | + }); |
| 91 | + }); |
| 92 | + }); |
| 93 | + |
| 94 | + it('should set cedarPropertyIri to the UUID from the context IRI', () => { |
| 95 | + const filters = CedarTemplateFilterMapper.fromTemplate(makeTemplate()); |
| 96 | + const schoolType = filters.find((f) => f.key === 'School Type')!; |
| 97 | + const studyDesign = filters.find((f) => f.key === 'Study Design')!; |
| 98 | + |
| 99 | + expect(schoolType.cedarPropertyIri).toBe('uuid-school-type'); |
| 100 | + expect(studyDesign.cedarPropertyIri).toBe('uuid-study-design'); |
| 101 | + }); |
| 102 | + |
| 103 | + it('should set operator to AnyOf', () => { |
| 104 | + const filters = CedarTemplateFilterMapper.fromTemplate(makeTemplate()); |
| 105 | + |
| 106 | + filters.forEach((f) => { |
| 107 | + expect(f.operator).toBe(FilterOperatorOption.AnyOf); |
| 108 | + }); |
| 109 | + }); |
| 110 | + |
| 111 | + it('should use propertyLabels for the filter label', () => { |
| 112 | + const filters = CedarTemplateFilterMapper.fromTemplate(makeTemplate()); |
| 113 | + |
| 114 | + expect(filters[0].label).toBe('School Type'); |
| 115 | + expect(filters[1].label).toBe('Study Design'); |
| 116 | + }); |
| 117 | + |
| 118 | + it('should skip fields with no literals', () => { |
| 119 | + const template = makeTemplate(); |
| 120 | + (template.properties['School Type'] as any)._valueConstraints = { literals: [] }; |
| 121 | + |
| 122 | + const filters = CedarTemplateFilterMapper.fromTemplate(template); |
| 123 | + |
| 124 | + expect(filters.some((f) => f.key === 'School Type')).toBe(false); |
| 125 | + }); |
| 126 | + |
| 127 | + it('should skip fields with an empty label', () => { |
| 128 | + const template = makeTemplate(); |
| 129 | + template._ui.propertyLabels['School Type'] = ' '; |
| 130 | + |
| 131 | + const filters = CedarTemplateFilterMapper.fromTemplate(template); |
| 132 | + |
| 133 | + expect(filters.some((f) => f.key === 'School Type')).toBe(false); |
| 134 | + }); |
| 135 | + |
| 136 | + it('should return an empty array when no filterable fields exist', () => { |
| 137 | + const template = makeTemplate({ |
| 138 | + _ui: { order: ['About'], propertyLabels: { About: 'About' }, propertyDescriptions: {} }, |
| 139 | + }); |
| 140 | + |
| 141 | + expect(CedarTemplateFilterMapper.fromTemplate(template)).toEqual([]); |
| 142 | + }); |
| 143 | + }); |
| 144 | +}); |
0 commit comments