1 | import { Test } from '@nestjs/testing';
|
2 | import { Request } from 'express';
|
3 |
|
4 | import { AppController } from './app.controller';
|
5 | import { FsMock, PathMock } from './third-party';
|
6 |
|
7 | describe('AppController', () => {
|
8 | let appController: AppController;
|
9 | let fs: FsMock;
|
10 | let path: PathMock;
|
11 | const packageJsonData: object = {
|
12 | name: 'lxdhub-api',
|
13 | version: '1.1.4'
|
14 | };
|
15 | const apiSettings = {
|
16 | docUrl: '/api/v1/docs'
|
17 | };
|
18 |
|
19 |
|
20 | const request: Request = {
|
21 | protocol: 'http',
|
22 | get(type: string) {
|
23 | return 'localhost:8080';
|
24 | }
|
25 | } as Request;
|
26 |
|
27 | beforeEach(async () => {
|
28 |
|
29 | const module = await Test.createTestingModule({
|
30 | controllers: [
|
31 | AppController
|
32 | ],
|
33 | providers: [
|
34 | {
|
35 | provide: 'Fs',
|
36 | useClass: FsMock
|
37 | },
|
38 | {
|
39 | provide: 'Path',
|
40 | useClass: PathMock
|
41 | },
|
42 | {
|
43 | provide: 'LXDHubAPISettings',
|
44 | useFactory: () => apiSettings
|
45 | }
|
46 | ]
|
47 | }).compile();
|
48 | fs = module.get<FsMock>('Fs');
|
49 | path = module.get<PathMock>('Path');
|
50 | appController = module.get<AppController>(AppController);
|
51 | });
|
52 |
|
53 | describe('apiInfo', () => {
|
54 | const packageJsonPath = './package.json';
|
55 | beforeEach(() => {
|
56 |
|
57 | jest.spyOn(path, 'join')
|
58 | .mockImplementation((...paths: string[]) => packageJsonPath);
|
59 | jest.spyOn(fs, 'readFile')
|
60 | .mockImplementation(async (path_: string) => JSON.stringify(packageJsonData));
|
61 | });
|
62 |
|
63 | it('should read the correct file', async () => {
|
64 | await appController.apiInfo(request);
|
65 | expect(fs.readFile).toHaveBeenCalledWith(packageJsonPath, 'utf8');
|
66 | expect(fs.readFile).toHaveBeenCalledTimes(1);
|
67 | });
|
68 |
|
69 | it('should return the correct packageJsonData', async () => {
|
70 | const data = await appController.apiInfo(request);
|
71 | expect(data.api_version).toBe('1.0');
|
72 | expect(data.name).toBe('lxdhub-api');
|
73 | expect(data.package_version).toBe('1.1.4');
|
74 | });
|
75 |
|
76 | it('should return null when repository object is not set in packageJson', async () => {
|
77 | const data = await appController.apiInfo(request);
|
78 | expect(data._links.repository).toBe(null);
|
79 | });
|
80 |
|
81 | it('should return the repository url', async () => {
|
82 | const packageJsondata = {
|
83 | repository: {
|
84 | url: 'https://repo.com'
|
85 | }
|
86 | };
|
87 | jest.spyOn(fs, 'readFile')
|
88 | .mockImplementation(async (path_: string) => JSON.stringify(packageJsondata));
|
89 | const data = await appController.apiInfo(request);
|
90 |
|
91 | expect(data._links.repository).toBe('https://repo.com');
|
92 | });
|
93 |
|
94 | it('should return the correct docs url', async () => {
|
95 | const data = await appController.apiInfo(request);
|
96 | expect(data._links.docs).toBe('http://localhost:8080/api/v1/docs');
|
97 | });
|
98 |
|
99 | it('should throw an error, if the package.json is not valid', async () => {
|
100 | jest.spyOn(fs, 'readFile')
|
101 | .mockImplementation(async (path_: string) => 'not valid data');
|
102 |
|
103 |
|
104 | try {
|
105 | await appController.apiInfo(request);
|
106 | }
|
107 | catch (err) {
|
108 | expect(() => { throw err; }).toThrowError();
|
109 | }
|
110 | });
|
111 | });
|
112 | });
|