Skip to content

Commit a6015a5

Browse files
committed
Replace mocha with jest
1 parent 6674678 commit a6015a5

File tree

9 files changed

+2277
-1767
lines changed

9 files changed

+2277
-1767
lines changed

.babelrc

Lines changed: 0 additions & 3 deletions
This file was deleted.

.eslintrc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@
1818

1919

2020
"env": {
21-
"es6": true
21+
"es6": true,
22+
"jest/globals": true,
2223
},
2324

2425

2526
"plugins": [
26-
"react"
27+
"jest",
28+
"react",
2729
],
2830

2931

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,4 @@ coverage
3636
.nyc_output
3737

3838
package-lock.json
39+
yarn-error.log

.prettierrc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"printWidth": 100,
3+
"tabWidth": 2,
4+
"singleQuote": true,
5+
}

__tests__/autocomplete-test.js

Lines changed: 44 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,72 @@
1-
'use strict';
2-
3-
import Autocomplete from '../';
41
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 '..';
125

136
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'
2013
];
2114

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}`;
2816

2917
describe('<AutocompleteInput />', () => {
3018
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);
3423
});
3524

3625
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);
3930

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);
4238
});
4339

4440
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;
4744

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);
5049
});
5150

5251
it('should render custom text input', () => {
5352
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>} />
5955
);
6056

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);
6562
});
6663

6764
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);
6969

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));
7371
});
7472
});

babel.config.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
presets: ['module:metro-react-native-babel-preset']
3+
};

jest.config.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module.exports = {
2+
preset: 'react-native',
3+
verbose: true
4+
};

package.json

Lines changed: 19 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,10 @@
33
"version": "4.1.0",
44
"description": "Pure javascript autocomplete input for react-native",
55
"main": "index.js",
6-
"options": {
7-
"mocha": "--require react-native-mock/mock.js --require @babel/register ./__tests__/**/*.js"
8-
},
96
"scripts": {
10-
"coverage": "node_modules/.bin/nyc mocha $npm_package_options_mocha && node_modules/.bin/nyc report --reporter=lcov",
11-
"lint": "eslint ./*.js ./example/*.js",
127
"test": "npm run lint && npm run testonly",
13-
"testonly": "mocha $npm_package_options_mocha"
8+
"lint": "eslint ./*.js ./example/*.js ./__tests__/*.js",
9+
"testonly": "jest"
1410
},
1511
"repository": {
1612
"type": "git",
@@ -34,27 +30,23 @@
3430
"url": "https://github.com/l-urence/react-native-autocomplete-input/issues"
3531
},
3632
"homepage": "https://github.com/l-urence/react-native-autocomplete-input#readme",
37-
"dependencies": {},
3833
"devDependencies": {
39-
"@babel/register": "^7.4.4",
40-
"babel-eslint": "^7.1.1",
41-
"chai": "^3.5.0",
42-
"enzyme": "^2.3.0",
43-
"eslint": "^6.0.0",
44-
"eslint-config-airbnb": "^17.1.0",
45-
"eslint-plugin-babel": "^5.3.0",
46-
"eslint-plugin-import": "^2.2.0",
47-
"eslint-plugin-jsx-a11y": "^6.2.1",
48-
"eslint-plugin-react": "^7.14.0",
49-
"estraverse-fb": "^1.3.1",
50-
"metro-react-native-babel-preset": "^0.54.1",
51-
"mocha": "^6.1.4",
52-
"nyc": "^10.1.2",
53-
"react": "~15.5.4",
54-
"react-addons-test-utils": "^15.6.2",
55-
"react-dom": "~15.5.4",
56-
"react-native": "0.59.8",
57-
"react-native-mock": "^0.3.1",
58-
"sinon": "^1.17.4"
34+
"@babel/core": "^7.5.5",
35+
"@babel/runtime": "^7.5.5",
36+
"@react-native-community/eslint-config": "^0.0.5",
37+
"babel-jest": "^24.8.0",
38+
"eslint": "^6.1.0",
39+
"eslint-config-airbnb": "^17.1.1",
40+
"eslint-plugin-import": "^2.18.2",
41+
"eslint-plugin-jest": "^22.13.6",
42+
"eslint-plugin-jsx-a11y": "^6.2.3",
43+
"jest": "^24.8.0",
44+
"metro-react-native-babel-preset": "^0.55.0",
45+
"react": "^16.8.6",
46+
"react-native": "^0.60.4",
47+
"react-test-renderer": "16.8.6"
48+
},
49+
"jest": {
50+
"preset": "react-native"
5951
}
6052
}

0 commit comments

Comments
 (0)