1 /*jslint node:true, unused:true*/
2 /*global describe:true, it:true */
5 var assert = require('assert');
6 var fixtureLoader = require('gitter-web-test-utils/lib/test-fixtures');
8 var recentRoomService = require('../lib/recent-room-service');
9 var recentRoomCore = require('../lib/recent-room-core');
10 const roomFavouritesCore = require('../lib/room-favourites-core');
11 var persistenceService = require('gitter-web-persistence');
13 describe('recent-room-service', function() {
14 describe('ordering', function() {
15 var fixture = fixtureLoader.setup({
18 troupe1: { users: ['user1'] },
19 troupe2: { users: ['user1'] },
20 troupe3: { users: ['user1'] },
21 troupe4: { users: ['user1'] }
24 it('should rearrange the order of favourites correctly', function() {
28 return roomFavouritesCore.findFavouriteTroupesForUser(fixture.user1.id);
31 return recentRoomService
32 .updateFavourite(fixture.user1.id, fixture.troupe1.id, 1)
34 .then(function(favs) {
35 assert.equal(favs[fixture.troupe1.id], 1);
38 return recentRoomService.updateFavourite(fixture.user1.id, fixture.troupe2.id, 1);
41 .then(function(favs) {
42 assert.equal(favs[fixture.troupe1.id], 2);
43 assert.equal(favs[fixture.troupe2.id], 1);
46 return recentRoomService.updateFavourite(fixture.user1.id, fixture.troupe2.id, 3);
49 .then(function(favs) {
50 assert.equal(favs[fixture.troupe1.id], 2);
51 assert.equal(favs[fixture.troupe2.id], 3);
54 return recentRoomService.updateFavourite(fixture.user1.id, fixture.troupe3.id, 1);
57 .then(function(favs) {
58 assert.equal(favs[fixture.troupe3.id], 1);
59 assert.equal(favs[fixture.troupe1.id], 2);
60 assert.equal(favs[fixture.troupe2.id], 3);
63 return recentRoomService.updateFavourite(fixture.user1.id, fixture.troupe2.id, 2);
66 .then(function(favs) {
67 assert.equal(favs[fixture.troupe3.id], 1);
68 assert.equal(favs[fixture.troupe2.id], 2);
69 assert.equal(favs[fixture.troupe1.id], 3);
72 return recentRoomService.updateFavourite(fixture.user1.id, fixture.troupe1.id, 4);
75 .then(function(favs) {
76 assert.equal(favs[fixture.troupe3.id], 1);
77 assert.equal(favs[fixture.troupe2.id], 2);
78 assert.equal(favs[fixture.troupe1.id], 4);
81 return recentRoomService.updateFavourite(fixture.user1.id, fixture.troupe4.id, 1);
84 .then(function(favs) {
85 assert.equal(favs[fixture.troupe4.id], 1);
86 assert.equal(favs[fixture.troupe3.id], 2);
87 assert.equal(favs[fixture.troupe2.id], 3);
88 assert.equal(favs[fixture.troupe1.id], 4);
93 describe('#updateFavourite()', function() {
94 var fixture = fixtureLoader.setup({
96 troupe1: { users: ['user1'] }
99 it('should add a troupe to favourites', function() {
101 return recentRoomService
102 .updateFavourite(fixture.user1.id, fixture.troupe1.id, val)
104 return roomFavouritesCore.findFavouriteTroupesForUser(fixture.user1.id);
106 .then(function(favs) {
107 var isInTroupe = !!favs[fixture.troupe1.id];
108 assert(isInTroupe === val, 'Troupe should ' + (val ? '' : 'not ') + 'be a favourite');
113 .then(() => fav(true))
114 .then(() => fav(false))
115 .then(() => fav(true));
119 describe('#saveLastVisitedTroupeforUserId', function() {
120 var fixture = fixtureLoader.setup({
122 troupe1: { users: ['user1'] }
125 it('should record the time each troupe was last accessed by a user', function() {
126 return recentRoomService
127 .saveLastVisitedTroupeforUserId(fixture.user1.id, fixture.troupe1.id)
129 return persistenceService.User.findById(fixture.user1.id).exec();
131 .then(function(user) {
132 assert.equal(user.lastTroupe, fixture.troupe1.id);
134 return recentRoomCore.getTroupeLastAccessTimesForUser(fixture.user1.id);
136 .then(function(times) {
137 var troupeId = '' + fixture.troupe1.id;
139 var after = times[troupeId];
140 assert(after, 'Expected a value for last access time');
142 return recentRoomService
143 .saveLastVisitedTroupeforUserId(fixture.user1.id, fixture.troupe1.id)
145 return recentRoomCore.getTroupeLastAccessTimesForUser(fixture.user1.id);
147 .then(function(times) {
149 times[troupeId] > after,
150 'The last access time for this troupe has not changed. Before it was ' +
160 describe('#findInitialRoomUrlForUser', function() {
161 var fixture = fixtureLoader.setup({
165 troupe1: { users: ['user1'] },
166 troupeOneToOne: { oneToOne: true, users: ['user1', 'user2'] }
169 it('#01 should return null when a user has no troupes', function() {
170 return recentRoomService
171 .saveLastVisitedTroupeforUserId(fixture.userNoTroupes.id, fixture.troupe1.id)
173 fixture.userNoTroupes.lastTroupe = fixture.troupe1.id;
174 return recentRoomService.findInitialRoomUrlForUser(fixture.userNoTroupes);
176 .then(function(url) {
177 assert(url === null, 'Expected the url to be null');
181 it('#02 should return return the users last troupe when they have one', function() {
182 return recentRoomService
183 .saveLastVisitedTroupeforUserId(fixture.user1.id, fixture.troupe1.id)
185 return recentRoomService.findInitialRoomUrlForUser(fixture.user1);
187 .then(function(url) {
188 assert.strictEqual(url, '/' + fixture.troupe1.uri);
192 it('#03 should return the users something when the user has troupes, but no last troupe', function() {
193 return recentRoomService.findInitialRoomUrlForUser(fixture.user1).then(function(url) {
194 assert(url !== null, 'Expected the troupe not to be null');
198 it('#04 should return one to one rooms', function() {
199 return recentRoomService
200 .saveLastVisitedTroupeforUserId(fixture.user1.id, fixture.troupeOneToOne.id)
202 return recentRoomService.findInitialRoomUrlForUser(fixture.user1);
204 .then(function(url) {
205 assert.strictEqual(url, '/' + fixture.user2.username);