File size: 1,234 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { invariant } from '../invariant';

describe('invariant', () => {
  if (!__DEV__) {
    test('throws a generic invariant violation in production', () => {
      expect(() => {
        invariant(false, 'invariant');
      }).toThrow('Invariant failed');
    });

    test('does not throw when the condition is met in production', () => {
      expect(() => {
        invariant(true, 'invariant');
      }).not.toThrow();
    });
  }

  if (__DEV__) {
    test('throws when the condition is unmet', () => {
      expect(() => {
        invariant(false, 'invariant');
      }).toThrow('[InstantSearch] invariant');
    });

    test('does not throw when the condition is met', () => {
      expect(() => {
        invariant(true, 'invariant');
      }).not.toThrow();
    });

    test('lazily instantiates message', () => {
      const spy1 = jest.fn(() => 'invariant');
      const spy2 = jest.fn(() => 'invariant');

      expect(() => {
        invariant(false, spy1);
      }).toThrow('[InstantSearch] invariant');

      expect(spy1).toHaveBeenCalledTimes(1);

      expect(() => {
        invariant(true, spy2);
      }).not.toThrow('[InstantSearch] invariant');

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