Skip to content

Commit

Permalink
[Remote clusters] Reorganize test files (#82362)
Browse files Browse the repository at this point in the history
* Refactor Remote Clusters client integration tests for readability.
- Colocate helpers with test files.
- Remove default API responses from HTTP response mocking functions to make behavior clearer at call sites.
  • Loading branch information
cjcenizal authored Nov 3, 2020
1 parent adf0e24 commit b770e82
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { pageHelpers, nextTick, setupEnvironment } from './helpers';
import { NON_ALPHA_NUMERIC_CHARS, ACCENTED_CHARS } from './helpers/constants';

const { setup } = pageHelpers.remoteClustersAdd;
import { nextTick, setupEnvironment } from '../helpers';
import { NON_ALPHA_NUMERIC_CHARS, ACCENTED_CHARS } from './special_characters';
import { setup } from './remote_clusters_add.helpers';

describe('Create Remote cluster', () => {
describe('on component mount', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,6 @@
* you may not use this file except in compliance with the Elastic License.
*/

export const REMOTE_CLUSTER_EDIT_NAME = 'new-york';

export const REMOTE_CLUSTER_EDIT = {
name: REMOTE_CLUSTER_EDIT_NAME,
seeds: ['localhost:9400'],
skipUnavailable: true,
};

export const NON_ALPHA_NUMERIC_CHARS = [
'#',
'@',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@ import { RemoteClusterEdit } from '../../../public/application/sections/remote_c
import { createRemoteClustersStore } from '../../../public/application/store';
import { registerRouter } from '../../../public/application/services/routing';

import { REMOTE_CLUSTER_EDIT_NAME } from './constants';
export const REMOTE_CLUSTER_EDIT_NAME = 'new-york';

export const REMOTE_CLUSTER_EDIT = {
name: REMOTE_CLUSTER_EDIT_NAME,
seeds: ['localhost:9400'],
skipUnavailable: true,
};

const testBedConfig = {
store: createRemoteClustersStore,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@

import { act } from 'react-dom/test-utils';

import { RemoteClusterForm } from '../../public/application/sections/components/remote_cluster_form';
import { pageHelpers, setupEnvironment } from './helpers';
import { REMOTE_CLUSTER_EDIT, REMOTE_CLUSTER_EDIT_NAME } from './helpers/constants';

const { setup } = pageHelpers.remoteClustersEdit;
const { setup: setupRemoteClustersAdd } = pageHelpers.remoteClustersAdd;
import { RemoteClusterForm } from '../../../public/application/sections/components/remote_cluster_form';
import { setupEnvironment } from '../helpers';
import { setup as setupRemoteClustersAdd } from '../add/remote_clusters_add.helpers';
import {
setup,
REMOTE_CLUSTER_EDIT,
REMOTE_CLUSTER_EDIT_NAME,
} from './remote_clusters_edit.helpers';

describe('Edit Remote cluster', () => {
let server;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,22 @@ import sinon from 'sinon';

// Register helpers to mock HTTP Requests
const registerHttpRequestMockHelpers = (server) => {
const mockResponse = (defaultResponse, response) => [
const mockResponse = (response) => [
200,
{ 'Content-Type': 'application/json' },
JSON.stringify({ ...defaultResponse, ...response }),
JSON.stringify(response),
];

const setLoadRemoteClustersResponse = (response) => {
const defaultResponse = [];

server.respondWith('GET', '/api/remote_clusters', [
200,
{ 'Content-Type': 'application/json' },
JSON.stringify(response ? response : defaultResponse),
JSON.stringify(response),
]);
};

const setDeleteRemoteClusterResponse = (response) => {
const defaultResponse = {
itemsDeleted: [],
errors: [],
};

server.respondWith('DELETE', /api\/remote_clusters/, mockResponse(defaultResponse, response));
server.respondWith('DELETE', /api\/remote_clusters/, mockResponse(response));
};

return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,5 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { setup as remoteClustersAddSetup } from './remote_clusters_add.helpers';
import { setup as remoteClustersEditSetup } from './remote_clusters_edit.helpers';
import { setup as remoteClustersListSetup } from './remote_clusters_list.helpers';

export { nextTick, getRandomString, findTestSubject } from '../../../../../test_utils';

export { setupEnvironment } from './setup_environment';

export const pageHelpers = {
remoteClustersAdd: { setup: remoteClustersAddSetup },
remoteClustersEdit: { setup: remoteClustersEditSetup },
remoteClustersList: { setup: remoteClustersListSetup },
};
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,14 @@
*/
import { act } from 'react-dom/test-utils';

import {
pageHelpers,
setupEnvironment,
nextTick,
getRandomString,
findTestSubject,
} from './helpers';
import { getRouter } from '../../../public/application/services';
import { getRemoteClusterMock } from '../../../fixtures/remote_cluster';

import { getRouter } from '../../public/application/services';
import { getRemoteClusterMock } from '../../fixtures/remote_cluster';
import { PROXY_MODE } from '../../../common/constants';

import { PROXY_MODE } from '../../common/constants';
import { setupEnvironment, nextTick, getRandomString, findTestSubject } from '../helpers';

const { setup } = pageHelpers.remoteClustersList;
import { setup } from './remote_clusters_list.helpers';

describe('<RemoteClusterList />', () => {
let server;
Expand All @@ -33,8 +27,7 @@ describe('<RemoteClusterList />', () => {
});

beforeEach(() => {
// Set "default" mock responses by not providing any arguments
httpRequestsMockHelpers.setLoadRemoteClustersResponse();
httpRequestsMockHelpers.setLoadRemoteClustersResponse([]);
});

describe('on component mount', () => {
Expand Down

0 comments on commit b770e82

Please sign in to comment.