-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathoso-instance.spec.ts
46 lines (39 loc) · 1.59 KB
/
oso-instance.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import { ExecutionContext, UnauthorizedException } from '@nestjs/common';
import { HttpArgumentsHost } from '@nestjs/common/interfaces';
import { Test, TestingModule } from '@nestjs/testing';
import { OsoInstance } from './oso-instance';
import { mockDeep } from 'jest-mock-extended';
describe('OsoInstance', () => {
let osoInstance: OsoInstance;
let executionContext: ExecutionContext;
beforeEach(async () => {
const testingModule: TestingModule = await Test.createTestingModule({
imports: [OsoInstance]
}).compile();
executionContext = mockDeep<ExecutionContext>();
osoInstance = testingModule.get<OsoInstance>(OsoInstance);
});
afterEach(() => {
jest.resetAllMocks();
});
it('should be defined', () => {
expect(OsoInstance).toBeDefined();
});
it('should put itself into the request context on canActivate()', () => {
const mockSwitchToHttp = jest.spyOn(executionContext, 'switchToHttp');
const mockHttpContext: HttpArgumentsHost = mockDeep<HttpArgumentsHost>();
const mockGetRequest = jest.spyOn(mockHttpContext, 'getRequest');
const mockRequest = {
oso: undefined
};
mockSwitchToHttp.mockReturnValueOnce(mockHttpContext);
mockGetRequest.mockReturnValueOnce(mockRequest);
const returnValue = osoInstance.canActivate(executionContext);
expect(returnValue).toBeTruthy();
expect(mockRequest.oso).toBeDefined();
expect(mockRequest.oso).toEqual(osoInstance);
});
it('should throw an UnauthorizedException on unauthorized()', () => {
expect(() => {osoInstance.unauthorized();}).toThrow(UnauthorizedException);
});
});