@@ -110,29 +110,59 @@ describe('usePrependedMessagesCount', () => {
110110 return < div > { prependCount } </ div > ;
111111 } ;
112112
113- it ( 'calculates the prepended messages using the id prop' , async ( ) => {
113+ const expectPrependCount = ( count , root ) => {
114+ expect ( root . findByType ( 'div' ) . props . children ) . toStrictEqual ( count ) ;
115+ } ;
116+
117+ it ( 'determines 0 prepended messages for empty message list' , async ( ) => {
114118 const render = await renderer . create ( < TestCase messages = { [ ] } /> ) ;
115- const expectPrependCount = ( count ) => {
116- expect ( render . root . findByType ( 'div' ) . props . children ) . toStrictEqual ( count ) ;
117- } ;
118119
119- expectPrependCount ( 0 ) ;
120+ expectPrependCount ( 0 , render . root ) ;
121+ } ) ;
122+
123+ const messageBatch = ( ids , status ) => ids . map ( ( id ) => ( { id, status } ) ) ;
124+ const firstBatch = ( status ) => messageBatch ( [ 'a' ] , status ) ;
125+ const secondBatch = ( status ) => messageBatch ( [ 'c' , 'b' ] , status ) ;
126+ const thirdBatch = ( status ) => messageBatch ( [ 'e' , 'd' ] , status ) ;
127+
128+ const testPrependCount = async ( status , first , second , third ) => {
129+ const firstMessage = firstBatch ( status ) ;
130+ const secondMsgBatch = secondBatch ( status ) ;
131+ const thirdMsgBatch = thirdBatch ( status ) ;
132+
133+ const render = await renderer . create ( < TestCase messages = { [ ] } /> ) ;
120134
121135 await renderer . act ( async ( ) => {
122- await render . update ( < TestCase messages = { [ { id : 'a' } ] } /> ) ;
123- expectPrependCount ( 0 ) ;
136+ await render . update ( < TestCase messages = { [ ... firstMessage ] } /> ) ;
137+ expectPrependCount ( first , render . root ) ;
124138 } ) ;
125139
126140 await renderer . act ( async ( ) => {
127- await render . update ( < TestCase messages = { [ { id : 'c' } , { id : 'b' } , { id : 'a' } ] } /> ) ;
128- expectPrependCount ( 2 ) ;
141+ await render . update ( < TestCase messages = { [ ... secondMsgBatch , ... firstMessage ] } /> ) ;
142+ expectPrependCount ( second , render . root ) ;
129143 } ) ;
130144
131145 await renderer . act ( async ( ) => {
132146 await render . update (
133- < TestCase messages = { [ { id : 'e' } , { id : 'd' } , { id : 'c' } , { id : 'b' } , { id : 'a' } ] } /> ,
147+ < TestCase messages = { [ ... thirdMsgBatch , ... secondMsgBatch , ... firstMessage ] } /> ,
134148 ) ;
135- expectPrependCount ( 4 ) ;
149+ expectPrependCount ( third , render . root ) ;
136150 } ) ;
151+ } ;
152+
153+ it ( 'calculates the prepended count for messages of status "received"' , async ( ) => {
154+ await testPrependCount ( 'received' , 0 , 2 , 4 ) ;
155+ } ) ;
156+
157+ it ( 'ignores the messages of status "sending" from the prepended messages count' , async ( ) => {
158+ await testPrependCount ( 'sending' , 0 , 0 , 0 ) ;
159+ } ) ;
160+
161+ it ( 'ignores the messages of status "failed" from the prepended messages count' , async ( ) => {
162+ await testPrependCount ( 'failed' , 0 , 0 , 0 ) ;
163+ } ) ;
164+
165+ it ( 'calculates the prepended messages count for the messages of status undefined' , async ( ) => {
166+ await testPrependCount ( undefined , 0 , 2 , 4 ) ;
137167 } ) ;
138168} ) ;
0 commit comments