Skip to content

Commit

Permalink
fix: tests pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
juliaam committed Sep 6, 2024
1 parent d5ab50e commit 11c8d13
Showing 1 changed file with 36 additions and 44 deletions.
80 changes: 36 additions & 44 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 { Request, Response } from 'express';
import type { NextFunction, Request, Response } from 'express';
import jwt from 'jsonwebtoken';
import { mock, mockDeep } from 'vitest-mock-extended';

Expand All @@ -11,49 +11,44 @@ import { AuthorizeTwitterService } from '../services/authorize-twitter-service';
import type { TwitterService } from '../services/twitter-service';
import { TwitterController } from './twitter-controller';

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 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,
mockLogger,
next,
req,
res,
twitterServiceMock,
};
};

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(),
});

authorizeTwitterService = mock<AuthorizeTwitterService>(
new AuthorizeTwitterService(
mockLogger,
twitterServiceMock,
accountRepositoryMock,
tokenRepositoryMock
)
);

authController = new TwitterController(authorizeTwitterService);

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('callback', () => {
it('returns code', async () => {
const { authController, authorizeTwitterService, next, req, res } =
makeSut();

const spyAuthorizeTwitter = vi
.spyOn(authorizeTwitterService, 'execute')
.mockReturnThis();
Expand All @@ -71,7 +66,6 @@ describe('[Controller] Twitter', () => {

describe('login', () => {
it('return URLs when the token is valid', async () => {
const { authController, next, req, res } = makeSut();
const mockPayload = {
name: 'John Doe',
userId: '12345',
Expand All @@ -90,8 +84,6 @@ describe('[Controller] Twitter', () => {
});

it('returns 401 when token is invalid', () => {
const { authController, next, req, res } = makeSut();

req.headers.authorization = undefined;

authController.login(req, res, next);
Expand Down

0 comments on commit 11c8d13

Please sign in to comment.