|
| 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 | + * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side |
| 5 | + * Public License v 1"; you may not use this file except in compliance with, at |
| 6 | + * your election, the "Elastic License 2.0", the "GNU Affero General Public |
| 7 | + * License v3.0 only", or the "Server Side Public License, v 1". |
| 8 | + */ |
| 9 | + |
| 10 | +import React from 'react'; |
| 11 | +import { render } from '@testing-library/react'; |
| 12 | + |
| 13 | +import { Panel } from '../types'; |
| 14 | +import { |
| 15 | + LEFT_SECTION_TEST_ID, |
| 16 | + PREVIEW_SECTION_TEST_ID, |
| 17 | + SETTINGS_MENU_BUTTON_TEST_ID, |
| 18 | + RIGHT_SECTION_TEST_ID, |
| 19 | +} from './test_ids'; |
| 20 | +import { initialUiState, type State } from '../store/state'; |
| 21 | +import { TestProvider } from '../test/provider'; |
| 22 | +import { REDUX_ID_FOR_MEMORY_STORAGE } from '../constants'; |
| 23 | +import { Container } from './container'; |
| 24 | + |
| 25 | +const id = REDUX_ID_FOR_MEMORY_STORAGE; |
| 26 | +const registeredPanels: Panel[] = [ |
| 27 | + { |
| 28 | + key: 'key', |
| 29 | + component: () => <div>{'component'}</div>, |
| 30 | + }, |
| 31 | +]; |
| 32 | + |
| 33 | +describe('Container', () => { |
| 34 | + it(`shouldn't render flyout if no panels`, () => { |
| 35 | + const state: State = { |
| 36 | + panels: { |
| 37 | + byId: {}, |
| 38 | + }, |
| 39 | + ui: initialUiState, |
| 40 | + }; |
| 41 | + |
| 42 | + const result = render( |
| 43 | + <TestProvider state={state}> |
| 44 | + <Container registeredPanels={registeredPanels} /> |
| 45 | + </TestProvider> |
| 46 | + ); |
| 47 | + |
| 48 | + expect(result.asFragment()).toMatchInlineSnapshot(`<DocumentFragment />`); |
| 49 | + }); |
| 50 | + |
| 51 | + it('should render collapsed flyout (right section)', () => { |
| 52 | + const state: State = { |
| 53 | + panels: { |
| 54 | + byId: { |
| 55 | + [id]: { |
| 56 | + right: { |
| 57 | + id: 'key', |
| 58 | + }, |
| 59 | + left: undefined, |
| 60 | + preview: undefined, |
| 61 | + }, |
| 62 | + }, |
| 63 | + }, |
| 64 | + ui: initialUiState, |
| 65 | + }; |
| 66 | + |
| 67 | + const { getByTestId } = render( |
| 68 | + <TestProvider state={state}> |
| 69 | + <Container registeredPanels={registeredPanels} /> |
| 70 | + </TestProvider> |
| 71 | + ); |
| 72 | + |
| 73 | + expect(getByTestId(RIGHT_SECTION_TEST_ID)).toBeInTheDocument(); |
| 74 | + }); |
| 75 | + |
| 76 | + it('should render expanded flyout (right and left sections)', () => { |
| 77 | + const state: State = { |
| 78 | + panels: { |
| 79 | + byId: { |
| 80 | + [id]: { |
| 81 | + right: { |
| 82 | + id: 'key', |
| 83 | + }, |
| 84 | + left: { |
| 85 | + id: 'key', |
| 86 | + }, |
| 87 | + preview: undefined, |
| 88 | + }, |
| 89 | + }, |
| 90 | + }, |
| 91 | + ui: initialUiState, |
| 92 | + }; |
| 93 | + |
| 94 | + const { getByTestId } = render( |
| 95 | + <TestProvider state={state}> |
| 96 | + <Container registeredPanels={registeredPanels} /> |
| 97 | + </TestProvider> |
| 98 | + ); |
| 99 | + |
| 100 | + expect(getByTestId(LEFT_SECTION_TEST_ID)).toBeInTheDocument(); |
| 101 | + }); |
| 102 | + |
| 103 | + it('should render preview section', () => { |
| 104 | + const state: State = { |
| 105 | + panels: { |
| 106 | + byId: { |
| 107 | + [id]: { |
| 108 | + right: undefined, |
| 109 | + left: undefined, |
| 110 | + preview: [ |
| 111 | + { |
| 112 | + id: 'key', |
| 113 | + }, |
| 114 | + ], |
| 115 | + }, |
| 116 | + }, |
| 117 | + }, |
| 118 | + ui: initialUiState, |
| 119 | + }; |
| 120 | + |
| 121 | + const { getByTestId } = render( |
| 122 | + <TestProvider state={state}> |
| 123 | + <Container registeredPanels={registeredPanels} /> |
| 124 | + </TestProvider> |
| 125 | + ); |
| 126 | + |
| 127 | + expect(getByTestId(PREVIEW_SECTION_TEST_ID)).toBeInTheDocument(); |
| 128 | + }); |
| 129 | + |
| 130 | + it('should not render flyout when right has value but does not matches registered panels', () => { |
| 131 | + const state: State = { |
| 132 | + panels: { |
| 133 | + byId: { |
| 134 | + [id]: { |
| 135 | + right: { |
| 136 | + id: 'key1', |
| 137 | + }, |
| 138 | + left: undefined, |
| 139 | + preview: undefined, |
| 140 | + }, |
| 141 | + }, |
| 142 | + }, |
| 143 | + ui: initialUiState, |
| 144 | + }; |
| 145 | + |
| 146 | + const { queryByTestId } = render( |
| 147 | + <TestProvider state={state}> |
| 148 | + <Container data-test-subj="my-test-flyout" registeredPanels={registeredPanels} /> |
| 149 | + </TestProvider> |
| 150 | + ); |
| 151 | + |
| 152 | + expect(queryByTestId('my-test-flyout')).toBeNull(); |
| 153 | + expect(queryByTestId(RIGHT_SECTION_TEST_ID)).toBeNull(); |
| 154 | + }); |
| 155 | + |
| 156 | + it('should render the menu to change display options', () => { |
| 157 | + const state: State = { |
| 158 | + panels: { |
| 159 | + byId: { |
| 160 | + [id]: { |
| 161 | + right: { |
| 162 | + id: 'key', |
| 163 | + }, |
| 164 | + left: undefined, |
| 165 | + preview: undefined, |
| 166 | + }, |
| 167 | + }, |
| 168 | + }, |
| 169 | + ui: initialUiState, |
| 170 | + }; |
| 171 | + |
| 172 | + const { getByTestId } = render( |
| 173 | + <TestProvider state={state}> |
| 174 | + <Container registeredPanels={registeredPanels} /> |
| 175 | + </TestProvider> |
| 176 | + ); |
| 177 | + |
| 178 | + expect(getByTestId(SETTINGS_MENU_BUTTON_TEST_ID)).toBeInTheDocument(); |
| 179 | + }); |
| 180 | +}); |
0 commit comments