|
| 1 | +import React from 'react'; |
| 2 | +import type { ComponentProps } from 'react'; |
| 3 | +import { |
| 4 | + FieldCombobox, |
| 5 | + SINGLE_SELECT_LABEL, |
| 6 | + getParentPaths, |
| 7 | + isOptionDisabled, |
| 8 | +} from './field-combobox'; |
| 9 | +import { render, screen } from '@testing-library/react'; |
| 10 | +import { expect } from 'chai'; |
| 11 | +import { setInputElementValue } from '../../../../test/form-helper'; |
| 12 | +import type { StageWizardFields } from '.'; |
| 13 | + |
| 14 | +const SAMPLE_FIELDS: StageWizardFields = [ |
| 15 | + { |
| 16 | + name: '_id', |
| 17 | + type: 'ObjectId', |
| 18 | + }, |
| 19 | + { |
| 20 | + name: 'address', |
| 21 | + type: 'String', |
| 22 | + }, |
| 23 | + { |
| 24 | + name: 'address.city', |
| 25 | + type: 'String', |
| 26 | + }, |
| 27 | + { |
| 28 | + name: 'address.zip', |
| 29 | + type: 'Int32', |
| 30 | + }, |
| 31 | +]; |
| 32 | + |
| 33 | +const renderFieldCombobox = ( |
| 34 | + props: Partial<ComponentProps<typeof FieldCombobox>> = {} |
| 35 | +) => { |
| 36 | + render(<FieldCombobox fields={SAMPLE_FIELDS} {...props} />); |
| 37 | +}; |
| 38 | +describe('field-combobox', function () { |
| 39 | + context('component', function () { |
| 40 | + it('does not render custom field label for available options', function () { |
| 41 | + renderFieldCombobox(); |
| 42 | + setInputElementValue(new RegExp(SINGLE_SELECT_LABEL, 'i'), '_id'); |
| 43 | + const option = screen.getByRole('option', { name: '_id' }); |
| 44 | + expect(option).to.exist; |
| 45 | + }); |
| 46 | + it('renders custom field label for unavailable options', function () { |
| 47 | + renderFieldCombobox(); |
| 48 | + setInputElementValue(new RegExp(SINGLE_SELECT_LABEL, 'i'), 'email'); |
| 49 | + const option = screen.getByRole('option', { name: /field: "email"/i }); |
| 50 | + expect(option).to.exist; |
| 51 | + }); |
| 52 | + }); |
| 53 | + context('helpers', function () { |
| 54 | + describe('getParentPaths', function () { |
| 55 | + it('should return possible parent paths for provided list of paths', function () { |
| 56 | + expect(getParentPaths([])).to.deep.equal([]); |
| 57 | + expect(getParentPaths(['address'])).to.deep.equal(['address']); |
| 58 | + expect(getParentPaths(['address'], ['address'])).to.deep.equal([]); |
| 59 | + |
| 60 | + expect(getParentPaths(['address', 'city'])).to.deep.equal([ |
| 61 | + 'address', |
| 62 | + 'address.city', |
| 63 | + ]); |
| 64 | + expect(getParentPaths(['address', 'city'], ['address'])).to.deep.equal([ |
| 65 | + 'address.city', |
| 66 | + ]); |
| 67 | + |
| 68 | + expect(getParentPaths(['address', 'country', 'city'])).to.deep.equal([ |
| 69 | + 'address', |
| 70 | + 'address.country', |
| 71 | + 'address.country.city', |
| 72 | + ]); |
| 73 | + expect( |
| 74 | + getParentPaths( |
| 75 | + ['address', 'country', 'city'], |
| 76 | + ['address', 'address.country'] |
| 77 | + ) |
| 78 | + ).to.deep.equal(['address.country.city']); |
| 79 | + expect( |
| 80 | + getParentPaths(['address', 'country', 'city'], ['address.country']) |
| 81 | + ).to.deep.equal(['address', 'address.country.city']); |
| 82 | + }); |
| 83 | + }); |
| 84 | + |
| 85 | + describe('isOptionDisabled', function () { |
| 86 | + const options = [ |
| 87 | + '_id', |
| 88 | + 'address', |
| 89 | + 'address.city', |
| 90 | + 'address.state', |
| 91 | + 'address.street', |
| 92 | + 'address.zipcode', |
| 93 | + 'address.nested', |
| 94 | + 'address.nested.cityname', |
| 95 | + 'address.nested.countryname', |
| 96 | + 'cusine', |
| 97 | + 'name', |
| 98 | + 'stars', |
| 99 | + ]; |
| 100 | + |
| 101 | + it('should return false for options when there is nothing in projectedfields', function () { |
| 102 | + options.forEach((option) => { |
| 103 | + expect(isOptionDisabled([], option)).to.be.false; |
| 104 | + }); |
| 105 | + }); |
| 106 | + |
| 107 | + it('should return false when projected fields do not include any nested field', function () { |
| 108 | + options.forEach((option) => { |
| 109 | + expect(isOptionDisabled(['_id', 'name', 'stars', 'cusine'], option)) |
| 110 | + .to.be.false; |
| 111 | + }); |
| 112 | + }); |
| 113 | + |
| 114 | + context( |
| 115 | + 'when there is a nested children in projected field', |
| 116 | + function () { |
| 117 | + it('should return true for its parent and the children of the projected nested children and false for rest', function () { |
| 118 | + // Check with a nested-nested property |
| 119 | + expect(isOptionDisabled(['address.nested'], '_id')).to.be.false; |
| 120 | + // Since a children is already projected the parent cannot be |
| 121 | + expect(isOptionDisabled(['address.nested'], 'address')).to.be.true; |
| 122 | + expect(isOptionDisabled(['address.nested'], 'address.city')).to.be |
| 123 | + .false; |
| 124 | + expect(isOptionDisabled(['address.nested'], 'address.state')).to.be |
| 125 | + .false; |
| 126 | + expect(isOptionDisabled(['address.nested'], 'address.street')).to.be |
| 127 | + .false; |
| 128 | + expect(isOptionDisabled(['address.nested'], 'address.zipcode')).to |
| 129 | + .be.false; |
| 130 | + expect(isOptionDisabled(['address.nested'], 'address.nested')).to.be |
| 131 | + .false; |
| 132 | + // Since the parent of the following paths is already projected hence children cannot be |
| 133 | + expect( |
| 134 | + isOptionDisabled(['address.nested'], 'address.nested.cityname') |
| 135 | + ).to.be.true; |
| 136 | + expect( |
| 137 | + isOptionDisabled(['address.nested'], 'address.nested.countryname') |
| 138 | + ).to.be.true; |
| 139 | + expect(isOptionDisabled(['address.nested'], 'cusine')).to.be.false; |
| 140 | + expect(isOptionDisabled(['address.nested'], 'name')).to.be.false; |
| 141 | + expect(isOptionDisabled(['address.nested'], 'stars')).to.be.false; |
| 142 | + |
| 143 | + // This time check with a simple nested property |
| 144 | + expect(isOptionDisabled(['address.city'], '_id')).to.be.false; |
| 145 | + // Since a children is already projected the parent cannot be |
| 146 | + expect(isOptionDisabled(['address.city'], 'address')).to.be.true; |
| 147 | + expect(isOptionDisabled(['address.city'], 'address.city')).to.be |
| 148 | + .false; |
| 149 | + expect(isOptionDisabled(['address.city'], 'address.state')).to.be |
| 150 | + .false; |
| 151 | + expect(isOptionDisabled(['address.city'], 'address.street')).to.be |
| 152 | + .false; |
| 153 | + expect(isOptionDisabled(['address.city'], 'address.zipcode')).to.be |
| 154 | + .false; |
| 155 | + expect(isOptionDisabled(['address.city'], 'address.nested')).to.be |
| 156 | + .false; |
| 157 | + expect( |
| 158 | + isOptionDisabled(['address.city'], 'address.nested.cityname') |
| 159 | + ).to.be.false; |
| 160 | + expect( |
| 161 | + isOptionDisabled(['address.city'], 'address.nested.countryname') |
| 162 | + ).to.be.false; |
| 163 | + expect(isOptionDisabled(['address.city'], 'cusine')).to.be.false; |
| 164 | + expect(isOptionDisabled(['address.city'], 'name')).to.be.false; |
| 165 | + expect(isOptionDisabled(['address.city'], 'stars')).to.be.false; |
| 166 | + }); |
| 167 | + } |
| 168 | + ); |
| 169 | + }); |
| 170 | + }); |
| 171 | +}); |
0 commit comments