@@ -4,20 +4,21 @@ import { IdentitiesController } from './identities.controller';
44import { IdentitiesService } from './identities.service' ;
55import { Identities , IdentitiesSchema } from './_schemas/identities.schema' ;
66import { HttpStatus } from '@nestjs/common' ;
7- import { Connection , Model , Types , connect } from 'mongoose' ;
7+ import { Model , Types } from 'mongoose' ;
88import { Response } from 'express' ;
99import { getModelToken } from '@nestjs/mongoose' ;
1010import { IdentitiesDtoStub } from './_stubs/identities.dto.stub' ;
11- import { MongoMemoryServer } from 'mongodb-memory-server' ;
1211import { IdentitiesValidationService } from './validations/identities.validation.service' ;
1312import { IdentitiesValidationModule } from './validations/identities.validation.module' ;
1413import { MockResponse , createResponse } from 'node-mocks-http' ;
14+ import { ValidationSchemaException } from '~/_common/errors/ValidationException' ;
15+ import { MongoDbTestInstance } from '~/_common/tests/mongodb.test.instance' ;
1516
1617describe ( 'IdentitiesController' , ( ) => {
1718 let controller : IdentitiesController ;
1819 let service : IdentitiesService ;
19- let mongod : MongoMemoryServer ;
20- let mongoConnection : Connection ;
20+ let mongoDbTestInstance : MongoDbTestInstance ;
21+
2122 let identitiesModel : Model < Identities > ;
2223 //let request: MockRequest<Request>;
2324 let response : MockResponse < Response > ;
@@ -31,14 +32,10 @@ describe('IdentitiesController', () => {
3132 let _id : Types . ObjectId ;
3233
3334 beforeAll ( async ( ) => {
34- mongod = await MongoMemoryServer . create ( {
35- binary : {
36- version : '5.0.22' ,
37- } ,
38- } ) ;
39- const uri = await mongod . getUri ( ) ;
40- mongoConnection = ( await connect ( uri ) ) . connection ;
41- identitiesModel = mongoConnection . model < Identities > ( Identities . name , IdentitiesSchema ) ;
35+ mongoDbTestInstance = new MongoDbTestInstance ( ) ;
36+ await mongoDbTestInstance . start ( ) ;
37+ identitiesModel = await mongoDbTestInstance . getModel < Identities > ( Identities . name , IdentitiesSchema ) ;
38+
4239 const module : TestingModule = await Test . createTestingModule ( {
4340 controllers : [ IdentitiesController ] ,
4441 providers : [
@@ -59,32 +56,37 @@ describe('IdentitiesController', () => {
5956 } ) ;
6057
6158 afterAll ( async ( ) => {
62- await mongoConnection . dropDatabase ( ) ;
63- await mongoConnection . close ( ) ;
64- await mongod . stop ( ) ;
59+ await mongoDbTestInstance . stop ( ) ;
6560 } ) ;
6661
67- afterEach ( ( ) => {
68- const collections = mongoConnection . collections ;
69- Object . keys ( collections ) . forEach ( async ( key ) => {
70- await collections [ key ] . deleteMany ( { } ) ;
71- } ) ;
62+ afterEach ( async ( ) => {
63+ await mongoDbTestInstance . clearDatabase ( ) ;
64+ jest . clearAllMocks ( ) ;
7265 } ) ;
7366
7467 describe ( 'create' , ( ) => {
7568 it ( 'should create an identity' , async ( ) => {
69+ jest . spyOn ( service , 'create' ) . mockResolvedValue ( IdentitiesDtoStub ( ) as Identities ) ;
70+
7671 const createIdentity = await controller . create ( response , IdentitiesDtoStub ( ) ) ;
77- expect ( createIdentity . statusCode ) . toBe ( HttpStatus . CREATED ) ;
72+ expect ( createIdentity . statusCode ) . toMatch ( new RegExp ( `( ${ HttpStatus . CREATED } )|( ${ HttpStatus . ACCEPTED } )` ) ) ;
7873 } ) ;
7974
80- it ( 'should throw an error when creating an identity' , async ( ) => {
81- jest . spyOn ( service , 'create' ) . mockRejectedValueOnce ( new Error ( 'Error' ) ) ;
82- try {
83- await controller . create ( response , IdentitiesDtoStub ( ) ) ;
84- } catch ( error ) {
85- expect ( response . statusCode ) . toBe ( HttpStatus . BAD_REQUEST ) ;
86- }
87- } ) ;
75+ // it('should handle ValidationSchemaException by returning the appropriate status code and message', async () => {
76+ // jest.spyOn(service, 'create').mockRejectedValue(
77+ // new ValidationSchemaException({
78+ // message: 'Schema validation failed',
79+ // validations: { field: 'error' },
80+ // statusCode: HttpStatus.BAD_REQUEST,
81+ // }),
82+ // );
83+
84+ // const response = createResponse();
85+ // const identity = IdentitiesDtoStub();
86+ // const createIdentity = await controller.create(response, identity);
87+ // expect(createIdentity.statusCode).toBe(HttpStatus.ACCEPTED);
88+ // expect(response).toHaveProperty('greger');
89+ // });
8890 } ) ;
8991
9092 describe ( 'search' , ( ) => {
0 commit comments