Skip to content

Commit

Permalink
GameEngine specs
Browse files Browse the repository at this point in the history
  • Loading branch information
tomdionysus committed Apr 16, 2020
1 parent 4d37324 commit 0a050f2
Show file tree
Hide file tree
Showing 2 changed files with 179 additions and 4 deletions.
8 changes: 4 additions & 4 deletions lib/GameEngine.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ class GameEngine {
* Mark the whole GameEngine to be redrawn.
*/
redraw() {
this.clear = true
this._doredraw = true

// Redraw All Scenes
this.redrawScenes()
Expand All @@ -227,7 +227,7 @@ class GameEngine {

this.redraw()

if(this.clear) {
if(this._doredraw) {
context.fillStyle = 'black'
context.fillRect(0, 0, this.element.width, this.element.height)
}
Expand All @@ -254,9 +254,9 @@ class GameEngine {
context.restore()

// HUD
if(this.showHUD && this.clear) { this._drawHUD(context) }
if(this.showHUD && this._doredraw) { this._drawHUD(context) }

this.clear = false
this._doredraw = false

if(this.running) window.requestAnimationFrame(this._tick.bind(this),0)
}
Expand Down
175 changes: 175 additions & 0 deletions spec/GameEngine_spec.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,177 @@
const GameEngine = require("../lib/GameEngine")

describe('Entity', () => {
it('should allow New', () => {
var x1 = new GameEngine()
var x2 = new GameEngine()

expect(x1).not.toBe(x2)
})

it('should have correct mixins', () => {
var x1 = new GameEngine()

expect(x1._events).not.toBeUndefined()
expect(x1._scenes).not.toBeUndefined()
expect(x1._entities).not.toBeUndefined()
})

it('should use option defaults', () => {
var options = {
targetId: 'targetId',
fullscreen: 'fullscreen',
showHUD: 'showHUD',
scale: 'scale',
x: 3,
y: 4,
maxScale: 5,
minScale: 6,
minX: 7,
minY: 8,
maxX: 9,
maxY: 10,
globalAlpha: 11,
enableScroll: 'enableScroll',
enableZoom: 'enableZoom'
}

var x1 = new GameEngine(options)

expect(x1.targetId).toEqual('targetId')
expect(x1.fullscreen).toBeTruthy()
expect(x1.showHUD).toBeTruthy()
expect(x1.scale).toEqual('scale')
expect(x1.x).toEqual(3)
expect(x1.y).toEqual(4)
expect(x1.maxScale).toEqual(5)
expect(x1.minScale).toEqual(6)
expect(x1.minX).toEqual(7)
expect(x1.minY).toEqual(8)
expect(x1.maxX).toEqual(9)
expect(x1.maxY).toEqual(10)
expect(x1.globalAlpha).toEqual(11)
expect(x1.enableScroll).toBeTruthy()
expect(x1.enableZoom).toBeTruthy()
})

it('should have correct events', () => {
var x1 = new GameEngine()

expect(Object(x1._events).keys).toEqual()
})

describe('setGlobalAlpha', () => {
it('should set globalAlpha', () => {
var x1 = new GameEngine()

x1.setGlobalAlpha(19)

expect(x1.globalAlpha).toEqual(19)
})
})

describe('init', () => {
it('should log debug and call callback', () => {
var x1 = new GameEngine()
var cb = generateSpyObject(['callback'])

spyOn(console,'debug')
x1.init(cb.callback)

expect(cb.callback).toHaveBeenCalledWith()
expect(console.debug).toHaveBeenCalledWith('init')
})
})

describe('stop', () => {
it('should set running to false', () => {
var x1 = new GameEngine()

x1.running = true
x1.stop()

expect(x1.running).toBeFalsy()
})
})

describe('addAsset', () => {
it('should set asset name and src', () => {
var x1 = new GameEngine()

x1.addAsset('asset','source')

expect(x1._assetDefs['asset']).toEqual('source')
})
})

describe('getAsset', () => {
it('should return asset with name', () => {
var x1 = new GameEngine()

x1.assets = {}
x1.assets['asset'] = 'source'

expect(x1.getAsset('asset')).toEqual('source')

})

it('should throw if asset not found', () => {
var x1 = new GameEngine()

x1.assets = {}
x1.assets['asset'] = 'source'

expect(()=>{x1.getAsset('asset2')}).toThrow('Asset not found: asset2')

})
})

describe('addAudio', () => {
it('should set audio name, type and src', () => {
var x1 = new GameEngine()

x1.addAudio('audio','source','type')

expect(x1._audioDefs['audio']).toEqual({ src: 'source', type: 'type' })
})
})

describe('getAudio', () => {
it('should return audio with name', () => {
var x1 = new GameEngine()

x1.audio = {}
x1.audio['audio'] = 'source'

expect(x1.getAudio('audio')).toEqual('source')

})

it('should throw if audio not found', () => {
var x1 = new GameEngine()

x1.audio = {}
x1.audio['audio'] = 'source'

expect(()=>{x1.getAudio('audio2')}).toThrow('Audio not found: audio2')

})
})

describe('redraw', () => {
it('should set _doredraw and call redrawScenes and redrawEntities', () => {
var x1 = new GameEngine()

spyOn(x1,'redrawScenes')
spyOn(x1,'redrawEntities')

x1.redraw()

expect(x1._doredraw).toBeTruthy()

expect(x1.redrawScenes).toHaveBeenCalledWith()
expect(x1.redrawEntities).toHaveBeenCalledWith()
})
})

})

0 comments on commit 0a050f2

Please sign in to comment.