Skip to content

Commit 77b491f

Browse files
Merge pull request #6 from vinodnextcoder/feature/user
fix unit test
2 parents 7fcbe39 + 5622fb5 commit 77b491f

File tree

1 file changed

+50
-19
lines changed

1 file changed

+50
-19
lines changed

src/auth/auth.controller.spec.ts

Lines changed: 50 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { MockResponse, createResponse } from "node-mocks-http";
55
import { Response } from "express";
66
import { JwtService } from "@nestjs/jwt";
77

8-
describe("User Controller", () => {
8+
describe("Auth Controller", () => {
99
let controller: AuthController;
1010
let service: AuthService;
1111
const mockUserRequest = {
@@ -26,33 +26,64 @@ describe("User Controller", () => {
2626
}),
2727
},
2828
},
29-
{
30-
provide: JwtService,
31-
useValue: {
32-
signAsync: jest.fn(),
33-
},
34-
},
29+
JwtService
3530
],
3631
}).compile();
3732

3833
controller = module.get<AuthController>(AuthController);
3934
service = module.get<AuthService>(AuthService);
4035
});
4136

42-
describe("auth controller ()", () => {
43-
it("should create access token", async () => {
44-
let mockResponse: MockResponse<Response> = createResponse();
37+
describe("signIn", () => {
38+
it("should create access token and set cookies", async () => {
39+
const mockResponse: MockResponse<Response> = createResponse();
40+
mockResponse.json = jest.fn();
41+
mockResponse.cookie = jest.fn();
42+
43+
await controller.signIn(mockUserRequest, mockResponse);
44+
45+
expect(mockResponse.json).toHaveBeenCalledWith({
46+
message: "SUCCESS",
47+
isSuccess: true,
48+
statusCode:200,
49+
data: null,
50+
});
51+
52+
expect(mockResponse.cookie).toHaveBeenCalledWith(
53+
'access_token',
54+
'eyJhbGciOiJIUzI1NiJ9.sss.aaaaa',
55+
expect.objectContaining({
56+
httpOnly: true,
57+
expires: expect.any(Date),
58+
path: '/',
59+
sameSite: 'none',
60+
secure: true,
61+
})
62+
);
63+
64+
expect(mockResponse.cookie).toHaveBeenCalledWith(
65+
'refresh_token',
66+
'eyJhbGciOiJIUzI1NiJ9.sss.aaaaa',
67+
expect.objectContaining({
68+
httpOnly: true,
69+
expires: expect.any(Date),
70+
path: '/',
71+
sameSite: 'none',
72+
secure: true,
73+
})
74+
);
75+
});
76+
77+
it("should call the service's signIn method", async () => {
78+
const mockResponse: MockResponse<Response> = createResponse();
4579
mockResponse.json = jest.fn();
4680
mockResponse.cookie = jest.fn();
47-
const expectedResponse = {
48-
access_token: "eyJhbGciOiJIUzI1NiJ9.sss.aaaaa",
49-
refresh_token: "eyJhbGciOiJIUzI1NiJ9.sss.aaaaa",
50-
};
51-
const createSpy = jest
52-
.spyOn(service, "signIn")
53-
.mockResolvedValueOnce(expectedResponse);
81+
82+
const signInSpy = jest.spyOn(service, "signIn");
83+
5484
await controller.signIn(mockUserRequest, mockResponse);
55-
expect(createSpy).toHaveBeenCalled();
85+
86+
expect(signInSpy).toHaveBeenCalledWith("te@test.com", "Sairam1@");
5687
});
5788
});
58-
});
89+
});

0 commit comments

Comments
 (0)