Skip to content

Commit

Permalink
Fix failing tests from kfetch refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
eliperelman committed Apr 24, 2019
1 parent 32af0ba commit a02bda7
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand Down
23 changes: 2 additions & 21 deletions src/legacy/ui/public/kfetch/kfetch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {
Expand Down
38 changes: 38 additions & 0 deletions src/legacy/ui/public/kfetch/kfetch_test_setup.ts
Original file line number Diff line number Diff line change
@@ -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 };
}
Original file line number Diff line number Diff line change
Expand Up @@ -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`,
Expand Down Expand Up @@ -47,6 +50,7 @@ export const mockPersistedLogFactory = jest.fn<jest.Mocked<typeof mockPersistedL
export const mockGetAutocompleteSuggestions = 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());
Expand All @@ -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
Expand Down

0 comments on commit a02bda7

Please sign in to comment.