Skip to content

Commit

Permalink
fix: code style fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
DominMFD committed Sep 12, 2024
1 parent f93f881 commit 53bb7d0
Showing 1 changed file with 30 additions and 36 deletions.
66 changes: 30 additions & 36 deletions src/features/twitter/controllers/twitter-controller.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { NextFunction, Request, Response } from 'express';
import type { Request, Response } from 'express';
import { mock, mockDeep } from 'vitest-mock-extended';

import { HttpError } from '@/shared/errors/http-error';
Expand All @@ -13,35 +13,39 @@ import { LoginTwitterService } from '../services/login-twitter-service';
import type { TwitterService } from '../services/twitter-service';
import { TwitterController } from './twitter-controller';

describe('[Controller] Twitter', () => {
let mockLogger: Logger;
let twitterServiceMock: TwitterService;
let authorizeTwitterService: AuthorizeTwitterService;
let authController: TwitterController;
let req: Request;
let res: Response;
let next: NextFunction;
beforeEach(() => {
mockLogger = mock<Logger>(loggerMock);

twitterServiceMock = mock<TwitterService>({
getTwitterOAuthToken: vi.fn(),
getTwitterUser: vi.fn(),
});

const makeSut = () => {
const mockLogger: Logger = mock<Logger>(loggerMock);
const twitterServiceMock = mock<TwitterService>({
getTwitterOAuthToken: vi.fn(),
getTwitterUser: vi.fn(),
});
const authorizeTwitterService = mock<AuthorizeTwitterService>(
new AuthorizeTwitterService(
mockLogger,
twitterServiceMock,
accountRepositoryMock,
tokenRepositoryMock
)
);

const loginTwitterService = mock<LoginTwitterService>(
new LoginTwitterService()
new LoginTwitterService({
secretKey: 'secret_key',
})
);

const authController = new TwitterController(
authorizeTwitterService,
loginTwitterService
);

authController = new TwitterController(authorizeTwitterService);


const req = mockDeep<Request>();
const res = {
json: vi.fn(),
send: vi.fn(),
status: vi.fn().mockReturnThis(),
} as unknown as Response;
const next = vi.fn();
return {
authController,
authorizeTwitterService,
Expand All @@ -53,26 +57,16 @@ describe('[Controller] Twitter', () => {
twitterServiceMock,
};
};
req = mockDeep<Request>();

res = {
json: vi.fn(),
send: vi.fn(),
status: vi.fn().mockReturnThis(),
} as unknown as Response;

next = vi.fn() as unknown as NextFunction;
});

describe('[Controller] Twitter', () => {
describe('callback', () => {
it('should be return code', async () => {
const { authController, authorizeTwitterService, next, req, res } =
makeSut();
const spyAuthorizeTwitter = vi
.spyOn(authorizeTwitterService, 'execute')
.mockReturnThis();
req.query = { code: '123', state: '123' };

await authController.callback(req, res, next);

expect(spyAuthorizeTwitter).toHaveBeenCalledWith({
code: '123',
state: '123',
Expand All @@ -82,7 +76,7 @@ describe('[Controller] Twitter', () => {
});

describe('login', () => {
it('return a URL link on successful login', () => {
it('should be return a URL link on successful login', () => {
const { authController, loginTwitterService, next, req, res } = makeSut();

req.headers.authorization = 'Bearer token';
Expand All @@ -99,9 +93,9 @@ describe('[Controller] Twitter', () => {
}),
it('should return 401 if authorization header is missing', () => {
const { authController, next, req, res } = makeSut();
const error = new HttpError(HttpStatusCode.badRequest, 'Unauthorized');

req.headers.authorization = undefined;
const error = new HttpError(HttpStatusCode.badRequest, 'Unauthorized');

authController.login(req, res, next);

Expand Down

0 comments on commit 53bb7d0

Please sign in to comment.