Skip to content

Commit

Permalink
fix(AxisManager): fix moveTo method for zero position (#119)
Browse files Browse the repository at this point in the history
* fix(AxisManager): fix moveTo method for zero position
* test(InputObserver): test outside position and test bounce
  • Loading branch information
daybrush committed Feb 13, 2019
1 parent 52e8254 commit 8725391
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/AxisManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export class AxisManager {
}
moveTo(pos: Axis): { [key: string]: Axis } {
const delta = this.map(this._pos, (v, key) => {
return pos[key] ? pos[key] - this._pos[key] : 0;
return key in pos ? pos[key] - this._pos[key] : 0;
});

this.set(pos);
Expand Down
75 changes: 75 additions & 0 deletions test/unit/InputObserver.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {AxisManager} from "../../src/AxisManager";
import {InterruptManager} from "../../src/InterruptManager";
import {EventManager} from "../../src/EventManager";
import Component from "@egjs/component";
import { doesNotThrow } from "assert";

describe("InputObserver", function () {
describe("observer test", function() {
Expand Down Expand Up @@ -74,5 +75,79 @@ describe("InputObserver", function () {
// Then
expect(this.inst.get(inputType)).to.be.eql({"y": 200});
});
it("should check delta that there is no bounce and the position goes to zero.", done => {
// Given
const inputType = {
axes: ["y"]
};
// start pos
let y = 50;
this.axes.setTo({y: 50}, 0);
this.axes.on("change", ({pos, delta}) => {
// Then
// Find the value as approximated as possible due to floating decimal point problems.
expect(delta.y + y).to.be.closeTo(pos.y, 0.0000001);

y = pos.y;
});
this.axes.on("finish", () => {
done();
});

// When
// The last y position should be zero and neither should Delta.
this.axes.setTo({y: 0}, 300);
});
it("should check delta that there is no bounce and the position is out", done => {
// Given
const inputType = {
axes: ["y"]
};
// start pos
let y = 50;
this.axes.setTo({y: 50}, 0);
this.axes.on("change", ({pos, delta}) => {
// Then
// Find the value as approximated as possible due to floating decimal point problems.
expect(delta.y + 50).to.be.closeTo(pos.y, 0.0000001);
});
this.axes.on("finish", () => {
done();
});

// When

this.inst.hold(inputType);
// The last y position should be zero and neither should Delta.
// y goes to zero without bounce.
this.inst.change(inputType, {}, {y: -60});
this.inst.release(inputType);
});
it("should check delta that there is bounce and the position is out", done => {
// Given
const inputType = {
axes: ["x"]
};

// start pos
let x = 50;
this.axes.setTo({x: 50}, 0);
this.axes.on("change", ({pos, delta}) => {
// Then
// Find the value as approximated as possible due to floating decimal point problems.
expect(delta.x + x).to.be.closeTo(pos.x, 0.0000001);
x = pos.x;
});
this.axes.on("finish", () => {
done();
});

// When
this.inst.hold(inputType);
// x bounces by -10 and returns to zero.
this.inst.change(inputType, {}, {x: -60});
this.inst.release(inputType);
});

});
});

0 comments on commit 8725391

Please sign in to comment.