|
| 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 { act } from 'react-dom/test-utils'; |
| 8 | + |
| 9 | +import { setupEnvironment, pageHelpers } from './helpers'; |
| 10 | +import { ComponentTemplateDetailsTestBed } from './helpers/component_template_details.helpers'; |
| 11 | +import { ComponentTemplateDeserialized } from '../../shared_imports'; |
| 12 | + |
| 13 | +const { setup } = pageHelpers.componentTemplateDetails; |
| 14 | + |
| 15 | +jest.mock('ui/i18n', () => { |
| 16 | + const I18nContext = ({ children }: any) => children; |
| 17 | + return { I18nContext }; |
| 18 | +}); |
| 19 | + |
| 20 | +const COMPONENT_TEMPLATE: ComponentTemplateDeserialized = { |
| 21 | + name: 'comp-1', |
| 22 | + template: { |
| 23 | + mappings: { properties: { ip_address: { type: 'ip' } } }, |
| 24 | + aliases: { mydata: {} }, |
| 25 | + settings: { number_of_shards: 1 }, |
| 26 | + }, |
| 27 | + version: 1, |
| 28 | + _meta: { description: 'component template test' }, |
| 29 | + _kbnMeta: { usedBy: ['template_1'] }, |
| 30 | +}; |
| 31 | + |
| 32 | +const COMPONENT_TEMPLATE_ONLY_REQUIRED_FIELDS: ComponentTemplateDeserialized = { |
| 33 | + name: 'comp-base', |
| 34 | + template: {}, |
| 35 | + _kbnMeta: { usedBy: [] }, |
| 36 | +}; |
| 37 | + |
| 38 | +describe('<ComponentTemplateDetails />', () => { |
| 39 | + const { server, httpRequestsMockHelpers } = setupEnvironment(); |
| 40 | + let testBed: ComponentTemplateDetailsTestBed; |
| 41 | + |
| 42 | + afterAll(() => { |
| 43 | + server.restore(); |
| 44 | + }); |
| 45 | + |
| 46 | + describe('With component template details', () => { |
| 47 | + beforeEach(async () => { |
| 48 | + httpRequestsMockHelpers.setLoadComponentTemplateResponse(COMPONENT_TEMPLATE); |
| 49 | + |
| 50 | + await act(async () => { |
| 51 | + testBed = setup({ |
| 52 | + componentTemplateName: COMPONENT_TEMPLATE.name, |
| 53 | + onClose: () => {}, |
| 54 | + }); |
| 55 | + }); |
| 56 | + |
| 57 | + testBed.component.update(); |
| 58 | + }); |
| 59 | + |
| 60 | + test('renders the details flyout', () => { |
| 61 | + const { exists, find, actions, component } = testBed; |
| 62 | + |
| 63 | + // Verify flyout exists with correct title |
| 64 | + expect(exists('componentTemplateDetails')).toBe(true); |
| 65 | + expect(find('componentTemplateDetails.title').text()).toBe(COMPONENT_TEMPLATE.name); |
| 66 | + |
| 67 | + // Verify footer does not display since "actions" prop was not provided |
| 68 | + expect(exists('componentTemplateDetails.footer')).toBe(false); |
| 69 | + |
| 70 | + // Verify tabs exist |
| 71 | + expect(exists('settingsTab')).toBe(true); |
| 72 | + expect(exists('mappingsTab')).toBe(true); |
| 73 | + expect(exists('aliasesTab')).toBe(true); |
| 74 | + // Summary tab should be active by default |
| 75 | + expect(find('summaryTab').props()['aria-selected']).toBe(true); |
| 76 | + |
| 77 | + // [Summary tab] Verify description list items |
| 78 | + expect(exists('summaryTabContent.usedByTitle')).toBe(true); |
| 79 | + expect(exists('summaryTabContent.versionTitle')).toBe(true); |
| 80 | + expect(exists('summaryTabContent.metaTitle')).toBe(true); |
| 81 | + |
| 82 | + // [Settings tab] Navigate to tab and verify content |
| 83 | + act(() => { |
| 84 | + actions.clickSettingsTab(); |
| 85 | + }); |
| 86 | + |
| 87 | + component.update(); |
| 88 | + |
| 89 | + expect(exists('settingsTabContent')).toBe(true); |
| 90 | + |
| 91 | + // [Mappings tab] Navigate to tab and verify content |
| 92 | + act(() => { |
| 93 | + actions.clickMappingsTab(); |
| 94 | + }); |
| 95 | + |
| 96 | + component.update(); |
| 97 | + expect(exists('mappingsTabContent')).toBe(true); |
| 98 | + |
| 99 | + // [Aliases tab] Navigate to tab and verify content |
| 100 | + act(() => { |
| 101 | + actions.clickAliasesTab(); |
| 102 | + }); |
| 103 | + |
| 104 | + component.update(); |
| 105 | + expect(exists('aliasesTabContent')).toBe(true); |
| 106 | + }); |
| 107 | + }); |
| 108 | + |
| 109 | + describe('With only required component template fields', () => { |
| 110 | + beforeEach(async () => { |
| 111 | + httpRequestsMockHelpers.setLoadComponentTemplateResponse( |
| 112 | + COMPONENT_TEMPLATE_ONLY_REQUIRED_FIELDS |
| 113 | + ); |
| 114 | + |
| 115 | + await act(async () => { |
| 116 | + testBed = setup({ |
| 117 | + componentTemplateName: COMPONENT_TEMPLATE_ONLY_REQUIRED_FIELDS.name, |
| 118 | + onClose: () => {}, |
| 119 | + }); |
| 120 | + }); |
| 121 | + |
| 122 | + testBed.component.update(); |
| 123 | + }); |
| 124 | + |
| 125 | + test('renders the details flyout', () => { |
| 126 | + const { exists, actions, component } = testBed; |
| 127 | + |
| 128 | + // [Summary tab] Verify optional description list items do not display |
| 129 | + expect(exists('summaryTabContent.usedByTitle')).toBe(false); |
| 130 | + expect(exists('summaryTabContent.versionTitle')).toBe(false); |
| 131 | + expect(exists('summaryTabContent.metaTitle')).toBe(false); |
| 132 | + // Verify callout renders indicating the component template is not in use |
| 133 | + expect(exists('notInUseCallout')).toBe(true); |
| 134 | + |
| 135 | + // [Settings tab] Navigate to tab and verify info callout |
| 136 | + act(() => { |
| 137 | + actions.clickSettingsTab(); |
| 138 | + }); |
| 139 | + |
| 140 | + component.update(); |
| 141 | + |
| 142 | + expect(exists('noSettingsCallout')).toBe(true); |
| 143 | + |
| 144 | + // [Mappings tab] Navigate to tab and verify info callout |
| 145 | + act(() => { |
| 146 | + actions.clickMappingsTab(); |
| 147 | + }); |
| 148 | + |
| 149 | + component.update(); |
| 150 | + expect(exists('noMappingsCallout')).toBe(true); |
| 151 | + |
| 152 | + // [Aliases tab] Navigate to tab and verify info callout |
| 153 | + act(() => { |
| 154 | + actions.clickAliasesTab(); |
| 155 | + }); |
| 156 | + |
| 157 | + component.update(); |
| 158 | + expect(exists('noAliasesCallout')).toBe(true); |
| 159 | + }); |
| 160 | + }); |
| 161 | + |
| 162 | + describe('With actions', () => { |
| 163 | + beforeEach(async () => { |
| 164 | + httpRequestsMockHelpers.setLoadComponentTemplateResponse(COMPONENT_TEMPLATE); |
| 165 | + |
| 166 | + await act(async () => { |
| 167 | + testBed = setup({ |
| 168 | + componentTemplateName: COMPONENT_TEMPLATE.name, |
| 169 | + onClose: () => {}, |
| 170 | + actions: [ |
| 171 | + { |
| 172 | + name: 'Test', |
| 173 | + icon: 'info', |
| 174 | + closePopoverOnClick: true, |
| 175 | + handleActionClick: () => {}, |
| 176 | + }, |
| 177 | + ], |
| 178 | + }); |
| 179 | + }); |
| 180 | + |
| 181 | + testBed.component.update(); |
| 182 | + }); |
| 183 | + |
| 184 | + test('should render a footer with context menu', () => { |
| 185 | + const { exists, actions, component, find } = testBed; |
| 186 | + |
| 187 | + // Verify footer exists |
| 188 | + expect(exists('componentTemplateDetails.footer')).toBe(true); |
| 189 | + expect(exists('manageComponentTemplateButton')).toBe(true); |
| 190 | + |
| 191 | + // Click manage button and verify actions |
| 192 | + act(() => { |
| 193 | + actions.clickManageButton(); |
| 194 | + }); |
| 195 | + |
| 196 | + component.update(); |
| 197 | + |
| 198 | + expect(exists('manageComponentTemplateContextMenu')).toBe(true); |
| 199 | + expect(find('manageComponentTemplateContextMenu.action').length).toEqual(1); |
| 200 | + }); |
| 201 | + }); |
| 202 | + |
| 203 | + describe('Error handling', () => { |
| 204 | + const error = { |
| 205 | + status: 500, |
| 206 | + error: 'Internal server error', |
| 207 | + message: 'Internal server error', |
| 208 | + }; |
| 209 | + |
| 210 | + beforeEach(async () => { |
| 211 | + httpRequestsMockHelpers.setLoadComponentTemplateResponse(undefined, { body: error }); |
| 212 | + |
| 213 | + await act(async () => { |
| 214 | + testBed = setup({ |
| 215 | + componentTemplateName: COMPONENT_TEMPLATE.name, |
| 216 | + onClose: () => {}, |
| 217 | + }); |
| 218 | + }); |
| 219 | + |
| 220 | + testBed.component.update(); |
| 221 | + }); |
| 222 | + |
| 223 | + test('should render an error message if error fetching pipelines', async () => { |
| 224 | + const { exists, find } = testBed; |
| 225 | + |
| 226 | + expect(exists('sectionError')).toBe(true); |
| 227 | + expect(find('sectionError').text()).toContain('Error loading component template'); |
| 228 | + }); |
| 229 | + }); |
| 230 | +}); |
0 commit comments