add MockSkinPool for testing
This commit is contained in:
35
test/fixtures/MockSkinPool.js
vendored
Normal file
35
test/fixtures/MockSkinPool.js
vendored
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
const Skin = require('../../src/Skin');
|
||||||
|
|
||||||
|
class MockSkinPool {
|
||||||
|
constructor () {
|
||||||
|
this._allDrawables = [];
|
||||||
|
}
|
||||||
|
|
||||||
|
static forDrawableSkin (drawable) {
|
||||||
|
const pool = new MockSkinPool();
|
||||||
|
pool.addDrawable(drawable);
|
||||||
|
pool.addSkin(drawable.skin);
|
||||||
|
return pool;
|
||||||
|
}
|
||||||
|
|
||||||
|
_skinWasAltered (skin) {
|
||||||
|
for (let i = 0; i < this._allDrawables.length; i++) {
|
||||||
|
const drawable = this._allDrawables[i];
|
||||||
|
if (drawable && drawable._skin === skin) {
|
||||||
|
drawable._skinWasAltered();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
addDrawable (drawable) {
|
||||||
|
this._allDrawables.push(drawable);
|
||||||
|
return drawable;
|
||||||
|
}
|
||||||
|
|
||||||
|
addSkin (skin) {
|
||||||
|
skin.addListener(Skin.Events.WasAltered, this._skinWasAltered.bind(this, skin));
|
||||||
|
return skin;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = MockSkinPool;
|
||||||
@@ -8,6 +8,7 @@ global.document = {
|
|||||||
|
|
||||||
const Drawable = require('../../src/Drawable');
|
const Drawable = require('../../src/Drawable');
|
||||||
const MockSkin = require('../fixtures/MockSkin');
|
const MockSkin = require('../fixtures/MockSkin');
|
||||||
|
const MockSkinPool = require('../fixtures/MockSkinPool');
|
||||||
const Rectangle = require('../../src/Rectangle');
|
const Rectangle = require('../../src/Rectangle');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -31,6 +32,7 @@ test('translate by position', t => {
|
|||||||
const drawable = new Drawable();
|
const drawable = new Drawable();
|
||||||
drawable.skin = new MockSkin();
|
drawable.skin = new MockSkin();
|
||||||
drawable.skin.size = [200, 50];
|
drawable.skin.size = [200, 50];
|
||||||
|
MockSkinPool.forDrawableSkin(drawable);
|
||||||
|
|
||||||
expected.initFromBounds(0, 200, -50, 0);
|
expected.initFromBounds(0, 200, -50, 0);
|
||||||
t.same(snapToNearest(drawable.getAABB()), expected);
|
t.same(snapToNearest(drawable.getAABB()), expected);
|
||||||
@@ -47,6 +49,7 @@ test('translate by costume center', t => {
|
|||||||
const drawable = new Drawable();
|
const drawable = new Drawable();
|
||||||
drawable.skin = new MockSkin();
|
drawable.skin = new MockSkin();
|
||||||
drawable.skin.size = [200, 50];
|
drawable.skin.size = [200, 50];
|
||||||
|
MockSkinPool.forDrawableSkin(drawable);
|
||||||
|
|
||||||
drawable.skin.setRotationCenter(1, 0);
|
drawable.skin.setRotationCenter(1, 0);
|
||||||
expected.initFromBounds(-1, 199, -50, 0);
|
expected.initFromBounds(-1, 199, -50, 0);
|
||||||
@@ -64,6 +67,7 @@ test('translate and rotate', t => {
|
|||||||
const drawable = new Drawable();
|
const drawable = new Drawable();
|
||||||
drawable.skin = new MockSkin();
|
drawable.skin = new MockSkin();
|
||||||
drawable.skin.size = [200, 50];
|
drawable.skin.size = [200, 50];
|
||||||
|
MockSkinPool.forDrawableSkin(drawable);
|
||||||
|
|
||||||
drawable.updateProperties({position: [1, 2], direction: 0});
|
drawable.updateProperties({position: [1, 2], direction: 0});
|
||||||
expected.initFromBounds(1, 51, 2, 202);
|
expected.initFromBounds(1, 51, 2, 202);
|
||||||
@@ -90,6 +94,7 @@ test('rotate by non-right-angles', t => {
|
|||||||
drawable.skin = new MockSkin();
|
drawable.skin = new MockSkin();
|
||||||
drawable.skin.size = [10, 10];
|
drawable.skin.size = [10, 10];
|
||||||
drawable.skin.setRotationCenter(5, 5);
|
drawable.skin.setRotationCenter(5, 5);
|
||||||
|
MockSkinPool.forDrawableSkin(drawable);
|
||||||
|
|
||||||
expected.initFromBounds(-5, 5, -5, 5);
|
expected.initFromBounds(-5, 5, -5, 5);
|
||||||
t.same(snapToNearest(drawable.getAABB()), expected);
|
t.same(snapToNearest(drawable.getAABB()), expected);
|
||||||
@@ -106,6 +111,7 @@ test('scale', t => {
|
|||||||
const drawable = new Drawable();
|
const drawable = new Drawable();
|
||||||
drawable.skin = new MockSkin();
|
drawable.skin = new MockSkin();
|
||||||
drawable.skin.size = [200, 50];
|
drawable.skin.size = [200, 50];
|
||||||
|
MockSkinPool.forDrawableSkin(drawable);
|
||||||
|
|
||||||
drawable.updateProperties({scale: [100, 50]});
|
drawable.updateProperties({scale: [100, 50]});
|
||||||
expected.initFromBounds(0, 200, -25, 0);
|
expected.initFromBounds(0, 200, -25, 0);
|
||||||
@@ -128,6 +134,7 @@ test('rotate and scale', t => {
|
|||||||
const drawable = new Drawable();
|
const drawable = new Drawable();
|
||||||
drawable.skin = new MockSkin();
|
drawable.skin = new MockSkin();
|
||||||
drawable.skin.size = [100, 1000];
|
drawable.skin.size = [100, 1000];
|
||||||
|
MockSkinPool.forDrawableSkin(drawable);
|
||||||
|
|
||||||
drawable.skin.setRotationCenter(50, 50);
|
drawable.skin.setRotationCenter(50, 50);
|
||||||
expected.initFromBounds(-50, 50, -950, 50);
|
expected.initFromBounds(-50, 50, -950, 50);
|
||||||
|
|||||||
Reference in New Issue
Block a user