|
| 1 | +/* @flow */ |
| 2 | + |
| 3 | +import type { TypeConverterOpts } from '../../../composeWithMongoose'; |
| 4 | +import { |
| 5 | + mergeCustomizationOptions, |
| 6 | + mergeFieldMaps, |
| 7 | + mergeStringAndStringArraysFields, |
| 8 | +} from '../index'; |
| 9 | + |
| 10 | +const baseFields = { |
| 11 | + remove: ['id', 'friends', 'health', 'appearsIn'], |
| 12 | + only: ['id'], |
| 13 | +}; |
| 14 | + |
| 15 | +const childFields = { |
| 16 | + remove: ['id', 'appearsIn', 'dob', 'health'], |
| 17 | + only: ['id'], |
| 18 | +}; |
| 19 | + |
| 20 | +const expectedFields = { |
| 21 | + remove: ['id', 'friends', 'health', 'appearsIn', 'dob'], |
| 22 | + only: ['id'], |
| 23 | +}; |
| 24 | + |
| 25 | +const baseInputTypeFields = { |
| 26 | + only: ['id', 'appearsIn'], |
| 27 | + remove: ['dob', 'gender'], |
| 28 | +}; |
| 29 | + |
| 30 | +const childInputTypeFields = { |
| 31 | + only: ['id', 'friends', 'appearsIn'], |
| 32 | + remove: ['dob'], |
| 33 | + required: ['id', 'dob', 'gender'], |
| 34 | +}; |
| 35 | + |
| 36 | +const expectedInputTypes = { |
| 37 | + only: ['id', 'appearsIn', 'friends'], |
| 38 | + remove: ['dob', 'gender'], |
| 39 | + required: ['id', 'dob', 'gender'], |
| 40 | +}; |
| 41 | + |
| 42 | +const optsTypes = ['string', 'string[]']; |
| 43 | + |
| 44 | +describe('mergeStringAndStringArraysFields()', () => { |
| 45 | + it('should concat two Arrays', () => { |
| 46 | + expect( |
| 47 | + mergeStringAndStringArraysFields(baseInputTypeFields.remove, childFields.only, optsTypes[0]) |
| 48 | + ).toEqual([...baseInputTypeFields.remove, ...childFields.only]); |
| 49 | + }); |
| 50 | + |
| 51 | + it('should combine two input strings into an array', () => { |
| 52 | + expect( |
| 53 | + mergeStringAndStringArraysFields( |
| 54 | + baseInputTypeFields.remove[0], |
| 55 | + baseInputTypeFields.remove[1], |
| 56 | + optsTypes[1] |
| 57 | + ) |
| 58 | + ).toEqual(baseInputTypeFields.remove); |
| 59 | + }); |
| 60 | + |
| 61 | + it('should combine an array and a string into an array', () => { |
| 62 | + expect( |
| 63 | + mergeStringAndStringArraysFields( |
| 64 | + childInputTypeFields.required[0], |
| 65 | + baseInputTypeFields.remove, |
| 66 | + optsTypes[0] |
| 67 | + ) |
| 68 | + ).toEqual(childInputTypeFields.required); |
| 69 | + |
| 70 | + expect( |
| 71 | + mergeStringAndStringArraysFields( |
| 72 | + baseInputTypeFields.only, |
| 73 | + childInputTypeFields.only[1], |
| 74 | + optsTypes[0] |
| 75 | + ) |
| 76 | + ).toEqual(expectedInputTypes.only); |
| 77 | + }); |
| 78 | + |
| 79 | + it('should remove repeated fields from the array', () => { |
| 80 | + expect( |
| 81 | + mergeStringAndStringArraysFields(baseFields.remove, childFields.remove, optsTypes[0]) |
| 82 | + ).toEqual(expectedFields.remove); |
| 83 | + |
| 84 | + expect( |
| 85 | + mergeStringAndStringArraysFields(undefined, childInputTypeFields.required, optsTypes[0]) |
| 86 | + ).toEqual(expectedInputTypes.required); |
| 87 | + |
| 88 | + expect( |
| 89 | + mergeStringAndStringArraysFields( |
| 90 | + baseInputTypeFields.only, |
| 91 | + childInputTypeFields.only, |
| 92 | + optsTypes[0] |
| 93 | + ) |
| 94 | + ).toEqual(expectedInputTypes.only); |
| 95 | + }); |
| 96 | + |
| 97 | + it('should return an ARRAY of the defined if other one is undefined', () => { |
| 98 | + expect(mergeStringAndStringArraysFields(baseFields.remove, undefined, optsTypes[0])).toEqual( |
| 99 | + baseFields.remove |
| 100 | + ); |
| 101 | + |
| 102 | + expect(mergeStringAndStringArraysFields(undefined, childFields.only, optsTypes[0])).toEqual( |
| 103 | + childFields.only |
| 104 | + ); |
| 105 | + }); |
| 106 | + |
| 107 | + it('should operate normally with ARRAY of optsTypes', () => { |
| 108 | + expect(mergeStringAndStringArraysFields(baseFields.remove, undefined, optsTypes)).toEqual( |
| 109 | + baseFields.remove |
| 110 | + ); |
| 111 | + |
| 112 | + expect(mergeStringAndStringArraysFields(undefined, childFields.only, optsTypes)).toEqual( |
| 113 | + childFields.only |
| 114 | + ); |
| 115 | + }); |
| 116 | + |
| 117 | + it('should return child field if not amongst opsType', () => { |
| 118 | + expect(mergeStringAndStringArraysFields(baseFields.remove, undefined, 'boolean')).toEqual( |
| 119 | + undefined |
| 120 | + ); |
| 121 | + |
| 122 | + expect(mergeStringAndStringArraysFields(undefined, childFields.only, 'number')).toEqual( |
| 123 | + childFields.only |
| 124 | + ); |
| 125 | + }); |
| 126 | +}); |
| 127 | + |
| 128 | +describe('mergeFieldMaps()', () => { |
| 129 | + it('should merge fields', () => { |
| 130 | + expect(mergeFieldMaps(baseFields, childFields)).toEqual(expectedFields); |
| 131 | + expect(mergeFieldMaps(baseInputTypeFields, childInputTypeFields)).toEqual(expectedInputTypes); |
| 132 | + }); |
| 133 | +}); |
| 134 | + |
| 135 | +describe('mergeCustomizationOptions()', () => { |
| 136 | + const baseCustomOptions: TypeConverterOpts = { |
| 137 | + fields: baseFields, |
| 138 | + inputType: { |
| 139 | + name: 'BaseInput', |
| 140 | + description: 'Hello Base', |
| 141 | + fields: baseInputTypeFields, |
| 142 | + }, |
| 143 | + resolvers: { |
| 144 | + findMany: { |
| 145 | + limit: { defaultValue: 20 }, |
| 146 | + // sort: false, |
| 147 | + skip: false, |
| 148 | + filter: { |
| 149 | + isRequired: true, |
| 150 | + removeFields: ['id', 'dob'], |
| 151 | + operators: { |
| 152 | + one: ['gt', 'gte', 'lt'], |
| 153 | + two: ['gt', 'gte', 'lt', 'in[]', 'nin[]'], |
| 154 | + }, |
| 155 | + }, |
| 156 | + }, |
| 157 | + findById: false, |
| 158 | + }, |
| 159 | + }; |
| 160 | + |
| 161 | + const childCustomOptions: TypeConverterOpts = { |
| 162 | + fields: childFields, |
| 163 | + inputType: { |
| 164 | + name: 'ChildInputs', |
| 165 | + description: 'Hello Child', |
| 166 | + fields: childInputTypeFields, |
| 167 | + }, |
| 168 | + resolvers: { |
| 169 | + findMany: { |
| 170 | + limit: { defaultValue: 50 }, |
| 171 | + sort: false, |
| 172 | + // skip: false, |
| 173 | + filter: { |
| 174 | + removeFields: ['gender', 'dob', 'age'], |
| 175 | + operators: { |
| 176 | + one: ['gt', 'lte', 'ne', 'in[]', 'nin[]'], |
| 177 | + two: ['gt', 'gte', 'lt', 'lte', 'ne'], |
| 178 | + three: ['gte', 'lt'], |
| 179 | + }, |
| 180 | + }, |
| 181 | + }, |
| 182 | + updateById: { |
| 183 | + input: { |
| 184 | + removeFields: ['one', 'two', 'five'], |
| 185 | + requiredFields: ['eight', 'two', 'five'], |
| 186 | + }, |
| 187 | + }, |
| 188 | + }, |
| 189 | + }; |
| 190 | + |
| 191 | + const expected: TypeConverterOpts = { |
| 192 | + fields: expectedFields, |
| 193 | + inputType: { |
| 194 | + name: 'ChildInputs', |
| 195 | + description: 'Hello Child', |
| 196 | + fields: expectedInputTypes, |
| 197 | + }, |
| 198 | + resolvers: { |
| 199 | + findMany: { |
| 200 | + limit: { defaultValue: 50 }, |
| 201 | + sort: false, |
| 202 | + skip: false, |
| 203 | + filter: { |
| 204 | + isRequired: true, |
| 205 | + removeFields: ['id', 'dob', 'gender', 'age'], |
| 206 | + operators: { |
| 207 | + one: ['gt', 'gte', 'lt', 'lte', 'ne', 'in[]', 'nin[]'], |
| 208 | + two: ['gt', 'gte', 'lt', 'in[]', 'nin[]', 'lte', 'ne'], |
| 209 | + three: ['gte', 'lt'], |
| 210 | + }, |
| 211 | + }, |
| 212 | + }, |
| 213 | + findById: false, |
| 214 | + updateById: { |
| 215 | + input: { |
| 216 | + removeFields: ['one', 'two', 'five'], |
| 217 | + requiredFields: ['eight', 'two', 'five'], |
| 218 | + }, |
| 219 | + }, |
| 220 | + }, |
| 221 | + }; |
| 222 | + |
| 223 | + it('should merge customisation Options', () => { |
| 224 | + expect(mergeCustomizationOptions(baseCustomOptions, childCustomOptions)).toEqual(expected); |
| 225 | + }); |
| 226 | + |
| 227 | + /* it('should produce error if using different schema composers', () => { |
| 228 | + expect( fixme: error is not caught. |
| 229 | + mergeCustomizationOptions( |
| 230 | + { schemaComposer: new SchemaComposer() }, |
| 231 | + { schemaComposer: new SchemaComposer() } |
| 232 | + ) |
| 233 | + ).toThrow('[Discriminators] ChildModel should have same schemaComposer as its BaseModels'); |
| 234 | + }); */ |
| 235 | +}); |
0 commit comments