summary refs log tree commit diff
path: root/syweb
diff options
context:
space:
mode:
authorKegan Dougal <kegan@matrix.org>2014-11-14 16:36:02 +0000
committerKegan Dougal <kegan@matrix.org>2014-11-14 16:36:02 +0000
commitd22d9b22b106007615de49ffc019581d1eca275e (patch)
tree5141946533756fed63c43678de832ffa73e4caad /syweb
parentMove getUserPowerLevel to modelService. (diff)
downloadsynapse-d22d9b22b106007615de49ffc019581d1eca275e.tar.xz
Add more modelService unit tests.
Diffstat (limited to 'syweb')
-rw-r--r--syweb/webclient/test/unit/model-service.spec.js132
1 files changed, 132 insertions, 0 deletions
diff --git a/syweb/webclient/test/unit/model-service.spec.js b/syweb/webclient/test/unit/model-service.spec.js
index b99a76310b..4639a6984c 100644
--- a/syweb/webclient/test/unit/model-service.spec.js
+++ b/syweb/webclient/test/unit/model-service.spec.js
@@ -36,11 +36,13 @@ describe('ModelService', function() {
         room.current_room_state.storeStateEvent({
             content: { membership: "join" },
             user_id: "@adam:matrix.org",
+            state_key: "@adam:matrix.org",
             type: "m.room.member"
         });
         room.current_room_state.storeStateEvent({
             content: { membership: "join" },
             user_id: "@beth:matrix.org",
+            state_key: "@beth:matrix.org",
             type: "m.room.member"
         });
         room.current_room_state.storeStateEvent({
@@ -61,4 +63,134 @@ describe('ModelService', function() {
         num = modelService.getUserPowerLevel(roomId, "@unknown:matrix.org");
         expect(num).toEqual(50);
     }));
+    
+    it('should be able to get a user', inject(
+    function(modelService) {
+        var roomId = "!wefiohwefuiow:matrix.org";
+        var userId = "@bob:matrix.org";
+        
+        var presenceEvent = {
+            content: {
+                user_id: userId,
+                displayname: "Bob",
+                last_active_ago: 1415981891580
+            },
+            type: "m.presence",
+            event_id: "weofhwe@matrix.org"
+        };
+        
+        modelService.setUser(presenceEvent);
+        var user = modelService.getUser(userId);
+        expect(user.event).toEqual(presenceEvent);
+    }));
+    
+    it('should be able to create and get alias mappings.', inject(
+    function(modelService) {
+        var roomId = "!wefiohwefuiow:matrix.org";
+        var alias = "#foobar:matrix.org";
+        
+        modelService.createRoomIdToAliasMapping(roomId, alias);
+        
+        expect(modelService.getRoomIdToAliasMapping(roomId)).toEqual(alias);
+        expect(modelService.getAliasToRoomIdMapping(alias)).toEqual(roomId);
+        
+    }));
+    
+    it('should clobber alias mappings.', inject(
+    function(modelService) {
+        var roomId = "!wefiohwefuiow:matrix.org";
+        var alias = "#foobar:matrix.org";
+        var newAlias = "#foobarNEW:matrix.org";
+        
+        modelService.createRoomIdToAliasMapping(roomId, alias);
+        
+        expect(modelService.getRoomIdToAliasMapping(roomId)).toEqual(alias);
+        expect(modelService.getAliasToRoomIdMapping(alias)).toEqual(roomId);
+        
+        modelService.createRoomIdToAliasMapping(roomId, newAlias);
+        
+        expect(modelService.getRoomIdToAliasMapping(roomId)).toEqual(newAlias);
+        expect(modelService.getAliasToRoomIdMapping(newAlias)).toEqual(roomId);
+        
+    }));
+    
+    it('should update RoomMember when User is updated to point to the latest info.', inject(
+    function(modelService) {
+        var roomId = "!wefiohwefuiow:matrix.org";
+        var userId = "@bob:matrix.org";
+        
+        var presenceEvent = {
+            content: {
+                user_id: userId,
+                displayname: "Bob",
+                last_active_ago: 1415
+            },
+            type: "m.presence",
+            event_id: "weofhwe@matrix.org"
+        };
+        
+        var newPresenceEvent = {
+            content: {
+                user_id: userId,
+                displayname: "The only and only Bob",
+                last_active_ago: 1900
+            },
+            type: "m.presence",
+            event_id: "weofhtweterte@matrix.org"
+        };
+        
+        modelService.setUser(presenceEvent);
+        
+        modelService.getRoom(roomId).current_room_state.storeStateEvent({
+            type: "m.room.member",
+            id: "fwefw:matrix.org",
+            user_id: userId,
+            state_key: userId,
+            content: {
+                membership: "join"
+            }
+        });
+        
+        var roomMember = modelService.getMember(roomId, userId);
+        expect(roomMember.user.event).toEqual(presenceEvent);
+        expect(roomMember.user.event.content.displayname).toEqual("Bob");
+        
+        modelService.setUser(newPresenceEvent);
+        
+        expect(roomMember.user.event.content.displayname).toEqual("The only and only Bob");
+        
+    }));
+    
+    it('should normalise power levels between 0-100.', inject(
+    function(modelService) {
+        var roomId = "!foo:matrix.org";
+        
+        var room = modelService.getRoom(roomId);
+        room.current_room_state.storeStateEvent({
+            content: { membership: "join" },
+            user_id: "@adam:matrix.org",
+            state_key: "@adam:matrix.org",
+            type: "m.room.member"
+        });
+        room.current_room_state.storeStateEvent({
+            content: { membership: "join" },
+            user_id: "@beth:matrix.org",
+            state_key: "@beth:matrix.org",
+            type: "m.room.member"
+        });
+        room.current_room_state.storeStateEvent({
+            content: {
+                "@adam:matrix.org": 1000,
+                "default": 500
+            },
+            user_id: "@adam:matrix.org",
+            type: "m.room.power_levels"
+        });
+        
+        var roomMember = modelService.getMember(roomId, "@beth:matrix.org");
+        expect(roomMember.power_level).toEqual(500);
+        expect(roomMember.power_level_norm).toEqual(50);
+
+        
+    }));
 });