Welcome to mirror list, hosted at ThFree Co, Russian Federation.

finite_state_machine_spec.js « utils « lib « frontend « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: cfde3b8596ee93de1401e210ef8e1924a60e5656 (plain)
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
import { machine, transition } from '~/lib/utils/finite_state_machine';

describe('Finite State Machine', () => {
  const STATE_IDLE = 'idle';
  const STATE_LOADING = 'loading';
  const STATE_ERRORED = 'errored';

  const TRANSITION_START_LOAD = 'START_LOAD';
  const TRANSITION_LOAD_ERROR = 'LOAD_ERROR';
  const TRANSITION_LOAD_SUCCESS = 'LOAD_SUCCESS';
  const TRANSITION_ACKNOWLEDGE_ERROR = 'ACKNOWLEDGE_ERROR';

  const definition = {
    initial: STATE_IDLE,
    states: {
      [STATE_IDLE]: {
        on: {
          [TRANSITION_START_LOAD]: STATE_LOADING,
        },
      },
      [STATE_LOADING]: {
        on: {
          [TRANSITION_LOAD_ERROR]: STATE_ERRORED,
          [TRANSITION_LOAD_SUCCESS]: STATE_IDLE,
        },
      },
      [STATE_ERRORED]: {
        on: {
          [TRANSITION_ACKNOWLEDGE_ERROR]: STATE_IDLE,
          [TRANSITION_START_LOAD]: STATE_LOADING,
        },
      },
    },
  };

  describe('machine', () => {
    const STATE_IMPOSSIBLE = 'impossible';
    const badDefinition = {
      init: definition.initial,
      badKeyShouldBeStates: definition.states,
    };
    const unstartableDefinition = {
      initial: STATE_IMPOSSIBLE,
      states: definition.states,
    };
    let liveMachine;

    beforeEach(() => {
      liveMachine = machine(definition);
    });

    it('throws an error if the machine definition is invalid', () => {
      expect(() => machine(badDefinition)).toThrow(
        'A state machine must have an initial state (`.initial`) and a dictionary of possible states (`.states`)',
      );
    });

    it('throws an error if the initial state is invalid', () => {
      expect(() => machine(unstartableDefinition)).toThrow(
        `Cannot initialize the state machine to state '${STATE_IMPOSSIBLE}'. Is that one of the machine's defined states?`,
      );
    });

    it.each`
      partOfMachine | equals                               | description            | eqDescription
      ${'keys'}     | ${['is', 'send', 'value', 'states']} | ${'keys'}              | ${'the correct array'}
      ${'is'}       | ${expect.any(Function)}              | ${'`is` property'}     | ${'a function'}
      ${'send'}     | ${expect.any(Function)}              | ${'`send` property'}   | ${'a function'}
      ${'value'}    | ${definition.initial}                | ${'`value` property'}  | ${'the same as the `initial` value of the machine definition'}
      ${'states'}   | ${definition.states}                 | ${'`states` property'} | ${'the same as the `states` value of the machine definition'}
    `("The machine's $description should be $eqDescription", ({ partOfMachine, equals }) => {
      const test = partOfMachine === 'keys' ? Object.keys(liveMachine) : liveMachine[partOfMachine];

      expect(test).toEqual(equals);
    });

    it.each`
      initialState          | transitionEvent                 | expectedState
      ${definition.initial} | ${TRANSITION_START_LOAD}        | ${STATE_LOADING}
      ${STATE_LOADING}      | ${TRANSITION_LOAD_ERROR}        | ${STATE_ERRORED}
      ${STATE_ERRORED}      | ${TRANSITION_ACKNOWLEDGE_ERROR} | ${STATE_IDLE}
      ${STATE_IDLE}         | ${TRANSITION_START_LOAD}        | ${STATE_LOADING}
      ${STATE_LOADING}      | ${TRANSITION_LOAD_SUCCESS}      | ${STATE_IDLE}
    `(
      'properly steps from $initialState to $expectedState when the event "$transitionEvent" is sent',
      ({ initialState, transitionEvent, expectedState }) => {
        liveMachine.value = initialState;

        liveMachine.send(transitionEvent);

        expect(liveMachine.is(expectedState)).toBe(true);
        expect(liveMachine.value).toBe(expectedState);
      },
    );

    it.each`
      initialState     | transitionEvent
      ${STATE_IDLE}    | ${TRANSITION_ACKNOWLEDGE_ERROR}
      ${STATE_IDLE}    | ${TRANSITION_LOAD_SUCCESS}
      ${STATE_IDLE}    | ${TRANSITION_LOAD_ERROR}
      ${STATE_IDLE}    | ${'RANDOM_FOO'}
      ${STATE_LOADING} | ${TRANSITION_START_LOAD}
      ${STATE_LOADING} | ${TRANSITION_ACKNOWLEDGE_ERROR}
      ${STATE_LOADING} | ${'RANDOM_FOO'}
      ${STATE_ERRORED} | ${TRANSITION_LOAD_ERROR}
      ${STATE_ERRORED} | ${TRANSITION_LOAD_SUCCESS}
      ${STATE_ERRORED} | ${'RANDOM_FOO'}
    `(
      `does not perform any transition if the machine can't move from "$initialState" using the "$transitionEvent" event`,
      ({ initialState, transitionEvent }) => {
        liveMachine.value = initialState;

        liveMachine.send(transitionEvent);

        expect(liveMachine.is(initialState)).toBe(true);
        expect(liveMachine.value).toBe(initialState);
      },
    );

    describe('send', () => {
      it.each`
        startState       | transitionEvent                 | result
        ${STATE_IDLE}    | ${TRANSITION_START_LOAD}        | ${STATE_LOADING}
        ${STATE_LOADING} | ${TRANSITION_LOAD_SUCCESS}      | ${STATE_IDLE}
        ${STATE_LOADING} | ${TRANSITION_LOAD_ERROR}        | ${STATE_ERRORED}
        ${STATE_ERRORED} | ${TRANSITION_ACKNOWLEDGE_ERROR} | ${STATE_IDLE}
        ${STATE_ERRORED} | ${TRANSITION_START_LOAD}        | ${STATE_LOADING}
      `(
        'successfully transitions to $result from $startState when the transition $transitionEvent is received',
        ({ startState, transitionEvent, result }) => {
          liveMachine.value = startState;

          expect(liveMachine.send(transitionEvent)).toEqual(result);
        },
      );

      it.each`
        startState       | transitionEvent
        ${STATE_IDLE}    | ${TRANSITION_ACKNOWLEDGE_ERROR}
        ${STATE_IDLE}    | ${TRANSITION_LOAD_SUCCESS}
        ${STATE_IDLE}    | ${TRANSITION_LOAD_ERROR}
        ${STATE_IDLE}    | ${'RANDOM_FOO'}
        ${STATE_LOADING} | ${TRANSITION_START_LOAD}
        ${STATE_LOADING} | ${TRANSITION_ACKNOWLEDGE_ERROR}
        ${STATE_LOADING} | ${'RANDOM_FOO'}
        ${STATE_ERRORED} | ${TRANSITION_LOAD_ERROR}
        ${STATE_ERRORED} | ${TRANSITION_LOAD_SUCCESS}
        ${STATE_ERRORED} | ${'RANDOM_FOO'}
      `(
        'remains as $startState if an undefined transition ($transitionEvent) is received',
        ({ startState, transitionEvent }) => {
          liveMachine.value = startState;

          expect(liveMachine.send(transitionEvent)).toEqual(startState);
        },
      );

      describe('detached', () => {
        it.each`
          startState       | transitionEvent                 | result
          ${STATE_IDLE}    | ${TRANSITION_START_LOAD}        | ${STATE_LOADING}
          ${STATE_LOADING} | ${TRANSITION_LOAD_SUCCESS}      | ${STATE_IDLE}
          ${STATE_LOADING} | ${TRANSITION_LOAD_ERROR}        | ${STATE_ERRORED}
          ${STATE_ERRORED} | ${TRANSITION_ACKNOWLEDGE_ERROR} | ${STATE_IDLE}
          ${STATE_ERRORED} | ${TRANSITION_START_LOAD}        | ${STATE_LOADING}
        `(
          'successfully transitions to $result from $startState when the transition $transitionEvent is received outside the context of the machine',
          ({ startState, transitionEvent, result }) => {
            const liveSend = machine({
              ...definition,
              initial: startState,
            }).send;

            expect(liveSend(transitionEvent)).toEqual(result);
          },
        );

        it.each`
          startState       | transitionEvent
          ${STATE_IDLE}    | ${TRANSITION_ACKNOWLEDGE_ERROR}
          ${STATE_IDLE}    | ${TRANSITION_LOAD_SUCCESS}
          ${STATE_IDLE}    | ${TRANSITION_LOAD_ERROR}
          ${STATE_IDLE}    | ${'RANDOM_FOO'}
          ${STATE_LOADING} | ${TRANSITION_START_LOAD}
          ${STATE_LOADING} | ${TRANSITION_ACKNOWLEDGE_ERROR}
          ${STATE_LOADING} | ${'RANDOM_FOO'}
          ${STATE_ERRORED} | ${TRANSITION_LOAD_ERROR}
          ${STATE_ERRORED} | ${TRANSITION_LOAD_SUCCESS}
          ${STATE_ERRORED} | ${'RANDOM_FOO'}
        `(
          'remains as $startState if an undefined transition ($transitionEvent) is received',
          ({ startState, transitionEvent }) => {
            const liveSend = machine({
              ...definition,
              initial: startState,
            }).send;

            expect(liveSend(transitionEvent)).toEqual(startState);
          },
        );
      });
    });

    describe('is', () => {
      it.each`
        bool     | test             | actual
        ${true}  | ${STATE_IDLE}    | ${STATE_IDLE}
        ${false} | ${STATE_LOADING} | ${STATE_IDLE}
        ${false} | ${STATE_ERRORED} | ${STATE_IDLE}
        ${true}  | ${STATE_LOADING} | ${STATE_LOADING}
        ${false} | ${STATE_IDLE}    | ${STATE_LOADING}
        ${false} | ${STATE_ERRORED} | ${STATE_LOADING}
        ${true}  | ${STATE_ERRORED} | ${STATE_ERRORED}
        ${false} | ${STATE_IDLE}    | ${STATE_ERRORED}
        ${false} | ${STATE_LOADING} | ${STATE_ERRORED}
      `(
        'returns "$bool" for "$test" when the current state is "$actual"',
        ({ bool, test, actual }) => {
          liveMachine = machine({
            ...definition,
            initial: actual,
          });

          expect(liveMachine.is(test)).toEqual(bool);
        },
      );

      describe('detached', () => {
        it.each`
          bool     | test             | actual
          ${true}  | ${STATE_IDLE}    | ${STATE_IDLE}
          ${false} | ${STATE_LOADING} | ${STATE_IDLE}
          ${false} | ${STATE_ERRORED} | ${STATE_IDLE}
          ${true}  | ${STATE_LOADING} | ${STATE_LOADING}
          ${false} | ${STATE_IDLE}    | ${STATE_LOADING}
          ${false} | ${STATE_ERRORED} | ${STATE_LOADING}
          ${true}  | ${STATE_ERRORED} | ${STATE_ERRORED}
          ${false} | ${STATE_IDLE}    | ${STATE_ERRORED}
          ${false} | ${STATE_LOADING} | ${STATE_ERRORED}
        `(
          'returns "$bool" for "$test" when the current state is "$actual"',
          ({ bool, test, actual }) => {
            const liveIs = machine({
              ...definition,
              initial: actual,
            }).is;

            expect(liveIs(test)).toEqual(bool);
          },
        );
      });
    });
  });

  describe('transition', () => {
    it.each`
      startState       | transitionEvent                 | result
      ${STATE_IDLE}    | ${TRANSITION_START_LOAD}        | ${STATE_LOADING}
      ${STATE_LOADING} | ${TRANSITION_LOAD_SUCCESS}      | ${STATE_IDLE}
      ${STATE_LOADING} | ${TRANSITION_LOAD_ERROR}        | ${STATE_ERRORED}
      ${STATE_ERRORED} | ${TRANSITION_ACKNOWLEDGE_ERROR} | ${STATE_IDLE}
      ${STATE_ERRORED} | ${TRANSITION_START_LOAD}        | ${STATE_LOADING}
    `(
      'successfully transitions to $result from $startState when the transition $transitionEvent is received',
      ({ startState, transitionEvent, result }) => {
        expect(transition(definition, startState, transitionEvent)).toEqual(result);
      },
    );

    it.each`
      startState       | transitionEvent
      ${STATE_IDLE}    | ${TRANSITION_ACKNOWLEDGE_ERROR}
      ${STATE_IDLE}    | ${TRANSITION_LOAD_SUCCESS}
      ${STATE_IDLE}    | ${TRANSITION_LOAD_ERROR}
      ${STATE_IDLE}    | ${'RANDOM_FOO'}
      ${STATE_LOADING} | ${TRANSITION_START_LOAD}
      ${STATE_LOADING} | ${TRANSITION_ACKNOWLEDGE_ERROR}
      ${STATE_LOADING} | ${'RANDOM_FOO'}
      ${STATE_ERRORED} | ${TRANSITION_LOAD_ERROR}
      ${STATE_ERRORED} | ${TRANSITION_LOAD_SUCCESS}
      ${STATE_ERRORED} | ${'RANDOM_FOO'}
    `(
      'remains as $startState if an undefined transition ($transitionEvent) is received',
      ({ startState, transitionEvent }) => {
        expect(transition(definition, startState, transitionEvent)).toEqual(startState);
      },
    );

    it('remains as the provided starting state if it is an unrecognized state', () => {
      expect(transition(definition, 'RANDOM_FOO', TRANSITION_START_LOAD)).toEqual('RANDOM_FOO');
    });
  });
});