@@ -15,13 +15,13 @@ limitations under the License.
1515*/
1616
1717import { mocked , Mocked } from "jest-mock" ;
18- import { IMatrixProfile , MatrixClient , Room } from "matrix-js-sdk/src/matrix" ;
18+ import { IMatrixProfile , MatrixClient , MatrixEvent , Room , RoomMemberEvent } from "matrix-js-sdk/src/matrix" ;
1919
2020import { UserProfilesStore } from "../../src/stores/UserProfilesStores" ;
21- import { filterConsole , mkRoomMemberJoinEvent , stubClient } from "../test-utils" ;
21+ import { filterConsole , mkRoomMember , mkRoomMemberJoinEvent , stubClient } from "../test-utils" ;
2222
2323describe ( "UserProfilesStore" , ( ) => {
24- const userUnknownId = "@unknown:example.com" ;
24+ const userIdDoesNotExist = "@unknown:example.com" ;
2525 const user1Id = "@user1:example.com" ;
2626 const user1Profile : IMatrixProfile = { displayname : "User 1" , avatar_url : null } ;
2727 const user2Id = "@user2:example.com" ;
@@ -36,7 +36,7 @@ describe("UserProfilesStore", () => {
3636 "Error retrieving profile for userId @user3:example.com" ,
3737 ) ;
3838
39- beforeAll ( ( ) => {
39+ beforeEach ( ( ) => {
4040 mockClient = mocked ( stubClient ( ) ) ;
4141 room = new Room ( "!room:example.com" , mockClient , mockClient . getSafeUserId ( ) ) ;
4242 room . currentState . setStateEvents ( [
@@ -58,37 +58,67 @@ describe("UserProfilesStore", () => {
5858 expect ( userProfilesStore . getProfile ( user1Id ) ) . toBeUndefined ( ) ;
5959 } ) ;
6060
61- it ( "fetchProfile should return the profile from the API and cache it" , async ( ) => {
62- const profile = await userProfilesStore . fetchProfile ( user1Id ) ;
63- expect ( profile ) . toBe ( user1Profile ) ;
64- expect ( userProfilesStore . getProfile ( user1Id ) ) . toBe ( user1Profile ) ;
65- } ) ;
61+ describe ( "fetchProfile" , ( ) => {
62+ it ( "should return the profile from the API and cache it" , async ( ) => {
63+ const profile = await userProfilesStore . fetchProfile ( user1Id ) ;
64+ expect ( profile ) . toBe ( user1Profile ) ;
65+ expect ( userProfilesStore . getProfile ( user1Id ) ) . toBe ( user1Profile ) ;
66+ } ) ;
6667
67- it ( "fetchProfile for an unknown user should return null and cache it" , async ( ) => {
68- const profile = await userProfilesStore . fetchProfile ( userUnknownId ) ;
69- expect ( profile ) . toBeNull ( ) ;
70- expect ( userProfilesStore . getProfile ( userUnknownId ) ) . toBeNull ( ) ;
68+ it ( "for an user that does not exist should return null and cache it" , async ( ) => {
69+ const profile = await userProfilesStore . fetchProfile ( userIdDoesNotExist ) ;
70+ expect ( profile ) . toBeNull ( ) ;
71+ expect ( userProfilesStore . getProfile ( userIdDoesNotExist ) ) . toBeNull ( ) ;
72+ } ) ;
7173 } ) ;
7274
7375 it ( "getOnlyKnownProfile should return undefined if the profile was not fetched" , ( ) => {
7476 expect ( userProfilesStore . getOnlyKnownProfile ( user1Id ) ) . toBeUndefined ( ) ;
7577 } ) ;
7678
77- it ( "fetchOnlyKnownProfile should return undefined if no room shared with the user" , async ( ) => {
78- const profile = await userProfilesStore . fetchOnlyKnownProfile ( user1Id ) ;
79- expect ( profile ) . toBeUndefined ( ) ;
80- expect ( userProfilesStore . getOnlyKnownProfile ( user1Id ) ) . toBeUndefined ( ) ;
81- } ) ;
79+ describe ( "fetchOnlyKnownProfile" , ( ) => {
80+ it ( "should return undefined if no room shared with the user" , async ( ) => {
81+ const profile = await userProfilesStore . fetchOnlyKnownProfile ( user1Id ) ;
82+ expect ( profile ) . toBeUndefined ( ) ;
83+ expect ( userProfilesStore . getOnlyKnownProfile ( user1Id ) ) . toBeUndefined ( ) ;
84+ } ) ;
8285
83- it ( "fetchOnlyKnownProfile for a known user should return the profile from the API and cache it" , async ( ) => {
84- const profile = await userProfilesStore . fetchOnlyKnownProfile ( user2Id ) ;
85- expect ( profile ) . toBe ( user2Profile ) ;
86- expect ( userProfilesStore . getOnlyKnownProfile ( user2Id ) ) . toBe ( user2Profile ) ;
86+ it ( "for a known user should return the profile from the API and cache it" , async ( ) => {
87+ const profile = await userProfilesStore . fetchOnlyKnownProfile ( user2Id ) ;
88+ expect ( profile ) . toBe ( user2Profile ) ;
89+ expect ( userProfilesStore . getOnlyKnownProfile ( user2Id ) ) . toBe ( user2Profile ) ;
90+ } ) ;
91+
92+ it ( "for a known user not found via API should return null and cache it" , async ( ) => {
93+ const profile = await userProfilesStore . fetchOnlyKnownProfile ( user3Id ) ;
94+ expect ( profile ) . toBeNull ( ) ;
95+ expect ( userProfilesStore . getOnlyKnownProfile ( user3Id ) ) . toBeNull ( ) ;
96+ } ) ;
8797 } ) ;
8898
89- it ( "fetchOnlyKnownProfile for a known user not found via API should return null and cache it" , async ( ) => {
90- const profile = await userProfilesStore . fetchOnlyKnownProfile ( user3Id ) ;
91- expect ( profile ) . toBeNull ( ) ;
92- expect ( userProfilesStore . getOnlyKnownProfile ( user3Id ) ) . toBeNull ( ) ;
99+ describe ( "when there are cached values and membership updates" , ( ) => {
100+ beforeEach ( async ( ) => {
101+ await userProfilesStore . fetchProfile ( user1Id ) ;
102+ await userProfilesStore . fetchOnlyKnownProfile ( user2Id ) ;
103+ } ) ;
104+
105+ describe ( "and membership events with the same values appear" , ( ) => {
106+ beforeEach ( ( ) => {
107+ const roomMember1 = mkRoomMember ( room . roomId , user1Id ) ;
108+ roomMember1 . rawDisplayName = user1Profile . displayname ;
109+ roomMember1 . getMxcAvatarUrl = ( ) => null ;
110+ mockClient . emit ( RoomMemberEvent . Membership , { } as MatrixEvent , roomMember1 ) ;
111+
112+ const roomMember2 = mkRoomMember ( room . roomId , user2Id ) ;
113+ roomMember2 . rawDisplayName = user2Profile . displayname ;
114+ roomMember2 . getMxcAvatarUrl = ( ) => null ;
115+ mockClient . emit ( RoomMemberEvent . Membership , { } as MatrixEvent , roomMember2 ) ;
116+ } ) ;
117+
118+ it ( "should not invalidate the cache" , ( ) => {
119+ expect ( userProfilesStore . getProfile ( user1Id ) ) . toBe ( user1Profile ) ;
120+ expect ( userProfilesStore . getOnlyKnownProfile ( user2Id ) ) . toBe ( user2Profile ) ;
121+ } ) ;
122+ } ) ;
93123 } ) ;
94124} ) ;
0 commit comments