All files / xstate/test guards.test.ts

97.67% Statements 42/43
100% Branches 4/4
95.24% Functions 20/21
97.44% Lines 38/39

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 202 203 204 205 206 207 208 209 210 211 212 2131x 1x   1x         1x                   3x       2x         2x                                         2x         1x 1x             1x               1x 1x             1x 1x           1x 1x     1x 1x     1x 1x     1x     1x 1x     1x     1x 1x       1x 1x                                                                                       1x             1x                           2x                           1x 1x           1x 1x           1x 1x            
import { assert } from 'chai';
import { Machine, matchesState } from '../src/index';
 
describe('guard conditions', () => {
  // type LightMachineEvents =
  //   | { type: 'TIMER'; elapsed: number }
  //   | { type: 'EMERGENCY'; isEmergency: boolean };
 
  const lightMachine = Machine<{ elapsed: number }>(
    {
      key: 'light',
      initial: 'green',
      states: {
        green: {
          on: {
            TIMER: [
              {
                target: 'green',
                cond: ({ elapsed }) => elapsed < 100
              },
              {
                target: 'yellow',
                cond: ({ elapsed }) => elapsed >= 100 && elapsed < 200
              }
            ],
            EMERGENCY: {
              target: 'red',
              cond: (_, event) => event.isEmergency
            }
          }
        },
        yellow: {
          on: {
            TIMER: {
              target: 'red',
              cond: 'minTimeElapsed'
            }
          }
        },
        red: {
          on: {
            BAD_COND: { target: 'red', cond: 'doesNotExist' }
          }
        }
      }
    },
    {
      guards: {
        minTimeElapsed: ({ elapsed }) => elapsed >= 100 && elapsed < 200
      }
    }
  );
 
  it('should transition only if condition is met', () => {
    assert.equal(
      lightMachine.transition('green', 'TIMER', {
        elapsed: 50
      }).value,
      'green'
    );
 
    assert.deepEqual(
      lightMachine.transition('green', 'TIMER', {
        elapsed: 120
      }).value,
      'yellow'
    );
  });
 
  it('should transition if condition based on event is met', () => {
    assert.deepEqual(
      lightMachine.transition('green', { type: 'EMERGENCY', isEmergency: true })
        .value,
      'red'
    );
  });
 
  it('should not transition if condition based on event is not met', () => {
    assert.deepEqual(
      lightMachine.transition('green', { type: 'EMERGENCY' }).value,
      'green'
    );
  });
 
  it('should not transition if no condition is met', () => {
    const nextState = lightMachine.transition('green', 'TIMER', {
      elapsed: 9000
    });
    assert.deepEqual(nextState.value, 'green');
    assert.isEmpty(nextState.actions);
  });
 
  it('should work with defined string transitions', () => {
    const nextState = lightMachine.transition('yellow', 'TIMER', {
      elapsed: 150
    });
    assert.equal(nextState.value, 'red');
  });
 
  it('should work with defined string transitions (condition not met)', () => {
    const nextState = lightMachine.transition('yellow', 'TIMER', {
      elapsed: 10
    });
    assert.equal(nextState.value, 'yellow');
  });
 
  it('should throw if string transition is not defined', () => {
    assert.throws(() => lightMachine.transition('red', 'BAD_COND'));
  });
});
 
describe('guard conditions', () => {
  const machine = Machine({
    key: 'microsteps',
    parallel: true,
    states: {
      A: {
        initial: 'A0',
        states: {
          A0: {
            on: {
              A: 'A1'
            }
          },
          A1: {
            on: {
              A: 'A2'
            }
          },
          A2: {
            on: {
              A: 'A3'
            }
          },
          A3: {
            on: {
              '': 'A4'
            }
          },
          A4: {
            on: {
              '': 'A5'
            }
          },
          A5: {}
        }
      },
      B: {
        initial: 'B0',
        states: {
          B0: {
            on: {
              T1: [
                {
                  target: 'B1',
                  cond: (_state, _event, interim) =>
                    matchesState('A.A1', interim)
                }
              ],
              T2: [
                {
                  target: 'B2',
                  cond: (_state, _event, interim) =>
                    matchesState('A.A2', interim)
                }
              ],
              T3: [
                {
                  target: 'B3',
                  cond: (_state, _event, interim) =>
                    matchesState('A.A3', interim)
                }
              ],
              '': [
                {
                  target: 'B4',
                  cond: (_state, _event, interim) =>
                    matchesState('A.A4', interim)
                }
              ]
            }
          },
          B1: {},
          B2: {},
          B3: {},
          B4: {}
        }
      }
    }
  });
 
  it('should guard against transition', () => {
    assert.deepEqual(machine.transition({ A: 'A2', B: 'B0' }, 'T1').value, {
      A: 'A2',
      B: 'B0'
    });
  });
 
  it('should allow a matching transition', () => {
    assert.deepEqual(machine.transition({ A: 'A2', B: 'B0' }, 'T2').value, {
      A: 'A2',
      B: 'B2'
    });
  });
 
  it('should check guards with interim states', () => {
    assert.deepEqual(machine.transition({ A: 'A2', B: 'B0' }, 'A').value, {
      A: 'A5',
      B: 'B4'
    });
  });
});