|
| 1 | +/* @flow */ |
| 2 | + |
| 3 | +import mongoose from 'mongoose'; |
| 4 | +import MongodbMemoryServer from 'mongodb-memory-server'; |
| 5 | +import { composeWithMongoose } from '../../index'; |
| 6 | + |
| 7 | +// May require additional time for downloading MongoDB binaries |
| 8 | +jasmine.DEFAULT_TIMEOUT_INTERVAL = 60000; |
| 9 | + |
| 10 | +let mongoServer; |
| 11 | +beforeAll(async () => { |
| 12 | + mongoServer = new MongodbMemoryServer(); |
| 13 | + const mongoUri = await mongoServer.getConnectionString(); |
| 14 | + await mongoose.connect( |
| 15 | + mongoUri, |
| 16 | + { useNewUrlParser: true } |
| 17 | + ); |
| 18 | +}); |
| 19 | + |
| 20 | +afterAll(() => { |
| 21 | + mongoose.disconnect(); |
| 22 | + mongoServer.stop(); |
| 23 | +}); |
| 24 | + |
| 25 | +describe('issue #117', () => { |
| 26 | + it('`populate()` method for arrays is broken', async () => { |
| 27 | + const PlayerSchema = new mongoose.Schema({ |
| 28 | + name: { |
| 29 | + type: String, |
| 30 | + required: true, |
| 31 | + }, |
| 32 | + surname: { |
| 33 | + type: String, |
| 34 | + required: true, |
| 35 | + default: [], |
| 36 | + }, |
| 37 | + sex: { |
| 38 | + type: String, |
| 39 | + required: true, |
| 40 | + enum: ['m', 'f'], |
| 41 | + }, |
| 42 | + }); |
| 43 | + |
| 44 | + const GameSchema = new mongoose.Schema({ |
| 45 | + players: { |
| 46 | + required: true, |
| 47 | + type: [ |
| 48 | + { |
| 49 | + type: mongoose.Schema.Types.ObjectId, |
| 50 | + ref: 'PlayerModel', |
| 51 | + }, |
| 52 | + ], |
| 53 | + }, |
| 54 | + }); |
| 55 | + |
| 56 | + const GameModel = mongoose.model('GameModel', GameSchema); |
| 57 | + const PlayerModel = mongoose.model('PlayerModel', PlayerSchema); |
| 58 | + |
| 59 | + const player1 = await PlayerModel.create({ name: '1', surname: '1', sex: 'm' }); |
| 60 | + const player2 = await PlayerModel.create({ name: '2', surname: '2', sex: 'f' }); |
| 61 | + const game = await GameModel.create({ players: [player1, player2] }); |
| 62 | + |
| 63 | + const id = game._id; |
| 64 | + const g1 = await GameModel.findOne({ _id: id }).populate('players'); |
| 65 | + expect(g1.toJSON()).toEqual({ |
| 66 | + __v: 0, |
| 67 | + _id: expect.anything(), |
| 68 | + players: [ |
| 69 | + { __v: 0, _id: expect.anything(), name: '1', sex: 'm', surname: '1' }, |
| 70 | + { __v: 0, _id: expect.anything(), name: '2', sex: 'f', surname: '2' }, |
| 71 | + ], |
| 72 | + }); |
| 73 | + |
| 74 | + composeWithMongoose(GameModel); |
| 75 | + const g2 = await GameModel.findOne({ _id: id }).populate('players'); |
| 76 | + |
| 77 | + // WAS SUCH ERROR |
| 78 | + // expect(g2.toJSON()).toEqual({ __v: 0, _id: expect.anything(), players: [] }); |
| 79 | + |
| 80 | + // EXPECTED BEHAVIOR |
| 81 | + expect(g2.toJSON()).toEqual({ |
| 82 | + __v: 0, |
| 83 | + _id: expect.anything(), |
| 84 | + players: [ |
| 85 | + { __v: 0, _id: expect.anything(), name: '1', sex: 'm', surname: '1' }, |
| 86 | + { __v: 0, _id: expect.anything(), name: '2', sex: 'f', surname: '2' }, |
| 87 | + ], |
| 88 | + }); |
| 89 | + }); |
| 90 | +}); |
0 commit comments