@@ -89,16 +89,79 @@ describe('Query class', () => {
8989 expect ( ( ) => regexQuery . regex ( "fieldUid" , "[a-z" ) ) . toThrow ( "Invalid regexPattern: Must be a valid regular expression" ) ;
9090 } ) ;
9191
92- it ( 'should throw error when regex method is called with invalid characters ' , async ( ) => {
92+ it ( 'should throw error when regex method is called with invalid regex pattern ' , async ( ) => {
9393 const regexQuery = getQueryObject ( client , 'referenced-content-type-uid' ) ;
94- expect ( ( ) => regexQuery . regex ( "fieldUid" , "test<script>" ) ) . toThrow ( "Invalid regexPattern: Must be a valid regular expression" ) ;
94+ // Use an actually invalid regex pattern (unclosed bracket)
95+ expect ( ( ) => regexQuery . regex ( "fieldUid" , "test[invalid(" ) ) . toThrow ( "Invalid regexPattern: Must be a valid regular expression" ) ;
9596 } ) ;
9697
9798 it ( 'should add a regex parameter to _parameters when regex method is called with valid regex' , ( ) => {
9899 query . regex ( 'fieldUid' , '^ABCXYZ123' ) ;
99100 expect ( query . _parameters [ 'fieldUid' ] ) . toEqual ( { $regex : '^ABCXYZ123' } ) ;
100101 } ) ;
101102
103+ describe ( 'regex with special characters and spaces' , ( ) => {
104+ it ( 'should accept regex pattern with spaces' , ( ) => {
105+ const regexQuery = getQueryObject ( client , 'contentTypeUid' ) ;
106+ expect ( ( ) => regexQuery . regex ( 'title' , '.*test er.*' , 'i' ) ) . not . toThrow ( ) ;
107+ expect ( regexQuery . _parameters [ 'title' ] ) . toEqual ( { $regex : '.*test er.*' , $options : 'i' } ) ;
108+ } ) ;
109+
110+ it ( 'should accept regex pattern with multiple spaces (e.g. user search)' , ( ) => {
111+ const regexQuery = getQueryObject ( client , 'contentTypeUid' ) ;
112+ expect ( ( ) => regexQuery . regex ( 'title' , '.*global flex.*' , 'i' ) ) . not . toThrow ( ) ;
113+ expect ( regexQuery . _parameters [ 'title' ] ) . toEqual ( { $regex : '.*global flex.*' , $options : 'i' } ) ;
114+ } ) ;
115+
116+ it ( 'should accept regex pattern with colon' , ( ) => {
117+ const regexQuery = getQueryObject ( client , 'contentTypeUid' ) ;
118+ expect ( ( ) => regexQuery . regex ( 'title' , '.*test:value.*' , 'i' ) ) . not . toThrow ( ) ;
119+ expect ( regexQuery . _parameters [ 'title' ] . $regex ) . toBe ( '.*test:value.*' ) ;
120+ } ) ;
121+
122+ it ( 'should accept regex pattern with comma' , ( ) => {
123+ const regexQuery = getQueryObject ( client , 'contentTypeUid' ) ;
124+ expect ( ( ) => regexQuery . regex ( 'title' , '.*test,value.*' , 'i' ) ) . not . toThrow ( ) ;
125+ expect ( regexQuery . _parameters [ 'title' ] . $regex ) . toBe ( '.*test,value.*' ) ;
126+ } ) ;
127+
128+ it ( 'should accept regex pattern with ampersand' , ( ) => {
129+ const regexQuery = getQueryObject ( client , 'contentTypeUid' ) ;
130+ expect ( ( ) => regexQuery . regex ( 'title' , '.*test&value.*' , 'i' ) ) . not . toThrow ( ) ;
131+ expect ( regexQuery . _parameters [ 'title' ] . $regex ) . toBe ( '.*test&value.*' ) ;
132+ } ) ;
133+
134+ it ( 'should accept regex pattern with at sign (e.g. email)' , ( ) => {
135+ const regexQuery = getQueryObject ( client , 'contentTypeUid' ) ;
136+ expect ( ( ) => regexQuery . regex ( 'email' , '.*@example.com.*' , 'i' ) ) . not . toThrow ( ) ;
137+ expect ( regexQuery . _parameters [ 'email' ] . $regex ) . toBe ( '.*@example.com.*' ) ;
138+ } ) ;
139+
140+ it ( 'should accept regex pattern with semicolon, equals, slash' , ( ) => {
141+ const regexQuery = getQueryObject ( client , 'contentTypeUid' ) ;
142+ expect ( ( ) => regexQuery . regex ( 'url' , '.*https://example.com.*' , 'i' ) ) . not . toThrow ( ) ;
143+ expect ( regexQuery . _parameters [ 'url' ] . $regex ) . toBe ( '.*https://example.com.*' ) ;
144+ } ) ;
145+
146+ it ( 'should accept regex pattern with hash and percent' , ( ) => {
147+ const regexQuery = getQueryObject ( client , 'contentTypeUid' ) ;
148+ expect ( ( ) => regexQuery . regex ( 'title' , '.*test#tag.*' , 'i' ) ) . not . toThrow ( ) ;
149+ expect ( ( ) => regexQuery . regex ( 'title' , '.*test%20.*' , 'i' ) ) . not . toThrow ( ) ;
150+ } ) ;
151+
152+ it ( 'should accept regex on nested/global field with space in pattern' , ( ) => {
153+ const regexQuery = getQueryObject ( client , 'contentTypeUid' ) ;
154+ expect ( ( ) => regexQuery . regex ( 'card.heading' , '.*two words.*' , 'i' ) ) . not . toThrow ( ) ;
155+ expect ( regexQuery . _parameters [ 'card.heading' ] ) . toEqual ( { $regex : '.*two words.*' , $options : 'i' } ) ;
156+ } ) ;
157+
158+ it ( 'should add regex options when third argument provided' , ( ) => {
159+ const regexQuery = getQueryObject ( client , 'contentTypeUid' ) ;
160+ regexQuery . regex ( 'fieldUid' , '.*search.*' , 'i' ) ;
161+ expect ( regexQuery . _parameters [ 'fieldUid' ] ) . toEqual ( { $regex : '.*search.*' , $options : 'i' } ) ;
162+ } ) ;
163+ } ) ;
164+
102165 it ( 'should add a containedIn parameter to _parameters' , ( ) => {
103166 query . containedIn ( 'fieldUid' , [ 'value1' , 'value2' ] ) ;
104167 expect ( query . _parameters [ 'fieldUid' ] ) . toEqual ( { '$in' : [ 'value1' , 'value2' ] } ) ;
0 commit comments