All files / xstate/test multiple.test.ts

100% Statements 36/36
100% Branches 0/0
100% Functions 18/18
100% Lines 36/36

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 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 2021x 1x   1x 1x                                                                                                                                                                                                                                                                             1x 1x 1x 1x   1x 1x         1x 1x     1x 1x 1x     1x 1x 1x     1x 1x 1x       1x 1x 1x       1x 1x 1x       1x 1x 1x       1x 1x 1x       1x 1x 1x          
import { assert } from 'chai';
import { Machine } from '../src/index';
 
describe('multiple', () => {
  const machine = Machine({
    key: 'machine',
    initial: 'simple',
    states: {
      simple: {
        on: {
          DEEP_M: 'para.K.M',
          DEEP_CM: [{ target: ['para.A.C', 'para.K.M'] }],
          DEEP_MR: [{ target: ['para.K.M', 'para.P.R'] }],
          DEEP_CMR: [{ target: ['para.A.C', 'para.K.M', 'para.P.R'] }],
          BROKEN_SAME_REGION: [{ target: ['para.A.C', 'para.A.B'] }],
          BROKEN_DIFFERENT_REGIONS: [
            { target: ['para.A.C', 'para.K.M', 'other'] }
          ],
          BROKEN_DIFFERENT_REGIONS_2: [{ target: ['para.A.C', 'para2.K2.M2'] }],
          BROKEN_DIFFERENT_REGIONS_3: [
            { target: ['para2.K2.L2.L2A', 'other'] }
          ],
          BROKEN_DIFFERENT_REGIONS_4: [
            { target: ['para2.K2.L2.L2A.L2C', 'para2.K2.M2'] }
          ],
          INITIAL: 'para'
        }
      },
      other: {
        initial: 'X',
        states: {
          X: {}
        }
      },
      para: {
        parallel: true,
        states: {
          A: {
            initial: 'B',
            states: {
              B: {},
              C: {}
            }
          },
          K: {
            initial: 'L',
            states: {
              L: {},
              M: {}
            }
          },
          P: {
            initial: 'Q',
            states: {
              Q: {},
              R: {}
            }
          }
        }
      },
      para2: {
        parallel: true,
        states: {
          A2: {
            initial: 'B2',
            states: {
              B2: {},
              C2: {}
            }
          },
          K2: {
            initial: 'L2',
            states: {
              L2: {
                parallel: true,
                states: {
                  L2A: {
                    initial: 'L2B',
                    states: {
                      L2B: {},
                      L2C: {}
                    }
                  },
                  L2K: {
                    initial: 'L2L',
                    states: {
                      L2L: {},
                      L2M: {}
                    }
                  },
                  L2P: {
                    initial: 'L2Q',
                    states: {
                      L2Q: {},
                      L2R: {}
                    }
                  }
                }
              },
              M2: {
                parallel: true,
                states: {
                  M2A: {
                    initial: 'M2B',
                    states: {
                      M2B: {},
                      M2C: {}
                    }
                  },
                  M2K: {
                    initial: 'M2L',
                    states: {
                      M2L: {},
                      M2M: {}
                    }
                  },
                  M2P: {
                    initial: 'M2Q',
                    states: {
                      M2Q: {},
                      M2R: {}
                    }
                  }
                }
              }
            }
          },
          P2: {
            initial: 'Q2',
            states: {
              Q2: {},
              R2: {}
            }
          }
        }
      }
    }
  });
 
  describe('transitions to parallel states', () => {
    const stateSimple = machine.initialState;
    const stateInitial = machine.transition(stateSimple, 'INITIAL');
    const stateM = machine.transition(stateSimple, 'DEEP_M');
 
    it('should enter initial states of parallel states', () => {
      assert.deepEqual(stateInitial.value, {
        para: { A: 'B', K: 'L', P: 'Q' }
      });
    });
 
    it('should enter specific states in one region', () => {
      assert.deepEqual(stateM.value, { para: { A: 'B', K: 'M', P: 'Q' } });
    });
 
    it('should enter specific states in all regions', () => {
      const stateCMR = machine.transition(stateSimple, 'DEEP_CMR');
      assert.deepEqual(stateCMR.value, { para: { A: 'C', K: 'M', P: 'R' } });
    });
 
    it('should enter specific states in some regions', () => {
      const stateMR = machine.transition(stateSimple, 'DEEP_MR');
      assert.deepEqual(stateMR.value, { para: { A: 'B', K: 'M', P: 'R' } });
    });
 
    it('should reject two targets in the same region', () => {
      assert.throws(() =>
        machine.transition(stateSimple, 'BROKEN_SAME_REGION')
      );
    });
 
    it('should reject targets inside and outside a region', () => {
      assert.throws(() =>
        machine.transition(stateSimple, 'BROKEN_DIFFERENT_REGIONS')
      );
    });
 
    it('should reject two targets in different regions', () => {
      assert.throws(() =>
        machine.transition(stateSimple, 'BROKEN_DIFFERENT_REGIONS_2')
      );
    });
 
    it('should reject two targets in different regions at different levels', () => {
      assert.throws(() =>
        machine.transition(stateSimple, 'BROKEN_DIFFERENT_REGIONS_3')
      );
    });
 
    it('should reject two deep targets in different regions at top level', () => {
      assert.throws(() =>
        machine.transition(stateSimple, 'BROKEN_DIFFERENT_REGIONS_3')
      );
    });
 
    it('should reject two deep targets in different regions at different levels', () => {
      assert.throws(() =>
        machine.transition(stateSimple, 'BROKEN_DIFFERENT_REGIONS_4')
      );
    });
  });
});