|
| 1 | +import React from 'react'; |
| 2 | +import ReactDOM from 'react-dom'; |
| 3 | +import { expect } from 'chai'; |
| 4 | +import assign from 'lodash.assign'; |
| 5 | +import TestUtils from 'react-addons-test-utils'; |
| 6 | + |
| 7 | +import { SLDSInput } from '../../components'; |
| 8 | +const { |
| 9 | + // Simulate, |
| 10 | + findRenderedDOMComponentWithTag, |
| 11 | + scryRenderedDOMComponentsWithTag, |
| 12 | + findRenderedDOMComponentWithClass |
| 13 | +} = TestUtils; |
| 14 | + |
| 15 | +describe.only('SLDS INPUT **************************************************', () => { |
| 16 | + const defaultProps = { |
| 17 | + id: 'unique-id-2', |
| 18 | + placeholder: 'Placeholder Text' |
| 19 | + }; |
| 20 | + |
| 21 | + let body; |
| 22 | + |
| 23 | + const renderInput = instance => { |
| 24 | + body = document.createElement('div'); |
| 25 | + document.body.appendChild(body); |
| 26 | + return ReactDOM.render(instance, body); |
| 27 | + }; |
| 28 | + |
| 29 | + function removeInput () { |
| 30 | + ReactDOM.unmountComponentAtNode(body); |
| 31 | + document.body.removeChild(body); |
| 32 | + } |
| 33 | + |
| 34 | + const createInput = (props) => React.createElement(SLDSInput, assign({}, defaultProps, props)); |
| 35 | + const getInput = (props) => renderInput(createInput(props)); |
| 36 | + |
| 37 | + describe('Standard Input with Label', () => { |
| 38 | + let component; |
| 39 | + let wrapper; |
| 40 | + let input; |
| 41 | + let label; |
| 42 | + |
| 43 | + beforeEach(() => { |
| 44 | + component = getInput({ label: 'Input Label' }); |
| 45 | + wrapper = findRenderedDOMComponentWithClass(component, 'slds-form-element'); |
| 46 | + input = findRenderedDOMComponentWithTag(component, 'input'); |
| 47 | + label = findRenderedDOMComponentWithClass(component, 'slds-form-element__label'); |
| 48 | + }); |
| 49 | + |
| 50 | + afterEach(() => { |
| 51 | + removeInput(); |
| 52 | + }); |
| 53 | + |
| 54 | + it('is wrapped in div with class "slds-form-element"', () => { |
| 55 | + expect(wrapper.className).to.include('slds-form-element'); |
| 56 | + }); |
| 57 | + |
| 58 | + it('renders label', () => { |
| 59 | + expect(label.textContent).to.equal('Input Label'); |
| 60 | + }); |
| 61 | + |
| 62 | + it('renders input element with class "slds-input"', () => { |
| 63 | + expect(input.className).to.include('slds-input'); |
| 64 | + }); |
| 65 | + |
| 66 | + it('has an id', () => { |
| 67 | + expect(input.getAttribute('id')).to.be.ok; |
| 68 | + }); |
| 69 | + |
| 70 | + it('renders placeholder text', () => { |
| 71 | + expect(input.getAttribute('placeholder')).to.equal('Placeholder Text'); |
| 72 | + }); |
| 73 | + }); |
| 74 | + |
| 75 | + describe('Input without Label', () => { |
| 76 | + let component; |
| 77 | + let label; |
| 78 | + |
| 79 | + beforeEach(() => { |
| 80 | + component = getInput({ assistiveText: 'Assistive Label' }); |
| 81 | + label = findRenderedDOMComponentWithClass(component, 'slds-form-element__label'); |
| 82 | + }); |
| 83 | + |
| 84 | + afterEach(() => { |
| 85 | + removeInput(); |
| 86 | + }); |
| 87 | + |
| 88 | + it('renders label (assitive)', () => { |
| 89 | + expect(label.textContent).to.equal('Assistive Label'); |
| 90 | + }); |
| 91 | + |
| 92 | + it('label has class "slds-assistive-text"', () => { |
| 93 | + expect(label.className).to.include('slds-assistive-text'); |
| 94 | + }); |
| 95 | + }); |
| 96 | + |
| 97 | + describe('Read Only Input', () => { |
| 98 | + let component; |
| 99 | + let label; |
| 100 | + let input; |
| 101 | + |
| 102 | + beforeEach(() => { |
| 103 | + component = getInput({ label: 'Input Label', readOnly: true }); |
| 104 | + label = scryRenderedDOMComponentsWithTag(component, 'span')[0]; |
| 105 | + input = scryRenderedDOMComponentsWithTag(component, 'span')[1]; |
| 106 | + }); |
| 107 | + |
| 108 | + afterEach(() => { |
| 109 | + removeInput(); |
| 110 | + }); |
| 111 | + |
| 112 | + it('label is a span and has class "slds-form-element__label"', () => { |
| 113 | + expect(label.className).to.include('slds-form-element__label'); |
| 114 | + }); |
| 115 | + |
| 116 | + it('input is a span and has class "slds-form-element__static"', () => { |
| 117 | + expect(input.className).to.include('slds-form-element__static'); |
| 118 | + }); |
| 119 | + }); |
| 120 | + |
| 121 | + describe('Required Input in Error State', () => { |
| 122 | + let component; |
| 123 | + let wrapper; |
| 124 | + let error; |
| 125 | + |
| 126 | + beforeEach(() => { |
| 127 | + component = getInput({ label: 'Input Label', required: true, errorText: 'Error Message' }); |
| 128 | + wrapper = findRenderedDOMComponentWithClass(component, 'slds-form-element'); |
| 129 | + error = findRenderedDOMComponentWithClass(component, 'slds-form-element__help'); |
| 130 | + }); |
| 131 | + |
| 132 | + afterEach(() => { |
| 133 | + removeInput(); |
| 134 | + }); |
| 135 | + |
| 136 | + it('input wrapper has class "is-required"', () => { |
| 137 | + expect(wrapper.className).to.include('is-required'); |
| 138 | + }); |
| 139 | + |
| 140 | + it('input wrapper has class "slds-has-error"', () => { |
| 141 | + expect(wrapper.className).to.include('slds-has-error'); |
| 142 | + }); |
| 143 | + |
| 144 | + it('renders error message', () => { |
| 145 | + expect(error.textContent).to.equal('Error Message'); |
| 146 | + }); |
| 147 | + }); |
| 148 | + |
| 149 | + |
| 150 | + // describe('Input with Clickable Icon', () => { |
| 151 | + // let component; |
| 152 | + // let input; |
| 153 | + // let label; |
| 154 | + // let icon; |
| 155 | + |
| 156 | + // beforeEach(() => { |
| 157 | + // component = getInput({ label: 'Input Label' }); |
| 158 | + // input = findRenderedDOMComponentWithTag(component, 'input'); |
| 159 | + // inputWrapper = findRenderedDOMComponentWithClass(component, 'slds-form-element__control'); |
| 160 | + // label = findRenderedDOMComponentWithClass(component, 'slds-form-element__label'); |
| 161 | + // icon = findRenderedDOMComponentWithTag(component, 'svg'); |
| 162 | + // }); |
| 163 | + |
| 164 | + // afterEach(() => { |
| 165 | + // removeInput(); |
| 166 | + // }); |
| 167 | + |
| 168 | + // // input wrapper has class "slds-input-has-icon" |
| 169 | + // // icon has class "slds-input__icon" |
| 170 | + // // icon can be clicked (Simulate) |
| 171 | + // }); |
| 172 | +}); |
0 commit comments