@@ -5,6 +5,8 @@ import { SessionManager } from '../../sessions/SessionManager';
55import { RumEvent } from '../../dispatch/dataplane' ;
66import { DEFAULT_CONFIG , mockFetch } from '../../test-utils/test-utils' ;
77import { INSTALL_MODULE , INSTALL_SCRIPT } from '../../utils/constants' ;
8+ import EventBus , { Topic } from '../../event-bus/EventBus' ;
9+ jest . mock ( '../../event-bus/EventBus' ) ;
810
911global . fetch = mockFetch ;
1012const getSession = jest . fn ( ( ) => ( {
@@ -492,6 +494,60 @@ describe('EventCache tests', () => {
492494 expect ( eventCache . getEventBatch ( ) . length ) . toEqual ( 1 ) ;
493495 } ) ;
494496
497+ test ( 'when event is recorded then events subscribers are notified with parsed rum event' , async ( ) => {
498+ // Init
499+ const EVENT1_SCHEMA = 'com.amazon.rum.event1' ;
500+ const bus = new EventBus ( ) ;
501+ const eventCache : EventCache = Utils . createEventCache (
502+ DEFAULT_CONFIG ,
503+ bus
504+ ) ;
505+
506+ const event = {
507+ id : expect . stringMatching ( / [ 0 - 9 a - f \- ] + / ) ,
508+ timestamp : new Date ( ) ,
509+ type : EVENT1_SCHEMA ,
510+ metadata : `{"version":"1.0.0","aws:client":"${ INSTALL_MODULE } ","aws:clientVersion":"${ WEB_CLIENT_VERSION } "}` ,
511+ details : '{}'
512+ } ;
513+
514+ // Run
515+ eventCache . recordEvent ( EVENT1_SCHEMA , { } ) ;
516+ const eventBatch : RumEvent [ ] = eventCache . getEventBatch ( ) ;
517+ expect ( eventBatch ) . toEqual ( expect . arrayContaining ( [ event ] ) ) ;
518+ // eslint-disable-next-line
519+ expect ( bus . dispatch ) . toHaveBeenCalledWith (
520+ Topic . EVENT ,
521+ expect . objectContaining ( {
522+ id : expect . stringMatching ( / [ 0 - 9 a - f \- ] + / ) ,
523+ timestamp : new Date ( ) ,
524+ type : EVENT1_SCHEMA ,
525+ metadata : expect . objectContaining ( {
526+ version : '1.0.0' ,
527+ 'aws:client' : INSTALL_MODULE ,
528+ 'aws:clientVersion' : WEB_CLIENT_VERSION
529+ } ) ,
530+ details : expect . objectContaining ( { } )
531+ } )
532+ ) ;
533+ } ) ;
534+
535+ test ( 'when cache is disabled then subscribers are not notified' , async ( ) => {
536+ // Init
537+ const EVENT1_SCHEMA = 'com.amazon.rum.event1' ;
538+ const bus = new EventBus ( ) ;
539+ const eventCache : EventCache = Utils . createEventCache (
540+ DEFAULT_CONFIG ,
541+ bus
542+ ) ;
543+ // Run
544+ eventCache . disable ( ) ;
545+ eventCache . recordEvent ( EVENT1_SCHEMA , { } ) ;
546+ const eventBatch : RumEvent [ ] = eventCache . getEventBatch ( ) ;
547+ expect ( eventBatch ) . toHaveLength ( 0 ) ;
548+ expect ( bus . dispatch ) . not . toHaveBeenCalled ( ) ; // eslint-disable-line
549+ } ) ;
550+
495551 test ( 'when event limit is zero then recordEvent records all events' , async ( ) => {
496552 // Init
497553 const eventCount = 0 ;
0 commit comments