summaryrefslogtreecommitdiff
path: root/spec/frontend/actioncable_link_spec.js
blob: b15b93cb11d910e890f6c7485aff27b622bf8d59 (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
import { print } from 'graphql';
import { gql } from '@apollo/client/core';
import cable from '~/actioncable_consumer';
import ActionCableLink from '~/actioncable_link';

// Mock uuids module for determinism
jest.mock('~/lib/utils/uuids', () => ({
  uuids: () => ['testuuid'],
}));

const TEST_OPERATION = {
  query: gql`
    query foo {
      project {
        id
      }
    }
  `,
  operationName: 'foo',
  variables: [],
};

/**
 * Create an observer that passes calls to the given spy.
 *
 * This helps us assert which calls were made in what order.
 */
const createSpyObserver = (spy) => ({
  next: (...args) => spy('next', ...args),
  error: (...args) => spy('error', ...args),
  complete: (...args) => spy('complete', ...args),
});

const notify = (...notifications) => {
  notifications.forEach((data) => cable.subscriptions.notifyAll('received', data));
};

const getSubscriptionCount = () => cable.subscriptions.subscriptions.length;

describe('~/actioncable_link', () => {
  let cableLink;

  beforeEach(() => {
    jest.spyOn(cable.subscriptions, 'create');

    cableLink = new ActionCableLink();
  });

  describe('request', () => {
    let subscription;
    let spy;

    beforeEach(() => {
      spy = jest.fn();
      subscription = cableLink.request(TEST_OPERATION).subscribe(createSpyObserver(spy));
    });

    afterEach(() => {
      subscription.unsubscribe();
    });

    it('creates a subscription', () => {
      expect(getSubscriptionCount()).toBe(1);
      expect(cable.subscriptions.create).toHaveBeenCalledWith(
        {
          channel: 'GraphqlChannel',
          nonce: 'testuuid',
          ...TEST_OPERATION,
          query: print(TEST_OPERATION.query),
        },
        { received: expect.any(Function) },
      );
    });

    it('when "unsubscribe", unsubscribes underlying cable subscription', () => {
      subscription.unsubscribe();

      expect(getSubscriptionCount()).toBe(0);
    });

    it('when receives data, triggers observer until no ".more"', () => {
      notify(
        { result: 'test result', more: true },
        { result: 'test result 2', more: true },
        { result: 'test result 3' },
        { result: 'test result 4' },
      );

      expect(spy.mock.calls).toEqual([
        ['next', 'test result'],
        ['next', 'test result 2'],
        ['next', 'test result 3'],
        ['complete'],
      ]);
    });

    it('when receives errors, triggers observer', () => {
      notify(
        { result: 'test result', more: true },
        { result: 'test result 2', errors: ['boom!'], more: true },
        { result: 'test result 3' },
      );

      expect(spy.mock.calls).toEqual([
        ['next', 'test result'],
        ['error', ['boom!']],
      ]);
    });
  });
});