@@ -26,7 +26,7 @@ describe('ajaxStream', () => {
2626 it ( 'pulls items from the stream and calls the handler' , async ( ) => {
2727 const handler = jest . fn ( ( ) => ( { } ) ) ;
2828 const { req, sendText, done } = mockRequest ( ) ;
29- const messages = [ '{ "hello": "world" }\n' , '{ "tis": "fate" }\n' ] . map ( m => ` ${ m . length } : ${ m } ` ) ;
29+ const messages = [ '{ "hello": "world" }\n' , '{ "tis": "fate" }\n' ] ;
3030
3131 const promise = ajaxStream ( '' , { } , req , {
3232 url : '/test/endpoint' ,
@@ -43,12 +43,34 @@ describe('ajaxStream', () => {
4343 expect ( handler ) . toHaveBeenCalledWith ( { tis : 'fate' } ) ;
4444 } ) ;
4545
46+ it ( 'handles newlines in values' , async ( ) => {
47+ const handler = jest . fn ( ( ) => ( { } ) ) ;
48+ const { req, sendText, done } = mockRequest ( ) ;
49+ const messages = [
50+ JSON . stringify ( { hello : 'wo\nrld' } ) ,
51+ '\n' ,
52+ JSON . stringify ( { tis : 'fa\nte' } ) ,
53+ '\n' ,
54+ ] ;
55+
56+ const promise = ajaxStream ( '' , { } , req , {
57+ url : '/test/endpoint' ,
58+ onResponse : handler ,
59+ } ) ;
60+
61+ messages . forEach ( sendText ) ;
62+ done ( ) ;
63+
64+ await promise ;
65+ expect ( handler ) . toHaveBeenCalledTimes ( 2 ) ;
66+ expect ( handler ) . toHaveBeenCalledWith ( { hello : 'wo\nrld' } ) ;
67+ expect ( handler ) . toHaveBeenCalledWith ( { tis : 'fa\nte' } ) ;
68+ } ) ;
69+
4670 it ( 'handles partial messages' , async ( ) => {
4771 const handler = jest . fn ( ( ) => ( { } ) ) ;
4872 const { req, sendText, done } = mockRequest ( ) ;
49- const messages = [ '{ "hello": "world" }\n' , '{ "tis": "fate" }\n' ]
50- . map ( m => `${ m . length } :${ m } ` )
51- . join ( '' ) ;
73+ const messages = [ '{ "hello": "world" }\n' , '{ "tis": "fate" }\n' ] . join ( '' ) ;
5274
5375 const promise = ajaxStream ( '' , { } , req , {
5476 url : '/test/endpoint' ,
@@ -117,7 +139,7 @@ describe('ajaxStream', () => {
117139 it ( 'rejects if the payload contains invalid JSON' , async ( ) => {
118140 const handler = jest . fn ( ( ) => ( { } ) ) ;
119141 const { req, sendText, done } = mockRequest ( ) ;
120- const messages = [ '{ waut? }\n' ] . map ( m => ` ${ m . length } : ${ m } ` ) . join ( '' ) ;
142+ const messages = [ '{ waut? }\n' ] . join ( '' ) ;
121143
122144 const promise = ajaxStream ( '' , { } , req , {
123145 url : '/test/endpoint' ,
@@ -130,32 +152,12 @@ describe('ajaxStream', () => {
130152 expect ( await promise . then ( ( ) => true ) . catch ( ( ) => false ) ) . toBeFalsy ( ) ;
131153 } ) ;
132154
133- it ( 'rejects if the delim is invalid' , async ( ) => {
134- const handler = jest . fn ( ( ) => ( { } ) ) ;
135- const { req, sendText, done } = mockRequest ( ) ;
136- const messages = '{ "hi": "there" }' ;
137-
138- const promise = ajaxStream ( '' , { } , req , {
139- url : '/test/endpoint' ,
140- onResponse : handler ,
141- } ) ;
142-
143- sendText ( messages ) ;
144- done ( ) ;
145-
146- expect ( await promise . then ( ( ) => true ) . catch ( ( { message } ) => message ) ) . toMatch (
147- / i n v a l i d s t r e a m r e s p o n s e / i
148- ) ;
149- } ) ;
150-
151155 it ( 'rejects if the handler throws' , async ( ) => {
152156 const handler = jest . fn ( ( ) => {
153157 throw new Error ( 'DOH!' ) ;
154158 } ) ;
155159 const { req, sendText, done } = mockRequest ( ) ;
156- const messages = [ '{ "hello": "world" }\n' , '{ "tis": "fate" }\n' ]
157- . map ( m => `${ m . length } :${ m } ` )
158- . join ( '' ) ;
160+ const messages = [ '{ "hello": "world" }\n' , '{ "tis": "fate" }\n' ] . join ( '' ) ;
159161
160162 const promise = ajaxStream ( '' , { } , req , {
161163 url : '/test/endpoint' ,
0 commit comments