summary refs log tree commit diff
path: root/syweb/webclient/test/unit/recents-service.spec.js
blob: a2f9ecbaf8a0c1d8d821aefaae45c59581212f3d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
describe('RecentsService', function() {
    var scope;
    var MSG_EVENT = "__test__";
    
    var testEventContainsBingWord, testIsLive, testEvent;
    
    var eventHandlerService = {
        MSG_EVENT: MSG_EVENT,
        eventContainsBingWord: function(event) {
            return testEventContainsBingWord;
        }
    };

    // setup the service and mocked dependencies
    beforeEach(function() {
        
        // set default mock values
        testEventContainsBingWord = false;
        testIsLive = true;
        testEvent = {
            content: {
                body: "Hello world",
                msgtype: "m.text"
            },
            user_id: "@alfred:localhost",
            room_id: "!fl1bb13:localhost",
            event_id: "fwuegfw@localhost"
        }
        
        // mocked dependencies
        module(function ($provide) {
          $provide.value('eventHandlerService', eventHandlerService);
        });
        
        // tested service
        module('recentsService');
    });
    
    beforeEach(inject(function($rootScope) {
        scope = $rootScope;
    }));

    it('should start with no unread messages.', inject(
    function(recentsService) {
        expect(recentsService.getUnreadMessages()).toEqual({});
        expect(recentsService.getUnreadBingMessages()).toEqual({});
    }));
    
    it('should NOT add an unread message to the room currently selected.', inject(
    function(recentsService) {
        recentsService.setSelectedRoomId(testEvent.room_id);
        scope.$broadcast(MSG_EVENT, testEvent, testIsLive);
        expect(recentsService.getUnreadMessages()).toEqual({});
        expect(recentsService.getUnreadBingMessages()).toEqual({});
    }));
    
    it('should add an unread message to the room NOT currently selected.', inject(
    function(recentsService) {
        recentsService.setSelectedRoomId("!someotherroomid:localhost");
        scope.$broadcast(MSG_EVENT, testEvent, testIsLive);
        
        var unread = {};
        unread[testEvent.room_id] = 1;
        expect(recentsService.getUnreadMessages()).toEqual(unread);
    }));
    
    it('should add an unread message and an unread bing message if a message contains a bing word.', inject(
    function(recentsService) {
        recentsService.setSelectedRoomId("!someotherroomid:localhost");
        testEventContainsBingWord = true;
        scope.$broadcast(MSG_EVENT, testEvent, testIsLive);
        
        var unread = {};
        unread[testEvent.room_id] = 1;
        expect(recentsService.getUnreadMessages()).toEqual(unread);
        
        var bing = {};
        bing[testEvent.room_id] = testEvent;
        expect(recentsService.getUnreadBingMessages()).toEqual(bing);
    }));
    
    it('should clear both unread and unread bing messages when markAsRead is called.', inject(
    function(recentsService) {
        recentsService.setSelectedRoomId("!someotherroomid:localhost");
        testEventContainsBingWord = true;
        scope.$broadcast(MSG_EVENT, testEvent, testIsLive);
        
        var unread = {};
        unread[testEvent.room_id] = 1;
        expect(recentsService.getUnreadMessages()).toEqual(unread);
        
        var bing = {};
        bing[testEvent.room_id] = testEvent;
        expect(recentsService.getUnreadBingMessages()).toEqual(bing);
        
        recentsService.markAsRead(testEvent.room_id);
        
        unread[testEvent.room_id] = 0;
        bing[testEvent.room_id] = undefined;
        expect(recentsService.getUnreadMessages()).toEqual(unread);
        expect(recentsService.getUnreadBingMessages()).toEqual(bing);
    }));
    
    it('should not add messages as unread if they are not live.', inject(
    function(recentsService) {
        testIsLive = false;
        
        recentsService.setSelectedRoomId("!someotherroomid:localhost");
        testEventContainsBingWord = true;
        scope.$broadcast(MSG_EVENT, testEvent, testIsLive);
    
        expect(recentsService.getUnreadMessages()).toEqual({});
        expect(recentsService.getUnreadBingMessages()).toEqual({});
    }));
    
    it('should increment the unread message count.', inject(
    function(recentsService) {
        recentsService.setSelectedRoomId("!someotherroomid:localhost");
        scope.$broadcast(MSG_EVENT, testEvent, testIsLive);
    
        var unread = {};
        unread[testEvent.room_id] = 1;
        expect(recentsService.getUnreadMessages()).toEqual(unread);
        
        scope.$broadcast(MSG_EVENT, testEvent, testIsLive);
        
        unread[testEvent.room_id] = 2;
        expect(recentsService.getUnreadMessages()).toEqual(unread);
    }));
    
    it('should set the bing event to the latest message to contain a bing word.', inject(
    function(recentsService) {
        recentsService.setSelectedRoomId("!someotherroomid:localhost");
        testEventContainsBingWord = true;
        scope.$broadcast(MSG_EVENT, testEvent, testIsLive);
    
        var nextEvent = angular.copy(testEvent);
        nextEvent.content.body = "Goodbye cruel world.";
        nextEvent.event_id = "erfuerhfeaaaa@localhost";
        scope.$broadcast(MSG_EVENT, nextEvent, testIsLive);
        
        var bing = {};
        bing[testEvent.room_id] = nextEvent;
        expect(recentsService.getUnreadBingMessages()).toEqual(bing);
    }));
    
    it('should do nothing when marking an unknown room ID as read.', inject(
    function(recentsService) {
        recentsService.markAsRead("!someotherroomid:localhost");
        expect(recentsService.getUnreadMessages()).toEqual({});
        expect(recentsService.getUnreadBingMessages()).toEqual({});
    }));
});