Skip to content

Commit 3e962eb

Browse files
committed
Auth and Company tests and Github Actions
1 parent 6f83012 commit 3e962eb

File tree

9 files changed

+220
-12
lines changed

9 files changed

+220
-12
lines changed

.github/workflows/nest.yml

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
name: Node.js CI
2+
3+
on:
4+
push:
5+
branches: [ main ]
6+
pull_request:
7+
branches: [ main ]
8+
9+
jobs:
10+
build:
11+
runs-on: ubuntu-latest
12+
13+
strategy:
14+
matrix:
15+
node-version: [18.x, 20.x] # Defina as versões do Node.js que você quer testar
16+
17+
steps:
18+
- uses: actions/checkout@v2
19+
20+
- name: Use Node.js ${{ matrix.node-version }}
21+
uses: actions/setup-node@v1
22+
with:
23+
node-version: ${{ matrix.node-version }}
24+
25+
- name: Install dependencies
26+
run: npm install
27+
28+
- name: Build
29+
run: npm run build
30+
31+
- name: Run tests
32+
run: npm test

package-lock.json

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
"@nestjs/testing": "^10.0.0",
6262
"@types/bcrypt": "^5.0.2",
6363
"@types/express": "^4.17.17",
64-
"@types/jest": "^29.5.2",
64+
"@types/jest": "^29.5.11",
6565
"@types/node": "^20.3.1",
6666
"@types/passport-local": "^1.0.38",
6767
"@types/supertest": "^2.0.12",
@@ -70,11 +70,11 @@
7070
"eslint": "^8.42.0",
7171
"eslint-config-prettier": "^9.0.0",
7272
"eslint-plugin-prettier": "^5.0.0",
73-
"jest": "^29.5.0",
73+
"jest": "^29.7.0",
7474
"prettier": "^3.0.0",
7575
"source-map-support": "^0.5.21",
7676
"supertest": "^6.3.3",
77-
"ts-jest": "^29.1.0",
77+
"ts-jest": "^29.1.1",
7878
"ts-loader": "^9.4.3",
7979
"ts-node": "^10.9.1",
8080
"tsconfig-paths": "^4.2.0",

src/auth/auth.service.spec.ts

+20-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,29 @@
11
import { Test, TestingModule } from '@nestjs/testing';
22
import { AuthService } from './auth.service';
3+
import { User } from '../user/schema/user.schema'; // Ajuste o caminho conforme necessário
4+
import { Model } from 'mongoose';
5+
import { getModelToken } from '@nestjs/mongoose';
6+
import { find } from 'rxjs';
7+
8+
const mockUserModel = {
9+
findOne: jest.fn(),
10+
find: jest.fn(),
11+
updateOne: jest.fn(),
12+
updateMany: jest.fn(),
13+
};
314

415
describe('AuthService', () => {
516
let service: AuthService;
617

718
beforeEach(async () => {
819
const module: TestingModule = await Test.createTestingModule({
9-
providers: [AuthService],
20+
providers: [
21+
AuthService,
22+
{
23+
provide: getModelToken(User.name),
24+
useValue: mockUserModel,
25+
},
26+
],
1027
}).compile();
1128

1229
service = module.get<AuthService>(AuthService);
@@ -15,4 +32,6 @@ describe('AuthService', () => {
1532
it('should be defined', () => {
1633
expect(service).toBeDefined();
1734
});
35+
36+
1837
});

src/auth/auth.service.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { Injectable } from "@nestjs/common";
2-
import { InjectModel } from '@nestjs/mongoose';
3-
import { Model } from 'mongoose';
2+
import { InjectModel } from "@nestjs/mongoose";
3+
import { Model } from "mongoose";
44
import * as bcrypt from "bcrypt";
55
import * as jwt from "jsonwebtoken";
66
import mongoose from "mongoose";
7-
import { User } from 'src/user/schema/user.schema'; // Ajuste o caminho conforme necessário
7+
import { User } from "../user/schema/user.schema"; // Ajuste o caminho conforme necessário
88

99
const SECRET = "ManoVeioPeriferico#666"
1010
@Injectable()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { Test, TestingModule } from '@nestjs/testing';
2+
import { MyController } from './my.controller';
3+
import { MyService } from './my.service';
4+
5+
describe('MyController', () => {
6+
let controller: MyController;
7+
8+
beforeEach(async () => {
9+
const module: TestingModule = await Test.createTestingModule({
10+
controllers: [MyController],
11+
providers: [MyService],
12+
}).compile();
13+
14+
controller = module.get<MyController>(MyController);
15+
});
16+
17+
it('should be defined', () => {
18+
expect(controller).toBeDefined();
19+
});
20+
21+
// Adicione mais testes aqui...
22+
});

src/company/dto/create-company.dto.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { IsNotEmpty, IsString, IsOptional, IsBoolean } from 'class-validator';
1+
import { IsNotEmpty, IsString, IsOptional, IsBoolean, IsObject } from 'class-validator';
22

33
export class CreateCompanyDto {
44
@IsNotEmpty()
@@ -17,9 +17,13 @@ export class CreateCompanyDto {
1717
@IsString()
1818
status: string;
1919

20+
@IsObject()
21+
@IsOptional()
22+
planId: {}; // Representando um ObjectId como string
23+
2024
@IsString()
2125
@IsOptional()
22-
planId: string; // Representando um ObjectId como string
26+
planName: string; // Representando um ObjectId como string
2327

2428
@IsBoolean()
2529
@IsOptional()

src/company/service/company.model.ts

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
export class Company {
2+
_id?: string;
3+
name: string;
4+
phone: string;
5+
password: string;
6+
status: string;
7+
planId: {};
8+
planName?: string;
9+
campaignsEnabled?: boolean;
10+
dueDate?: string;
11+
recurrence?: boolean;
12+
createdAt?: Date;
13+
updatedAt?: Date;
14+
15+
constructor(partial: Partial<Company>) {
16+
Object.assign(this, partial);
17+
}
18+
19+
public toObject() {
20+
return this;
21+
}
22+
}
+109
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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

Comments
 (0)