|
4 | 4 | * you may not use this file except in compliance with the Elastic License. |
5 | 5 | */ |
6 | 6 |
|
7 | | -import { MockRouter, mockConfig, mockLogger } from '../../__mocks__'; |
| 7 | +import { MockRouter, mockRequestHandler, mockDependencies } from '../../__mocks__'; |
8 | 8 |
|
9 | 9 | import { registerWSOverviewRoute } from './overview'; |
10 | 10 |
|
11 | | -jest.mock('node-fetch'); |
12 | | -const fetch = jest.requireActual('node-fetch'); |
13 | | -const { Response } = fetch; |
14 | | -// eslint-disable-next-line @typescript-eslint/no-var-requires |
15 | | -const fetchMock = require('node-fetch') as jest.Mocked<typeof fetch>; |
16 | | - |
17 | | -const ORG_ROUTE = 'http://localhost:3002/ws/org'; |
18 | | - |
19 | | -describe('engine routes', () => { |
| 11 | +describe('Overview route', () => { |
20 | 12 | describe('GET /api/workplace_search/overview', () => { |
21 | | - const AUTH_HEADER = 'Basic 123'; |
22 | | - const mockRequest = { |
23 | | - headers: { |
24 | | - authorization: AUTH_HEADER, |
25 | | - }, |
26 | | - query: {}, |
27 | | - }; |
28 | | - |
29 | | - const mockRouter = new MockRouter({ method: 'get', payload: 'query' }); |
| 13 | + let mockRouter: MockRouter; |
30 | 14 |
|
31 | 15 | beforeEach(() => { |
32 | 16 | jest.clearAllMocks(); |
33 | | - mockRouter.createRouter(); |
| 17 | + mockRouter = new MockRouter({ method: 'get', payload: 'query' }); |
34 | 18 |
|
35 | 19 | registerWSOverviewRoute({ |
| 20 | + ...mockDependencies, |
36 | 21 | router: mockRouter.router, |
37 | | - log: mockLogger, |
38 | | - config: mockConfig, |
39 | 22 | }); |
40 | 23 | }); |
41 | 24 |
|
42 | | - describe('when the underlying Workplace Search API returns a 200', () => { |
43 | | - beforeEach(() => { |
44 | | - WorkplaceSearchAPI.shouldBeCalledWith(ORG_ROUTE, { |
45 | | - headers: { Authorization: AUTH_HEADER }, |
46 | | - }).andReturn({ accountsCount: 1 }); |
47 | | - }); |
48 | | - |
49 | | - it('should return 200 with a list of overview from the Workplace Search API', async () => { |
50 | | - await mockRouter.callRoute(mockRequest); |
51 | | - |
52 | | - expect(mockRouter.response.ok).toHaveBeenCalledWith({ |
53 | | - body: { accountsCount: 1 }, |
54 | | - headers: { 'content-type': 'application/json' }, |
55 | | - }); |
| 25 | + it('creates a request handler', () => { |
| 26 | + expect(mockRequestHandler.createRequest).toHaveBeenCalledWith({ |
| 27 | + path: '/ws/org', |
| 28 | + hasValidData: expect.any(Function), |
56 | 29 | }); |
57 | 30 | }); |
58 | 31 |
|
59 | | - describe('when the Workplace Search URL is invalid', () => { |
60 | | - beforeEach(() => { |
61 | | - WorkplaceSearchAPI.shouldBeCalledWith(ORG_ROUTE, { |
62 | | - headers: { Authorization: AUTH_HEADER }, |
63 | | - }).andReturnError(); |
64 | | - }); |
| 32 | + describe('hasValidData', () => { |
| 33 | + it('should correctly validate that the response has data', () => { |
| 34 | + const response = { |
| 35 | + accountsCount: 1, |
| 36 | + }; |
65 | 37 |
|
66 | | - it('should return 502 with a message', async () => { |
67 | | - await mockRouter.callRoute(mockRequest); |
| 38 | + const { hasValidData } = mockRequestHandler.createRequest.mock.calls[0][0]; |
68 | 39 |
|
69 | | - expect(mockRouter.response.customError).toHaveBeenCalledWith({ |
70 | | - statusCode: 502, |
71 | | - body: 'cannot-connect', |
72 | | - }); |
73 | | - expect(mockLogger.error).toHaveBeenCalledWith('Cannot connect to Workplace Search: Failed'); |
74 | | - expect(mockLogger.debug).not.toHaveBeenCalled(); |
| 40 | + expect(hasValidData(response)).toBe(true); |
75 | 41 | }); |
76 | | - }); |
77 | 42 |
|
78 | | - describe('when the Workplace Search API returns invalid data', () => { |
79 | | - beforeEach(() => { |
80 | | - WorkplaceSearchAPI.shouldBeCalledWith(ORG_ROUTE, { |
81 | | - headers: { Authorization: AUTH_HEADER }, |
82 | | - }).andReturnInvalidData(); |
83 | | - }); |
| 43 | + it('should correctly validate that a response does not have data', () => { |
| 44 | + const response = { |
| 45 | + foo: 'bar', |
| 46 | + }; |
84 | 47 |
|
85 | | - it('should return 502 with a message', async () => { |
86 | | - await mockRouter.callRoute(mockRequest); |
| 48 | + const hasValidData = mockRequestHandler.createRequest.mock.calls[0][0].hasValidData; |
87 | 49 |
|
88 | | - expect(mockRouter.response.customError).toHaveBeenCalledWith({ |
89 | | - statusCode: 502, |
90 | | - body: 'cannot-connect', |
91 | | - }); |
92 | | - expect(mockLogger.error).toHaveBeenCalledWith( |
93 | | - 'Cannot connect to Workplace Search: Error: Invalid data received from Workplace Search: {"foo":"bar"}' |
94 | | - ); |
95 | | - expect(mockLogger.debug).toHaveBeenCalled(); |
| 50 | + expect(hasValidData(response)).toBe(false); |
96 | 51 | }); |
97 | 52 | }); |
98 | | - |
99 | | - const WorkplaceSearchAPI = { |
100 | | - shouldBeCalledWith(expectedUrl: string, expectedParams: object) { |
101 | | - return { |
102 | | - andReturn(response: object) { |
103 | | - fetchMock.mockImplementation((url: string, params: object) => { |
104 | | - expect(url).toEqual(expectedUrl); |
105 | | - expect(params).toEqual(expectedParams); |
106 | | - |
107 | | - return Promise.resolve(new Response(JSON.stringify(response))); |
108 | | - }); |
109 | | - }, |
110 | | - andReturnInvalidData() { |
111 | | - fetchMock.mockImplementation((url: string, params: object) => { |
112 | | - expect(url).toEqual(expectedUrl); |
113 | | - expect(params).toEqual(expectedParams); |
114 | | - |
115 | | - return Promise.resolve(new Response(JSON.stringify({ foo: 'bar' }))); |
116 | | - }); |
117 | | - }, |
118 | | - andReturnError() { |
119 | | - fetchMock.mockImplementation((url: string, params: object) => { |
120 | | - expect(url).toEqual(expectedUrl); |
121 | | - expect(params).toEqual(expectedParams); |
122 | | - |
123 | | - return Promise.reject('Failed'); |
124 | | - }); |
125 | | - }, |
126 | | - }; |
127 | | - }, |
128 | | - }; |
129 | 53 | }); |
130 | 54 | }); |
0 commit comments