3 const assert = require('assert');
4 const fixtureLoader = require('gitter-web-test-utils/lib/test-fixtures');
5 const roomFavouritesCore = require('../lib/room-favourites-core');
7 describe('recent-room-core', function() {
8 describe('ordering #slow', function() {
9 var fixture = fixtureLoader.setup({
12 troupe1: { users: ['user1'] },
13 troupe2: { users: ['user1'] },
14 troupe3: { users: ['user1'] },
15 troupe4: { users: ['user1'] }
18 it('should rearrange the order of favourites correctly', function() {
22 return roomFavouritesCore.findFavouriteTroupesForUser(fixture.user1.id);
25 return roomFavouritesCore
26 .updateFavourite(fixture.user1.id, fixture.troupe1.id, 1)
28 .then(function(favs) {
29 assert.equal(favs[fixture.troupe1.id], 1);
32 return roomFavouritesCore.updateFavourite(fixture.user1.id, fixture.troupe2.id, 1);
35 .then(function(favs) {
36 assert.equal(favs[fixture.troupe1.id], 2);
37 assert.equal(favs[fixture.troupe2.id], 1);
40 return roomFavouritesCore.updateFavourite(fixture.user1.id, fixture.troupe2.id, 3);
43 .then(function(favs) {
44 assert.equal(favs[fixture.troupe1.id], 2);
45 assert.equal(favs[fixture.troupe2.id], 3);
48 return roomFavouritesCore.updateFavourite(fixture.user1.id, fixture.troupe3.id, 1);
51 .then(function(favs) {
52 assert.equal(favs[fixture.troupe3.id], 1);
53 assert.equal(favs[fixture.troupe1.id], 2);
54 assert.equal(favs[fixture.troupe2.id], 3);
57 return roomFavouritesCore.updateFavourite(fixture.user1.id, fixture.troupe2.id, 2);
60 .then(function(favs) {
61 assert.equal(favs[fixture.troupe3.id], 1);
62 assert.equal(favs[fixture.troupe2.id], 2);
63 assert.equal(favs[fixture.troupe1.id], 3);
66 return roomFavouritesCore.updateFavourite(fixture.user1.id, fixture.troupe1.id, 4);
69 .then(function(favs) {
70 assert.equal(favs[fixture.troupe3.id], 1);
71 assert.equal(favs[fixture.troupe2.id], 2);
72 assert.equal(favs[fixture.troupe1.id], 4);
75 return roomFavouritesCore.updateFavourite(fixture.user1.id, fixture.troupe4.id, 1);
78 .then(function(favs) {
79 assert.equal(favs[fixture.troupe4.id], 1);
80 assert.equal(favs[fixture.troupe3.id], 2);
81 assert.equal(favs[fixture.troupe2.id], 3);
82 assert.equal(favs[fixture.troupe1.id], 4);
87 describe('updateFavourite #slow', function() {
88 var fixture = fixtureLoader.setup({
90 troupe1: { users: ['user1'] }
93 it('should add a troupe to favourites', function() {
95 return roomFavouritesCore
96 .updateFavourite(fixture.user1.id, fixture.troupe1.id, val)
98 return roomFavouritesCore.findFavouriteTroupesForUser(fixture.user1.id);
100 .then(function(favs) {
101 var isInTroupe = !!favs[fixture.troupe1.id];
102 assert(isInTroupe === val, 'Troupe should ' + (val ? '' : 'not ') + 'be a favourite');
107 .then(() => fav(true))
108 .then(() => fav(false))
109 .then(() => fav(true));