|
| 1 | +import React from 'react'; |
| 2 | +import {shallow} from 'enzyme'; |
| 3 | +import {assert} from 'chai'; |
| 4 | +import TextField from 'src/TextField/TextField'; |
| 5 | +import TextFieldLabel from 'src/TextField/TextFieldLabel'; |
| 6 | + |
| 7 | +describe('<TextField />', () => { |
| 8 | + it('shrinks TextFieldLabel when defaultValue is set and value is null', () => { |
| 9 | + const wrapper = shallow( |
| 10 | + <TextField |
| 11 | + floatingLabelText="floating label text" |
| 12 | + defaultValue="default value" |
| 13 | + value={null} |
| 14 | + /> |
| 15 | + ); |
| 16 | + |
| 17 | + assert.equal(wrapper.find(TextFieldLabel).props().shrink, true, 'should shrink TextFieldLabel'); |
| 18 | + |
| 19 | + // set a new prop to trigger componentWillReceiveProps |
| 20 | + wrapper.setProps({id: '1'}); |
| 21 | + assert.equal(wrapper.find(TextFieldLabel).props().shrink, true, 'should shrink TextFieldLabel'); |
| 22 | + }); |
| 23 | + |
| 24 | + it(`unshrinks TextFieldLabel when defaultValue is set, the component has had input change, |
| 25 | + and value is re-set to null`, () => { |
| 26 | + const wrapper = shallow( |
| 27 | + <TextField |
| 28 | + floatingLabelText="floating label text" |
| 29 | + defaultValue="default value" |
| 30 | + value={null} |
| 31 | + /> |
| 32 | + ); |
| 33 | + assert.equal(wrapper.find(TextFieldLabel).props().shrink, true, 'should shrink TextFieldLabel'); |
| 34 | + |
| 35 | + // make input change |
| 36 | + const input = wrapper.find('input'); |
| 37 | + input.simulate('change', {target: {value: 'foo'}}); |
| 38 | + assert.equal(wrapper.find(TextFieldLabel).props().shrink, true, 'should shrink TextFieldLabel'); |
| 39 | + |
| 40 | + // set value to null again, which should unshrink the TextFieldLabel, even though TextField's isClean |
| 41 | + // state propety is false. |
| 42 | + wrapper.setProps({value: null}); |
| 43 | + assert.equal(wrapper.state().isClean, false); |
| 44 | + assert.equal(wrapper.find(TextFieldLabel).props().shrink, false, 'should shrink TextFieldLabel'); |
| 45 | + }); |
| 46 | +}); |
0 commit comments