From bec8548e635902aca13b8b5d3df6a15b5d95c293 Mon Sep 17 00:00:00 2001 From: Jeferson Amorim Date: Sat, 8 Jun 2019 10:17:17 -0300 Subject: [PATCH] Fixing bug related to skier crash state --- src/Entities/Skier.js | 16 +++++++++++++-- src/Entities/Skier.test.js | 42 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 2 deletions(-) create mode 100644 src/Entities/Skier.test.js diff --git a/src/Entities/Skier.js b/src/Entities/Skier.js index 5074172..522c02b 100755 --- a/src/Entities/Skier.js +++ b/src/Entities/Skier.js @@ -61,7 +61,16 @@ export class Skier extends Entity { this.y -= Constants.SKIER_STARTING_SPEED; } + checkCrashState(direction) { + if ( this.direction === Constants.SKIER_DIRECTIONS.CRASH ) { + this.setDirection(direction); + } + } + turnLeft() { + + this.checkCrashState(Constants.SKIER_DIRECTIONS.LEFT); + if(this.direction === Constants.SKIER_DIRECTIONS.LEFT) { this.moveSkierLeft(); } @@ -71,11 +80,14 @@ export class Skier extends Entity { } turnRight() { + + this.checkCrashState(Constants.SKIER_DIRECTIONS.RIGHT); + if(this.direction === Constants.SKIER_DIRECTIONS.RIGHT) { this.moveSkierRight(); } else { - this.setDirection(this.direction + 1); + this.setDirection(this.direction + 1); } } @@ -115,4 +127,4 @@ export class Skier extends Entity { this.setDirection(Constants.SKIER_DIRECTIONS.CRASH); } }; -} \ No newline at end of file +} diff --git a/src/Entities/Skier.test.js b/src/Entities/Skier.test.js new file mode 100644 index 0000000..f7686d4 --- /dev/null +++ b/src/Entities/Skier.test.js @@ -0,0 +1,42 @@ +import "babel-polyfill"; +import * as Constants from "../Constants"; +import { Skier } from "./Skier"; + + +describe('when skier crashed', () => { + + let skier, fake; + + beforeEach(() => { + skier = new Skier(0, 0); + fake = jest.fn(); + skier.direction = Constants.SKIER_DIRECTIONS.CRASH; + }); + + describe('on left key stroke', () => { + + test('turn left should set direction as left', () => { + skier.turnLeft(); + expect(skier.direction).toBe(Constants.SKIER_DIRECTIONS.LEFT); + }); + + test('turn left should move skier to left side', () => { + skier.moveSkierLeft = fake; + skier.turnLeft(); + expect(fake.mock.calls.length).toBe(1); + }); + }) + + describe('on righ key stroke', () => { + test('turn righ should set direction as right', () => { + skier.turnRight(); + expect(skier.direction).toBe(Constants.SKIER_DIRECTIONS.RIGHT); + }); + + test('turn right should move skier to right side', () => { + skier.moveSkierRight = fake; + skier.turnRight(); + expect(fake.mock.calls.length).toBe(1); + }); + }); +});