Skip to content

Commit

Permalink
Merge pull request #2 from fga-eps-mds/develop
Browse files Browse the repository at this point in the history
aumentando cobertura de testes
  • Loading branch information
victorleaoo authored Jul 27, 2024
2 parents eab2fcb + 2246c67 commit 5c9f89b
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
46 changes: 46 additions & 0 deletions src/index.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import request from 'supertest';
import express, { Express, Request, Response } from 'express';
import httpProxy from 'express-http-proxy';
import cors from 'cors';
import { getHost, getUrl } from './utils';
import dotenv from 'dotenv';

dotenv.config();

// Mock das funções utilitárias
jest.mock('./utils', () => ({
getHost: jest.fn().mockReturnValue('http://example.com'),
getUrl: jest.fn().mockReturnValue('/mocked-path'),
}));

// Configuração do servidor para testes
const createServer = (): Express => {
const app: Express = express();
app.use(cors());

app.get('/', (req: Request, res: Response) => {
res.send('Express + TypeScript Server');
});

app.use('/*', httpProxy(jest.requireMock('./utils').getHost, {
proxyReqPathResolver(req) {
return jest.requireMock('./utils').getUrl(req);
}
}));

return app;
};

describe('Express Server', () => {
let app: Express;

beforeEach(() => {
app = createServer();
});

it('should return 200 OK for GET /', async () => {
const response = await request(app).get('/');
expect(response.status).toBe(200);
expect(response.text).toBe('Express + TypeScript Server');
});
});
11 changes: 11 additions & 0 deletions src/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ const videoMockedRequest = {
url: '/api'
} as Request

const adminMockedRequest = {
baseUrl: '/adminservice',
url: '/api'
} as Request

const noSuportedMockedRequest = {
baseUrl: '/noService',
url: '/?all=true'
Expand All @@ -26,6 +31,12 @@ describe('Should test get url complement', () => {
const url = getHost(videoMockedRequest)
expect(url).toEqual('http://localhost:8002')
})

it('Should return admin api url', () => {
const url = getHost(adminMockedRequest)
expect(url).toEqual('http://localhost:8080')
})

it('Should return null api url', () => {
const url = getHost(noSuportedMockedRequest)
expect(url).toEqual('')
Expand Down

0 comments on commit 5c9f89b

Please sign in to comment.