1
+ import { Test , TestingModule } from "@nestjs/testing" ;
2
+ import { CompanyService } from "./company.service" ;
3
+ import { getModelToken } from "@nestjs/mongoose" ;
4
+ import { Model } from "mongoose" ;
5
+ import { Company } from "./company.model" ;
6
+ import { Query } from "mongoose" ;
7
+ import { CreateCompanyDto } from "../dto/create-company.dto" ;
8
+
9
+ describe ( "CompanyService" , ( ) => {
10
+ let service : CompanyService ;
11
+ let companyModel : Model < Company > ;
12
+
13
+ beforeEach ( async ( ) => {
14
+ const module : TestingModule = await Test . createTestingModule ( {
15
+ providers : [
16
+ CompanyService ,
17
+ {
18
+ provide : getModelToken ( Company . name ) ,
19
+ useValue : {
20
+ create : jest . fn ( ) ,
21
+ find : jest . fn ( ) ,
22
+ findOne : jest . fn ( ) ,
23
+ findByIdAndUpdate : jest . fn ( ) ,
24
+ updateMany : jest . fn ( ) ,
25
+ findByIdAndDelete : jest . fn ( ) ,
26
+ } ,
27
+ } ,
28
+ ] ,
29
+ } ) . compile ( ) ;
30
+
31
+ service = module . get < CompanyService > ( CompanyService ) ;
32
+ companyModel = module . get < Model < Company > > ( getModelToken ( Company . name ) ) ;
33
+ } ) ;
34
+
35
+ it ( "should be defined" , ( ) => {
36
+ expect ( service ) . toBeDefined ( ) ;
37
+ } ) ;
38
+
39
+ describe ( "create" , ( ) => {
40
+ it ( "should create a new company" , async ( ) => {
41
+ const request : CreateCompanyDto = {
42
+ name : "example" ,
43
+ planId : "example" ,
44
+ planName : "example" ,
45
+ isActive : true ,
46
+ status : "example" ,
47
+ dueDate : new Date ( ) . toString ( ) ,
48
+ recurrence : true ,
49
+ phone : "example" ,
50
+ password : "example" ,
51
+ campaignsEnabled : true , // Updated to string value
52
+ } ;
53
+
54
+ const expectedResult = new Company ( {
55
+ ...request ,
56
+ _id : "6586033c9f031a4394c1852f" ,
57
+ createdAt : new Date ( ) ,
58
+ updatedAt : new Date ( ) ,
59
+ } ) ;
60
+
61
+ jest . spyOn ( companyModel , "create" ) . mockResolvedValue ( expectedResult as any ) ;
62
+
63
+ const result = await service . create ( request ) ;
64
+
65
+ expect ( companyModel . create ) . toHaveBeenCalledWith ( request ) ;
66
+ expect ( result ) . toEqual ( expectedResult ) ;
67
+ } ) ;
68
+ } ) ;
69
+
70
+
71
+ describe ( "findAll" , ( ) => {
72
+ it ( "should find all companies" , async ( ) => {
73
+ const mockCompanyData = [
74
+ new Company ( {
75
+ name : "Test Company" ,
76
+ phone : "123456789" ,
77
+ planName : "Test Plan" ,
78
+ planId : "Test Plan" ,
79
+ toObject : function ( ) { return this ; } ,
80
+ } ) ,
81
+ ] ;
82
+
83
+ const mockExecResult = jest . fn ( ) . mockResolvedValue ( mockCompanyData ) ;
84
+ const mockSortResult = jest . fn ( ) . mockReturnThis ( ) ;
85
+ const mockPopulateResult = jest . fn ( ) . mockReturnThis ( ) ;
86
+
87
+ jest . spyOn ( companyModel , "find" ) . mockImplementation ( ( ) => ( {
88
+ populate : mockPopulateResult ,
89
+ sort : mockSortResult ,
90
+ exec : mockExecResult ,
91
+ } ) as unknown as Query < unknown [ ] , unknown , { } , Company , "find" > ) ;
92
+
93
+ const result = await service . findAll ( ) ;
94
+
95
+ expect ( mockPopulateResult ) . toHaveBeenCalled ( ) ;
96
+ expect ( mockSortResult ) . toHaveBeenCalled ( ) ;
97
+ expect ( mockExecResult ) . toHaveBeenCalled ( ) ;
98
+ expect ( result ) . toEqual ( mockCompanyData . map ( company => {
99
+ const companyObj = company . toObject ( ) ;
100
+ const planName = ( company . planId as any ) . name ; // Casting para Plan, pois sabemos que foi populado
101
+ return {
102
+ ...companyObj ,
103
+ planName // Adiciona a propriedade planName com o valor do nome do plano
104
+ } ;
105
+ } ) ) ;
106
+ } ) ;
107
+ } ) ;
108
+
109
+ } ) ;
0 commit comments