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); }); }); }); });