-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathleaderboard2-spec.js
121 lines (97 loc) · 4.83 KB
/
leaderboard2-spec.js
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
describe("leaderboard", function(){
describe( "when making urls to call SO", function(){
it ( "should transform array of ids [1] into an API call to stack overflow", function(){
var soUrl = Leaderboard.getSoUrl([1]);
expect( soUrl ).toEqual( "http://api.stackexchange.com/2.2/users/1?site=stackoverflow&pagesize=100&page=1&order=desc&sort=reputation&jsonp=?" )
});
it ( "should transform arrays of ids [1,2,3] into an API call to stack overflow", function(){
var soUrl = Leaderboard.getSoUrl([1,2,3]);
expect( soUrl ).toEqual( "http://api.stackexchange.com/2.2/users/1;2;3?site=stackoverflow&pagesize=100&page=1&order=desc&sort=reputation&jsonp=?" )
});
it ( "should transform empty arrays of ids [] into undefined", function(){
var soUrl = Leaderboard.getSoUrl([]);
expect( soUrl ).toBeUndefined();
});
});
describe( "when handling data returned from SO", function(){
it( "should transform SO user object into one which we can use", function(){
var example_so_user = {
"user_id": 1,
"display_name": "Jeff Atwood",
"reputation": 15160,
"email_hash": "51d623f33f8b83095db84ff35e15dbe",
"badge_counts": {
"gold": 21,
"silver": 65,
"bronze": 79
},
profile_image: "https://www.gravatar.com/avatar/df2385041c2449f404071930e98c9c28?s=128&d=identicon&r=PG",
"ANY_OTHER_CRAP": "IS_IGNORED"
}
var user = Leaderboard.convertSoUser(0, example_so_user);
expect( user.realname ).toEqual( "Jeff Atwood" );
expect( user.reputation ).toEqual( 15160 );
expect( user.scores.gold ).toEqual( 21 );
expect( user.scores.silver ).toEqual( 65 );
expect( user.scores.bronze ).toEqual( 79 );
expect( user.profileImage ).toEqual( "https://www.gravatar.com/avatar/df2385041c2449f404071930e98c9c28?s=128&d=identicon&r=PG" );
});
});
describe( "when sorting multiple users", function(){
it ( "should sort users by reputation", function(){
var example_user_1 = { "reputation": 1 };
var example_user_2 = { "reputation": 2 };
var example_user_3 = { "reputation": 3 };
var sortedUsers = [example_user_1, example_user_3, example_user_2].sort( Leaderboard.reputationSort );
expect( sortedUsers[0] ).toEqual( example_user_3 );
expect( sortedUsers[1] ).toEqual( example_user_2 );
expect( sortedUsers[2] ).toEqual( example_user_1 );
});
it ( "should sort users by uid in case of identical rep", function(){
var example_user_1 = { "reputation": 3, "user_id": 43 };
var example_user_2 = { "reputation": 3, "user_id": 53 };
var example_user_3 = { "reputation": 3, "user_id": 13 };
var sortedUsers = [example_user_1, example_user_2, example_user_3].sort( Leaderboard.reputationSort );
expect( sortedUsers[0] ).toEqual( example_user_3 );
expect( sortedUsers[1] ).toEqual( example_user_1 );
expect( sortedUsers[2] ).toEqual( example_user_2 );
});
});
describe( "when creating divs", function(){
it ( "should create a div with class of 'user'", function(){
var userDiv = Leaderboard.createDomObject( 0, {} );
expect( userDiv.hasClass("user") ).toBeTruthy();
});
it ( "should create a div with class of 'first' for first place", function(){
var userDiv = Leaderboard.createDomObject( 0, {} );
expect( userDiv.hasClass("first") ).toBeTruthy();
});
it ( "should create a div with class of 'second' for first place", function(){
var userDiv = Leaderboard.createDomObject( 1, {} );
expect( userDiv.hasClass("second") ).toBeTruthy();
});
it ( "should create a div with class of 'third' for first place", function(){
var userDiv = Leaderboard.createDomObject( 2, {} );
expect( userDiv.hasClass("third") ).toBeTruthy();
});
});
describe( "when creating an individual user's div", function(){
it ( "should add a subdiv for the user's name", function(){
var userDiv = Leaderboard.createDomObject( 0, {"realname":"Sparkes the horse"} );
expect( userDiv.children(".stats").children(".name").text() ).toBe( "Sparkes the horse" );
});
it ( "should add a subdiv for the user's reputation", function(){
var userDiv = Leaderboard.createDomObject( 0, {"reputation":123456} );
expect( userDiv.children(".stats").children(".reputation").text() ).toBe( "123456" );
});
// disabled, why this no pass?
xit ( "should add a backround picture", function(){
var userDiv = Leaderboard.createDomObject( 0, {"gravatarImg":"http://example.com"} );
expect( userDiv.css("background-image") ).toBe( "url(http://example.com)" );
});
it ( "should add a subdiv for the user's badges", function(){
var userDiv = Leaderboard.createDomObject( 0, {"scores":{"gold": 100}} );
expect( userDiv.children(".stats").children(".badges").children("#countgold").text() ).toBe( "100" );
});
});
});