@@ -48,22 +48,25 @@ describe("Weekly XP Leaderboards", () => {
4848 const results = await lb . getResults ( 0 , 10 , leaderboardsConfig , true ) ;
4949
5050 //THEN
51- expect ( results ) . toEqual ( [
52- {
53- ...user1 ,
54- rank : 1 ,
55- timeTypedSeconds : 10 ,
56- totalXp : 150 ,
57- isPremium : false ,
58- } ,
59- {
60- ...user2 ,
61- rank : 2 ,
62- timeTypedSeconds : 7 ,
63- totalXp : 100 ,
64- isPremium : true ,
65- } ,
66- ] ) ;
51+ expect ( results ) . toEqual ( {
52+ count : 2 ,
53+ entries : [
54+ {
55+ ...user1 ,
56+ rank : 1 ,
57+ timeTypedSeconds : 10 ,
58+ totalXp : 150 ,
59+ isPremium : false ,
60+ } ,
61+ {
62+ ...user2 ,
63+ rank : 2 ,
64+ timeTypedSeconds : 7 ,
65+ totalXp : 100 ,
66+ isPremium : true ,
67+ } ,
68+ ] ,
69+ } ) ;
6770 } ) ;
6871 } ) ;
6972
@@ -77,10 +80,13 @@ describe("Weekly XP Leaderboards", () => {
7780 const results = await lb . getResults ( 0 , 10 , leaderboardsConfig , true ) ;
7881
7982 //THEN
80- expect ( results ) . toEqual ( [
81- { rank : 1 , totalXp : 150 , ...user1 } ,
82- { rank : 2 , totalXp : 100 , ...user2 } ,
83- ] ) ;
83+ expect ( results ) . toEqual ( {
84+ count : 2 ,
85+ entries : [
86+ { rank : 1 , totalXp : 150 , ...user1 } ,
87+ { rank : 2 , totalXp : 100 , ...user2 } ,
88+ ] ,
89+ } ) ;
8490 } ) ;
8591
8692 it ( "gets results for page" , async ( ) => {
@@ -94,10 +100,13 @@ describe("Weekly XP Leaderboards", () => {
94100 const results = await lb . getResults ( 1 , 2 , leaderboardsConfig , true ) ;
95101
96102 //THEN
97- expect ( results ) . toEqual ( [
98- { rank : 3 , totalXp : 50 , ...user3 } ,
99- { rank : 4 , totalXp : 25 , ...user4 } ,
100- ] ) ;
103+ expect ( results ) . toEqual ( {
104+ count : 4 ,
105+ entries : [
106+ { rank : 3 , totalXp : 50 , ...user3 } ,
107+ { rank : 4 , totalXp : 25 , ...user4 } ,
108+ ] ,
109+ } ) ;
101110 } ) ;
102111
103112 it ( "gets results without premium" , async ( ) => {
@@ -109,10 +118,73 @@ describe("Weekly XP Leaderboards", () => {
109118 const results = await lb . getResults ( 0 , 10 , leaderboardsConfig , false ) ;
110119
111120 //THEN
112- expect ( results ) . toEqual ( [
113- { rank : 1 , totalXp : 150 , ...user1 , isPremium : undefined } ,
114- { rank : 2 , totalXp : 100 , ...user2 , isPremium : undefined } ,
121+ expect ( results ) . toEqual ( {
122+ count : 2 ,
123+ entries : [
124+ { rank : 1 , totalXp : 150 , ...user1 , isPremium : undefined } ,
125+ { rank : 2 , totalXp : 100 , ...user2 , isPremium : undefined } ,
126+ ] ,
127+ } ) ;
128+ } ) ;
129+
130+ it ( "gets results for friends only" , async ( ) => {
131+ //GIVEN
132+ const _user1 = await givenResult ( 100 ) ;
133+ const user2 = await givenResult ( 75 ) ;
134+ const _user3 = await givenResult ( 50 ) ;
135+ const user4 = await givenResult ( 25 ) ;
136+
137+ //WHEN
138+ const results = await lb . getResults ( 0 , 5 , leaderboardsConfig , true , [
139+ user2 . uid ,
140+ user4 . uid ,
141+ new ObjectId ( ) . toHexString ( ) ,
142+ ] ) ;
143+
144+ //THEN
145+ expect ( results ) . toEqual ( {
146+ count : 2 ,
147+ entries : [
148+ { rank : 2 , friendsRank : 1 , totalXp : 75 , ...user2 } ,
149+ { rank : 4 , friendsRank : 2 , totalXp : 25 , ...user4 } ,
150+ ] ,
151+ } ) ;
152+ } ) ;
153+
154+ it ( "gets results for friends only with page" , async ( ) => {
155+ //GIVEN
156+ const user1 = await givenResult ( 100 ) ;
157+ const user2 = await givenResult ( 75 ) ;
158+ const _user3 = await givenResult ( 50 ) ;
159+ const user4 = await givenResult ( 25 ) ;
160+ const _user5 = await givenResult ( 5 ) ;
161+
162+ //WHEN
163+ const results = await lb . getResults ( 1 , 2 , leaderboardsConfig , true , [
164+ user1 . uid ,
165+ user2 . uid ,
166+ user4 . uid ,
167+ new ObjectId ( ) . toHexString ( ) ,
115168 ] ) ;
169+
170+ //THEN
171+ expect ( results ) . toEqual ( {
172+ count : 3 ,
173+ entries : [ { rank : 4 , friendsRank : 3 , totalXp : 25 , ...user4 } ] ,
174+ } ) ;
175+ } ) ;
176+
177+ it ( "should return empty list if no friends" , async ( ) => {
178+ //GIVEN
179+
180+ //WHEN
181+ const results = await lb . getResults ( 0 , 5 , leaderboardsConfig , true , [ ] ) ;
182+
183+ //THEN
184+ expect ( results ) . toEqual ( {
185+ count : 0 ,
186+ entries : [ ] ,
187+ } ) ;
116188 } ) ;
117189 } ) ;
118190
@@ -127,18 +199,30 @@ describe("Weekly XP Leaderboards", () => {
127199 //THEN
128200 expect ( rank ) . toEqual ( { rank : 2 , totalXp : 100 , ...user1 } ) ;
129201 } ) ;
130- } ) ;
131202
132- describe ( "getCount" , ( ) => {
133- it ( "gets count" , async ( ) => {
203+ it ( "should return null for unknown user" , async ( ) => {
204+ expect ( await lb . getRank ( "decoy" , leaderboardsConfig ) ) . toBeNull ( ) ;
205+ expect (
206+ await lb . getRank ( "decoy" , leaderboardsConfig , [ "unknown" , "unknown2" ] )
207+ ) . toBeNull ( ) ;
208+ } ) ;
209+
210+ it ( "gets rank for friends" , async ( ) => {
134211 //GIVEN
135- await givenResult ( 100 ) ;
136- await givenResult ( 150 ) ;
212+ const user1 = await givenResult ( 50 ) ;
213+ const user2 = await givenResult ( 60 ) ;
214+ const _user3 = await givenResult ( 70 ) ;
137215
138- //WHEN
139- const count = await lb . getCount ( ) ;
140- //THEN
141- expect ( count ) . toEqual ( 2 ) ;
216+ const friends = [ user1 . uid , user2 . uid , "decoy" ] ;
217+
218+ //WHEN / THEN
219+ expect (
220+ await lb . getRank ( user2 . uid , leaderboardsConfig , friends )
221+ ) . toEqual ( { rank : 2 , friendsRank : 1 , totalXp : 60 , ...user2 } ) ;
222+
223+ expect (
224+ await lb . getRank ( user1 . uid , leaderboardsConfig , friends )
225+ ) . toEqual ( { rank : 3 , friendsRank : 2 , totalXp : 50 , ...user1 } ) ;
142226 } ) ;
143227 } ) ;
144228
@@ -154,9 +238,10 @@ describe("Weekly XP Leaderboards", () => {
154238 ) ;
155239 //THEN
156240 expect ( await lb . getRank ( cheater . uid , leaderboardsConfig ) ) . toBeNull ( ) ;
157- expect ( await lb . getResults ( 0 , 50 , leaderboardsConfig , true ) ) . toEqual ( [
158- { rank : 1 , totalXp : 1000 , ...validUser } ,
159- ] ) ;
241+ expect ( await lb . getResults ( 0 , 50 , leaderboardsConfig , true ) ) . toEqual ( {
242+ count : 1 ,
243+ entries : [ { rank : 1 , totalXp : 1000 , ...validUser } ] ,
244+ } ) ;
160245 } ) ;
161246
162247 async function givenResult (
0 commit comments