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

event_hub_factory_spec.js « helpers « frontend « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: d29b9ddba34b8c8b6f86f7fe30f9c677783d5ac1 (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
import createEventHub from '~/helpers/event_hub_factory';

const TEST_EVENT = 'foobar';

describe('event bus factory', () => {
  let eventBus;
  let handler;

  beforeEach(() => {
    eventBus = createEventHub();
    handler = jest.fn();
  });

  afterEach(() => {
    eventBus = null;
  });

  describe('instance', () => {
    it.each`
      method
      ${'$on'}
      ${'$once'}
      ${'$off'}
      ${'$emit'}
    `('has $method method', ({ method }) => {
      expect(eventBus[method]).toEqual(expect.any(Function));
    });
  });

  describe('$on', () => {
    beforeEach(() => {
      eventBus.$on(TEST_EVENT, handler);
    });

    it('calls handler when event is emitted', () => {
      eventBus.$emit(TEST_EVENT);
      expect(handler).toHaveBeenCalledWith();
    });

    it('calls handler with multiple args', () => {
      eventBus.$emit(TEST_EVENT, 'arg1', 'arg2', 'arg3');
      expect(handler).toHaveBeenCalledWith('arg1', 'arg2', 'arg3');
    });

    it('calls handler multiple times', () => {
      eventBus.$emit(TEST_EVENT, 'arg1', 'arg2', 'arg3');
      eventBus.$emit(TEST_EVENT, 'arg1', 'arg2', 'arg3');

      expect(handler).toHaveBeenCalledTimes(2);
    });

    it('does not call handler after $off with handler', () => {
      eventBus.$off(TEST_EVENT, handler);

      eventBus.$emit(TEST_EVENT);

      expect(handler).not.toHaveBeenCalled();
    });

    it('does not call handler after $off', () => {
      eventBus.$off(TEST_EVENT);

      eventBus.$emit(TEST_EVENT);

      expect(handler).not.toHaveBeenCalled();
    });
  });

  describe('$once', () => {
    beforeEach(() => {
      eventBus.$once(TEST_EVENT, handler);
    });

    it('calls handler when event is emitted', () => {
      eventBus.$emit(TEST_EVENT);
      expect(handler).toHaveBeenCalled();
    });

    it('calls the handler only once when event is emitted multiple times', () => {
      eventBus.$emit(TEST_EVENT);
      eventBus.$emit(TEST_EVENT);
      expect(handler).toHaveBeenCalledTimes(1);
    });

    describe('when the handler thows an error', () => {
      beforeEach(() => {
        handler = jest.fn().mockImplementation(() => {
          throw new Error();
        });
        eventBus.$once(TEST_EVENT, handler);
      });

      it('calls off when event is emitted', () => {
        expect(() => {
          eventBus.$emit(TEST_EVENT);
        }).toThrow();
        expect(() => {
          eventBus.$emit(TEST_EVENT);
        }).not.toThrow();

        expect(handler).toHaveBeenCalledTimes(1);
      });
    });
  });
});