-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
1,207 additions
and
1,142 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
export class Connection { | ||
_id?: string; | ||
name: string; | ||
phone: string; | ||
instanceName: string; | ||
instanceStatus: boolean; | ||
messages: []; | ||
isActive?: boolean; | ||
createdAt?: Date; | ||
updatedAt?: Date; | ||
|
||
constructor(partial: Partial<Connection>) { | ||
Object.assign(this, partial); | ||
} | ||
|
||
public toObject() { | ||
return this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import { Test, TestingModule } from "@nestjs/testing"; | ||
import { ConnectionService } from "./connection.service"; | ||
import { getModelToken } from "@nestjs/mongoose"; | ||
import { Model } from "mongoose"; | ||
import { Connection } from "./connection.model"; | ||
import { Query } from "mongoose"; | ||
import { CreateConnectionDto } from "../dto/create-connection.dto"; | ||
import { EvolutionService } from "../../evolution/service/evolution.service"; | ||
|
||
|
||
describe("ConnectionService", () => { | ||
let service: ConnectionService; | ||
let connectionModel: Model<Connection>; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [ | ||
ConnectionService, | ||
EvolutionService, | ||
{ | ||
provide: getModelToken(Connection.name), | ||
useValue: { | ||
create: jest.fn(), | ||
find: jest.fn(), | ||
findOne: jest.fn(), | ||
findByIdAndUpdate: jest.fn(), | ||
updateMany: jest.fn(), | ||
findByIdAndDelete: jest.fn(), | ||
}, | ||
}, | ||
], | ||
}).compile(); | ||
|
||
service = module.get<ConnectionService>(ConnectionService); | ||
connectionModel = module.get<Model<Connection>>(getModelToken(Connection.name)); | ||
}); | ||
|
||
it("should be defined", () => { | ||
expect(service).toBeDefined(); | ||
}); | ||
|
||
describe("create", () => { | ||
it("should create a new connection", async () => { | ||
const request: CreateConnectionDto = { | ||
name: "Test Connection", | ||
phone: "123456789", | ||
instanceName: "test-instance", | ||
instanceStatus: true, | ||
}; | ||
|
||
const expectedResult = new Connection({ | ||
...request, | ||
_id: "6586033c9f031a4394c1852f", | ||
createdAt: new Date(), | ||
updatedAt: new Date(), | ||
}); | ||
|
||
jest.spyOn(connectionModel, "create").mockResolvedValue(expectedResult as any); | ||
|
||
const result = await service.create(request); | ||
|
||
expect(connectionModel.create).toHaveBeenCalledWith(request); | ||
expect(result).toEqual(expectedResult); | ||
}); | ||
}); | ||
|
||
|
||
// describe("findAll", () => { | ||
// it("should find all companies", async () => { | ||
// const mockConnectionData = [ | ||
// new Connection({ | ||
// name: "Test Connection", | ||
// phone: "123456789", | ||
// instanceName: "test-instance", | ||
// instanceStatus: true, | ||
// toObject: function () { return this; }, | ||
// }), | ||
// ]; | ||
|
||
// const mockExecResult = jest.fn().mockResolvedValue(mockConnectionData); | ||
// const mockSortResult = jest.fn().mockReturnThis(); | ||
// const mockPopulateResult = jest.fn().mockReturnThis(); | ||
|
||
// jest.spyOn(connectionModel, "find").mockImplementation(() => ({ | ||
// populate: mockPopulateResult, | ||
// sort: mockSortResult, | ||
// exec: mockExecResult, | ||
// }) as unknown as Query<unknown[], unknown, {}, Connection, "find">); | ||
|
||
// const result = await service.findAll(); | ||
|
||
// expect(mockPopulateResult).toHaveBeenCalled(); | ||
// expect(mockSortResult).toHaveBeenCalled(); | ||
// expect(mockExecResult).toHaveBeenCalled(); | ||
// expect(result).toEqual(mockConnectionData.map((item) => { return item.toObject(); })); | ||
// }); | ||
// }); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
export class Contact { | ||
_id?: string; | ||
name: string; | ||
phone: string; | ||
email: string; | ||
status: string; | ||
city: string; | ||
state: string; | ||
country: string; | ||
profilePictureUrl: string; | ||
badges: []; | ||
messages: []; | ||
// groupId: string; | ||
isActive?: boolean; | ||
createdAt?: Date; | ||
updatedAt?: Date; | ||
|
||
constructor(partial: Partial<Contact>) { | ||
Object.assign(this, partial); | ||
} | ||
|
||
public toObject() { | ||
return this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
import { Test, TestingModule, mockResolvedValue } from "@nestjs/testing"; | ||
import { ContactService } from "./contact.service"; | ||
import { ConnectionService } from "../../connection/service/connection.service"; | ||
import { EvolutionService } from "../../evolution/service/evolution.service"; | ||
import { getModelToken } from "@nestjs/mongoose"; | ||
import { Model } from "mongoose"; | ||
import { Contact } from "./contact.model"; | ||
import { Query } from "mongoose"; | ||
import { CreateContactDto } from "../dto/create-contact.dto"; | ||
|
||
|
||
describe("ContactService", () => { | ||
let service: ContactService; | ||
let contactModel: Model<Contact>; | ||
|
||
const mockConnectionService = { | ||
getConnectionByInstanceName: jest.fn().mockResolvedValue({}), | ||
}; | ||
|
||
const mockEvolutionService = { | ||
sendSimpleMessage: jest.fn().mockResolvedValue({ | ||
|
||
}), | ||
}; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [ | ||
ContactService, | ||
{ | ||
provide: ConnectionService, | ||
useValue: mockConnectionService, // Forneça um mock para ConnectionService | ||
}, | ||
{ | ||
provide: EvolutionService, | ||
useValue: mockEvolutionService, // Forneça um mock para EvolutionService | ||
}, | ||
{ | ||
provide: getModelToken(Contact.name), | ||
useValue: { | ||
create: jest.fn(), | ||
find: jest.fn(), | ||
findOne: jest.fn(), | ||
findByIdAndUpdate: jest.fn(), | ||
updateMany: jest.fn(), | ||
findByIdAndDelete: jest.fn(), | ||
}, | ||
}, | ||
], | ||
}).compile(); | ||
|
||
service = module.get<ContactService>(ContactService); | ||
contactModel = module.get<Model<Contact>>(getModelToken(Contact.name)); | ||
}); | ||
|
||
it("should be defined", () => { | ||
expect(service).toBeDefined(); | ||
}); | ||
|
||
describe("create", () => { | ||
it("should create a new contact", async () => { | ||
const request: CreateContactDto = { | ||
name: "Test Contact", | ||
phone: "123456789", | ||
email: "[email protected]", | ||
status: "ativo", | ||
city: "cidade", | ||
state: "AC", | ||
country: "Brasil", | ||
profilePictureUrl: "", | ||
badges: ["teste"], | ||
messages: [], | ||
}; | ||
|
||
const expectedResult = { | ||
...request, | ||
_id: "6586033c9f031a4394c1852f", | ||
createdAt: new Date(), | ||
updatedAt: new Date(), | ||
badges: [], // Explicitly define the type of badges as an empty array | ||
}; | ||
|
||
jest.spyOn(contactModel, "create").mockResolvedValue(expectedResult as any); | ||
|
||
const result = await service.create(request); | ||
|
||
expect(contactModel.create).toHaveBeenCalledWith(request); | ||
expect(result).toEqual(expectedResult); | ||
}); | ||
}); | ||
|
||
|
||
// describe("findAll", () => { | ||
// it("should find all companies", async () => { | ||
// const mockContactData = [ | ||
// new Contact({ | ||
// name: "Test Contact", | ||
// phone: "123456789", | ||
// planName: "Test Plan", | ||
// planId: "Test Plan", | ||
// toObject: function () { return this; }, | ||
// }), | ||
// ]; | ||
|
||
// const mockExecResult = jest.fn().mockResolvedValue(mockContactData); | ||
// const mockSortResult = jest.fn().mockReturnThis(); | ||
// const mockPopulateResult = jest.fn().mockReturnThis(); | ||
|
||
// jest.spyOn(contactModel, "find").mockImplementation(() => ({ | ||
// populate: mockPopulateResult, | ||
// sort: mockSortResult, | ||
// exec: mockExecResult, | ||
// }) as unknown as Query<unknown[], unknown, {}, Contact, "find">); | ||
|
||
// const result = await service.findAll(); | ||
|
||
// expect(mockPopulateResult).toHaveBeenCalled(); | ||
// expect(mockSortResult).toHaveBeenCalled(); | ||
// expect(mockExecResult).toHaveBeenCalled(); | ||
// expect(result).toEqual(mockContactData.map(contact => { | ||
// const contactObj = contact.toObject(); | ||
// const planName = (contact.planId as any).name; // Casting para Plan, pois sabemos que foi populado | ||
// return { | ||
// ...contactObj, | ||
// planName // Adiciona a propriedade planName com o valor do nome do plano | ||
// }; | ||
// })); | ||
// }); | ||
// }); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters