|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const {test, threw} = require('tap') |
| 4 | +const Chain = require('../../../lib/chain.js') |
| 5 | +const actions = require('../../fixtures/actions/index.js') |
| 6 | + |
| 7 | +class ActionsChain extends Chain { |
| 8 | + constructor(state) { |
| 9 | + super(state, actions) |
| 10 | + this.count = 0 |
| 11 | + } |
| 12 | + $incr() { |
| 13 | + return ++this.count |
| 14 | + } |
| 15 | +} |
| 16 | + |
| 17 | +test('SetupChain.serial() as a builtin action', async (t) => { |
| 18 | + const chain = new ActionsChain() |
| 19 | + |
| 20 | + t.test('chain.serial is a function', async (t) => { |
| 21 | + t.type(chain.serial, 'function', 'serial is a function') |
| 22 | + }) |
| 23 | + |
| 24 | + t.test('Success; Call serial for an existing action', async (t) => { |
| 25 | + const state = await chain |
| 26 | + .serial(5, 'delay', {value: '!template("Bodhi {{!incr}}")'}, 'all_names') |
| 27 | + .execute() |
| 28 | + |
| 29 | + const expected = ['Bodhi 1', 'Bodhi 2', 'Bodhi 3', 'Bodhi 4', 'Bodhi 5'] |
| 30 | + t.same(state.all_names, expected, 'Expected result in state') |
| 31 | + t.same(chain.state.all_names, expected, 'Expected result in chain.state') |
| 32 | + }) |
| 33 | + |
| 34 | + t.test('Success; Call serial using a lookup', async (t) => { |
| 35 | + const state = await chain |
| 36 | + .serial(2, 'greet', {names: '#all_names', greeting: 'Hi'}, 'all_greetings') |
| 37 | + .execute() |
| 38 | + |
| 39 | + const greetings = [ |
| 40 | + 'Hi Bodhi 1' |
| 41 | + , 'Hi Bodhi 2' |
| 42 | + , 'Hi Bodhi 3' |
| 43 | + , 'Hi Bodhi 4' |
| 44 | + , 'Hi Bodhi 5' |
| 45 | + ] |
| 46 | + const expected = [greetings, greetings] |
| 47 | + t.same(state.all_greetings, expected, 'Expected result in state') |
| 48 | + t.same(chain.state.all_greetings, expected, 'Result in chain.state') |
| 49 | + }) |
| 50 | + |
| 51 | + t.test('Error: times is not a number', async (t) => { |
| 52 | + const msg = new RegExp( |
| 53 | + 'requires \'times\' to be an integer and greater than or equal to 0' |
| 54 | + ) |
| 55 | + |
| 56 | + t.rejects(chain.serial().execute(), { |
| 57 | + err: msg |
| 58 | + }, 'No parameters') |
| 59 | + |
| 60 | + t.rejects(chain.serial(null).execute(), { |
| 61 | + err: msg |
| 62 | + }, 'times is null') |
| 63 | + |
| 64 | + t.rejects(chain.serial({}).execute(), { |
| 65 | + err: msg |
| 66 | + }, 'times is an object') |
| 67 | + |
| 68 | + t.rejects(chain.serial('ten').execute(), { |
| 69 | + err: msg |
| 70 | + }, 'times is a string') |
| 71 | + |
| 72 | + t.rejects(chain.serial(-1).execute(), { |
| 73 | + err: msg |
| 74 | + }, 'times is a a negetive number') |
| 75 | + |
| 76 | + t.rejects(chain.serial(1.1).execute(), { |
| 77 | + err: msg |
| 78 | + }, 'times is a decimal') |
| 79 | + |
| 80 | + t.rejects(chain.serial(Number.POSITIVE_INFINITY).execute(), { |
| 81 | + err: msg |
| 82 | + }, 'times is a infinate') |
| 83 | + |
| 84 | + t.rejects(chain.serial(NaN).execute(), { |
| 85 | + err: msg |
| 86 | + }, 'times is NaN') |
| 87 | + }) |
| 88 | + |
| 89 | + t.test('Error: action is not a function', async (t) => { |
| 90 | + const msg = new RegExp('\'NOPE\' does not exist as a chain action') |
| 91 | + t.rejects(chain.serial(10, 'NOPE').execute(), { |
| 92 | + err: msg |
| 93 | + }, 'Unknown action') |
| 94 | + }) |
| 95 | +}).catch(threw) |
0 commit comments