From a7caf07ec3f97ca743164c79bd349590edddd390 Mon Sep 17 00:00:00 2001 From: cha0s Date: Sun, 8 Sep 2019 21:19:55 -0500 Subject: [PATCH] feat: Actions tests --- packages/behavior/item/actions.spec.js | 92 ++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) diff --git a/packages/behavior/item/actions.spec.js b/packages/behavior/item/actions.spec.js index e69de29..a3aa7a8 100644 --- a/packages/behavior/item/actions.spec.js +++ b/packages/behavior/item/actions.spec.js @@ -0,0 +1,92 @@ +import {expect} from 'chai'; + +import {TickingPromise} from '@avocado/core'; + +import {buildInvoke, buildTraversal, buildValue} from '../builders'; +import {Context} from '../context'; +import {Actions} from './actions'; +import './initialize' + +describe('behavior', () => { + describe('Actions', () => { + const context = new Context(); + beforeEach(() => { + context.clear(); + }); + it('may resolve immediately', (done) => { + const actions = new Actions(); + context.add('test', 69); + context.add('test2', 420); + actions.fromJSON({ + type: 'actions', + traversals: [ + buildTraversal(['test']), + buildTraversal(['test2']), + ], + }); + actions.on('actionsFinished', () => { + done(); + }) + actions.tick(context, 0); + expect(actions.index).to.equal(0); + }); + it('may defer', async () => { + const actions = new Actions(); + let resolve; + const promise = new Promise((_) => resolve = _); + context.add('test', promise); + context.add('test2', new Promise(() => {})); + actions.fromJSON({ + type: 'actions', + traversals: [ + buildTraversal(['test']), + buildTraversal(['test2']), + ], + }); + actions.tick(context, 0); + // Waiting? + expect(actions.index).to.equal(0); + resolve(); + return promise.finally(() => { + // Waiting on next... + expect(actions.index).to.equal(1); + }); + }); + it('may defer with ticking', async () => { + const actions = new Actions(); + let resolve; + let duration = 1; + const tickingPromise = new TickingPromise( + () => {}, + (elapsed, resolve) => { + duration -= elapsed; + if (duration <= 0) { + resolve(); + } + }, + ); + context.add('test', tickingPromise); + context.add('test2', new Promise(() => {})); + actions.fromJSON({ + type: 'actions', + traversals: [ + buildTraversal(['test']), + buildTraversal(['test2']), + ], + }); + actions.tick(context, 0); + // Waiting? + expect(actions.index).to.equal(0); + actions.tick(context, 0.5); + expect(actions.index).to.equal(0); + actions.tick(context, 0.5); + // Still gotta be waiting... + expect(actions.index).to.equal(0); + return tickingPromise.finally(() => { + // Waiting on next... + expect(duration).to.equal(0); + expect(actions.index).to.equal(1); + }); + }); + }); +});