@@ -1437,6 +1437,7 @@ describe("ClientWidgetApi", () => {
14371437 describe ( "org.matrix.msc2876.read_events action" , ( ) => {
14381438 it ( "reads events from a specific room" , async ( ) => {
14391439 const roomId = "!room:example.org" ;
1440+ jest . spyOn ( clientWidgetApi , "getWidgetVersions" ) . mockResolvedValue ( [ ] ) ;
14401441 const event = createRoomEvent ( { room_id : roomId , type : "net.example.test" , content : "test" } ) ;
14411442 driver . readRoomTimeline . mockImplementation ( async ( rId ) => {
14421443 if ( rId === roomId ) return [ event ] ;
@@ -1481,6 +1482,7 @@ describe("ClientWidgetApi", () => {
14811482 it ( "reads events from all rooms" , async ( ) => {
14821483 const roomId = "!room:example.org" ;
14831484 const otherRoomId = "!other-room:example.org" ;
1485+ jest . spyOn ( clientWidgetApi , "getWidgetVersions" ) . mockResolvedValue ( [ ] ) ;
14841486 const event = createRoomEvent ( { room_id : roomId , type : "net.example.test" , content : "test" } ) ;
14851487 const otherRoomEvent = createRoomEvent ( { room_id : otherRoomId , type : "net.example.test" , content : "hi" } ) ;
14861488 driver . getKnownRooms . mockReturnValue ( [ roomId , otherRoomId ] ) ;
@@ -1534,7 +1536,8 @@ describe("ClientWidgetApi", () => {
15341536 } ) ;
15351537
15361538 it ( "reads state events with any state key" , async ( ) => {
1537- driver . readRoomTimeline . mockResolvedValue ( [
1539+ jest . spyOn ( clientWidgetApi , "getWidgetVersions" ) . mockResolvedValue ( [ ] ) ;
1540+ driver . readRoomState . mockResolvedValue ( [
15381541 createRoomEvent ( { type : "net.example.test" , state_key : "A" } ) ,
15391542 createRoomEvent ( { type : "net.example.test" , state_key : "B" } ) ,
15401543 ] ) ;
@@ -1556,22 +1559,15 @@ describe("ClientWidgetApi", () => {
15561559 emitEvent ( new CustomEvent ( "" , { detail : event } ) ) ;
15571560
15581561 await waitFor ( ( ) => {
1559- expect ( transport . reply ) . toBeCalledWith ( event , {
1562+ expect ( transport . reply ) . toHaveBeenCalledWith ( event , {
15601563 events : [
15611564 createRoomEvent ( { type : "net.example.test" , state_key : "A" } ) ,
15621565 createRoomEvent ( { type : "net.example.test" , state_key : "B" } ) ,
15631566 ] ,
15641567 } ) ;
15651568 } ) ;
15661569
1567- expect ( driver . readRoomTimeline ) . toBeCalledWith (
1568- "!room-id" ,
1569- "net.example.test" ,
1570- undefined ,
1571- undefined ,
1572- 0 ,
1573- undefined ,
1574- ) ;
1570+ expect ( driver . readRoomState ) . toHaveBeenLastCalledWith ( "!room-id" , "net.example.test" , undefined ) ;
15751571 } ) ;
15761572
15771573 it ( "fails to read state events with any state key" , async ( ) => {
@@ -1600,6 +1596,37 @@ describe("ClientWidgetApi", () => {
16001596 } ) ;
16011597
16021598 it ( "reads state events with a specific state key" , async ( ) => {
1599+ jest . spyOn ( clientWidgetApi , "getWidgetVersions" ) . mockResolvedValue ( [ ] ) ;
1600+ driver . readRoomState . mockResolvedValue ( [ createRoomEvent ( { type : "net.example.test" , state_key : "B" } ) ] ) ;
1601+
1602+ const event : IReadEventFromWidgetActionRequest = {
1603+ api : WidgetApiDirection . FromWidget ,
1604+ widgetId : "test" ,
1605+ requestId : "0" ,
1606+ action : WidgetApiFromWidgetAction . MSC2876ReadEvents ,
1607+ data : {
1608+ type : "net.example.test" ,
1609+ state_key : "B" ,
1610+ } ,
1611+ } ;
1612+
1613+ await loadIframe ( [ "org.matrix.msc2762.receive.state_event:net.example.test#B" ] ) ;
1614+ clientWidgetApi . setViewedRoomId ( "!room-id" ) ;
1615+
1616+ emitEvent ( new CustomEvent ( "" , { detail : event } ) ) ;
1617+
1618+ await waitFor ( ( ) => {
1619+ expect ( transport . reply ) . toHaveBeenCalledWith ( event , {
1620+ events : [ createRoomEvent ( { type : "net.example.test" , state_key : "B" } ) ] ,
1621+ } ) ;
1622+ } ) ;
1623+
1624+ expect ( driver . readRoomState ) . toHaveBeenLastCalledWith ( "!room-id" , "net.example.test" , "B" ) ;
1625+ } ) ;
1626+
1627+ it ( "reads state events with a specific state key from the timeline when using UnstableApiVersion.MSC2762_UPDATE_STATE" , async ( ) => {
1628+ jest . spyOn ( clientWidgetApi , "getWidgetVersions" ) . mockResolvedValue ( CurrentApiVersions ) ;
1629+ // with version MSC2762_UPDATE_STATE we wan the read Events action to read state events from the timeline.
16031630 driver . readRoomTimeline . mockResolvedValue ( [ createRoomEvent ( { type : "net.example.test" , state_key : "B" } ) ] ) ;
16041631
16051632 const event : IReadEventFromWidgetActionRequest = {
@@ -1614,24 +1641,32 @@ describe("ClientWidgetApi", () => {
16141641 } ;
16151642
16161643 await loadIframe ( [ "org.matrix.msc2762.receive.state_event:net.example.test#B" ] ) ;
1644+
16171645 clientWidgetApi . setViewedRoomId ( "!room-id" ) ;
16181646
1647+ // we clear the mock here because setViewedRoomId will push the room state and therefore read it
1648+ // from the driver.
1649+ driver . readRoomState . mockClear ( ) ;
1650+ // clearing this as well so it gets the same treatment as readRoomState for reference
1651+ driver . readRoomTimeline . mockClear ( ) ;
1652+
16191653 emitEvent ( new CustomEvent ( "" , { detail : event } ) ) ;
16201654
16211655 await waitFor ( ( ) => {
1622- expect ( transport . reply ) . toBeCalledWith ( event , {
1656+ expect ( transport . reply ) . toHaveBeenCalledWith ( event , {
16231657 events : [ createRoomEvent ( { type : "net.example.test" , state_key : "B" } ) ] ,
16241658 } ) ;
16251659 } ) ;
16261660
1627- expect ( driver . readRoomTimeline ) . toBeCalledWith (
1661+ expect ( driver . readRoomTimeline ) . toHaveBeenLastCalledWith (
16281662 "!room-id" ,
16291663 "net.example.test" ,
16301664 undefined ,
16311665 "B" ,
16321666 0 ,
16331667 undefined ,
16341668 ) ;
1669+ expect ( driver . readRoomState ) . not . toHaveBeenCalled ( ) ;
16351670 } ) ;
16361671
16371672 it ( "fails to read state events with a specific state key" , async ( ) => {
0 commit comments