Skip to content

Commit

Permalink
Split reactive_form into smaller files
Browse files Browse the repository at this point in the history
  • Loading branch information
yuliacech committed Mar 1, 2021
1 parent f75fc23 commit 934ce2d
Show file tree
Hide file tree
Showing 6 changed files with 339 additions and 241 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { act } from 'react-dom/test-utils';
import { setupEnvironment } from '../../helpers/setup_environment';
import { EditPolicyTestBed, setup } from '../edit_policy.helpers';
import { getDefaultHotPhasePolicy } from '../constants';

describe('<EditPolicy /> cold phase', () => {
let testBed: EditPolicyTestBed;
const { server, httpRequestsMockHelpers } = setupEnvironment();

beforeAll(() => {
jest.useFakeTimers();
});

afterAll(() => {
jest.useRealTimers();
server.restore();
});

beforeEach(async () => {
httpRequestsMockHelpers.setLoadPolicies([getDefaultHotPhasePolicy('my_policy')]);
httpRequestsMockHelpers.setListNodes({
nodesByRoles: { data: ['node1'] },
nodesByAttributes: { 'attribute:true': ['node1'] },
isUsingDeprecatedDataRoleConfig: true,
});
httpRequestsMockHelpers.setNodesDetails('attribute:true', [
{ nodeId: 'testNodeId', stats: { name: 'testNodeName', host: 'testHost' } },
]);
httpRequestsMockHelpers.setLoadSnapshotPolicies([]);

await act(async () => {
testBed = await setup();
});

const { component } = testBed;
component.update();
});

test('shows timing only when enabled', async () => {
const { actions } = testBed;
expect(actions.cold.hasMinAgeInput()).toBeFalsy();
await actions.cold.enable(true);
expect(actions.cold.hasMinAgeInput()).toBeTruthy();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import {
DELETE_PHASE_POLICY,
getDefaultHotPhasePolicy,
NEW_SNAPSHOT_POLICY_NAME,
SNAPSHOT_POLICY_NAME,
} from '../constants';
import { act } from 'react-dom/test-utils';
import { EditPolicyTestBed, setup } from '../edit_policy.helpers';
import { API_BASE_PATH } from '../../../../common/constants';
import { setupEnvironment } from '../../helpers/setup_environment';

describe('<EditPolicy /> delete phase', () => {
let testBed: EditPolicyTestBed;
const { server, httpRequestsMockHelpers } = setupEnvironment();

afterAll(() => {
server.restore();
});

beforeEach(async () => {
httpRequestsMockHelpers.setLoadPolicies([DELETE_PHASE_POLICY]);
httpRequestsMockHelpers.setLoadSnapshotPolicies([
SNAPSHOT_POLICY_NAME,
NEW_SNAPSHOT_POLICY_NAME,
]);

await act(async () => {
testBed = await setup();
});

const { component } = testBed;
component.update();
});

test('is hidden when disabled', async () => {
httpRequestsMockHelpers.setLoadPolicies([getDefaultHotPhasePolicy('my_policy')]);

await act(async () => {
testBed = await setup();
});

const { component, actions } = testBed;
component.update();

expect(actions.delete.isShown()).toBeFalsy();
await actions.delete.enablePhase();
expect(actions.delete.isShown()).toBeTruthy();
});

test('shows timing after it was enabled', async () => {
httpRequestsMockHelpers.setLoadPolicies([getDefaultHotPhasePolicy('my_policy')]);

await act(async () => {
testBed = await setup();
});

const { component, actions } = testBed;
component.update();

expect(actions.delete.hasMinAgeInput()).toBeFalsy();
await actions.delete.enablePhase();
expect(actions.delete.hasMinAgeInput()).toBeTruthy();
});

describe('wait for snapshot', () => {
test('shows snapshot policy name', () => {
expect(testBed.find('snapshotPolicyCombobox').prop('data-currentvalue')).toEqual([
{
label: DELETE_PHASE_POLICY.policy.phases.delete?.actions.wait_for_snapshot?.policy,
},
]);
});

test('updates snapshot policy name', async () => {
const { actions } = testBed;

await actions.setWaitForSnapshotPolicy(NEW_SNAPSHOT_POLICY_NAME);
await actions.savePolicy();

const expected = {
name: DELETE_PHASE_POLICY.name,
phases: {
...DELETE_PHASE_POLICY.policy.phases,
delete: {
...DELETE_PHASE_POLICY.policy.phases.delete,
actions: {
...DELETE_PHASE_POLICY.policy.phases.delete?.actions,
wait_for_snapshot: {
policy: NEW_SNAPSHOT_POLICY_NAME,
},
},
},
},
};

const latestRequest = server.requests[server.requests.length - 1];
expect(latestRequest.url).toBe(`${API_BASE_PATH}/policies`);
expect(latestRequest.method).toBe('POST');
expect(JSON.parse(JSON.parse(latestRequest.requestBody).body)).toEqual(expected);
});

test('shows a callout when the input is not an existing policy', async () => {
const { actions } = testBed;

await actions.setWaitForSnapshotPolicy('my_custom_policy');
expect(testBed.find('noPoliciesCallout').exists()).toBeFalsy();
expect(testBed.find('policiesErrorCallout').exists()).toBeFalsy();
expect(testBed.find('customPolicyCallout').exists()).toBeTruthy();
});

test('removes the action if field is empty', async () => {
const { actions } = testBed;

await actions.setWaitForSnapshotPolicy('');
await actions.savePolicy();

const expected = {
name: DELETE_PHASE_POLICY.name,
phases: {
...DELETE_PHASE_POLICY.policy.phases,
delete: {
...DELETE_PHASE_POLICY.policy.phases.delete,
actions: {
...DELETE_PHASE_POLICY.policy.phases.delete?.actions,
},
},
},
};

delete expected.phases.delete.actions.wait_for_snapshot;

const latestRequest = server.requests[server.requests.length - 1];
expect(JSON.parse(JSON.parse(latestRequest.requestBody).body)).toEqual(expected);
});

test('shows a callout when there are no snapshot policies', async () => {
// need to call setup on testBed again for it to use a newly defined snapshot policies response
httpRequestsMockHelpers.setLoadSnapshotPolicies([]);
await act(async () => {
testBed = await setup();
});

testBed.component.update();
expect(testBed.find('customPolicyCallout').exists()).toBeFalsy();
expect(testBed.find('policiesErrorCallout').exists()).toBeFalsy();
expect(testBed.find('noPoliciesCallout').exists()).toBeTruthy();
});

test('shows a callout when there is an error loading snapshot policies', async () => {
// need to call setup on testBed again for it to use a newly defined snapshot policies response
httpRequestsMockHelpers.setLoadSnapshotPolicies([], { status: 500, body: 'error' });
await act(async () => {
testBed = await setup();
});

testBed.component.update();
expect(testBed.find('customPolicyCallout').exists()).toBeFalsy();
expect(testBed.find('noPoliciesCallout').exists()).toBeFalsy();
expect(testBed.find('policiesErrorCallout').exists()).toBeTruthy();
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { act } from 'react-dom/test-utils';
import { EditPolicyTestBed, setup } from '../edit_policy.helpers';
import { setupEnvironment } from '../../helpers/setup_environment';
import { getDefaultHotPhasePolicy } from '../constants';

describe('<EditPolicy /> request flyout', () => {
let testBed: EditPolicyTestBed;
const { server, httpRequestsMockHelpers } = setupEnvironment();

beforeAll(() => {
jest.useFakeTimers();
});

afterAll(() => {
jest.useRealTimers();
server.restore();
});

beforeEach(async () => {
httpRequestsMockHelpers.setLoadPolicies([getDefaultHotPhasePolicy('my_policy')]);

await act(async () => {
testBed = await setup();
});

const { component } = testBed;
component.update();
});

test('renders a json in flyout for a default policy', async () => {
const { find, component } = testBed;
await act(async () => {
find('requestButton').simulate('click');
});
component.update();

const json = component.find(`code`).text();
const expected = `PUT _ilm/policy/my_policy\n${JSON.stringify(
{
policy: {
phases: {
hot: {
min_age: '0ms',
actions: {
rollover: {
max_age: '30d',
max_size: '50gb',
},
},
},
},
},
},
null,
2
)}`;
expect(json).toBe(expected);
});
});

This file was deleted.

Loading

0 comments on commit 934ce2d

Please sign in to comment.