All files / xstate/test stateIn.test.ts

95.45% Statements 21/22
100% Branches 0/0
85.71% Functions 6/7
95.45% Lines 21/22

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 2031x 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';
 
const machine = Machine({
  parallel: true,
  states: {
    a: {
      initial: 'a1',
      states: {
        a1: {
          on: {
            EVENT1: {
              target: 'a2',
              in: 'b.b2'
            },
            EVENT2: {
              target: 'a2',
              in: { b: 'b2' }
            }
          }
        },
        a2: {}
      }
    },
    b: {
      initial: 'b1',
      states: {
        b1: {
          on: {
            EVENT: {
              target: 'b2',
              in: 'a.a2'
            }
          }
        },
        b2: {
          parallel: true,
          states: {
            foo: {
              initial: 'foo1',
              states: {
                foo1: {
                  on: {
                    EVENT_DEEP: { target: 'foo2', in: 'bar.bar1' }
                  }
                },
                foo2: {}
              }
            },
            bar: {
              initial: 'bar1',
              states: {
                bar1: {},
                bar2: {}
              }
            }
          }
        }
      }
    }
  }
});
 
const lightMachine = Machine({
  id: 'light',
  initial: 'green',
  states: {
    green: { on: { TIMER: 'yellow' } },
    yellow: { on: { TIMER: 'red' } },
    red: {
      initial: 'walk',
      states: {
        walk: {},
        wait: {},
        stop: {}
      },
      on: {
        TIMER: [
          {
            target: 'green',
            in: { red: 'stop' }
          }
        ]
      }
    }
  }
});
 
describe('transition "in" check', () => {
  it('should transition if string state path matches current state value', () => {
    assert.deepEqual(
      machine.transition(
        {
          a: 'a1',
          b: {
            b2: {
              foo: 'foo2',
              bar: 'bar1'
            }
          }
        },
        'EVENT1'
      ).value,
      {
        a: 'a2',
        b: {
          b2: {
            foo: 'foo2',
            bar: 'bar1'
          }
        }
      }
    );
  });
 
  it('should not transition if string state path does not match current state value', () => {
    assert.deepEqual(machine.transition({ a: 'a1', b: 'b1' }, 'EVENT1').value, {
      a: 'a1',
      b: 'b1'
    });
  });
 
  it('should not transition if state value matches current state value', () => {
    assert.deepEqual(
      machine.transition(
        {
          a: 'a1',
          b: {
            b2: {
              foo: 'foo2',
              bar: 'bar1'
            }
          }
        },
        'EVENT2'
      ).value,
      {
        a: 'a2',
        b: {
          b2: {
            foo: 'foo2',
            bar: 'bar1'
          }
        }
      }
    );
  });
 
  xit('matching should be relative to grandparent (match)', () => {
    assert.deepEqual(
      machine.transition(
        { a: 'a1', b: { b2: { foo: 'foo1', bar: 'bar1' } } },
        'EVENT_DEEP'
      ).value,
      {
        a: 'a1',
        b: {
          b2: {
            foo: 'foo2',
            bar: 'bar1'
          }
        }
      }
    );
  });
 
  it('matching should be relative to grandparent (no match)', () => {
    assert.deepEqual(
      machine.transition(
        { a: 'a1', b: { b2: { foo: 'foo1', bar: 'bar2' } } },
        'EVENT_DEEP'
      ).value,
      {
        a: 'a1',
        b: {
          b2: {
            foo: 'foo1',
            bar: 'bar2'
          }
        }
      }
    );
  });
 
  it('should work to forbid events', () => {
    const walkState = lightMachine.transition('red.walk', 'TIMER');
 
    assert.deepEqual(walkState.value, { red: 'walk' });
 
    const waitState = lightMachine.transition('red.wait', 'TIMER');
 
    assert.deepEqual(waitState.value, { red: 'wait' });
 
    const stopState = lightMachine.transition('red.stop', 'TIMER');
 
    assert.deepEqual(
      stopState.value,
      'green',
      'Transition allowed due to "in" clause'
    );
  });
});