1
- import { expect } from 'chai' ;
1
+ import 'jest' ;
2
+ import 'react-native' ;
2
3
4
+ import { PATTERNS } from '../src/ParsedText' ;
3
5
import TextExtraction from '../src/lib/TextExtraction' ;
4
6
5
7
describe ( 'TextExtraction' , ( ) => {
6
-
7
8
describe ( '#parse' , ( ) => {
8
-
9
9
it ( 'returns an array with the text if there is no patterns' , ( ) => {
10
10
const textExtraction = new TextExtraction ( 'Some Text' ) ;
11
11
12
- expect ( textExtraction . parse ( ) ) . to . eql ( [ { children : 'Some Text' } ] ) ;
12
+ expect ( textExtraction . parse ( ) ) . toEqual ( [ { children : 'Some Text' } ] ) ;
13
13
} ) ;
14
14
15
15
it ( 'returns an array with the text if the text cant be parsed' , ( ) => {
16
16
const textExtraction = new TextExtraction ( 'Some Text' , [
17
17
{ pattern : / a b c d e f / } ,
18
18
] ) ;
19
19
20
- expect ( textExtraction . parse ( ) ) . to . eql ( [ { children : 'Some Text' } ] ) ;
20
+ expect ( textExtraction . parse ( ) ) . toEqual ( [ { children : 'Some Text' } ] ) ;
21
21
} ) ;
22
22
23
23
it ( 'returns an array with the text and return only present values' , ( ) => {
24
24
const textExtraction = new TextExtraction ( 'abcdef' , [
25
25
{ pattern : / a b c d e f / } ,
26
26
] ) ;
27
27
28
- expect ( textExtraction . parse ( ) ) . to . eql ( [ { children : 'abcdef' } ] ) ;
28
+ expect ( textExtraction . parse ( ) ) . toEqual ( [ { children : 'abcdef' } ] ) ;
29
29
} ) ;
30
30
31
31
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 : / b a r / } ,
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 : / b a r / } ] ,
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.' } ,
42
43
] ) ;
43
44
} ) ;
44
45
@@ -47,97 +48,119 @@ describe('TextExtraction', () => {
47
48
'https://website.bz' ,
48
49
'http://website2.it' ,
49
50
'https://t.co/hashKey' ,
50
- ]
51
+ ] ;
51
52
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 : / ( h t t p s ? : \/ \/ | w w w \. ) [ - a - z A - Z 0 - 9 @ : % . _ \+ ~ # = ] { 1 , 256 } \. [ a - z ] { 1 , 6 } \b ( [ - a - z A - Z 0 - 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
+ ) ;
55
62
56
63
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
+ } ) ;
62
68
63
- it ( 'pass the values to the callbacks' , ( done ) => {
69
+ it ( 'pass the values to the callbacks' , done => {
64
70
const textExtraction = new TextExtraction ( 'hello foo' , [
65
- { pattern : / f o o / , onPress : ( value ) => expect ( value ) . to . eql ( 'foo' ) && done ( ) } ,
71
+ {
72
+ pattern : / f o o / ,
73
+ onPress : value => {
74
+ expect ( value ) . toEqual ( 'foo' ) ;
75
+ done ( ) ;
76
+ } ,
77
+ } ,
66
78
] ) ;
67
79
68
-
69
80
const parsedText = textExtraction . parse ( ) ;
70
81
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 ) ;
74
85
75
- parsedText [ 1 ] . onPress ( ) ;
86
+ parsedText [ 1 ] . onPress ( parsedText [ 1 ] . children ) ;
76
87
} ) ;
77
88
78
89
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 : / ( h t t p s ? : \/ \/ ) ? ( [ \d a - z \. - ] + ) \. ( [ a - z \. ] { 2 , 6 } ) ( [ \/ \w \. - ] * ) * \/ ? / } ,
81
- { pattern : / b a r / } ,
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 : / ( h t t p s ? : \/ \/ ) ? ( [ \d a - z \. - ] + ) \. ( [ a - z \. ] { 2 , 6 } ) ( [ \/ \w \. - ] * ) * \/ ? / ,
95
+ } ,
96
+ { pattern : / b a r / } ,
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.' } ,
90
106
] ) ;
91
107
} ) ;
92
108
93
109
it ( 'respects the parsing order' , ( ) => {
94
- const textExtraction = new TextExtraction ( 'hello my website is http://foo.bar, bar is good.' , [
95
- { pattern : / b a r / } ,
96
- { pattern : / ^ ( h t t p s ? : \/ \/ ) ? ( [ \d a - 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 : / b a r / } ,
114
+ {
115
+ pattern : / ^ ( h t t p s ? : \/ \/ ) ? ( [ \d a - 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.' } ,
105
126
] ) ;
106
127
} ) ;
107
128
} ) ;
108
129
109
130
describe ( 'renderText prop' , ( ) => {
110
- it ( 'checks that renderText is a function' , ( done ) => {
131
+ it ( 'checks that renderText is a function' , done => {
111
132
const textExtraction = new TextExtraction ( 'Mention [@michel:561316513]' , [
112
- { pattern : / \[ ( @ [ ^ : ] + ) : ( [ ^ \] ] + ) \] / i, renderText : 'foo' }
133
+ { pattern : / \[ ( @ [ ^ : ] + ) : ( [ ^ \] ] + ) \] / i, renderText : 'foo' } ,
113
134
] ) ;
114
135
115
136
const parsedText = textExtraction . parse ( ) ;
116
137
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]' } ) ;
119
140
120
141
done ( ) ;
121
142
} ) ;
122
- it ( 'pass the values to the callbacks' , ( done ) => {
143
+ it ( 'pass the values to the callbacks' , done => {
123
144
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
+ } ,
132
156
] ) ;
133
157
134
158
const parsedText = textExtraction . parse ( ) ;
135
159
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^^' ) ;
138
162
139
163
done ( ) ;
140
164
} ) ;
141
165
} ) ;
142
-
143
166
} ) ;
0 commit comments