All files / xstate/test stateTree.test.ts

93.33% Statements 28/30
100% Branches 0/0
90% Functions 9/10
92.86% Lines 26/28

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 1401x 1x 1x   1x                                                                                                                                                         1x 1x 1x 1x   1x     1x 1x   1x                 1x 1x 1x 1x   1x   1x 1x       1x                     1x 1x 1x   1x 2x       2x            
import { StateTree } from '../src/StateTree';
import { Machine } from '../src';
import { assert } from 'chai';
 
const testMachine = Machine({
  id: 'test',
  initial: 'a',
  states: {
    a: {
      initial: 'one',
      states: {
        one: {},
        two: {
          type: 'final'
        }
      }
    },
    b: {
      type: 'parallel',
      states: {
        one: {
          initial: 'foo',
          states: { foo: {}, bar: { type: 'final' } }
        },
        two: {
          initial: 'foo',
          states: {
            foo: {
              initial: 'x',
              states: {
                x: {}
              }
            },
            bar: { type: 'final' }
          }
        }
      }
    },
    c: {
      initial: 'one',
      states: {
        one: {
          initial: 'aa',
          states: {
            aa: {
              initial: 'foo',
              states: { foo: {} }
            },
            bb: {
              initial: 'foo',
              states: { foo: {} }
            },
            cc: {}
          }
        },
        two: {
          initial: 'aa',
          states: {
            aa: {},
            bb: {},
            cc: {}
          }
        },
        three: {
          type: 'parallel',
          states: {
            aa: {
              initial: 'aaa',
              states: { aaa: {}, bbb: {} }
            },
            bb: {
              initial: 'aaa',
              states: { aaa: {}, bbb: {} }
            }
          }
        }
      }
    }
  }
});
 
describe('StateTree', () => {
  describe('.resolved', () => {
    it('represents the full value (compound)', () => {
      const st = new StateTree(testMachine, 'a').resolved;
 
      assert.deepEqual(st.value, { a: 'one' });
    });
 
    it('represents the full value (parallel)', () => {
      const st = new StateTree(testMachine, 'b').resolved;
 
      assert.deepEqual(st.value, {
        b: {
          one: 'foo',
          two: { foo: 'x' }
        }
      });
    });
  });
 
  describe('.combine', () => {
    it('combines two state trees (compound)', () => {
      const st_c = new StateTree(testMachine, 'c');
      const st_c_two = new StateTree(testMachine, { c: 'two' });
 
      const combined = st_c.combine(st_c_two);
 
      assert.deepEqual(combined.value, { c: { two: {} } });
      assert.deepEqual(combined.resolved.value, { c: { two: 'aa' } });
    });
  });
 
  xit('represents the full value (parallel deep)', () => {
    const st = new StateTree(testMachine, { b: 'two' });
 
    assert.deepEqual(st.value, {
      b: {
        one: 'foo',
        two: { foo: 'x' }
      }
    });
  });
 
  it('getEntryExitStates() should show correct entry/exit state nodes', () => {
    const st_A = new StateTree(testMachine, 'c').resolved; // { c: { one: 'aa' }}
    const st_B = new StateTree(testMachine, { c: { one: 'bb' } }).resolved;
 
    const res = st_B.getEntryExitStates(st_A);
    assert.deepEqual([...res.exit].map(n => n.id), [
      'test.c.one.aa.foo',
      'test.c.one.aa'
    ]);
    assert.deepEqual([...res.entry].map(n => n.id), [
      'test.c.one.bb',
      'test.c.one.bb.foo'
    ]);
  });
});