|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License; |
| 4 | + * you may not use this file except in compliance with the Elastic License. |
| 5 | + */ |
| 6 | + |
| 7 | +import { ReactWrapper } from 'enzyme'; |
| 8 | +import { act } from 'react-dom/test-utils'; |
| 9 | +import { |
| 10 | + registerTestBed, |
| 11 | + TestBed, |
| 12 | + TestBedConfig, |
| 13 | + findTestSubject, |
| 14 | + nextTick, |
| 15 | +} from '../../../../../test_utils'; |
| 16 | +import { IndexManagementHome } from '../../../public/application/sections/home'; // eslint-disable-line @kbn/eslint/no-restricted-paths |
| 17 | +import { BASE_PATH } from '../../../common/constants'; |
| 18 | +import { indexManagementStore } from '../../../public/application/store'; // eslint-disable-line @kbn/eslint/no-restricted-paths |
| 19 | +import { Template } from '../../../common/types'; |
| 20 | +import { WithAppDependencies, services } from './setup_environment'; |
| 21 | + |
| 22 | +const testBedConfig: TestBedConfig = { |
| 23 | + store: () => indexManagementStore(services as any), |
| 24 | + memoryRouter: { |
| 25 | + initialEntries: [`${BASE_PATH}indices`], |
| 26 | + componentRoutePath: `${BASE_PATH}:section(indices|templates)`, |
| 27 | + }, |
| 28 | + doMountAsync: true, |
| 29 | +}; |
| 30 | + |
| 31 | +const initTestBed = registerTestBed(WithAppDependencies(IndexManagementHome), testBedConfig); |
| 32 | + |
| 33 | +export interface IdxMgmtHomeTestBed extends TestBed<IdxMgmtTestSubjects> { |
| 34 | + findAction: (action: 'edit' | 'clone' | 'delete') => ReactWrapper; |
| 35 | + actions: { |
| 36 | + selectHomeTab: (tab: 'indicesTab' | 'templatesTab') => void; |
| 37 | + selectDetailsTab: (tab: 'summary' | 'settings' | 'mappings' | 'aliases') => void; |
| 38 | + clickReloadButton: () => void; |
| 39 | + clickTemplateAction: (name: Template['name'], action: 'edit' | 'clone' | 'delete') => void; |
| 40 | + clickTemplateAt: (index: number) => void; |
| 41 | + clickCloseDetailsButton: () => void; |
| 42 | + clickActionMenu: (name: Template['name']) => void; |
| 43 | + }; |
| 44 | +} |
| 45 | + |
| 46 | +export const setup = async (): Promise<IdxMgmtHomeTestBed> => { |
| 47 | + const testBed = await initTestBed(); |
| 48 | + |
| 49 | + /** |
| 50 | + * Additional helpers |
| 51 | + */ |
| 52 | + const findAction = (action: 'edit' | 'clone' | 'delete') => { |
| 53 | + const actions = ['edit', 'clone', 'delete']; |
| 54 | + const { component } = testBed; |
| 55 | + |
| 56 | + return component.find('.euiContextMenuItem').at(actions.indexOf(action)); |
| 57 | + }; |
| 58 | + |
| 59 | + /** |
| 60 | + * User Actions |
| 61 | + */ |
| 62 | + |
| 63 | + const selectHomeTab = (tab: 'indicesTab' | 'templatesTab') => { |
| 64 | + testBed.find(tab).simulate('click'); |
| 65 | + }; |
| 66 | + |
| 67 | + const selectDetailsTab = (tab: 'summary' | 'settings' | 'mappings' | 'aliases') => { |
| 68 | + const tabs = ['summary', 'settings', 'mappings', 'aliases']; |
| 69 | + |
| 70 | + testBed |
| 71 | + .find('templateDetails.tab') |
| 72 | + .at(tabs.indexOf(tab)) |
| 73 | + .simulate('click'); |
| 74 | + }; |
| 75 | + |
| 76 | + const clickReloadButton = () => { |
| 77 | + const { find } = testBed; |
| 78 | + find('reloadButton').simulate('click'); |
| 79 | + }; |
| 80 | + |
| 81 | + const clickActionMenu = async (templateName: Template['name']) => { |
| 82 | + const { component } = testBed; |
| 83 | + |
| 84 | + // When a table has > 2 actions, EUI displays an overflow menu with an id "<template_name>-actions" |
| 85 | + // The template name may contain a period (.) so we use bracket syntax for selector |
| 86 | + component.find(`div[id="${templateName}-actions"] button`).simulate('click'); |
| 87 | + }; |
| 88 | + |
| 89 | + const clickTemplateAction = ( |
| 90 | + templateName: Template['name'], |
| 91 | + action: 'edit' | 'clone' | 'delete' |
| 92 | + ) => { |
| 93 | + const actions = ['edit', 'clone', 'delete']; |
| 94 | + const { component } = testBed; |
| 95 | + |
| 96 | + clickActionMenu(templateName); |
| 97 | + |
| 98 | + component |
| 99 | + .find('.euiContextMenuItem') |
| 100 | + .at(actions.indexOf(action)) |
| 101 | + .simulate('click'); |
| 102 | + }; |
| 103 | + |
| 104 | + const clickTemplateAt = async (index: number) => { |
| 105 | + const { component, table, router } = testBed; |
| 106 | + const { rows } = table.getMetaData('templateTable'); |
| 107 | + const templateLink = findTestSubject(rows[index].reactWrapper, 'templateDetailsLink'); |
| 108 | + |
| 109 | + await act(async () => { |
| 110 | + const { href } = templateLink.props(); |
| 111 | + router.navigateTo(href!); |
| 112 | + await nextTick(); |
| 113 | + component.update(); |
| 114 | + }); |
| 115 | + }; |
| 116 | + |
| 117 | + const clickCloseDetailsButton = () => { |
| 118 | + const { find } = testBed; |
| 119 | + |
| 120 | + find('closeDetailsButton').simulate('click'); |
| 121 | + }; |
| 122 | + |
| 123 | + return { |
| 124 | + ...testBed, |
| 125 | + findAction, |
| 126 | + actions: { |
| 127 | + selectHomeTab, |
| 128 | + selectDetailsTab, |
| 129 | + clickReloadButton, |
| 130 | + clickTemplateAction, |
| 131 | + clickTemplateAt, |
| 132 | + clickCloseDetailsButton, |
| 133 | + clickActionMenu, |
| 134 | + }, |
| 135 | + }; |
| 136 | +}; |
| 137 | + |
| 138 | +type IdxMgmtTestSubjects = TestSubjects; |
| 139 | + |
| 140 | +export type TestSubjects = |
| 141 | + | 'aliasesTab' |
| 142 | + | 'appTitle' |
| 143 | + | 'cell' |
| 144 | + | 'closeDetailsButton' |
| 145 | + | 'createTemplateButton' |
| 146 | + | 'deleteSystemTemplateCallOut' |
| 147 | + | 'deleteTemplateButton' |
| 148 | + | 'deleteTemplatesConfirmation' |
| 149 | + | 'documentationLink' |
| 150 | + | 'emptyPrompt' |
| 151 | + | 'manageTemplateButton' |
| 152 | + | 'mappingsTab' |
| 153 | + | 'noAliasesCallout' |
| 154 | + | 'noMappingsCallout' |
| 155 | + | 'noSettingsCallout' |
| 156 | + | 'indicesList' |
| 157 | + | 'indicesTab' |
| 158 | + | 'reloadButton' |
| 159 | + | 'row' |
| 160 | + | 'sectionError' |
| 161 | + | 'sectionLoading' |
| 162 | + | 'settingsTab' |
| 163 | + | 'summaryTab' |
| 164 | + | 'summaryTitle' |
| 165 | + | 'systemTemplatesSwitch' |
| 166 | + | 'templateDetails' |
| 167 | + | 'templateDetails.manageTemplateButton' |
| 168 | + | 'templateDetails.sectionLoading' |
| 169 | + | 'templateDetails.tab' |
| 170 | + | 'templateDetails.title' |
| 171 | + | 'templateList' |
| 172 | + | 'templateTable' |
| 173 | + | 'templatesTab'; |
0 commit comments