Skip to content

Commit

Permalink
Fixing bug related to skier crash state
Browse files Browse the repository at this point in the history
  • Loading branch information
jeferson-amorim committed Jun 8, 2019
1 parent dfbad20 commit bec8548
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/Entities/Skier.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand All @@ -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);
}
}

Expand Down Expand Up @@ -115,4 +127,4 @@ export class Skier extends Entity {
this.setDirection(Constants.SKIER_DIRECTIONS.CRASH);
}
};
}
}
42 changes: 42 additions & 0 deletions src/Entities/Skier.test.js
Original file line number Diff line number Diff line change
@@ -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);
});
});
});

0 comments on commit bec8548

Please sign in to comment.