summaryrefslogtreecommitdiff
path: root/spec/frontend/lib/apollo/suppress_network_errors_during_navigation_link_spec.js
blob: 852106db44eeda7d2f48c96e34e6e675c962ad87 (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
import { ApolloLink, Observable } from 'apollo-link';
import waitForPromises from 'helpers/wait_for_promises';
import { getSuppressNetworkErrorsDuringNavigationLink } from '~/lib/apollo/suppress_network_errors_during_navigation_link';
import { isNavigatingAway } from '~/lib/utils/is_navigating_away';

jest.mock('~/lib/utils/is_navigating_away');

describe('getSuppressNetworkErrorsDuringNavigationLink', () => {
  const originalGon = window.gon;
  let subscription;

  beforeEach(() => {
    window.gon = originalGon;
  });

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

  const makeMockGraphQLErrorLink = () =>
    new ApolloLink(() =>
      Observable.of({
        errors: [
          {
            message: 'foo',
          },
        ],
      }),
    );

  const makeMockNetworkErrorLink = () =>
    new ApolloLink(
      () =>
        new Observable(() => {
          throw new Error('NetworkError');
        }),
    );

  const makeMockSuccessLink = () =>
    new ApolloLink(() => Observable.of({ data: { foo: { id: 1 } } }));

  const createSubscription = (otherLink, observer) => {
    const mockOperation = { operationName: 'foo' };
    const link = getSuppressNetworkErrorsDuringNavigationLink().concat(otherLink);
    subscription = link.request(mockOperation).subscribe(observer);
  };

  describe('when disabled', () => {
    it('returns null', () => {
      expect(getSuppressNetworkErrorsDuringNavigationLink()).toBe(null);
    });
  });

  describe('when enabled', () => {
    beforeEach(() => {
      window.gon = { features: { suppressApolloErrorsDuringNavigation: true } };
    });

    it('returns an ApolloLink', () => {
      expect(getSuppressNetworkErrorsDuringNavigationLink()).toEqual(expect.any(ApolloLink));
    });

    describe('suppression case', () => {
      describe('when navigating away', () => {
        beforeEach(() => {
          isNavigatingAway.mockReturnValue(true);
        });

        describe('given a network error', () => {
          it('does not forward the error', async () => {
            const spy = jest.fn();

            createSubscription(makeMockNetworkErrorLink(), {
              next: spy,
              error: spy,
              complete: spy,
            });

            // It's hard to test for something _not_ happening. The best we can
            // do is wait a bit to make sure nothing happens.
            await waitForPromises();
            expect(spy).not.toHaveBeenCalled();
          });
        });
      });
    });

    describe('non-suppression cases', () => {
      describe('when not navigating away', () => {
        beforeEach(() => {
          isNavigatingAway.mockReturnValue(false);
        });

        it('forwards successful requests', (done) => {
          createSubscription(makeMockSuccessLink(), {
            next({ data }) {
              expect(data).toEqual({ foo: { id: 1 } });
            },
            error: () => done.fail('Should not happen'),
            complete: () => done(),
          });
        });

        it('forwards GraphQL errors', (done) => {
          createSubscription(makeMockGraphQLErrorLink(), {
            next({ errors }) {
              expect(errors).toEqual([{ message: 'foo' }]);
            },
            error: () => done.fail('Should not happen'),
            complete: () => done(),
          });
        });

        it('forwards network errors', (done) => {
          createSubscription(makeMockNetworkErrorLink(), {
            next: () => done.fail('Should not happen'),
            error: (error) => {
              expect(error.message).toBe('NetworkError');
              done();
            },
            complete: () => done.fail('Should not happen'),
          });
        });
      });

      describe('when navigating away', () => {
        beforeEach(() => {
          isNavigatingAway.mockReturnValue(true);
        });

        it('forwards successful requests', (done) => {
          createSubscription(makeMockSuccessLink(), {
            next({ data }) {
              expect(data).toEqual({ foo: { id: 1 } });
            },
            error: () => done.fail('Should not happen'),
            complete: () => done(),
          });
        });

        it('forwards GraphQL errors', (done) => {
          createSubscription(makeMockGraphQLErrorLink(), {
            next({ errors }) {
              expect(errors).toEqual([{ message: 'foo' }]);
            },
            error: () => done.fail('Should not happen'),
            complete: () => done(),
          });
        });
      });
    });
  });
});