Skip to content

Commit c9862b9

Browse files
committed
Switch to jest for testing & fix tests, expose PATTERNS for direct testing parser regexps
1 parent 39f0437 commit c9862b9

File tree

3 files changed

+98
-72
lines changed

3 files changed

+98
-72
lines changed

package.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"description": "Parse text and make them into multiple React Native Text elements",
55
"main": "src/ParsedText.js",
66
"scripts": {
7-
"test": "mocha --recursive --compilers js:babel-register"
7+
"test": "jest"
88
},
99
"bugs": {
1010
"url": "https://github.com/taskrabbit/react-native-parsed-text/issues"
@@ -40,5 +40,8 @@
4040
"license": "MIT",
4141
"dependencies": {
4242
"prop-types": "^15.5.10"
43+
},
44+
"jest": {
45+
"preset": "react-native"
4346
}
4447
}

src/ParsedText.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import PropTypes from 'prop-types';
44

55
import TextExtraction from './lib/TextExtraction';
66

7-
const PATTERNS = {
7+
export const PATTERNS = {
88
url: /(https?:\/\/|www\.)[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&\/\/=]*)/i,
99
phone: /[\+]?[(]?[0-9]{3}[)]?[-\s\.]?[0-9]{3}[-\s\.]?[0-9]{4,7}/,
1010
email: /\S+@\S+\.\S+/,

test/TextExtraction.test.js

+93-70
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,45 @@
1-
import { expect } from 'chai';
1+
import 'jest';
2+
import 'react-native';
23

4+
import { PATTERNS } from '../src/ParsedText';
35
import TextExtraction from '../src/lib/TextExtraction';
46

57
describe('TextExtraction', () => {
6-
78
describe('#parse', () => {
8-
99
it('returns an array with the text if there is no patterns', () => {
1010
const textExtraction = new TextExtraction('Some Text');
1111

12-
expect(textExtraction.parse()).to.eql([{children: 'Some Text'}]);
12+
expect(textExtraction.parse()).toEqual([{ children: 'Some Text' }]);
1313
});
1414

1515
it('returns an array with the text if the text cant be parsed', () => {
1616
const textExtraction = new TextExtraction('Some Text', [
1717
{ pattern: /abcdef/ },
1818
]);
1919

20-
expect(textExtraction.parse()).to.eql([{children: 'Some Text'}]);
20+
expect(textExtraction.parse()).toEqual([{ children: 'Some Text' }]);
2121
});
2222

2323
it('returns an array with the text and return only present values', () => {
2424
const textExtraction = new TextExtraction('abcdef', [
2525
{ pattern: /abcdef/ },
2626
]);
2727

28-
expect(textExtraction.parse()).to.eql([{children: 'abcdef'}]);
28+
expect(textExtraction.parse()).toEqual([{ children: 'abcdef' }]);
2929
});
3030

3131
it('returns an array with text parts if there is matches', () => {
32-
const textExtraction = new TextExtraction('hello my website is http://foo.bar, bar is good.', [
33-
{ pattern: /bar/ },
34-
]);
35-
36-
expect(textExtraction.parse()).to.eql([
37-
{children: 'hello my website is http://foo.'},
38-
{children: 'bar'},
39-
{children: ', '},
40-
{children: 'bar'},
41-
{children: ' is good.'},
32+
const textExtraction = new TextExtraction(
33+
'hello my website is http://foo.bar, bar is good.',
34+
[{ pattern: /bar/ }],
35+
);
36+
37+
expect(textExtraction.parse()).toEqual([
38+
{ children: 'hello my website is http://foo.' },
39+
{ children: 'bar' },
40+
{ children: ', ' },
41+
{ children: 'bar' },
42+
{ children: ' is good.' },
4243
]);
4344
});
4445

@@ -47,97 +48,119 @@ describe('TextExtraction', () => {
4748
'https://website.bz',
4849
'http://website2.it',
4950
'https://t.co/hashKey',
50-
]
51+
];
5152
const textExtraction = new TextExtraction(
52-
`this is my website ${urls[0]} and this is also ${urls[1]} ig this one also ${urls[2]}`,
53-
[{ pattern: /(https?:\/\/|www\.)[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-z]{1,6}\b([-a-zA-Z0-9@:%_\+.~#?&\/\/=]*)/i }]
54-
)
53+
`this is my website ${urls[0]} and this is also ${
54+
urls[1]
55+
} and why not this one also ${urls[2]}`,
56+
[
57+
{
58+
pattern: PATTERNS.url,
59+
},
60+
],
61+
);
5562

5663
const parsedText = textExtraction.parse();
57-
expect(parsedText[1].children).to.eql(urls[0])
58-
expect(parsedText[3].children).to.eql(urls[1])
59-
expect(parsedText[5].children).to.eql(urls[2])
60-
61-
})
64+
expect(parsedText[1].children).toEqual(urls[0]);
65+
expect(parsedText[3].children).toEqual(urls[1]);
66+
expect(parsedText[5].children).toEqual(urls[2]);
67+
});
6268

63-
it('pass the values to the callbacks', (done) => {
69+
it('pass the values to the callbacks', done => {
6470
const textExtraction = new TextExtraction('hello foo', [
65-
{ pattern: /foo/, onPress: (value) => expect(value).to.eql('foo') && done() },
71+
{
72+
pattern: /foo/,
73+
onPress: value => {
74+
expect(value).toEqual('foo');
75+
done();
76+
},
77+
},
6678
]);
6779

68-
6980
const parsedText = textExtraction.parse();
7081

71-
expect(parsedText[0]).to.eql({children: 'hello '});
72-
expect(parsedText[1].children).to.eql('foo');
73-
expect(parsedText[1].onPress).to.be.instanceof(Function);
82+
expect(parsedText[0]).toEqual({ children: 'hello ' });
83+
expect(parsedText[1].children).toEqual('foo');
84+
expect(parsedText[1].onPress).toBeInstanceOf(Function);
7485

75-
parsedText[1].onPress();
86+
parsedText[1].onPress(parsedText[1].children);
7687
});
7788

7889
it('only allow a text to be parsed once', () => {
79-
const textExtraction = new TextExtraction('hello my website is http://foo.bar, bar is good.', [
80-
{ pattern: /(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?/ },
81-
{ pattern: /bar/ },
82-
]);
83-
84-
expect(textExtraction.parse()).to.eql([
85-
{children: 'hello my website is '},
86-
{children: 'http://foo.bar'},
87-
{children: ', '},
88-
{children: 'bar'},
89-
{children: ' is good.'},
90+
const textExtraction = new TextExtraction(
91+
'hello my website is http://foo.bar, bar is good.',
92+
[
93+
{
94+
pattern: /(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?/,
95+
},
96+
{ pattern: /bar/ },
97+
],
98+
);
99+
100+
expect(textExtraction.parse()).toEqual([
101+
{ children: 'hello my website is ' },
102+
{ children: 'http://foo.bar' },
103+
{ children: ', ' },
104+
{ children: 'bar' },
105+
{ children: ' is good.' },
90106
]);
91107
});
92108

93109
it('respects the parsing order', () => {
94-
const textExtraction = new TextExtraction('hello my website is http://foo.bar, bar is good.', [
95-
{ pattern: /bar/ },
96-
{ pattern: /^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/ },
97-
]);
98-
99-
expect(textExtraction.parse()).to.eql([
100-
{children: 'hello my website is http://foo.'},
101-
{children: 'bar'},
102-
{children: ', '},
103-
{children: 'bar'},
104-
{children: ' is good.'},
110+
const textExtraction = new TextExtraction(
111+
'hello my website is http://foo.bar, bar is good.',
112+
[
113+
{ pattern: /bar/ },
114+
{
115+
pattern: /^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/,
116+
},
117+
],
118+
);
119+
120+
expect(textExtraction.parse()).toEqual([
121+
{ children: 'hello my website is http://foo.' },
122+
{ children: 'bar' },
123+
{ children: ', ' },
124+
{ children: 'bar' },
125+
{ children: ' is good.' },
105126
]);
106127
});
107128
});
108129

109130
describe('renderText prop', () => {
110-
it('checks that renderText is a function', (done) => {
131+
it('checks that renderText is a function', done => {
111132
const textExtraction = new TextExtraction('Mention [@michel:561316513]', [
112-
{ pattern: /\[(@[^:]+):([^\]]+)\]/i, renderText: 'foo'}
133+
{ pattern: /\[(@[^:]+):([^\]]+)\]/i, renderText: 'foo' },
113134
]);
114135

115136
const parsedText = textExtraction.parse();
116137

117-
expect(parsedText[0]).to.eql({children: 'Mention '});
118-
expect(parsedText[1]).to.eql({children: '[@michel:561316513]'});
138+
expect(parsedText[0]).toEqual({ children: 'Mention ' });
139+
expect(parsedText[1]).toEqual({ children: '[@michel:561316513]' });
119140

120141
done();
121142
});
122-
it('pass the values to the callbacks', (done) => {
143+
it('pass the values to the callbacks', done => {
123144
const textExtraction = new TextExtraction('Mention [@michel:561316513]', [
124-
{ pattern: /\[(@[^:]+):([^\]]+)\]/i, renderText: (string, matches) => {
125-
let pattern = /\[(@[^:]+):([^\]]+)\]/i;
126-
let match = string.match(pattern);
127-
expect(matches[0]).to.eql("[@michel:561316513]")
128-
expect(matches[1]).to.eql("@michel")
129-
expect(matches[2]).to.eql("561316513")
130-
return `^^${match[1]}^^`;
131-
}}
145+
{
146+
pattern: /\[(@[^:]+):([^\]]+)\]/i,
147+
renderText: (string, matches) => {
148+
let pattern = /\[(@[^:]+):([^\]]+)\]/i;
149+
let match = string.match(pattern);
150+
expect(matches[0]).toEqual('[@michel:561316513]');
151+
expect(matches[1]).toEqual('@michel');
152+
expect(matches[2]).toEqual('561316513');
153+
return `^^${match[1]}^^`;
154+
},
155+
},
132156
]);
133157

134158
const parsedText = textExtraction.parse();
135159

136-
expect(parsedText[0]).to.eql({children: 'Mention '});
137-
expect(parsedText[1].children).to.eql('^^@michel^^');
160+
expect(parsedText[0]).toEqual({ children: 'Mention ' });
161+
expect(parsedText[1].children).toEqual('^^@michel^^');
138162

139163
done();
140164
});
141165
});
142-
143166
});

0 commit comments

Comments
 (0)