-
-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix:
defaultsAsNonNull
option now skips isSingleNested
mongoose s…
…chemas (because such sub-schemas break the "recursive default value assignation" behavior) see #358 closes #358
- Loading branch information
Showing
3 changed files
with
54 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { composeMongoose } from '../../index'; | ||
import { mongoose } from '../../__mocks__/mongooseCommon'; | ||
|
||
// mongoose.set('debug', true); | ||
|
||
const WithSubSchema = new mongoose.Schema({ | ||
subDocument: new mongoose.Schema({ | ||
field: { type: String, default: 'Hey' }, | ||
}), | ||
}); | ||
|
||
const WithoutSubSchema = new mongoose.Schema({ | ||
subDocument: { | ||
field: { type: String, default: 'Hey' }, | ||
}, | ||
}); | ||
|
||
const WithSubModel = mongoose.model('WithSubModel', WithSubSchema); | ||
const WithoutSubModel = mongoose.model('WithoutSubModel', WithoutSubSchema); | ||
|
||
describe('defaultsAsNonNull falsely reports non-nullability for subdocuments that have a Schema - issue #358', () => { | ||
it('with sub schema', async () => { | ||
const WithSubTC = composeMongoose(WithSubModel, { defaultsAsNonNull: true }); | ||
|
||
// sub-Schema breaks the "recursive default value assignation" behavior | ||
const data = new WithSubModel().subDocument; | ||
expect(data).toEqual(undefined); | ||
|
||
// so field should not be non-null | ||
expect(WithSubTC.getFieldTypeName('subDocument')).toBe('WithSubModelSubDocument'); | ||
}); | ||
|
||
it('as nested fields', async () => { | ||
const WithoutSubTC = composeMongoose(WithoutSubModel, { defaultsAsNonNull: true }); | ||
|
||
const data = new WithoutSubModel().subDocument; | ||
expect(data).toEqual({ field: 'Hey' }); | ||
|
||
// should be non-null! | ||
expect(WithoutSubTC.getFieldTypeName('subDocument')).toBe('WithoutSubModelSubDocument!'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters