diff --git a/.github/workflows/nest.yml b/.github/workflows/nest.yml new file mode 100644 index 0000000..e062187 --- /dev/null +++ b/.github/workflows/nest.yml @@ -0,0 +1,32 @@ +name: Node.js CI + +on: + push: + branches: [ main ] + pull_request: + branches: [ main ] + +jobs: + build: + runs-on: ubuntu-latest + + strategy: + matrix: + node-version: [18.x, 20.x] # Defina as versões do Node.js que você quer testar + + steps: + - uses: actions/checkout@v2 + + - name: Use Node.js ${{ matrix.node-version }} + uses: actions/setup-node@v1 + with: + node-version: ${{ matrix.node-version }} + + - name: Install dependencies + run: npm install + + - name: Build + run: npm run build + + - name: Run tests + run: npm test diff --git a/package-lock.json b/package-lock.json index 4343bc1..6648d15 100644 --- a/package-lock.json +++ b/package-lock.json @@ -49,7 +49,7 @@ "@nestjs/testing": "^10.0.0", "@types/bcrypt": "^5.0.2", "@types/express": "^4.17.17", - "@types/jest": "^29.5.2", + "@types/jest": "^29.5.11", "@types/node": "^20.3.1", "@types/passport-local": "^1.0.38", "@types/supertest": "^2.0.12", @@ -58,11 +58,11 @@ "eslint": "^8.42.0", "eslint-config-prettier": "^9.0.0", "eslint-plugin-prettier": "^5.0.0", - "jest": "^29.5.0", + "jest": "^29.7.0", "prettier": "^3.0.0", "source-map-support": "^0.5.21", "supertest": "^6.3.3", - "ts-jest": "^29.1.0", + "ts-jest": "^29.1.1", "ts-loader": "^9.4.3", "ts-node": "^10.9.1", "tsconfig-paths": "^4.2.0", diff --git a/package.json b/package.json index 7223d75..9ef7762 100644 --- a/package.json +++ b/package.json @@ -61,7 +61,7 @@ "@nestjs/testing": "^10.0.0", "@types/bcrypt": "^5.0.2", "@types/express": "^4.17.17", - "@types/jest": "^29.5.2", + "@types/jest": "^29.5.11", "@types/node": "^20.3.1", "@types/passport-local": "^1.0.38", "@types/supertest": "^2.0.12", @@ -70,11 +70,11 @@ "eslint": "^8.42.0", "eslint-config-prettier": "^9.0.0", "eslint-plugin-prettier": "^5.0.0", - "jest": "^29.5.0", + "jest": "^29.7.0", "prettier": "^3.0.0", "source-map-support": "^0.5.21", "supertest": "^6.3.3", - "ts-jest": "^29.1.0", + "ts-jest": "^29.1.1", "ts-loader": "^9.4.3", "ts-node": "^10.9.1", "tsconfig-paths": "^4.2.0", diff --git a/src/auth/auth.service.spec.ts b/src/auth/auth.service.spec.ts index 800ab66..bf6c2dc 100644 --- a/src/auth/auth.service.spec.ts +++ b/src/auth/auth.service.spec.ts @@ -1,12 +1,29 @@ import { Test, TestingModule } from '@nestjs/testing'; import { AuthService } from './auth.service'; +import { User } from '../user/schema/user.schema'; // Ajuste o caminho conforme necessário +import { Model } from 'mongoose'; +import { getModelToken } from '@nestjs/mongoose'; +import { find } from 'rxjs'; + +const mockUserModel = { + findOne: jest.fn(), + find: jest.fn(), + updateOne: jest.fn(), + updateMany: jest.fn(), +}; describe('AuthService', () => { let service: AuthService; beforeEach(async () => { const module: TestingModule = await Test.createTestingModule({ - providers: [AuthService], + providers: [ + AuthService, + { + provide: getModelToken(User.name), + useValue: mockUserModel, + }, + ], }).compile(); service = module.get(AuthService); @@ -15,4 +32,6 @@ describe('AuthService', () => { it('should be defined', () => { expect(service).toBeDefined(); }); + + }); diff --git a/src/auth/auth.service.ts b/src/auth/auth.service.ts index 6b5d81e..d9ce5e3 100644 --- a/src/auth/auth.service.ts +++ b/src/auth/auth.service.ts @@ -1,10 +1,10 @@ import { Injectable } from "@nestjs/common"; -import { InjectModel } from '@nestjs/mongoose'; -import { Model } from 'mongoose'; +import { InjectModel } from "@nestjs/mongoose"; +import { Model } from "mongoose"; import * as bcrypt from "bcrypt"; import * as jwt from "jsonwebtoken"; import mongoose from "mongoose"; -import { User } from 'src/user/schema/user.schema'; // Ajuste o caminho conforme necessário +import { User } from "../user/schema/user.schema"; // Ajuste o caminho conforme necessário const SECRET = "ManoVeioPeriferico#666" @Injectable() diff --git a/src/company/controller/company.controller.test.ts b/src/company/controller/company.controller.test.ts new file mode 100644 index 0000000..228fb48 --- /dev/null +++ b/src/company/controller/company.controller.test.ts @@ -0,0 +1,22 @@ +import { Test, TestingModule } from '@nestjs/testing'; +import { MyController } from './my.controller'; +import { MyService } from './my.service'; + +describe('MyController', () => { + let controller: MyController; + + beforeEach(async () => { + const module: TestingModule = await Test.createTestingModule({ + controllers: [MyController], + providers: [MyService], + }).compile(); + + controller = module.get(MyController); + }); + + it('should be defined', () => { + expect(controller).toBeDefined(); + }); + + // Adicione mais testes aqui... +}); diff --git a/src/company/dto/create-company.dto.ts b/src/company/dto/create-company.dto.ts index 586f89e..273c916 100644 --- a/src/company/dto/create-company.dto.ts +++ b/src/company/dto/create-company.dto.ts @@ -1,4 +1,4 @@ -import { IsNotEmpty, IsString, IsOptional, IsBoolean } from 'class-validator'; +import { IsNotEmpty, IsString, IsOptional, IsBoolean, IsObject } from 'class-validator'; export class CreateCompanyDto { @IsNotEmpty() @@ -17,9 +17,13 @@ export class CreateCompanyDto { @IsString() status: string; + @IsObject() + @IsOptional() + planId: {}; // Representando um ObjectId como string + @IsString() @IsOptional() - planId: string; // Representando um ObjectId como string + planName: string; // Representando um ObjectId como string @IsBoolean() @IsOptional() diff --git a/src/company/service/company.model.ts b/src/company/service/company.model.ts new file mode 100644 index 0000000..e886399 --- /dev/null +++ b/src/company/service/company.model.ts @@ -0,0 +1,22 @@ +export class Company { + _id?: string; + name: string; + phone: string; + password: string; + status: string; + planId: {}; + planName?: string; + campaignsEnabled?: boolean; + dueDate?: string; + recurrence?: boolean; + createdAt?: Date; + updatedAt?: Date; + + constructor(partial: Partial) { + Object.assign(this, partial); + } + + public toObject() { + return this; + } +} diff --git a/src/company/service/company.service.spec.ts b/src/company/service/company.service.spec.ts new file mode 100644 index 0000000..c1e1bb4 --- /dev/null +++ b/src/company/service/company.service.spec.ts @@ -0,0 +1,109 @@ +import { Test, TestingModule } from "@nestjs/testing"; +import { CompanyService } from "./company.service"; +import { getModelToken } from "@nestjs/mongoose"; +import { Model } from "mongoose"; +import { Company } from "./company.model"; +import { Query } from "mongoose"; +import { CreateCompanyDto } from "../dto/create-company.dto"; + +describe("CompanyService", () => { + let service: CompanyService; + let companyModel: Model; + + beforeEach(async () => { + const module: TestingModule = await Test.createTestingModule({ + providers: [ + CompanyService, + { + provide: getModelToken(Company.name), + useValue: { + create: jest.fn(), + find: jest.fn(), + findOne: jest.fn(), + findByIdAndUpdate: jest.fn(), + updateMany: jest.fn(), + findByIdAndDelete: jest.fn(), + }, + }, + ], + }).compile(); + + service = module.get(CompanyService); + companyModel = module.get>(getModelToken(Company.name)); + }); + + it("should be defined", () => { + expect(service).toBeDefined(); + }); + + describe("create", () => { + it("should create a new company", async () => { + const request: CreateCompanyDto = { + name: "example", + planId: "example", + planName: "example", + isActive: true, + status: "example", + dueDate: new Date().toString(), + recurrence: true, + phone: "example", + password: "example", + campaignsEnabled: true, // Updated to string value + }; + + const expectedResult = new Company({ + ...request, + _id: "6586033c9f031a4394c1852f", + createdAt: new Date(), + updatedAt: new Date(), + }); + + jest.spyOn(companyModel, "create").mockResolvedValue(expectedResult as any); + + const result = await service.create(request); + + expect(companyModel.create).toHaveBeenCalledWith(request); + expect(result).toEqual(expectedResult); + }); + }); + + + describe("findAll", () => { + it("should find all companies", async () => { + const mockCompanyData = [ + new Company({ + name: "Test Company", + phone: "123456789", + planName: "Test Plan", + planId: "Test Plan", + toObject: function () { return this; }, + }), + ]; + + const mockExecResult = jest.fn().mockResolvedValue(mockCompanyData); + const mockSortResult = jest.fn().mockReturnThis(); + const mockPopulateResult = jest.fn().mockReturnThis(); + + jest.spyOn(companyModel, "find").mockImplementation(() => ({ + populate: mockPopulateResult, + sort: mockSortResult, + exec: mockExecResult, + }) as unknown as Query); + + const result = await service.findAll(); + + expect(mockPopulateResult).toHaveBeenCalled(); + expect(mockSortResult).toHaveBeenCalled(); + expect(mockExecResult).toHaveBeenCalled(); + expect(result).toEqual(mockCompanyData.map(company => { + const companyObj = company.toObject(); + const planName = (company.planId as any).name; // Casting para Plan, pois sabemos que foi populado + return { + ...companyObj, + planName // Adiciona a propriedade planName com o valor do nome do plano + }; + })); + }); + }); + +}); \ No newline at end of file