From a02bda76e1c4f305608a7ee50ba442da71ea5742 Mon Sep 17 00:00:00 2001 From: Eli Perelman Date: Wed, 24 Apr 2019 12:19:37 -0500 Subject: [PATCH] Fix failing tests from kfetch refactor --- .../error_auto_create_index.test.js | 43 +++++++++++++------ src/legacy/ui/public/kfetch/kfetch.test.ts | 23 +--------- .../ui/public/kfetch/kfetch_test_setup.ts | 38 ++++++++++++++++ .../components/query_bar.test.mocks.ts | 8 ++++ 4 files changed, 78 insertions(+), 34 deletions(-) create mode 100644 src/legacy/ui/public/kfetch/kfetch_test_setup.ts diff --git a/src/legacy/ui/public/error_auto_create_index/error_auto_create_index.test.js b/src/legacy/ui/public/error_auto_create_index/error_auto_create_index.test.js index 291eff2454c2923..f1f84bc772088ed 100644 --- a/src/legacy/ui/public/error_auto_create_index/error_auto_create_index.test.js +++ b/src/legacy/ui/public/error_auto_create_index/error_auto_create_index.test.js @@ -17,20 +17,37 @@ * under the License. */ -jest.mock('../chrome', () => ({ - addBasePath: path => `myBase/${path}`, -})); -jest.mock('../metadata', () => ({ - metadata: { - version: 'my-version', - }, -})); - +// @ts-ignore import fetchMock from 'fetch-mock/es5/client'; -import { kfetch } from 'ui/kfetch'; +import { __newPlatformSetup__, kfetch } from '../kfetch'; + +/* eslint-disable @kbn/eslint/no-restricted-paths */ +import { HttpService } from '../../../../core/public/http'; +import { fatalErrorsServiceMock } from '../../../../core/public/fatal_errors/fatal_errors_service.mock'; +import { injectedMetadataServiceMock } from '../../../../core/public/injected_metadata/injected_metadata_service.mock'; +import { BasePathService } from '../../../../core/public/base_path'; +/* eslint-enable @kbn/eslint/no-restricted-paths */ + import { isAutoCreateIndexError } from './error_auto_create_index'; +function setupService() { + const httpService = new HttpService(); + const fatalErrors = fatalErrorsServiceMock.createSetupContract(); + const injectedMetadata = injectedMetadataServiceMock.createSetupContract(); + + injectedMetadata.getBasePath.mockReturnValue('http://localhost/myBase'); + + const basePath = new BasePathService().setup({ injectedMetadata }); + const http = httpService.setup({ basePath, fatalErrors, injectedMetadata }); + + return { httpService, fatalErrors, http }; +} + describe('isAutoCreateIndexError correctly handles KFetchError thrown by kfetch', () => { + beforeAll(() => { + __newPlatformSetup__(setupService().http); + }); + describe('404', () => { beforeEach(() => { fetchMock.post({ @@ -45,7 +62,7 @@ describe('isAutoCreateIndexError correctly handles KFetchError thrown by kfetch' test('should return false', async () => { expect.assertions(1); try { - await kfetch({ method: 'POST', pathname: 'my/path' }); + await kfetch({ method: 'POST', pathname: '/my/path' }); } catch (kfetchError) { expect(isAutoCreateIndexError(kfetchError)).toBe(false); } @@ -66,7 +83,7 @@ describe('isAutoCreateIndexError correctly handles KFetchError thrown by kfetch' test('should return false', async () => { expect.assertions(1); try { - await kfetch({ method: 'POST', pathname: 'my/path' }); + await kfetch({ method: 'POST', pathname: '/my/path' }); } catch (kfetchError) { expect(isAutoCreateIndexError(kfetchError)).toBe(false); } @@ -90,7 +107,7 @@ describe('isAutoCreateIndexError correctly handles KFetchError thrown by kfetch' test('should return true', async () => { expect.assertions(1); try { - await kfetch({ method: 'POST', pathname: 'my/path' }); + await kfetch({ method: 'POST', pathname: '/my/path' }); } catch (kfetchError) { expect(isAutoCreateIndexError(kfetchError)).toBe(true); } diff --git a/src/legacy/ui/public/kfetch/kfetch.test.ts b/src/legacy/ui/public/kfetch/kfetch.test.ts index 2c4cf1afbd9bf1b..6cea1ccdb769fd1 100644 --- a/src/legacy/ui/public/kfetch/kfetch.test.ts +++ b/src/legacy/ui/public/kfetch/kfetch.test.ts @@ -22,30 +22,11 @@ import fetchMock from 'fetch-mock/es5/client'; import { __newPlatformSetup__, addInterceptor, kfetch, KFetchOptions } from '.'; import { Interceptor, resetInterceptors, withDefaultOptions } from './kfetch'; import { KFetchError } from './kfetch_error'; - -/* eslint-disable @kbn/eslint/no-restricted-paths */ -import { HttpService } from '../../../../core/public/http'; -import { fatalErrorsServiceMock } from '../../../../core/public/fatal_errors/fatal_errors_service.mock'; -import { injectedMetadataServiceMock } from '../../../../core/public/injected_metadata/injected_metadata_service.mock'; -import { BasePathService } from '../../../../core/public/base_path'; -/* eslint-enable @kbn/eslint/no-restricted-paths */ - -function setupService() { - const httpService = new HttpService(); - const fatalErrors = fatalErrorsServiceMock.createSetupContract(); - const injectedMetadata = injectedMetadataServiceMock.createSetupContract(); - - injectedMetadata.getBasePath.mockReturnValue('http://localhost/myBase'); - - const basePath = new BasePathService().setup({ injectedMetadata }); - const http = httpService.setup({ basePath, fatalErrors, injectedMetadata }); - - return { httpService, fatalErrors, http }; -} +import { setup } from './kfetch_test_setup'; describe('kfetch', () => { beforeAll(() => { - __newPlatformSetup__(setupService().http); + __newPlatformSetup__(setup().http); }); afterEach(() => { diff --git a/src/legacy/ui/public/kfetch/kfetch_test_setup.ts b/src/legacy/ui/public/kfetch/kfetch_test_setup.ts new file mode 100644 index 000000000000000..943b6d1e6c56ff2 --- /dev/null +++ b/src/legacy/ui/public/kfetch/kfetch_test_setup.ts @@ -0,0 +1,38 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +/* eslint-disable @kbn/eslint/no-restricted-paths */ +import { HttpService } from '../../../../core/public/http'; +import { fatalErrorsServiceMock } from '../../../../core/public/fatal_errors/fatal_errors_service.mock'; +import { injectedMetadataServiceMock } from '../../../../core/public/injected_metadata/injected_metadata_service.mock'; +import { BasePathService } from '../../../../core/public/base_path'; +/* eslint-enable @kbn/eslint/no-restricted-paths */ + +export function setup() { + const httpService = new HttpService(); + const fatalErrors = fatalErrorsServiceMock.createSetupContract(); + const injectedMetadata = injectedMetadataServiceMock.createSetupContract(); + + injectedMetadata.getBasePath.mockReturnValue('http://localhost/myBase'); + + const basePath = new BasePathService().setup({ injectedMetadata }); + const http = httpService.setup({ basePath, fatalErrors, injectedMetadata }); + + return { httpService, fatalErrors, http }; +} diff --git a/src/legacy/ui/public/query_bar/components/query_bar.test.mocks.ts b/src/legacy/ui/public/query_bar/components/query_bar.test.mocks.ts index 7f0dae53a881322..126c32bb64a62f7 100644 --- a/src/legacy/ui/public/query_bar/components/query_bar.test.mocks.ts +++ b/src/legacy/ui/public/query_bar/components/query_bar.test.mocks.ts @@ -17,6 +17,9 @@ * under the License. */ +import { createKfetch } from 'ui/kfetch/kfetch'; +import { setup } from 'ui/kfetch/kfetch_test_setup'; + const mockChromeFactory = jest.fn(() => { return { getBasePath: () => `foo`, @@ -47,6 +50,7 @@ export const mockPersistedLogFactory = jest.fn Promise.resolve([])); const mockAutocompleteProvider = jest.fn(() => mockGetAutocompleteSuggestions); export const mockGetAutocompleteProvider = jest.fn(() => mockAutocompleteProvider); +const mockKfetch = jest.fn(() => createKfetch(setup().http)); jest.mock('ui/chrome', () => mockChromeFactory()); jest.mock('../../chrome', () => mockChromeFactory()); @@ -61,6 +65,10 @@ jest.mock('../../metadata', () => ({ jest.mock('../../autocomplete_providers', () => ({ getAutocompleteProvider: mockGetAutocompleteProvider, })); +jest.mock('ui/kfetch', () => ({ + __newPlatformSetup__: jest.fn(), + kfetch: mockKfetch, +})); import _ from 'lodash'; // Using doMock to avoid hoisting so that I can override only the debounce method in lodash