diff --git a/src/app/app.module.ts b/src/app/app.module.ts index dbc83a18..61ab03f9 100644 --- a/src/app/app.module.ts +++ b/src/app/app.module.ts @@ -18,6 +18,7 @@ import { ScoreComponent } from './score/score.component'; import { SigninComponent } from './signin/signin.component'; import { LoginComponent } from './login/login.component'; import { GameComponent } from './game/game.component'; +import { LeaderListComponent } from './leader-list/leader-list.component'; const appRoutes: Routes = [ {path: 'login', component: LoginComponent }, @@ -28,9 +29,6 @@ const appRoutes: Routes = [ {path: '', redirectTo: 'login', pathMatch: 'full'} ]; -import { LeaderListComponent } from './leader-list/leader-list.component'; - - @NgModule({ declarations: [ AppComponent, diff --git a/src/app/game/game.component.spec.ts b/src/app/game/game.component.spec.ts index 53c44710..bd3e9341 100644 --- a/src/app/game/game.component.spec.ts +++ b/src/app/game/game.component.spec.ts @@ -1,8 +1,8 @@ import { async, tick, ComponentFixture, TestBed, fakeAsync } from '@angular/core/testing'; import { Location } from "@angular/common"; -import { RouterTestingModule } from "@angular/router/testing"; import { Router } from "@angular/router"; import { Routes, RouterModule } from '@angular/router'; +import {APP_BASE_HREF} from '@angular/common'; import { AppComponent } from '../app.component'; import {KothMaterialModule} from '../koth-material/koth-material.module' import { AuthService } from '../auth.service'; @@ -19,9 +19,12 @@ import * as firebase from 'firebase/app'; import { FormsModule } from '@angular/forms'; describe('GameComponent', () => { + const appRoutes: Routes = [ + { path: 'game', component: GameComponent } + ]; let component: GameComponent; let fixture: ComponentFixture; - + beforeEach(async(() => { const firebaseConfig = { @@ -34,19 +37,21 @@ describe('GameComponent', () => { } TestBed.configureTestingModule({ imports: [ KothMaterialModule, + RouterModule.forRoot(appRoutes, {enableTracing: true}), AngularFireModule.initializeApp(firebaseConfig), AngularFireAuthModule, AngularFireDatabaseModule, RouterModule, FormsModule, ], declarations: [ GameComponent ], - providers: [AuthService, AngularFireAuth] + providers: [AuthService, AngularFireAuth, {provide: APP_BASE_HREF, useValue : '/' }], }).compileComponents(); })); beforeEach(() => { fixture = TestBed.createComponent(GameComponent); component = fixture.componentInstance; + component.currentUser = "pedro@pedro.com"; fixture.detectChanges(); }); @@ -63,15 +68,15 @@ describe('GameComponent', () => { beforeEach(function(){ player = new Player(); - + }); - + describe('when you are logged', function(){ - + beforeEach(function(){ // player.logout(); }); - + it("player after logout is null", function(){ expect(player).toBeNull; }); @@ -85,15 +90,15 @@ describe('GameComponent', () => { // it("is the King", function(){ // const leaderInfoDe = fixture.debugElement.query(By.css('.leaderInfo')); // const bgColor = leaderInfoDe.nativeElement.style.backgroundColor; - // expect(bgColor).toBe('green'); + // expect(bgColor).toBe('green'); // }); - + // it("is not the King", function(){ // const leaderInfoDe = fixture.debugElement.query(By.css('.container-game .leaderInfo')); // const bgColor = leaderInfoDe.nativeElement.style.backgroundColor; - // expect(bgColor).toBe('red'); + // expect(bgColor).toBe('red'); // }); - + }); // Este test nunca lo pasa porque el botón al comienzo esta sin pulsar siempre. // it("Has clicked Play on button", function(){ @@ -106,15 +111,14 @@ describe('GameComponent', () => { // expect(player.getTime()).toBeGreaterThan(0); // expect(playButtonEl.hasAttribute('disabled')).toEqual(true); // }); - + it("Has not clicked Play on button", function(){ player.setTime(0); player.setGameStatus(false); expect(player.getGameStatus()).toBeFalsy(); expect(player.getTime()).toEqual(0); - }); - + }); + }); }); - diff --git a/src/app/game/game.component.ts b/src/app/game/game.component.ts index 36c37058..4cdcdcad 100644 --- a/src/app/game/game.component.ts +++ b/src/app/game/game.component.ts @@ -37,7 +37,7 @@ export class GameComponent implements OnInit { } ngOnInit() { - this.currentUser = this.user.currentUser.email; + this.currentUser = this.user.currentUser; this.maxScore = this.database.list('/games', ref => ref.orderByChild('score').limitToLast(1)).valueChanges(); this.score = this.maxScore.score; // this.maxScore.subscribe(item => { diff --git a/src/app/score/score.component.html b/src/app/score/score.component.html index bf09444e..1513cf01 100644 --- a/src/app/score/score.component.html +++ b/src/app/score/score.component.html @@ -1,10 +1,10 @@

Score

-

Your score is: {{score}}

-

Congratulations, you have beaten your record: {{score}}

+

Your score is: {{score}}

+

Congratulations, you have beaten your record: {{score}}

Position on Ranking: {{rank}}

Your current record is: {{record}}

- + diff --git a/src/app/score/score.component.spec.ts b/src/app/score/score.component.spec.ts index 5addc557..80de1354 100644 --- a/src/app/score/score.component.spec.ts +++ b/src/app/score/score.component.spec.ts @@ -9,23 +9,28 @@ import { AngularFireAuthModule } from 'angularfire2/auth'; import { AngularFireDatabaseModule } from 'angularfire2/database'; import {KothMaterialModule} from '../koth-material/koth-material.module' import { Routes, RouterModule } from '@angular/router'; +import {APP_BASE_HREF} from '@angular/common'; import * as firebase from 'firebase/app'; import { environment } from '../../environments/environment'; describe('ScoreComponent', () => { + const appRoutes: Routes = [ + { path: 'score', component: ScoreComponent } + ]; let component: ScoreComponent; let fixture: ComponentFixture; beforeEach(async(() => { TestBed.configureTestingModule({ imports: [ KothMaterialModule, + RouterModule.forRoot(appRoutes, {enableTracing: true}), AngularFireModule.initializeApp(environment.firebase, 'my-app-name'),AngularFireAuthModule, AngularFireDatabaseModule, RouterModule ], declarations: [ ScoreComponent ], - providers: [AuthService,AngularFireAuth], + providers: [AuthService, AngularFireAuth, {provide: APP_BASE_HREF, useValue : '/' }], }) .compileComponents(); })); @@ -33,6 +38,7 @@ describe('ScoreComponent', () => { beforeEach(() => { fixture = TestBed.createComponent(ScoreComponent); component = fixture.componentInstance; + component.currentUser = "pedro@pedro.com"; fixture.detectChanges(); }); @@ -49,6 +55,7 @@ describe('ScoreComponent', () => { it('should display score', () => { component.record = 2; component.score = 0; + component.rank = 2; fixture.detectChanges(); const scoreText = fixture.debugElement.query(By.css('p')); const el = scoreText.nativeElement; @@ -58,6 +65,7 @@ describe('ScoreComponent', () => { it('should congrat you if you beat your record', () => { component.record = 0; component.score = 2; + component.rank = 2; fixture.detectChanges(); const scoreText = fixture.debugElement.query(By.css('p')); const el = scoreText.nativeElement; @@ -71,6 +79,7 @@ describe('ScoreComponent', () => { // el.map(x => expect(x.textContent).toEqual(`Your score is: ${component.score}`)); component.record = 0; component.score = 2; + component.rank = 1; fixture.detectChanges(); const rankingText = fixture.debugElement.queryAll(By.css('p'))[1]; const el = rankingText.nativeElement; @@ -80,15 +89,10 @@ describe('ScoreComponent', () => { it('should display record', () => { component.record = 0; component.score = 2; + component.rank = 1; fixture.detectChanges(); const recordText = fixture.debugElement.queryAll(By.css('p'))[2]; const el = recordText.nativeElement; expect(el.textContent).toEqual(`Your current record is: ${component.record}`); }); - - it('should have a button with name Retry', () => { - const retryBtn = fixture.debugElement.query(By.css('button')); - const el = retryBtn.nativeElement; - expect(el.textContent).toEqual('Retry'); - }); }); diff --git a/src/app/score/score.component.ts b/src/app/score/score.component.ts index 98f1e97e..48af51e5 100644 --- a/src/app/score/score.component.ts +++ b/src/app/score/score.component.ts @@ -1,8 +1,8 @@ -import { Component, OnInit } from '@angular/core'; +import { Component, OnInit, NgZone } from '@angular/core'; import { AuthService } from '../auth.service'; import { AngularFireDatabase, AngularFireList } from 'angularfire2/database'; import { Observable } from 'rxjs/Observable'; - +import { Router} from '@angular/router'; @Component({ selector: 'app-score', @@ -15,37 +15,38 @@ export class ScoreComponent implements OnInit { score: number; rank: number; record: number; + games: any; user_info: any; last_user_game: any; data : Observable; JSON = JSON; - + currentUser:any; user = null; - constructor(private auth: AuthService, public db: AngularFireDatabase) { - this.score = 0; - this.rank = 0; - this.record = 0; + constructor(private auth: AuthService, public db: AngularFireDatabase, private route: Router, private zone: NgZone) { + this.games = this.db.list('/games', ref => ref.orderByChild('score')).valueChanges(); } -ngOnInit() { - this.last_user_game = this.db.list('/games', ref => ref.orderByChild('email').equalTo('javier@test.com').limitToLast(1)).valueChanges(); - this.score = this.last_user_game.score; - this.last_user_game.subscribe(item => { - this.score = item[0].score; - }) - - this.user_info = this.db.list('/', ref => ref.orderByChild('email').equalTo('javier@test.com')).valueChanges(); - this.user_info.subscribe(item => { - item.map(x => { - if (x.highscore > this.record){ - this.record = x.highscore; - } - if (x.highrank > this.rank){ - this.rank = x.highrank; - } - }); - }); + ngOnInit() { + this.currentUser = this.auth.currentUser; + this.last_user_game = this.db.list('/games', ref => ref.orderByChild('email').equalTo(this.currentUser).limitToLast(1)).valueChanges(); + this.last_user_game.subscribe((score) => { + this.score = score[0].score; + }); + this.games.subscribe((game) => { + game = game.reverse(); + for (var i = 0; i < game.length; i++){ + if (game[i].email == this.currentUser){ + this.rank = i + 1; + this.record = game[i].score; + break; + } + } + }) + } + + onPressRetry(){ + this.route.navigate(['/game']); } } diff --git a/src/app/signin/signin.component.spec.ts b/src/app/signin/signin.component.spec.ts index 2ec9999f..c61fd88e 100644 --- a/src/app/signin/signin.component.spec.ts +++ b/src/app/signin/signin.component.spec.ts @@ -51,7 +51,7 @@ describe('SigninComponent', () => { }); it('User name should be an email', () => { - userT = 'Pilarica24@gmail.com'; + userT = 'dani.sanjuan@gmail.com'; fixture.detectChanges(); expect(userT).toMatch('^[a-zA-Z0-9_]+([\.-]?[a-zA-Z0-9_]+)*@[a-zA-Z0-9_]+([\.-]?[a-zA-Z0-9_]+)*(\.[a-zA-Z0-9_]{2,4})+$'); });