|
| 1 | +import { connect, Connection, Model } from 'mongoose'; |
| 2 | +import { getModelToken } from '@nestjs/mongoose'; |
| 3 | +import { MongoMemoryServer } from 'mongodb-memory-server'; |
| 4 | +import { Test } from '@nestjs/testing'; |
| 5 | +import { ListCommitmentsQuery } from '@modules/commitment/cqrs/query/list-commitments.query'; |
| 6 | +import { CommitmentPersistence, CommitmentRepository, CommitmentSchema } from '@infrastructure/mongo'; |
| 7 | +import { CQRSHandler, CQRSModule, ICQRSHandler } from '@common/cqrs'; |
| 8 | +import { CreateCommitmentStub } from '@modules/commitment/cqrs/tests/create-commitment.command.spec'; |
| 9 | +import { CreateCommitmentCommand } from '@modules/commitment'; |
| 10 | +import { ICommitmentRepository } from '@domain/commitment'; |
| 11 | + |
| 12 | +describe('ListCommitmentsQuery', () => { |
| 13 | + let listCommitmentsQuery: ListCommitmentsQuery; |
| 14 | + let commitmentCommand: CreateCommitmentCommand; |
| 15 | + let CommitmentModel: Model<CommitmentPersistence>; |
| 16 | + let mongod: MongoMemoryServer; |
| 17 | + let mongoConnection: Connection; |
| 18 | + beforeAll(async () => { |
| 19 | + mongod = await MongoMemoryServer.create(); |
| 20 | + const uri = mongod.getUri(); |
| 21 | + mongoConnection = (await connect(uri)).connection; |
| 22 | + CommitmentModel = mongoConnection.model(CommitmentPersistence.name, CommitmentSchema); |
| 23 | + const moduleRef = await Test.createTestingModule({ |
| 24 | + imports: [CQRSModule], |
| 25 | + providers: [ |
| 26 | + ListCommitmentsQuery, |
| 27 | + CreateCommitmentCommand, |
| 28 | + { provide: ICQRSHandler, useClass: CQRSHandler }, |
| 29 | + { provide: ICommitmentRepository, useClass: CommitmentRepository }, |
| 30 | + { provide: getModelToken(CommitmentPersistence.name), useValue: CommitmentModel }, |
| 31 | + ], |
| 32 | + }).compile(); |
| 33 | + listCommitmentsQuery = moduleRef.get<ListCommitmentsQuery>(ListCommitmentsQuery); |
| 34 | + commitmentCommand = moduleRef.get<CreateCommitmentCommand>(CreateCommitmentCommand); |
| 35 | + }); |
| 36 | + afterAll(async () => { |
| 37 | + await mongoConnection.dropDatabase(); |
| 38 | + await mongoConnection.close(); |
| 39 | + await mongod.stop(); |
| 40 | + }); |
| 41 | + afterEach(async () => { |
| 42 | + const collections = mongoConnection.collections; |
| 43 | + for (const key in collections) { |
| 44 | + const collection = collections[key]; |
| 45 | + await collection.deleteMany({}); |
| 46 | + } |
| 47 | + }); |
| 48 | + describe('getAllCommitmentsQuery', () => { |
| 49 | + it('should find all commitments', async () => { |
| 50 | + const commitment = CreateCommitmentStub(); |
| 51 | + await commitmentCommand.execute(commitment); |
| 52 | + const result = await listCommitmentsQuery.handle({ |
| 53 | + filter: { tags: ['FOOD'] }, |
| 54 | + page: 1, |
| 55 | + pageSize: 1, |
| 56 | + }); |
| 57 | + expect(result).toBeDefined(); |
| 58 | + expect(result?.items?.length).toEqual(1); |
| 59 | + expect(result?.pageInfo?.pageSize).toEqual(1); |
| 60 | + expect(result?.pageInfo?.totalPages).toEqual(1); |
| 61 | + expect(result?.pageInfo?.totalItems).toEqual(1); |
| 62 | + }); |
| 63 | + }); |
| 64 | +}); |
0 commit comments