Skip to content

Commit

Permalink
Merge pull request #2384 from sophiemoustard/COM-3694
Browse files Browse the repository at this point in the history
COM-3694: populate companies
  • Loading branch information
manonpalin committed Apr 23, 2024
2 parents 24ecebe + 3d34831 commit 400e8cd
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
5 changes: 4 additions & 1 deletion src/helpers/holdings.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ const Holding = require('../models/Holding');

exports.create = async payload => Holding.create(payload);

exports.list = async () => Holding.find({}, { _id: 1, name: 1 }).lean();
exports.list = async () => Holding
.find({}, { _id: 1, name: 1 })
.populate({ path: 'companies' })
.lean();

exports.update = async (holdingId, payload) => {
if (payload.company) return CompanyHolding.create({ holding: holdingId, company: payload.company });
Expand Down
10 changes: 7 additions & 3 deletions tests/unit/helpers/holdings.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,19 @@ describe('list', () => {
});

it('should return holdings', async () => {
const holdingList = [{ _id: new ObjectId(), name: 'Holding' }];
find.returns(SinonMongoose.stubChainedQueries(holdingList, ['lean']));
const holdingList = [{ _id: new ObjectId(), name: 'Holding', companies: [new ObjectId(), new ObjectId()] }];
find.returns(SinonMongoose.stubChainedQueries(holdingList));

const result = await HoldingHelper.list();

expect(result).toEqual(holdingList);
SinonMongoose.calledOnceWithExactly(
find,
[{ query: 'find', args: [{}, { _id: 1, name: 1 }] }, { query: 'lean', args: [] }]
[
{ query: 'find', args: [{}, { _id: 1, name: 1 }] },
{ query: 'populate', args: [{ path: 'companies' }] },
{ query: 'lean', args: [] },
]
);
});
});
Expand Down

0 comments on commit 400e8cd

Please sign in to comment.