File size: 3,461 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
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
import React from 'react';
import Enzyme, { shallow } from 'enzyme';
import Adapter from '@wojtekmaj/enzyme-adapter-react-17';
import injectScript from 'scriptjs';
import GoogleMapsLoader from '../GoogleMapsLoader';
import { wait } from '../../../../test/utils';

Enzyme.configure({ adapter: new Adapter() });

jest.mock('scriptjs');

describe('GoogleMapsLoader', () => {
  const defaultProps = {
    apiKey: 'API_KEY',
  };

  it('expect to call Google Maps API', async () => {
    const children = jest.fn((x) => x);

    const props = {
      ...defaultProps,
    };

    shallow(<GoogleMapsLoader {...props}>{children}</GoogleMapsLoader>);

    await wait(0);

    expect(injectScript).toHaveBeenLastCalledWith(
      'https://maps.googleapis.com/maps/api/js?v=quarterly&key=API_KEY',
      expect.any(Function)
    );
  });

  it('expect to call Google Maps API with a custom API Key', async () => {
    const children = jest.fn((x) => x);

    const props = {
      ...defaultProps,
      apiKey: 'CUSTOM_API_KEY',
    };

    shallow(<GoogleMapsLoader {...props}>{children}</GoogleMapsLoader>);

    await wait(0);

    expect(injectScript).toHaveBeenLastCalledWith(
      'https://maps.googleapis.com/maps/api/js?v=quarterly&key=CUSTOM_API_KEY',
      expect.any(Function)
    );
  });

  it('expect to call Google Maps API with a custom endpoint', async () => {
    const children = jest.fn((x) => x);

    const props = {
      ...defaultProps,
      endpoint:
        'https://maps.googleapis.com/maps/api/js?v=3.32&places,geometry',
    };

    shallow(<GoogleMapsLoader {...props}>{children}</GoogleMapsLoader>);

    await wait(0);

    expect(injectScript).toHaveBeenLastCalledWith(
      'https://maps.googleapis.com/maps/api/js?v=3.32&places,geometry&key=API_KEY',
      expect.any(Function)
    );
  });

  it("expect to render nothing when it's loading", () => {
    const children = jest.fn((x) => x);

    const props = {
      ...defaultProps,
    };

    const wrapper = shallow(
      <GoogleMapsLoader {...props}>{children}</GoogleMapsLoader>
    );

    expect(wrapper.type()).toBe(null);
    expect(children).not.toHaveBeenCalled();
  });

  it("expect to call children with the Google object when it's loaded", async () => {
    const children = jest.fn((x) => x);

    const google = {
      version: '3.1.1',
    };

    const props = {
      ...defaultProps,
    };

    injectScript.mockImplementationOnce((_, callback) => {
      global.google = google;
      callback();
    });

    const wrapper = shallow(
      <GoogleMapsLoader {...props}>{children}</GoogleMapsLoader>
    );

    await wait(0);

    expect(wrapper.type).not.toBe(null);
    expect(children).toHaveBeenCalledTimes(1);
    expect(children).toHaveBeenCalledWith(google);

    delete global.google;
  });

  it('expect to not call setState when we unmount before loading is complete', async () => {
    const children = jest.fn((x) => x);

    const props = {
      ...defaultProps,
    };

    let triggerLoadingComplete;
    injectScript.mockImplementationOnce((endpoint, callback) => {
      triggerLoadingComplete = callback;
    });

    const wrapper = shallow(
      <GoogleMapsLoader {...props}>{children}</GoogleMapsLoader>
    );

    await wait(0);

    expect(wrapper.type).not.toBe(null);
    expect(children).not.toHaveBeenCalled();

    wrapper.unmount();

    triggerLoadingComplete();

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