diff --git a/x-pack/plugins/snapshot_restore/__jest__/client_integration/helpers/home.helpers.ts b/x-pack/plugins/snapshot_restore/__jest__/client_integration/helpers/home.helpers.ts index 45b31a19693b7..9970b0100ceb8 100644 --- a/x-pack/plugins/snapshot_restore/__jest__/client_integration/helpers/home.helpers.ts +++ b/x-pack/plugins/snapshot_restore/__jest__/client_integration/helpers/home.helpers.ts @@ -14,6 +14,7 @@ import { AsyncTestBedConfig, delay, } from '@kbn/test-jest-helpers'; +import { HttpSetup } from 'src/core/public'; import { SnapshotRestoreHome } from '../../../public/application/sections/home/home'; import { BASE_PATH } from '../../../public/application/constants'; import { WithAppDependencies } from './setup_environment'; @@ -26,8 +27,6 @@ const testBedConfig: AsyncTestBedConfig = { doMountAsync: true, }; -const initTestBed = registerTestBed(WithAppDependencies(SnapshotRestoreHome), testBedConfig); - export interface HomeTestBed extends TestBed { actions: { clickReloadButton: () => void; @@ -40,7 +39,11 @@ export interface HomeTestBed extends TestBed { }; } -export const setup = async (): Promise => { +export const setup = async (httpSetup: HttpSetup): Promise => { + const initTestBed = registerTestBed( + WithAppDependencies(SnapshotRestoreHome, httpSetup), + testBedConfig + ); const testBed = await initTestBed(); const REPOSITORY_TABLE = 'repositoryTable'; const SNAPSHOT_TABLE = 'snapshotTable'; diff --git a/x-pack/plugins/snapshot_restore/__jest__/client_integration/helpers/http_requests.ts b/x-pack/plugins/snapshot_restore/__jest__/client_integration/helpers/http_requests.ts index 662c50a98bfe8..2ea523f77e3a2 100644 --- a/x-pack/plugins/snapshot_restore/__jest__/client_integration/helpers/http_requests.ts +++ b/x-pack/plugins/snapshot_restore/__jest__/client_integration/helpers/http_requests.ts @@ -5,117 +5,119 @@ * 2.0. */ -import sinon, { SinonFakeServer } from 'sinon'; +import { httpServiceMock } from '../../../../../../src/core/public/mocks'; import { API_BASE_PATH } from '../../../common'; +type HttpMethod = 'GET' | 'PUT' | 'POST'; type HttpResponse = Record | any[]; -const mockResponse = (defaultResponse: HttpResponse, response?: HttpResponse) => [ - 200, - { 'Content-Type': 'application/json' }, - JSON.stringify({ ...defaultResponse, ...response }), -]; +interface ResponseError { + statusCode: number; + message: string | Error; +} // Register helpers to mock HTTP Requests -const registerHttpRequestMockHelpers = (server: SinonFakeServer) => { - const setLoadRepositoriesResponse = (response: HttpResponse = {}) => { - const defaultResponse = { repositories: [] }; - - server.respondWith( - 'GET', - `${API_BASE_PATH}repositories`, - mockResponse(defaultResponse, response) - ); +const registerHttpRequestMockHelpers = ( + httpSetup: ReturnType +) => { + const mockResponses = new Map>>( + ['GET', 'PUT', 'POST'].map( + (method) => [method, new Map()] as [HttpMethod, Map>] + ) + ); + + const mockMethodImplementation = (method: HttpMethod, path: string) => + mockResponses.get(method)?.get(path) ?? Promise.resolve({}); + + httpSetup.get.mockImplementation((path) => + mockMethodImplementation('GET', path as unknown as string) + ); + httpSetup.post.mockImplementation((path) => + mockMethodImplementation('POST', path as unknown as string) + ); + httpSetup.put.mockImplementation((path) => + mockMethodImplementation('PUT', path as unknown as string) + ); + + const mockResponse = (method: HttpMethod, path: string, response?: unknown, error?: unknown) => { + const defuse = (promise: Promise) => { + promise.catch(() => {}); + return promise; + }; + + return mockResponses + .get(method)! + .set(path, error ? defuse(Promise.reject({ body: error })) : Promise.resolve(response)); }; - const setLoadRepositoryTypesResponse = (response: HttpResponse = []) => { - server.respondWith('GET', `${API_BASE_PATH}repository_types`, JSON.stringify(response)); - }; + const setLoadRepositoriesResponse = ( + response: HttpResponse = { repositories: [] }, + error?: ResponseError + ) => mockResponse('GET', `${API_BASE_PATH}repositories`, response, error); - const setGetRepositoryResponse = (response?: HttpResponse, delay = 0) => { - const defaultResponse = {}; - - server.respondWith( - 'GET', - /api\/snapshot_restore\/repositories\/.+/, - mockResponse(defaultResponse, response) - ); - }; + const setLoadRepositoryTypesResponse = (response: HttpResponse = [], error?: ResponseError) => + mockResponse('GET', `${API_BASE_PATH}repository_types`, response, error); - const setSaveRepositoryResponse = (response?: HttpResponse, error?: any) => { - const status = error ? error.status || 400 : 200; - const body = error ? JSON.stringify(error.body) : JSON.stringify(response); + const setGetRepositoryResponse = ( + repositoryName: string, + response?: HttpResponse, + error?: ResponseError + ) => mockResponse('GET', `${API_BASE_PATH}repositories/${repositoryName}`, response, error); - server.respondWith('PUT', `${API_BASE_PATH}repositories`, [ - status, - { 'Content-Type': 'application/json' }, - body, - ]); - }; + const setSaveRepositoryResponse = (response?: HttpResponse, error?: ResponseError) => + mockResponse('PUT', `${API_BASE_PATH}repositories`, response, error); - const setLoadSnapshotsResponse = (response: HttpResponse = {}) => { + const setLoadSnapshotsResponse = (response?: HttpResponse, error?: ResponseError) => { const defaultResponse = { errors: {}, snapshots: [], repositories: [], total: 0 }; - - server.respondWith('GET', `${API_BASE_PATH}snapshots`, mockResponse(defaultResponse, response)); + return mockResponse('GET', `${API_BASE_PATH}snapshots`, response ?? defaultResponse, error); }; - const setGetSnapshotResponse = (response?: HttpResponse) => { - const defaultResponse = {}; - - server.respondWith( + const setGetSnapshotResponse = ( + repositoryName: string, + snapshotName: string, + response?: HttpResponse, + error?: ResponseError + ) => + mockResponse( 'GET', - /\/api\/snapshot_restore\/snapshots\/.+/, - mockResponse(defaultResponse, response) + `${API_BASE_PATH}snapshots/${repositoryName}/${snapshotName}`, + response, + error ); - }; - - const setLoadIndicesResponse = (response: HttpResponse = {}) => { - const defaultResponse = { indices: [] }; - server.respondWith( - 'GET', - `${API_BASE_PATH}policies/indices`, - mockResponse(defaultResponse, response) + const setLoadIndicesResponse = ( + response: HttpResponse = { indices: [] }, + error?: ResponseError + ) => mockResponse('GET', `${API_BASE_PATH}policies/indices`, response, error); + + const setAddPolicyResponse = (response?: HttpResponse, error?: ResponseError) => + mockResponse('POST', `${API_BASE_PATH}policies`, response, error); + + const setCleanupRepositoryResponse = ( + repositoryName: string, + response?: HttpResponse, + error?: ResponseError + ) => + mockResponse('POST', `${API_BASE_PATH}repositories/${repositoryName}/cleanup`, response, error); + + const setGetPolicyResponse = ( + policyName: string, + response?: HttpResponse, + error?: ResponseError + ) => mockResponse('GET', `${API_BASE_PATH}policy/${policyName}`, response, error); + + const setRestoreSnapshotResponse = ( + repositoryName: string, + snapshotId: string, + response?: HttpResponse, + error?: ResponseError + ) => + mockResponse( + 'POST', + `${API_BASE_PATH}restore/${repositoryName}/${snapshotId}`, + response, + error ); - }; - - const setAddPolicyResponse = (response?: HttpResponse, error?: any) => { - const status = error ? error.status || 400 : 200; - const body = error ? JSON.stringify(error.body) : JSON.stringify(response); - - server.respondWith('POST', `${API_BASE_PATH}policies`, [ - status, - { 'Content-Type': 'application/json' }, - body, - ]); - }; - - const setCleanupRepositoryResponse = (response?: HttpResponse, error?: any) => { - const status = error ? error.status || 503 : 200; - const body = error ? JSON.stringify(error) : JSON.stringify(response); - - server.respondWith('POST', `${API_BASE_PATH}repositories/:name/cleanup`, [ - status, - { 'Content-Type': 'application/json' }, - body, - ]); - }; - - const setGetPolicyResponse = (response?: HttpResponse) => { - server.respondWith('GET', `${API_BASE_PATH}policy/:name`, [ - 200, - { 'Content-Type': 'application/json' }, - JSON.stringify(response), - ]); - }; - - const setRestoreSnapshotResponse = (response?: HttpResponse) => { - server.respondWith('POST', `${API_BASE_PATH}restore/:repository/:snapshot`, [ - 200, - { 'Content-Type': 'application/json' }, - JSON.stringify(response), - ]); - }; return { setLoadRepositoriesResponse, @@ -133,18 +135,11 @@ const registerHttpRequestMockHelpers = (server: SinonFakeServer) => { }; export const init = () => { - const server = sinon.fakeServer.create(); - server.respondImmediately = true; - - // Define default response for unhandled requests. - // We make requests to APIs which don't impact the component under test, e.g. UI metric telemetry, - // and we can mock them all with a 200 instead of mocking each one individually. - server.respondWith([200, {}, 'DefaultResponse']); - - const httpRequestsMockHelpers = registerHttpRequestMockHelpers(server); + const httpSetup = httpServiceMock.createSetupContract(); + const httpRequestsMockHelpers = registerHttpRequestMockHelpers(httpSetup); return { - server, + httpSetup, httpRequestsMockHelpers, }; }; diff --git a/x-pack/plugins/snapshot_restore/__jest__/client_integration/helpers/policy_add.helpers.ts b/x-pack/plugins/snapshot_restore/__jest__/client_integration/helpers/policy_add.helpers.ts index d921b96781d0c..a5cf9aceff8c4 100644 --- a/x-pack/plugins/snapshot_restore/__jest__/client_integration/helpers/policy_add.helpers.ts +++ b/x-pack/plugins/snapshot_restore/__jest__/client_integration/helpers/policy_add.helpers.ts @@ -6,6 +6,7 @@ */ import { registerTestBed, AsyncTestBedConfig } from '@kbn/test-jest-helpers'; +import { HttpSetup } from 'src/core/public'; import { PolicyAdd } from '../../../public/application/sections/policy_add'; import { formSetup, PolicyFormTestSubjects } from './policy_form.helpers'; import { WithAppDependencies } from './setup_environment'; @@ -18,9 +19,11 @@ const testBedConfig: AsyncTestBedConfig = { doMountAsync: true, }; -const initTestBed = registerTestBed( - WithAppDependencies(PolicyAdd), - testBedConfig -); +export const setup = async (httpSetup: HttpSetup) => { + const initTestBed = registerTestBed( + WithAppDependencies(PolicyAdd, httpSetup), + testBedConfig + ); -export const setup = formSetup.bind(null, initTestBed); + return formSetup(initTestBed); +}; diff --git a/x-pack/plugins/snapshot_restore/__jest__/client_integration/helpers/policy_edit.helpers.ts b/x-pack/plugins/snapshot_restore/__jest__/client_integration/helpers/policy_edit.helpers.ts index 461351e744125..3334dc5337a4d 100644 --- a/x-pack/plugins/snapshot_restore/__jest__/client_integration/helpers/policy_edit.helpers.ts +++ b/x-pack/plugins/snapshot_restore/__jest__/client_integration/helpers/policy_edit.helpers.ts @@ -6,6 +6,7 @@ */ import { registerTestBed, AsyncTestBedConfig } from '@kbn/test-jest-helpers'; +import { HttpSetup } from 'src/core/public'; import { PolicyEdit } from '../../../public/application/sections/policy_edit'; import { WithAppDependencies } from './setup_environment'; import { POLICY_NAME } from './constant'; @@ -19,9 +20,11 @@ const testBedConfig: AsyncTestBedConfig = { doMountAsync: true, }; -const initTestBed = registerTestBed( - WithAppDependencies(PolicyEdit), - testBedConfig -); +export const setup = async (httpSetup: HttpSetup) => { + const initTestBed = registerTestBed( + WithAppDependencies(PolicyEdit, httpSetup), + testBedConfig + ); -export const setup = formSetup.bind(null, initTestBed); + return formSetup(initTestBed); +}; diff --git a/x-pack/plugins/snapshot_restore/__jest__/client_integration/helpers/repository_add.helpers.ts b/x-pack/plugins/snapshot_restore/__jest__/client_integration/helpers/repository_add.helpers.ts index c6460078482bf..c4a5fc4b7be93 100644 --- a/x-pack/plugins/snapshot_restore/__jest__/client_integration/helpers/repository_add.helpers.ts +++ b/x-pack/plugins/snapshot_restore/__jest__/client_integration/helpers/repository_add.helpers.ts @@ -6,14 +6,11 @@ */ import { registerTestBed, TestBed } from '@kbn/test-jest-helpers'; +import { HttpSetup } from 'src/core/public'; import { RepositoryType } from '../../../common/types'; import { RepositoryAdd } from '../../../public/application/sections/repository_add'; import { WithAppDependencies } from './setup_environment'; -const initTestBed = registerTestBed(WithAppDependencies(RepositoryAdd), { - doMountAsync: true, -}); - export interface RepositoryAddTestBed extends TestBed { actions: { clickNextButton: () => void; @@ -23,7 +20,13 @@ export interface RepositoryAddTestBed extends TestBed }; } -export const setup = async (): Promise => { +export const setup = async (httpSetup: HttpSetup): Promise => { + const initTestBed = registerTestBed( + WithAppDependencies(RepositoryAdd, httpSetup), + { + doMountAsync: true, + } + ); const testBed = await initTestBed(); // User actions diff --git a/x-pack/plugins/snapshot_restore/__jest__/client_integration/helpers/repository_edit.helpers.ts b/x-pack/plugins/snapshot_restore/__jest__/client_integration/helpers/repository_edit.helpers.ts index 275c216d70664..f188dd3d61fb5 100644 --- a/x-pack/plugins/snapshot_restore/__jest__/client_integration/helpers/repository_edit.helpers.ts +++ b/x-pack/plugins/snapshot_restore/__jest__/client_integration/helpers/repository_edit.helpers.ts @@ -6,6 +6,7 @@ */ import { registerTestBed, AsyncTestBedConfig } from '@kbn/test-jest-helpers'; +import { HttpSetup } from 'src/core/public'; import { RepositoryEdit } from '../../../public/application/sections/repository_edit'; import { WithAppDependencies } from './setup_environment'; import { REPOSITORY_NAME } from './constant'; @@ -18,10 +19,12 @@ const testBedConfig: AsyncTestBedConfig = { doMountAsync: true, }; -export const setup = registerTestBed( - WithAppDependencies(RepositoryEdit), - testBedConfig -); +export const setup = (httpSetup: HttpSetup) => { + return registerTestBed( + WithAppDependencies(RepositoryEdit, httpSetup), + testBedConfig + ); +}; export type RepositoryEditTestSubjects = TestSubjects | ThreeLevelDepth | NonVisibleTestSubjects; diff --git a/x-pack/plugins/snapshot_restore/__jest__/client_integration/helpers/restore_snapshot.helpers.ts b/x-pack/plugins/snapshot_restore/__jest__/client_integration/helpers/restore_snapshot.helpers.ts index 86c93a6811bd4..a983aa56f4f23 100644 --- a/x-pack/plugins/snapshot_restore/__jest__/client_integration/helpers/restore_snapshot.helpers.ts +++ b/x-pack/plugins/snapshot_restore/__jest__/client_integration/helpers/restore_snapshot.helpers.ts @@ -6,6 +6,7 @@ */ import { act } from 'react-dom/test-utils'; +import { HttpSetup } from 'src/core/public'; import { registerTestBed, TestBed, AsyncTestBedConfig } from '@kbn/test-jest-helpers'; import { RestoreSnapshot } from '../../../public/application/sections/restore_snapshot'; import { WithAppDependencies } from './setup_environment'; @@ -18,11 +19,6 @@ const testBedConfig: AsyncTestBedConfig = { doMountAsync: true, }; -const initTestBed = registerTestBed( - WithAppDependencies(RestoreSnapshot), - testBedConfig -); - const setupActions = (testBed: TestBed) => { const { find, component, form, exists } = testBed; @@ -84,7 +80,12 @@ export type RestoreSnapshotTestBed = TestBed & { actions: Actions; }; -export const setup = async (): Promise => { +export const setup = async (httpSetup: HttpSetup): Promise => { + const initTestBed = registerTestBed( + WithAppDependencies(RestoreSnapshot, httpSetup), + testBedConfig + ); + const testBed = await initTestBed(); return { diff --git a/x-pack/plugins/snapshot_restore/__jest__/client_integration/helpers/setup_environment.tsx b/x-pack/plugins/snapshot_restore/__jest__/client_integration/helpers/setup_environment.tsx index 66ba21b136816..587d53f4c8a47 100644 --- a/x-pack/plugins/snapshot_restore/__jest__/client_integration/helpers/setup_environment.tsx +++ b/x-pack/plugins/snapshot_restore/__jest__/client_integration/helpers/setup_environment.tsx @@ -6,11 +6,10 @@ */ import React from 'react'; -import axios from 'axios'; -import axiosXhrAdapter from 'axios/lib/adapters/xhr'; import { i18n } from '@kbn/i18n'; import { LocationDescriptorObject } from 'history'; +import { HttpSetup } from 'src/core/public'; import { coreMock, scopedHistoryMock } from 'src/core/public/mocks'; import { setUiMetricService, httpService } from '../../../public/application/services/http'; import { @@ -22,8 +21,6 @@ import { textService } from '../../../public/application/services/text'; import { init as initHttpRequests } from './http_requests'; import { UiMetricService } from '../../../public/application/services'; -const mockHttpClient = axios.create({ adapter: axiosXhrAdapter }); - const history = scopedHistoryMock.create(); history.createHref.mockImplementation((location: LocationDescriptorObject) => { return `${location.pathname}?${location.search}`; @@ -48,18 +45,11 @@ const appDependencies = { }; export const setupEnvironment = () => { - // @ts-ignore - httpService.setup(mockHttpClient); breadcrumbService.setup(() => undefined); textService.setup(i18n); docTitleService.setup(() => undefined); - const { server, httpRequestsMockHelpers } = initHttpRequests(); - - return { - server, - httpRequestsMockHelpers, - }; + return initHttpRequests(); }; /** @@ -70,9 +60,12 @@ export const setupEnvironment = () => { this.terminate = () => {}; }; -export const WithAppDependencies = (Comp: any) => (props: any) => - ( +export const WithAppDependencies = (Comp: any, httpSetup: HttpSetup) => (props: any) => { + httpService.setup(httpSetup); + + return ( ); +}; diff --git a/x-pack/plugins/snapshot_restore/__jest__/client_integration/helpers/snapshot_list.helpers.ts b/x-pack/plugins/snapshot_restore/__jest__/client_integration/helpers/snapshot_list.helpers.ts index 261976623144b..b64ee2371da04 100644 --- a/x-pack/plugins/snapshot_restore/__jest__/client_integration/helpers/snapshot_list.helpers.ts +++ b/x-pack/plugins/snapshot_restore/__jest__/client_integration/helpers/snapshot_list.helpers.ts @@ -8,6 +8,7 @@ import { act } from 'react-dom/test-utils'; import { TestBedConfig, registerTestBed, TestBed } from '@kbn/test-jest-helpers'; +import { HttpSetup } from 'src/core/public'; import { BASE_PATH } from '../../../public/application/constants'; import { SnapshotList } from '../../../public/application/sections/home/snapshot_list'; import { WithAppDependencies } from './setup_environment'; @@ -19,9 +20,6 @@ const getTestBedConfig = (query?: string): TestBedConfig => ({ }, }); -const initTestBed = (query?: string) => - registerTestBed(WithAppDependencies(SnapshotList), getTestBedConfig(query))(); - export interface SnapshotListTestBed extends TestBed { actions: { setSearchText: (value: string, advanceTime?: boolean) => void; @@ -33,8 +31,12 @@ export interface SnapshotListTestBed extends TestBed { const searchBarSelector = 'snapshotListSearch'; const searchErrorSelector = 'snapshotListSearchError'; -export const setup = async (query?: string): Promise => { - const testBed = await initTestBed(query); +export const setup = async (httpSetup: HttpSetup, query?: string): Promise => { + const initTestBed = registerTestBed( + WithAppDependencies(SnapshotList, httpSetup), + getTestBedConfig(query) + ); + const testBed = await initTestBed(); const { form, component, find, exists } = testBed; const setSearchText = async (value: string, advanceTime = true) => { diff --git a/x-pack/plugins/snapshot_restore/__jest__/client_integration/home.test.ts b/x-pack/plugins/snapshot_restore/__jest__/client_integration/home.test.ts index 60fe9d2bd7128..740940b257a7a 100644 --- a/x-pack/plugins/snapshot_restore/__jest__/client_integration/home.test.ts +++ b/x-pack/plugins/snapshot_restore/__jest__/client_integration/home.test.ts @@ -41,16 +41,12 @@ const removeWhiteSpaceOnArrayValues = (array: any[]) => }); describe('', () => { - const { server, httpRequestsMockHelpers } = setupEnvironment(); + const { httpSetup, httpRequestsMockHelpers } = setupEnvironment(); let testBed: HomeTestBed; - afterAll(() => { - server.restore(); - }); - describe('on component mount', () => { beforeEach(async () => { - testBed = await setup(); + testBed = await setup(httpSetup); }); test('should set the correct app title', () => { @@ -79,7 +75,7 @@ describe('', () => { describe('tabs', () => { beforeEach(async () => { - testBed = await setup(); + testBed = await setup(httpSetup); await act(async () => { await nextTick(); @@ -132,7 +128,7 @@ describe('', () => { }); test('should display an empty prompt', async () => { - const { component, exists } = await setup(); + const { component, exists } = await setup(httpSetup); await act(async () => { await nextTick(); @@ -164,7 +160,7 @@ describe('', () => { beforeEach(async () => { httpRequestsMockHelpers.setLoadRepositoriesResponse({ repositories }); - testBed = await setup(); + testBed = await setup(httpSetup); await act(async () => { await nextTick(); @@ -208,7 +204,6 @@ describe('', () => { test('should have a button to reload the repositories', async () => { const { component, exists, actions } = testBed; - const totalRequests = server.requests.length; expect(exists('reloadButton')).toBe(true); await act(async () => { @@ -217,9 +212,9 @@ describe('', () => { component.update(); }); - expect(server.requests.length).toBe(totalRequests + 1); - expect(server.requests[server.requests.length - 1].url).toBe( - `${API_BASE_PATH}repositories` + expect(httpSetup.get).toHaveBeenLastCalledWith( + `${API_BASE_PATH}repositories`, + expect.anything() ); }); @@ -273,10 +268,10 @@ describe('', () => { component.update(); }); - const latestRequest = server.requests[server.requests.length - 1]; - - expect(latestRequest.method).toBe('DELETE'); - expect(latestRequest.url).toBe(`${API_BASE_PATH}repositories/${repo1.name}`); + expect(httpSetup.delete).toHaveBeenLastCalledWith( + `${API_BASE_PATH}repositories/${repo1.name}`, + expect.anything() + ); }); }); @@ -304,23 +299,20 @@ describe('', () => { }); test('should show a loading state while fetching the repository', async () => { - server.respondImmediately = false; const { find, exists, actions } = testBed; // By providing undefined, the "loading section" will be displayed - httpRequestsMockHelpers.setGetRepositoryResponse(undefined); + httpRequestsMockHelpers.setGetRepositoryResponse(repo1.name, undefined); await actions.clickRepositoryAt(0); expect(exists('repositoryDetail.sectionLoading')).toBe(true); expect(find('repositoryDetail.sectionLoading').text()).toEqual('Loading repository…'); - - server.respondImmediately = true; }); describe('when the repository has been fetched', () => { beforeEach(async () => { - httpRequestsMockHelpers.setGetRepositoryResponse({ + httpRequestsMockHelpers.setGetRepositoryResponse(repo1.name, { repository: { name: 'my-repo', type: 'fs', @@ -357,15 +349,15 @@ describe('', () => { component.update(); }); - const latestRequest = server.requests[server.requests.length - 1]; - - expect(latestRequest.method).toBe('GET'); - expect(latestRequest.url).toBe(`${API_BASE_PATH}repositories/${repo1.name}/verify`); + expect(httpSetup.get).toHaveBeenLastCalledWith( + `${API_BASE_PATH}repositories/${repo1.name}/verify`, + expect.anything() + ); }); describe('clean repository', () => { test('shows results when request succeeds', async () => { - httpRequestsMockHelpers.setCleanupRepositoryResponse({ + httpRequestsMockHelpers.setCleanupRepositoryResponse(repo1.name, { cleanup: { cleaned: true, response: { @@ -383,16 +375,17 @@ describe('', () => { }); component.update(); - const latestRequest = server.requests[server.requests.length - 1]; - expect(latestRequest.method).toBe('POST'); - expect(latestRequest.url).toBe(`${API_BASE_PATH}repositories/${repo1.name}/cleanup`); + expect(httpSetup.post).toHaveBeenLastCalledWith( + `${API_BASE_PATH}repositories/${repo1.name}/cleanup`, + expect.anything() + ); expect(exists('repositoryDetail.cleanupCodeBlock')).toBe(true); expect(exists('repositoryDetail.cleanupError')).toBe(false); }); test('shows error when success fails', async () => { - httpRequestsMockHelpers.setCleanupRepositoryResponse({ + httpRequestsMockHelpers.setCleanupRepositoryResponse(repo1.name, { cleanup: { cleaned: false, error: { @@ -408,9 +401,10 @@ describe('', () => { }); component.update(); - const latestRequest = server.requests[server.requests.length - 1]; - expect(latestRequest.method).toBe('POST'); - expect(latestRequest.url).toBe(`${API_BASE_PATH}repositories/${repo1.name}/cleanup`); + expect(httpSetup.post).toHaveBeenLastCalledWith( + `${API_BASE_PATH}repositories/${repo1.name}/cleanup`, + expect.anything() + ); expect(exists('repositoryDetail.cleanupCodeBlock')).toBe(false); expect(exists('repositoryDetail.cleanupError')).toBe(true); @@ -420,7 +414,7 @@ describe('', () => { describe('when the repository has been fetched (and has snapshots)', () => { beforeEach(async () => { - httpRequestsMockHelpers.setGetRepositoryResponse({ + httpRequestsMockHelpers.setGetRepositoryResponse(repo1.name, { repository: { name: 'my-repo', type: 'fs', @@ -448,7 +442,7 @@ describe('', () => { }); beforeEach(async () => { - testBed = await setup(); + testBed = await setup(httpSetup); await act(async () => { testBed.actions.selectTab('snapshots'); @@ -478,7 +472,7 @@ describe('', () => { total: 0, }); - testBed = await setup(); + testBed = await setup(httpSetup); await act(async () => { testBed.actions.selectTab('snapshots'); @@ -517,7 +511,7 @@ describe('', () => { total: 2, }); - testBed = await setup(); + testBed = await setup(httpSetup); await act(async () => { testBed.actions.selectTab('snapshots'); @@ -561,7 +555,7 @@ describe('', () => { }, }); - testBed = await setup(); + testBed = await setup(httpSetup); await act(async () => { testBed.actions.selectTab('snapshots'); @@ -589,7 +583,7 @@ describe('', () => { }, }); - testBed = await setup(); + testBed = await setup(httpSetup); await act(async () => { testBed.actions.selectTab('snapshots'); @@ -625,7 +619,6 @@ describe('', () => { test('should have a button to reload the snapshots', async () => { const { component, exists, actions } = testBed; - const totalRequests = server.requests.length; expect(exists('reloadButton')).toBe(true); await act(async () => { @@ -634,13 +627,19 @@ describe('', () => { component.update(); }); - expect(server.requests.length).toBe(totalRequests + 1); - expect(server.requests[server.requests.length - 1].url).toBe(`${API_BASE_PATH}snapshots`); + expect(httpSetup.get).toHaveBeenLastCalledWith( + `${API_BASE_PATH}snapshots`, + expect.anything() + ); }); describe('detail panel', () => { beforeEach(async () => { - httpRequestsMockHelpers.setGetSnapshotResponse(snapshot1); + httpRequestsMockHelpers.setGetSnapshotResponse( + snapshot1.repository, + snapshot1.snapshot, + snapshot1 + ); }); test('should show the detail when clicking on a snapshot', async () => { @@ -656,17 +655,18 @@ describe('', () => { // that makes the component crash. I tried a few things with no luck so, as this // is a low impact test, I prefer to skip it and move on. test.skip('should show a loading while fetching the snapshot', async () => { - server.respondImmediately = false; const { find, exists, actions } = testBed; // By providing undefined, the "loading section" will be displayed - httpRequestsMockHelpers.setGetSnapshotResponse(undefined); + httpRequestsMockHelpers.setGetSnapshotResponse( + snapshot1.repository, + snapshot1.snapshot, + undefined + ); await actions.clickSnapshotAt(0); expect(exists('snapshotDetail.sectionLoading')).toBe(true); expect(find('snapshotDetail.sectionLoading').text()).toEqual('Loading snapshot…'); - - server.respondImmediately = true; }); describe('on mount', () => { @@ -757,7 +757,11 @@ describe('', () => { const setSnapshotStateAndUpdateDetail = async (state: string) => { const updatedSnapshot = { ...snapshot1, state }; - httpRequestsMockHelpers.setGetSnapshotResponse(updatedSnapshot); + httpRequestsMockHelpers.setGetSnapshotResponse( + snapshot1.repository, + snapshot1.snapshot, + updatedSnapshot + ); await actions.clickSnapshotAt(itemIndexToClickOn); // click another snapshot to trigger the HTTP call }; @@ -806,7 +810,11 @@ describe('', () => { test('should display a message when snapshot in progress ', async () => { const { find, actions } = testBed; const updatedSnapshot = { ...snapshot1, state: 'IN_PROGRESS' }; - httpRequestsMockHelpers.setGetSnapshotResponse(updatedSnapshot); + httpRequestsMockHelpers.setGetSnapshotResponse( + snapshot1.repository, + snapshot1.snapshot, + updatedSnapshot + ); await actions.clickSnapshotAt(1); // click another snapshot to trigger the HTTP call actions.selectSnapshotDetailTab('failedIndices'); @@ -825,7 +833,11 @@ describe('', () => { beforeEach(async () => { const updatedSnapshot = { ...snapshot1, indexFailures }; - httpRequestsMockHelpers.setGetSnapshotResponse(updatedSnapshot); + httpRequestsMockHelpers.setGetSnapshotResponse( + updatedSnapshot.repository, + updatedSnapshot.snapshot, + updatedSnapshot + ); await testBed.actions.clickSnapshotAt(0); testBed.actions.selectSnapshotDetailTab('failedIndices'); });