@@ -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" ) . mockReturnValue ( new Promise ( ( r ) => r ( [ ] ) ) ) ;
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" ) . mockReturnValue ( new Promise ( ( r ) => r ( [ ] ) ) ) ;
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,6 +1536,7 @@ describe("ClientWidgetApi", () => {
15341536 } ) ;
15351537
15361538 it ( "reads state events with any state key" , async ( ) => {
1539+ jest . spyOn ( clientWidgetApi , "getWidgetVersions" ) . mockReturnValue ( new Promise ( ( r ) => r ( [ ] ) ) ) ;
15371540 driver . readRoomState . mockResolvedValue ( [
15381541 createRoomEvent ( { type : "net.example.test" , state_key : "A" } ) ,
15391542 createRoomEvent ( { type : "net.example.test" , state_key : "B" } ) ,
@@ -1593,6 +1596,7 @@ describe("ClientWidgetApi", () => {
15931596 } ) ;
15941597
15951598 it ( "reads state events with a specific state key" , async ( ) => {
1599+ jest . spyOn ( clientWidgetApi , "getWidgetVersions" ) . mockReturnValue ( new Promise ( ( r ) => r ( [ ] ) ) ) ;
15961600 driver . readRoomState . mockResolvedValue ( [ createRoomEvent ( { type : "net.example.test" , state_key : "B" } ) ] ) ;
15971601
15981602 const event : IReadEventFromWidgetActionRequest = {
@@ -1620,6 +1624,51 @@ describe("ClientWidgetApi", () => {
16201624 expect ( driver . readRoomState ) . toHaveBeenLastCalledWith ( "!room-id" , "net.example.test" , "B" ) ;
16211625 } ) ;
16221626
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" ) . mockReturnValue ( new Promise ( ( r ) => r ( CurrentApiVersions ) ) ) ;
1629+ // with version MSC2762_UPDATE_STATE we wan the read Events action to read state events from the timeline.
1630+ driver . readRoomTimeline . mockResolvedValue ( [ createRoomEvent ( { type : "net.example.test" , state_key : "B" } ) ] ) ;
1631+
1632+ const event : IReadEventFromWidgetActionRequest = {
1633+ api : WidgetApiDirection . FromWidget ,
1634+ widgetId : "test" ,
1635+ requestId : "0" ,
1636+ action : WidgetApiFromWidgetAction . MSC2876ReadEvents ,
1637+ data : {
1638+ type : "net.example.test" ,
1639+ state_key : "B" ,
1640+ } ,
1641+ } ;
1642+
1643+ await loadIframe ( [ "org.matrix.msc2762.receive.state_event:net.example.test#B" ] ) ;
1644+
1645+ clientWidgetApi . setViewedRoomId ( "!room-id" ) ;
1646+
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+
1653+ emitEvent ( new CustomEvent ( "" , { detail : event } ) ) ;
1654+
1655+ await waitFor ( ( ) => {
1656+ expect ( transport . reply ) . toHaveBeenCalledWith ( event , {
1657+ events : [ createRoomEvent ( { type : "net.example.test" , state_key : "B" } ) ] ,
1658+ } ) ;
1659+ } ) ;
1660+
1661+ expect ( driver . readRoomTimeline ) . toHaveBeenLastCalledWith (
1662+ "!room-id" ,
1663+ "net.example.test" ,
1664+ undefined ,
1665+ "B" ,
1666+ 0 ,
1667+ undefined ,
1668+ ) ;
1669+ expect ( driver . readRoomState ) . not . toHaveBeenCalled ( ) ;
1670+ } ) ;
1671+
16231672 it ( "fails to read state events with a specific state key" , async ( ) => {
16241673 const event : IReadEventFromWidgetActionRequest = {
16251674 api : WidgetApiDirection . FromWidget ,
0 commit comments