|
1 | | -'use strict'; |
2 | | - |
3 | | -import Autocomplete from '../'; |
4 | 1 | import React from 'react'; |
5 | | -import { describe, it } from 'mocha'; |
6 | | -import { expect } from 'chai'; |
7 | | -import { shallow } from 'enzyme'; |
8 | | -import { |
9 | | - ListView, |
10 | | - Text |
11 | | -} from 'react-native'; |
| 2 | +import renderer from 'react-test-renderer'; |
| 3 | +import { FlatList, Text, TextInput } from 'react-native'; |
| 4 | +import Autocomplete from '..'; |
12 | 5 |
|
13 | 6 | const ITEMS = [ |
14 | | - "A New Hope", |
15 | | - "The Empire Strikes Back", |
16 | | - "Return of the Jedi", |
17 | | - "The Phantom Menace", |
18 | | - "Attack of the Clones", |
19 | | - "Revenge of the Sith" |
| 7 | + 'A New Hope', |
| 8 | + 'The Empire Strikes Back', |
| 9 | + 'Return of the Jedi', |
| 10 | + 'The Phantom Menace', |
| 11 | + 'Attack of the Clones', |
| 12 | + 'Revenge of the Sith' |
20 | 13 | ]; |
21 | 14 |
|
22 | | -function overrideGetRowCount(autocomplete, data) { |
23 | | - const DataSourcePrototype = ListView.DataSource.prototype; |
24 | | - autocomplete.state().dataSource = Object.assign(DataSourcePrototype, { |
25 | | - getRowCount: () => data.length |
26 | | - }); |
27 | | -} |
| 15 | +const keyExtractor = (item, index) => `key-${index}`; |
28 | 16 |
|
29 | 17 | describe('<AutocompleteInput />', () => { |
30 | 18 | it('Should hide suggestion list on initial render', () => { |
31 | | - const autocomplete = shallow(<Autocomplete data={[]} />); |
32 | | - expect(autocomplete.length).to.equal(1); |
33 | | - expect(autocomplete.childAt(1).children()).to.have.length(0); |
| 19 | + const r = renderer.create(<Autocomplete data={[]} />); |
| 20 | + const autocomplete = r.root; |
| 21 | + |
| 22 | + expect(autocomplete.findAllByType(FlatList)).toHaveLength(0); |
34 | 23 | }); |
35 | 24 |
|
36 | 25 | it('Should show suggestion list when data gets updated with length > 0', () => { |
37 | | - const autocomplete = shallow(<Autocomplete data={[]} />); |
38 | | - overrideGetRowCount(autocomplete, ITEMS); |
| 26 | + const testRenderer = renderer.create(<Autocomplete data={[]} />); |
| 27 | + const autocomplete = testRenderer.root; |
| 28 | + |
| 29 | + expect(autocomplete.findAllByType(FlatList)).toHaveLength(0); |
39 | 30 |
|
40 | | - autocomplete.setProps({ data: ITEMS }); |
41 | | - expect(autocomplete.childAt(1).children()).to.have.length(1); |
| 31 | + testRenderer.update(<Autocomplete data={ITEMS} keyExtractor={keyExtractor} />); |
| 32 | + |
| 33 | + const list = autocomplete.findByType(FlatList); |
| 34 | + expect(list.props.data).toEqual(ITEMS); |
| 35 | + |
| 36 | + const texts = list.findAllByType(Text); |
| 37 | + expect(texts).toHaveLength(ITEMS.length); |
42 | 38 | }); |
43 | 39 |
|
44 | 40 | it('Should hide suggestion list when data gets updates with length < 1', () => { |
45 | | - const autocomplete = shallow(<Autocomplete data={[]} />); |
46 | | - overrideGetRowCount(autocomplete, []); |
| 41 | + const props = { data: ITEMS, keyExtractor }; |
| 42 | + const testRenderer = renderer.create(<Autocomplete {...props} />); |
| 43 | + const autocomplete = testRenderer.root; |
47 | 44 |
|
48 | | - autocomplete.setProps({ data: [] }); |
49 | | - expect(autocomplete.childAt(1).children()).to.have.length(0); |
| 45 | + expect(autocomplete.findAllByType(FlatList)).toHaveLength(1); |
| 46 | + testRenderer.update(<Autocomplete data={[]} />); |
| 47 | + |
| 48 | + expect(autocomplete.findAllByType(FlatList)).toHaveLength(0); |
50 | 49 | }); |
51 | 50 |
|
52 | 51 | it('should render custom text input', () => { |
53 | 52 | const text = 'Custom Text Input'; |
54 | | - const autocomplete = shallow( |
55 | | - <Autocomplete data={[]} foo="bar" renderTextInput={props => |
56 | | - <Text {...props}>{text}</Text> |
57 | | - } |
58 | | - /> |
| 53 | + const testRenderer = renderer.create( |
| 54 | + <Autocomplete data={[]} foo="bar" renderTextInput={props => <Text {...props}>{text}</Text>} /> |
59 | 55 | ); |
60 | 56 |
|
61 | | - const customInput = autocomplete.find('Text'); |
62 | | - expect(autocomplete.find('TextInput')).to.have.length(0); |
63 | | - expect(customInput.children().get(0)).to.equal(text); |
64 | | - expect(customInput.prop('foo')).to.equal('bar'); |
| 57 | + const autocomplete = testRenderer.root; |
| 58 | + const customTextInput = autocomplete.findByType(Text); |
| 59 | + |
| 60 | + expect(customTextInput.children[0].children).toEqual([text]); |
| 61 | + expect(autocomplete.findAllByType(TextInput)).toHaveLength(0); |
65 | 62 | }); |
66 | 63 |
|
67 | 64 | it('should render default <TextInput /> if no custom one is supplied', () => { |
68 | | - const autocomplete = shallow(<Autocomplete data={[]} foo="bar" />); |
| 65 | + const props = { foo: 'bar' }; |
| 66 | + const testRenderer = renderer.create(<Autocomplete data={[]} {...props} />); |
| 67 | + const autocomplete = testRenderer.root; |
| 68 | + const textInput = autocomplete.findByType(TextInput); |
69 | 69 |
|
70 | | - const textInput = autocomplete.childAt(0).children().first(); |
71 | | - expect(textInput.name()).to.equal('TextInput'); |
72 | | - expect(textInput.prop('foo')).to.equal('bar'); |
| 70 | + expect(textInput.props).toEqual(expect.objectContaining(props)); |
73 | 71 | }); |
74 | 72 | }); |
0 commit comments