All files / xstate/test scxml.test.ts

100% Statements 37/37
100% Branches 8/8
100% Functions 11/11
100% Lines 34/34

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 1691x     1x 1x     1x   1x   1x         1x                                                                                                                           1x                             37x   39x     37x   99x   37x   59x 59x 3x   59x   59x   106x   59x         24x   12x       24x       24x     1x 1x     1x 16x   37x       37x       37x                   37x 37x         37x          
import { assert } from 'chai';
// import { Element as XMLElement } from 'xml-js';
 
import * as fs from 'fs';
import * as path from 'path';
// import * as util from 'util';
 
import { toMachine } from '../src/scxml';
import { StateNode } from '../src/StateNode';
import { interpret, SimulatedClock } from '../src/interpreter';
import { State } from '../src';
import { pathsToStateValue } from '../src/utils';
// import { StateValue } from '../src/types';
// import { Event, StateValue, ActionObject } from '../src/types';
// import { actionTypes } from '../src/actions';
 
const testGroups = {
  actionSend: [
    'send1',
    'send2',
    'send3',
    'send4',
    'send7',
    'send8'
    // 'send9' - edge case, since initial transitions in xstate are not microstepped
  ],
  'assign-current-small-step': ['test0', 'test1', 'test2', 'test3', 'test4'],
  basic: ['basic1', 'basic2'],
  'cond-js': ['test0', 'test1', 'test2', 'TestConditionalTransition'],
  data: [], // 4.0
  'default-initial-state': ['initial1', 'initial2'],
  delayedSend: ['send1', 'send2', 'send3'], // 4.0
  documentOrder: ['documentOrder0'],
  error: [], // not implemented
  forEach: [], // not implemented
  hierarchy: ['hier0', 'hier1', 'hier2'],
  'hierarchy+documentOrder': ['test0', 'test1'],
  history: [
    'history0',
    'history1',
    'history2',
    'history3'
    // 'history4'
    // 'history5'
    // 'history6'
  ],
  misc: ['deep-initial'],
  // 'more-parallel': [
  //   'test0',
  //   'test1',
  //   'test2',
  //   'test3',
  //   'test4',
  //   'test5',
  //   'test6',
  //   'test7',
  //   'test8',
  //   'test9',
  //   'test10'
  // ], // not well-formed tests
  parallel: [
    'test0',
    'test1'
 
    // TODO: add support for parallel states with leaf nodes,
    // e.g.: { foo: { bar: undefined, baz: undefined } }
    // 'test2',
    // 'test3'
  ],
  'targetless-transition': [
    'test0',
    'test1'
    // 'test2', // TODO: parallel states with leaf node support
    // 'test3', // TODO: parallel states with leaf node support
  ]
  // 'parallel+interrupt': ['test0']
};
 
const overrides = {
  'assign-current-small-step': ['test0'],
  'targetless-transition': ['test0']
};
 
interface SCIONTest {
  initialConfiguration: string[];
  events: Array<{
    after?: number;
    event: { name: string };
    nextConfiguration: string[];
  }>;
}
 
function runTestToCompletion(machine: StateNode, test: SCIONTest): void {
  let nextState: State<any> = machine.getInitialState(
    pathsToStateValue(
      test.initialConfiguration.map(id => machine.getStateNodeById(id).path)
    )
  );
  const interpreter = interpret(machine, {
    clock: new SimulatedClock()
  }).onTransition(state => (nextState = state));
 
  interpreter.start(nextState);
 
  test.events.forEach(({ event, nextConfiguration, after }, i) => {
    if (after) {
      (interpreter.clock as SimulatedClock).increment(after);
    }
    interpreter.send(event.name);
 
    const stateIds = machine
      .getStateNodes(nextState)
      .map(stateNode => stateNode.id);
 
    assert.include(stateIds, nextConfiguration[0], `run ${i}`);
  });
}
 
function evalCond(expr: string, context: object | undefined) {
  const literalKeyExprs = context
    ? Object.keys(context)
        .map(key => `const ${key} = xs['${key}'];`)
        .join('\n')
    : '';
 
  const fn = new Function(
    `const xs = arguments[0]; ${literalKeyExprs} return ${expr}`
  ) as () => boolean;
 
  return fn;
}
 
describe('scxml', () => {
  const testGroupKeys = Object.keys(testGroups);
  // const testGroupKeys = ['actionSend'];
 
  testGroupKeys.forEach(testGroupName => {
    testGroups[testGroupName].forEach(testName => {
      const scxmlSource =
        overrides[testGroupName] &&
        overrides[testGroupName].indexOf(testName) !== -1
          ? `./fixtures/scxml/${testGroupName}/${testName}.scxml`
          : `../node_modules/scxml-test-framework/test/${testGroupName}/${testName}.scxml`;
      const scxmlDefinition = fs.readFileSync(
        path.resolve(__dirname, scxmlSource),
        { encoding: 'utf-8' }
      );
      const scxmlTest = JSON.parse(
        fs.readFileSync(
          path.resolve(
            __dirname,
            `../node_modules/scxml-test-framework/test/${testGroupName}/${testName}.json`
          ),
          { encoding: 'utf-8' }
        )
      ) as SCIONTest;
 
      it(`${testGroupName}/${testName}`, () => {
        const machine = toMachine(scxmlDefinition, {
          evalCond,
          delimiter: '$'
        });
        // console.dir(machine.config, { depth: null });
        runTestToCompletion(machine, scxmlTest);
      });
    });
  });
});