@@ -277,18 +277,41 @@ Cypress.Commands.add('waitForCollabora', (wrapped = false, federated = false) =>
277277 return cy . get ( '@loleafletframe' )
278278} )
279279
280- Cypress . Commands . add ( 'waitForPostMessage' , ( messageId , values = undefined ) => {
280+ Cypress . Commands . add ( 'waitForPostMessage' , ( messageId , expectedValues = undefined ) => {
281+ const checkExpectedValues = ( message , values ) => {
282+ for ( const [ key , value ] of Object . entries ( values ) ) {
283+ if ( ! message . Values [ key ] || message . Values [ key ] !== value ) {
284+ return false
285+ }
286+ }
287+
288+ return true
289+ }
290+
281291 cy . get ( '@postMessage' , { timeout : 20000 } ) . should ( spy => {
282292 const calls = spy . getCalls ( )
283- const findMatchingCall = calls . find ( call => call . args [ 0 ] . indexOf ( '"MessageId":"' + messageId + '"' ) !== - 1 )
284- if ( ! findMatchingCall ) {
285- return expect ( findMatchingCall ) . to . not . be . undefined
293+ const messagesMatchingId = [ ]
294+
295+ // Find all messages matching the given ID
296+ // We do it this way to avoid the shallow copy of Array.filter()
297+ for ( const call of calls ) {
298+ if ( call . args [ 0 ] . includes ( `"MessageId":"${ messageId } "` ) ) {
299+ messagesMatchingId . push ( JSON . parse ( call . args [ 0 ] ) )
300+ }
286301 }
287- if ( ! values ) {
288- const object = JSON . parse ( findMatchingCall . args [ 0 ] )
289- values . forEach ( value => {
290- expect ( object . Values ) . to . have . property ( value , values [ value ] )
291- } )
302+
303+ expect ( messagesMatchingId . length ) . to . be . greaterThan ( 0 )
304+
305+ if ( expectedValues ) {
306+ const messagesMatchingValues = [ ]
307+
308+ for ( const message of messagesMatchingId ) {
309+ if ( checkExpectedValues ( message , expectedValues ) ) {
310+ messagesMatchingValues . push ( message )
311+ }
312+ }
313+
314+ expect ( messagesMatchingValues . length ) . to . be . greaterThan ( 0 )
292315 }
293316 } )
294317} )
@@ -404,7 +427,6 @@ Cypress.Commands.add('verifyTemplateFields', (fields, fileId) => {
404427 break
405428 default :
406429 expect . fail ( 'Using a field type not yet supported' )
407- break
408430 }
409431 }
410432 } )
0 commit comments