All files / xstate/test matchesState.test.ts

100% Statements 44/44
100% Branches 0/0
100% Functions 15/15
100% Lines 44/44

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 1161x 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         1x 1x       1x 1x 1x                                 1x 1x 1x      
import { assert } from 'chai';
import { matchesState, Machine } from '../src/index';
 
describe('matchesState()', () => {
  it('should return true if two states are equivalent', () => {
    assert.ok(matchesState('a', 'a'));
 
    assert.ok(matchesState('b.b1', 'b.b1'));
  });
 
  it('should return true if two state values are equivalent', () => {
    assert.ok(matchesState({ a: 'b' }, { a: 'b' }));
    assert.ok(matchesState({ a: { b: 'c' } }, { a: { b: 'c' } }));
  });
 
  it('should return true if two parallel states are equivalent', () => {
    assert.ok(
      matchesState(
        { a: { b1: 'foo', b2: 'bar' } },
        { a: { b1: 'foo', b2: 'bar' } }
      )
    );
 
    assert.ok(
      matchesState(
        { a: { b1: 'foo', b2: 'bar' }, b: { b3: 'baz', b4: 'quo' } },
        { a: { b1: 'foo', b2: 'bar' }, b: { b3: 'baz', b4: 'quo' } }
      )
    );
 
    assert.ok(matchesState({ a: 'foo', b: 'bar' }, { a: 'foo', b: 'bar' }));
  });
 
  it('should return true if a state is a substate of a superstate', () => {
    assert.ok(matchesState('b', 'b.b1'));
 
    assert.ok(matchesState('foo.bar', 'foo.bar.baz.quo'));
  });
 
  it('should return true if a state value is a substate of a superstate value', () => {
    assert.ok(matchesState('b', { b: 'b1' }));
 
    assert.ok(matchesState({ foo: 'bar' }, { foo: { bar: { baz: 'quo' } } }));
  });
 
  it('should return true if a parallel state value is a substate of a superstate value', () => {
    assert.ok(matchesState('b', { b: 'b1', c: 'c1' }));
 
    assert.ok(
      matchesState(
        { foo: 'bar', fooAgain: 'barAgain' },
        { foo: { bar: { baz: 'quo' } }, fooAgain: { barAgain: 'baz' } }
      )
    );
  });
 
  it('should return false if two states are not equivalent', () => {
    assert.ok(!matchesState('a', 'b'));
 
    assert.ok(!matchesState('a.a1', 'b.b1'));
  });
 
  it('should return false if parent state is more specific than child state', () => {
    assert.ok(!matchesState('a.b.c', 'a.b'));
 
    assert.ok(!matchesState({ a: { b: { c: 'd' } } }, { a: 'b' }));
  });
 
  it('should return false if two state values are not equivalent', () => {
    assert.ok(!matchesState({ a: 'a1' }, { b: 'b1' }));
  });
 
  it('should return false if a state is not a substate of a superstate', () => {
    assert.ok(!matchesState('a', 'b.b1'));
 
    assert.ok(!matchesState('foo.false.baz', 'foo.bar.baz.quo'));
  });
 
  it('should return false if a state value is not a substate of a superstate value', () => {
    assert.ok(!matchesState('a', { b: 'b1' }));
 
    assert.ok(
      !matchesState({ foo: { false: 'baz' } }, { foo: { bar: { baz: 'quo' } } })
    );
  });
 
  it('should mix/match string state values and object state values', () => {
    assert.ok(matchesState('a.b.c', { a: { b: 'c' } }));
  });
});
 
describe('matches() method', () => {
  it('should execute matchesState on a State given the parent state value', () => {
    const machine = Machine({
      initial: 'foo',
      states: {
        foo: {
          initial: 'bar',
          states: {
            bar: {
              initial: 'baz',
              states: {
                baz: {}
              }
            }
          }
        }
      }
    });
 
    assert.ok(machine.initialState.matches('foo'));
    assert.ok(machine.initialState.matches({ foo: 'bar' }));
    assert.notOk(machine.initialState.matches('fake'));
  });
});