123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323 |
- /**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- var App = require('app');
- require('utils/action_sequence');
- describe('App.actionSequence', function () {
- var actionSequence;
- beforeEach(function () {
- actionSequence = App.actionSequence.create();
- });
- describe('#setSequence', function () {
- var cases = [
- {
- sequenceIn: [{}, {}],
- sequenceOut: [{}, {}],
- title: 'array passed'
- },
- {
- sequenceIn: {
- '0': {},
- '1': {},
- 'length': 2
- },
- sequenceOut: [{}],
- title: 'array-like object passed'
- },
- {
- sequenceIn: 0,
- sequenceOut: [{}],
- title: 'primitive passed'
- }
- ],
- result;
- cases.forEach(function (item) {
- describe(item.title, function () {
- beforeEach(function () {
- actionSequence.set('sequence', [{}]);
- result = actionSequence.setSequence(item.sequenceIn);
- });
- it('should return context', function () {
- expect(result).to.eql(actionSequence);
- });
- it('sequence property', function () {
- expect(actionSequence.get('sequence')).to.eql(item.sequenceOut);
- });
- });
- });
- });
- describe('#start', function () {
- beforeEach(function () {
- actionSequence.setProperties({
- actionCounter: 0,
- sequence: [{}]
- });
- sinon.stub(actionSequence, 'runNextAction', Em.K);
- actionSequence.start();
- });
- afterEach(function () {
- actionSequence.runNextAction.restore();
- });
- it('should set the counter', function () {
- expect(actionSequence.get('actionCounter')).to.equal(1);
- });
- it('should start the sequence', function () {
- expect(actionSequence.runNextAction.calledOnce).to.be.true;
- });
- it('should call runNextAction with correct arguments', function () {
- expect(actionSequence.runNextAction.calledWith(0, null)).to.be.true;
- });
- });
- describe('#onFinish', function () {
- var cases = [
- {
- callbackIn: Em.isNone,
- callbackOut: Em.isNone,
- title: 'function passed'
- },
- {
- callbackIn: 'function () {}',
- callbackOut: Em.clb,
- title: 'array-like object passed'
- },
- {
- callbackIn: 'function () {}',
- callbackOut: Em.clb,
- title: 'primitive passed'
- }
- ],
- result;
- cases.forEach(function (item) {
- describe(item.title, function () {
- beforeEach(function () {
- actionSequence.set('finishedCallback', Em.clb);
- result = actionSequence.onFinish(item.callbackIn);
- });
- it('should return context', function () {
- expect(result).to.eql(actionSequence);
- });
- it('finishedCallback property', function () {
- expect(actionSequence.get('finishedCallback')).to.eql(item.callbackOut);
- });
- });
- });
- });
- describe('#runNextAction', function () {
- var actions = {
- callback: Em.K,
- sync: function (prevResponse) {
- actions.callback(prevResponse);
- return prevResponse;
- },
- async: function (prevResponse) {
- actions.callback(prevResponse);
- return {
- done: function (callback) {
- return callback.call(this, prevResponse);
- }
- };
- }
- },
- prevResponse = {},
- cases = [
- {
- index: 0,
- actionCounter: 0,
- sequence: [
- {
- callback: actions.sync,
- type: 'sync'
- }
- ],
- actionCallCount: 0,
- title: 'no iterations left (case 1)'
- },
- {
- index: 3,
- actionCounter: 3,
- sequence: [
- {
- callback: actions.sync,
- type: 'sync'
- },
- {
- callback: actions.sync,
- type: 'sync'
- },
- {
- callback: actions.sync,
- type: 'sync'
- }
- ],
- actionCallCount: 0,
- title: 'no iterations left (case 2)'
- },
- {
- index: 1,
- actionCounter: 3,
- sequence: [
- {
- callback: actions.sync,
- type: 'sync'
- },
- {
- callback: actions.sync,
- type: 'sync'
- },
- {
- callback: actions.sync,
- type: 'sync'
- }
- ],
- actionCallCount: 2,
- title: 'starting from the middle'
- },
- {
- index: 0,
- actionCounter: 2,
- sequence: [
- {
- callback: actions.sync,
- type: 'sync'
- },
- {
- callback: actions.sync,
- type: 'sync'
- },
- {
- callback: actions.sync,
- type: 'sync'
- }
- ],
- actionCallCount: 2,
- title: 'ending at the middle'
- },
- {
- index: 0,
- actionCounter: 3,
- sequence: [
- {
- callback: actions.sync,
- type: 'sync'
- },
- {
- callback: actions.sync,
- type: 'sync'
- },
- {
- callback: actions.sync,
- type: 'sync'
- }
- ],
- actionCallCount: 3,
- title: 'all iterations'
- },
- {
- index: 0,
- actionCounter: 3,
- sequence: [
- {
- callback: actions.sync,
- type: 'sync'
- },
- {
- callback: actions.async,
- type: 'async'
- },
- {
- callback: actions.sync,
- type: 'sync'
- }
- ],
- actionCallCount: 3,
- title: 'asynchronous action'
- }
- ];
- cases.forEach(function (item) {
- describe(item.title, function () {
- beforeEach(function () {
- sinon.spy(actions, 'callback');
- sinon.stub(actionSequence, 'finishedCallback', Em.K);
- actionSequence.setProperties({
- context: actionSequence,
- actionCounter: item.actionCounter,
- sequence: item.sequence
- });
- actionSequence.runNextAction(item.index, prevResponse);
- });
- afterEach(function () {
- actions.callback.restore();
- actionSequence.finishedCallback.restore();
- });
- it('number of calls', function () {
- expect(actions.callback.callCount).to.equal(item.actionCallCount);
- });
- if (item.actionCallCount) {
- it('argument passed to callback', function () {
- expect(actions.callback.alwaysCalledWith(prevResponse)).to.be.true;
- });
- }
- it('finish callback', function () {
- expect(actionSequence.finishedCallback.calledOnce).to.be.true;
- });
- });
- });
- });
- });
|