|
| 1 | +import React from 'react'; |
| 2 | +import { render, screen, within } from '@testing-library/react'; |
| 3 | +import { expect } from 'chai'; |
| 4 | +import AppRegistry from 'hadron-app-registry'; |
| 5 | +import userEvent from '@testing-library/user-event'; |
| 6 | + |
| 7 | +import configureActions from '../actions'; |
| 8 | +import Field from './field'; |
| 9 | + |
| 10 | +describe('Field', function () { |
| 11 | + let container: HTMLElement; |
| 12 | + let testAppRegistry: AppRegistry; |
| 13 | + |
| 14 | + beforeEach(function () { |
| 15 | + testAppRegistry = new AppRegistry(); |
| 16 | + testAppRegistry.registerStore('Query.Store', { |
| 17 | + state: { |
| 18 | + filter: {}, |
| 19 | + valid: true, |
| 20 | + userTyping: false, |
| 21 | + }, |
| 22 | + filter: {}, |
| 23 | + listen: (( |
| 24 | + // eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 25 | + func: () => { |
| 26 | + /* noop */ |
| 27 | + } |
| 28 | + ) => { |
| 29 | + return () => {}; |
| 30 | + }) as any, |
| 31 | + } as any); |
| 32 | + }); |
| 33 | + |
| 34 | + describe('basic field name', function () { |
| 35 | + beforeEach(function () { |
| 36 | + render( |
| 37 | + <Field |
| 38 | + enableMaps={false} |
| 39 | + actions={configureActions()} |
| 40 | + localAppRegistry={testAppRegistry} |
| 41 | + name="testFieldName" |
| 42 | + path="test" |
| 43 | + types={[ |
| 44 | + { |
| 45 | + name: 'Double', |
| 46 | + path: 'testFieldName', |
| 47 | + probability: 0.7, |
| 48 | + fields: [], |
| 49 | + types: [], |
| 50 | + values: [123, 345, 999], |
| 51 | + }, |
| 52 | + { |
| 53 | + name: 'String', |
| 54 | + path: 'testFieldName', |
| 55 | + probability: 0.3, |
| 56 | + fields: [], |
| 57 | + types: [], |
| 58 | + values: ['testa', 'testb', 'testc'], |
| 59 | + }, |
| 60 | + ]} |
| 61 | + /> |
| 62 | + ); |
| 63 | + container = screen.getByTestId('schema-field'); |
| 64 | + }); |
| 65 | + |
| 66 | + it('renders the field name', function () { |
| 67 | + const fieldName = within(container).getByText('testFieldName'); |
| 68 | + expect(fieldName).to.be.visible; |
| 69 | + }); |
| 70 | + |
| 71 | + it('renders the various field types', function () { |
| 72 | + expect(within(container).getByText('String')).to.be.visible; |
| 73 | + expect(within(container).getByText('Double')).to.be.visible; |
| 74 | + expect(within(container).queryByText('Int32')).to.not.exist; |
| 75 | + }); |
| 76 | + }); |
| 77 | + |
| 78 | + describe('document field with nested fields', function () { |
| 79 | + beforeEach(function () { |
| 80 | + render( |
| 81 | + <Field |
| 82 | + enableMaps={false} |
| 83 | + actions={configureActions()} |
| 84 | + localAppRegistry={testAppRegistry} |
| 85 | + name="documentField" |
| 86 | + path="documentField" |
| 87 | + types={[ |
| 88 | + { |
| 89 | + name: 'Document', |
| 90 | + path: 'documentField', |
| 91 | + probability: 0.7, |
| 92 | + fields: [ |
| 93 | + { |
| 94 | + name: 'nestedFieldName', |
| 95 | + path: 'documentField.nestedFieldName', |
| 96 | + probability: 1, // Always exists |
| 97 | + fields: [], |
| 98 | + types: [ |
| 99 | + { |
| 100 | + name: 'String', |
| 101 | + path: 'documentField.nestedFieldName', |
| 102 | + probability: 1, |
| 103 | + fields: [], |
| 104 | + types: [], |
| 105 | + values: ['test11', 'test22'], |
| 106 | + }, |
| 107 | + ], |
| 108 | + }, |
| 109 | + ], |
| 110 | + types: [], |
| 111 | + }, |
| 112 | + { |
| 113 | + name: 'String', |
| 114 | + path: 'documentField', |
| 115 | + probability: 0.3, |
| 116 | + fields: [], |
| 117 | + types: [], |
| 118 | + }, |
| 119 | + ]} |
| 120 | + /> |
| 121 | + ); |
| 122 | + container = screen.getByTestId('schema-field'); |
| 123 | + }); |
| 124 | + |
| 125 | + it('renders the field name', function () { |
| 126 | + const fieldName = within(container).getByText('documentField'); |
| 127 | + expect(fieldName).to.be.visible; |
| 128 | + }); |
| 129 | + |
| 130 | + it('renders the field type', function () { |
| 131 | + expect(within(container).getByText('Document')).to.be.visible; |
| 132 | + }); |
| 133 | + |
| 134 | + it('does not render the nested fields', function () { |
| 135 | + expect(within(container).queryByText('nestedFieldName')).to.not.exist; |
| 136 | + }); |
| 137 | + |
| 138 | + it('renders the nested fields when the field is clicked', function () { |
| 139 | + const fieldNameButton = within(container).getByText('documentField'); |
| 140 | + userEvent.click(fieldNameButton, undefined, { |
| 141 | + skipPointerEventsCheck: true, |
| 142 | + }); |
| 143 | + expect(within(container).queryByText('nestedFieldName')).to.be.visible; |
| 144 | + }); |
| 145 | + }); |
| 146 | + |
| 147 | + describe('array field with various types', function () { |
| 148 | + beforeEach(function () { |
| 149 | + render( |
| 150 | + <Field |
| 151 | + enableMaps={false} |
| 152 | + actions={configureActions()} |
| 153 | + localAppRegistry={testAppRegistry} |
| 154 | + name="arrayField" |
| 155 | + path="test" |
| 156 | + types={[ |
| 157 | + { |
| 158 | + name: 'Array', |
| 159 | + path: 'arrayField', |
| 160 | + probability: 0.7, |
| 161 | + lengths: [2, 2], |
| 162 | + average_length: 2, |
| 163 | + total_count: 4, |
| 164 | + types: [ |
| 165 | + { |
| 166 | + name: 'String', |
| 167 | + path: 'documentField', |
| 168 | + probability: 0.6, |
| 169 | + types: [], |
| 170 | + values: ['test11', 'test22'], |
| 171 | + }, |
| 172 | + { |
| 173 | + name: 'Double', |
| 174 | + path: 'documentField', |
| 175 | + probability: 0.4, |
| 176 | + types: [], |
| 177 | + values: [555, 777], |
| 178 | + }, |
| 179 | + ], |
| 180 | + }, |
| 181 | + { |
| 182 | + name: 'Date', |
| 183 | + path: 'arrayField', |
| 184 | + probability: 0.3, |
| 185 | + fields: [], |
| 186 | + types: [], |
| 187 | + }, |
| 188 | + ]} |
| 189 | + /> |
| 190 | + ); |
| 191 | + container = screen.getByTestId('schema-field'); |
| 192 | + }); |
| 193 | + |
| 194 | + it('renders the field name', function () { |
| 195 | + expect(within(container).getByText('arrayField')).to.be.visible; |
| 196 | + }); |
| 197 | + |
| 198 | + it('renders the field type', function () { |
| 199 | + expect(within(container).getByText('Array')).to.be.visible; |
| 200 | + }); |
| 201 | + |
| 202 | + it('renders the various types found in the array', function () { |
| 203 | + expect(within(container).getByText('String')).to.be.visible; |
| 204 | + expect(within(container).getByText('Double')).to.be.visible; |
| 205 | + expect(within(container).queryByText('Int32')).to.not.exist; |
| 206 | + }); |
| 207 | + }); |
| 208 | +}); |
0 commit comments