Skip to content

Commit 1ab0229

Browse files
committed
test: hydrate test improved to support document check Automattic#15048
1 parent 3ddc1e6 commit 1ab0229

File tree

1 file changed

+45
-20
lines changed

1 file changed

+45
-20
lines changed

test/model.hydrate.test.js

+45-20
Original file line numberDiff line numberDiff line change
@@ -100,29 +100,54 @@ describe('model', function() {
100100
assert.deepEqual(hydrated.schema.tree, C.schema.tree);
101101
});
102102
it('should deeply hydrate the document with the `hydratedPopulatedDocs` option (gh-4727)', async function() {
103-
const userSchema = new Schema({
104-
name: String
105-
});
106-
const companySchema = new Schema({
103+
const StorySchema = new Schema({
104+
title: {
105+
type: String
106+
}
107+
}, { timestamps: true });
108+
109+
const UserSchema = new Schema({
107110
name: String,
108-
users: [{ ref: 'User', type: Schema.Types.ObjectId }]
109-
});
111+
stories: [{
112+
type: Schema.Types.ObjectId,
113+
ref: 'Story1'
114+
}]
115+
}, { timestamps: true });
116+
117+
const User = db.model('User1', UserSchema);
118+
const Story = db.model('Story1', StorySchema);
110119

111-
db.model('UserTestHydrate', userSchema);
112-
const Company = db.model('CompanyTestHydrate', companySchema);
120+
const story1 = await Story.create({ title: 'Ticket 1' });
121+
const story2 = await Story.create({ title: 'Ticket 2' });
113122

114-
const users = [{ _id: new mongoose.Types.ObjectId(), name: 'Val' }];
115-
const company = { _id: new mongoose.Types.ObjectId(), name: 'Booster', users: [users[0]] };
123+
await User.create({ name: 'Alex', stories: [story1, story2] });
116124

117-
const C = Company.hydrate(company, null, { hydratedPopulatedDocs: true });
118-
assert(C.populated('users'));
119-
assert.equal(C.users[0].name, 'Val');
125+
const populated = await User.findOne({ name: 'Alex' }).populate(['stories']).lean();
126+
const hydrated = User.hydrate(
127+
JSON.parse(JSON.stringify(populated)),
128+
null,
129+
{ hydratedPopulatedDocs: true }
130+
);
131+
132+
assert(hydrated.populated('stories'));
133+
134+
assert.equal(hydrated.stories[0]._id.toString(), story1._id.toString());
135+
assert(typeof hydrated.stories[0]._id == 'object');
136+
assert(hydrated.stories[0]._id instanceof mongoose.Types.ObjectId);
137+
assert(typeof hydrated.stories[0].createdAt == 'object');
138+
assert(hydrated.stories[0].createdAt instanceof Date);
139+
140+
assert.equal(hydrated.stories[1]._id.toString(), story2._id.toString());
141+
assert(typeof hydrated.stories[1]._id == 'object');
142+
assert(hydrated.stories[1]._id instanceof mongoose.Types.ObjectId);
143+
assert(typeof hydrated.stories[1].createdAt == 'object');
144+
assert(hydrated.stories[1].createdAt instanceof Date);
120145
});
121146
it('should hydrate documents in virtual populate (gh-14503)', async function() {
122147
const StorySchema = new Schema({
123148
userId: {
124149
type: Schema.Types.ObjectId,
125-
ref: 'User'
150+
ref: 'User2'
126151
},
127152
title: {
128153
type: String
@@ -134,19 +159,19 @@ describe('model', function() {
134159
}, { timestamps: true });
135160

136161
UserSchema.virtual('stories', {
137-
ref: 'Story',
162+
ref: 'Story2',
138163
localField: '_id',
139164
foreignField: 'userId'
140165
});
141166
UserSchema.virtual('storiesCount', {
142-
ref: 'Story',
167+
ref: 'Story2',
143168
localField: '_id',
144169
foreignField: 'userId',
145170
count: true
146171
});
147172

148-
const User = db.model('User', UserSchema);
149-
const Story = db.model('Story', StorySchema);
173+
const User = db.model('User2', UserSchema);
174+
const Story = db.model('Story2', StorySchema);
150175

151176
const user = await User.create({ name: 'Alex' });
152177
const story1 = await Story.create({ title: 'Ticket 1', userId: user._id });
@@ -160,15 +185,15 @@ describe('model', function() {
160185
);
161186

162187
assert(hydrated.populated('stories'));
188+
163189
assert.equal(hydrated.stories[0]._id.toString(), story1._id.toString());
164-
assert(typeof hydrated.stories[0]._id == 'object', typeof hydrated.stories[0]._id);
190+
assert(typeof hydrated.stories[0]._id == 'object');
165191
assert(hydrated.stories[0]._id instanceof mongoose.Types.ObjectId);
166192
assert(typeof hydrated.stories[0].createdAt == 'object');
167193
assert(hydrated.stories[0].createdAt instanceof Date);
168194

169195
assert.equal(hydrated.stories[1]._id.toString(), story2._id.toString());
170196
assert(typeof hydrated.stories[1]._id == 'object');
171-
172197
assert(hydrated.stories[1]._id instanceof mongoose.Types.ObjectId);
173198
assert(typeof hydrated.stories[1].createdAt == 'object');
174199
assert(hydrated.stories[1].createdAt instanceof Date);

0 commit comments

Comments
 (0)