|
| 1 | +/* |
| 2 | + * Copyright (c) 2019 LabKey Corporation |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +import React from 'react'; |
| 17 | +import { List } from 'immutable'; |
| 18 | +import { render, screen } from '@testing-library/react'; |
| 19 | + |
| 20 | +import { makeQueryInfo } from '../../test/testHelpers'; |
| 21 | +import assayGpatDataQueryInfo from '../../../test/data/assayGpatData-getQueryDetails.json'; |
| 22 | +import { QueryColumn } from '../../../public/QueryColumn'; |
| 23 | + |
| 24 | +import { Formsy } from './formsy'; |
| 25 | +import { QueryFormInputs } from './QueryFormInputs'; |
| 26 | + |
| 27 | +const QUERY_INFO = makeQueryInfo(assayGpatDataQueryInfo); |
| 28 | + |
| 29 | +describe('QueryFormInputs', () => { |
| 30 | + test('default properties with queryInfo', () => { |
| 31 | + const { container } = render( |
| 32 | + <Formsy> |
| 33 | + <QueryFormInputs queryInfo={QUERY_INFO} /> |
| 34 | + </Formsy> |
| 35 | + ); |
| 36 | + |
| 37 | + expect(document.querySelectorAll('input')).toHaveLength(9); |
| 38 | + expect(container.querySelectorAll('input:disabled')).toHaveLength(0); |
| 39 | + |
| 40 | + // Verify presence of expected fields |
| 41 | + expect(screen.getByLabelText('Participant ID')).toBeInTheDocument(); |
| 42 | + expect(screen.getByLabelText('Visit ID')).toBeInTheDocument(); |
| 43 | + |
| 44 | + // Check types where possible |
| 45 | + expect(screen.getByLabelText('Healthy')).toHaveAttribute('type', 'checkbox'); |
| 46 | + |
| 47 | + // default properties don't render file inputs |
| 48 | + expect(container.querySelectorAll('input[type="file"]')).toHaveLength(0); |
| 49 | + }); |
| 50 | + |
| 51 | + test('renderFieldLabel', () => { |
| 52 | + const { container } = render( |
| 53 | + <Formsy> |
| 54 | + <QueryFormInputs |
| 55 | + queryInfo={QUERY_INFO} |
| 56 | + renderFieldLabel={(queryColumn: QueryColumn, label: string) => { |
| 57 | + return <div className="jest-field-label-test">{queryColumn?.name || label}</div>; |
| 58 | + }} |
| 59 | + /> |
| 60 | + </Formsy> |
| 61 | + ); |
| 62 | + |
| 63 | + expect(container.querySelectorAll('.jest-field-label-test')).toHaveLength(9); |
| 64 | + }); |
| 65 | + |
| 66 | + test('render file inputs', () => { |
| 67 | + const { container } = render( |
| 68 | + <Formsy> |
| 69 | + <QueryFormInputs queryInfo={QUERY_INFO} renderFileInputs={true} /> |
| 70 | + </Formsy> |
| 71 | + ); |
| 72 | + |
| 73 | + expect(container.querySelectorAll('input[type="file"]')).toHaveLength(1); |
| 74 | + }); |
| 75 | + |
| 76 | + test('custom columnFilter', () => { |
| 77 | + const filter = (col: QueryColumn) => { |
| 78 | + return col.name === 'Healthy'; |
| 79 | + }; |
| 80 | + |
| 81 | + render( |
| 82 | + <Formsy> |
| 83 | + <QueryFormInputs columnFilter={filter} queryInfo={QUERY_INFO} /> |
| 84 | + </Formsy> |
| 85 | + ); |
| 86 | + |
| 87 | + expect(screen.getByLabelText('Healthy')).toBeInTheDocument(); |
| 88 | + expect(screen.queryByLabelText('Participant ID')).not.toBeInTheDocument(); |
| 89 | + }); |
| 90 | + |
| 91 | + test('disabledFields', () => { |
| 92 | + render( |
| 93 | + <Formsy> |
| 94 | + <QueryFormInputs |
| 95 | + disabledFields={List<string>(['date', 'ParticipantID', 'textarea'])} |
| 96 | + queryInfo={QUERY_INFO} |
| 97 | + /> |
| 98 | + </Formsy> |
| 99 | + ); |
| 100 | + |
| 101 | + const inputs = document.querySelectorAll('input'); |
| 102 | + expect(inputs).toHaveLength(9); |
| 103 | + expect(inputs[4].getAttribute('type')).toBe('text'); |
| 104 | + expect(inputs[4].getAttribute('name')).toBe('Date'); |
| 105 | + expect(inputs[4].getAttribute('value')).toBe(''); |
| 106 | + expect(inputs[4].getAttribute('disabled')).toBe(''); |
| 107 | + }); |
| 108 | + |
| 109 | + test('disabledFields, with fieldWithMixedValues', () => { |
| 110 | + render( |
| 111 | + <Formsy> |
| 112 | + <QueryFormInputs |
| 113 | + disabledFields={List<string>(['date', 'healthy'])} |
| 114 | + fieldWithMixedValues={['date', 'healthy', 'ParticipantID']} |
| 115 | + queryInfo={QUERY_INFO} |
| 116 | + /> |
| 117 | + </Formsy> |
| 118 | + ); |
| 119 | + |
| 120 | + const inputs = document.querySelectorAll('input'); |
| 121 | + expect(inputs).toHaveLength(9); |
| 122 | + expect(inputs[2].getAttribute('name')).toBe('ParticipantID'); |
| 123 | + expect(inputs[2].getAttribute('disabled')).toBeNull(); |
| 124 | + expect(inputs[2].getAttribute('placeholder')).toBe('Enter participant id'); // not disabled, show don't show Mixed placeholder |
| 125 | + expect(inputs[4].getAttribute('name')).toBe('Date'); |
| 126 | + expect(inputs[4].getAttribute('disabled')).toBe(''); // disabled |
| 127 | + expect(inputs[4].getAttribute('placeholder')).toBe('[Mixed]'); // disabled and has mix value |
| 128 | + expect(inputs[5].getAttribute('name')).toBe('DateOnly'); |
| 129 | + expect(inputs[5].getAttribute('placeholder')).toBe('Select dateonly'); |
| 130 | + expect(inputs[6].getAttribute('name')).toBe('TimeOnly'); |
| 131 | + expect(inputs[6].getAttribute('placeholder')).toBe('Select timeonly'); |
| 132 | + expect(inputs[7].getAttribute('placeholder')).toBeNull(); |
| 133 | + expect(inputs[7].getAttribute('title')).toBe('[Mixed]'); // disabled and has mix value, boolean |
| 134 | + }); |
| 135 | +}); |
0 commit comments