|
| 1 | +import { composeMongoose } from '../../index'; |
| 2 | +import { mongoose } from '../../__mocks__/mongooseCommon'; |
| 3 | + |
| 4 | +// mongoose.set('debug', true); |
| 5 | + |
| 6 | +const WithSubSchema = new mongoose.Schema({ |
| 7 | + subDocument: new mongoose.Schema({ |
| 8 | + field: { type: String, default: 'Hey' }, |
| 9 | + }), |
| 10 | +}); |
| 11 | + |
| 12 | +const WithoutSubSchema = new mongoose.Schema({ |
| 13 | + subDocument: { |
| 14 | + field: { type: String, default: 'Hey' }, |
| 15 | + }, |
| 16 | +}); |
| 17 | + |
| 18 | +const WithSubModel = mongoose.model('WithSubModel', WithSubSchema); |
| 19 | +const WithoutSubModel = mongoose.model('WithoutSubModel', WithoutSubSchema); |
| 20 | + |
| 21 | +describe('defaultsAsNonNull falsely reports non-nullability for subdocuments that have a Schema - issue #358', () => { |
| 22 | + it('with sub schema', async () => { |
| 23 | + const WithSubTC = composeMongoose(WithSubModel, { defaultsAsNonNull: true }); |
| 24 | + |
| 25 | + // sub-Schema breaks the "recursive default value assignation" behavior |
| 26 | + const data = new WithSubModel().subDocument; |
| 27 | + expect(data).toEqual(undefined); |
| 28 | + |
| 29 | + // so field should not be non-null |
| 30 | + expect(WithSubTC.getFieldTypeName('subDocument')).toBe('WithSubModelSubDocument'); |
| 31 | + }); |
| 32 | + |
| 33 | + it('as nested fields', async () => { |
| 34 | + const WithoutSubTC = composeMongoose(WithoutSubModel, { defaultsAsNonNull: true }); |
| 35 | + |
| 36 | + const data = new WithoutSubModel().subDocument; |
| 37 | + expect(data).toEqual({ field: 'Hey' }); |
| 38 | + |
| 39 | + // should be non-null! |
| 40 | + expect(WithoutSubTC.getFieldTypeName('subDocument')).toBe('WithoutSubModelSubDocument!'); |
| 41 | + }); |
| 42 | +}); |
0 commit comments