File size: 5,622 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
import PropTypes from 'prop-types';
import { createFakeMarkerInstance } from '../../test/mockGoogleMaps';
import * as utils from '../utils';

describe('utils', () => {
  describe('registerEvents', () => {
    it('expect to add listeners from events', () => {
      const onClick = () => {};
      const onMouseMove = () => {};
      const instance = createFakeMarkerInstance();

      const events = {
        onClick: 'click',
        onMouseMove: 'mousemove',
      };

      const props = {
        onClick,
        onMouseMove,
      };

      utils.registerEvents(events, props, instance);

      expect(instance.addListener).toHaveBeenCalledTimes(2);

      expect(instance.addListener).toHaveBeenCalledWith(
        'click',
        expect.any(Function)
      );

      expect(instance.addListener).toHaveBeenCalledWith(
        'mousemove',
        expect.any(Function)
      );
    });

    it('expect to add listeners with event & marker', () => {
      const onClick = jest.fn();
      const onMouseMove = jest.fn();
      const instance = createFakeMarkerInstance();
      const listeners = [];

      instance.addListener.mockImplementation((event, listener) =>
        listeners.push(listener)
      );

      const events = {
        onClick: 'click',
        onMouseMove: 'mousemove',
      };

      const props = {
        onClick,
        onMouseMove,
      };

      utils.registerEvents(events, props, instance);

      listeners.forEach((listener) => listener({ type: 'event' }));

      expect(onClick).toHaveBeenCalledWith({
        event: { type: 'event' },
        marker: instance,
      });

      expect(onMouseMove).toHaveBeenCalledWith({
        event: { type: 'event' },
        marker: instance,
      });
    });

    it('expect to only add listeners listed from events', () => {
      const onClick = () => {};
      const onMouseEnter = () => {};
      const instance = createFakeMarkerInstance();

      const events = {
        onClick: 'click',
        onMouseMove: 'mousemove',
      };

      const props = {
        onClick,
        onMouseEnter,
      };

      utils.registerEvents(events, props, instance);

      expect(instance.addListener).toHaveBeenCalledTimes(1);
      expect(instance.addListener).toHaveBeenCalledWith(
        'click',
        expect.any(Function)
      );
    });

    it('expect to only add listeners listed from props', () => {
      const onClick = () => {};
      const instance = createFakeMarkerInstance();

      const events = {
        onClick: 'click',
        onMouseMove: 'mousemove',
      };

      const props = {
        onClick,
      };

      utils.registerEvents(events, props, instance);

      expect(instance.addListener).toHaveBeenCalledTimes(1);
      expect(instance.addListener).toHaveBeenCalledWith(
        'click',
        expect.any(Function)
      );
    });

    it('expect to return a function that remove the listeners', () => {
      const onClick = () => {};
      const onMouseMove = () => {};
      const remove = jest.fn();
      const instance = createFakeMarkerInstance();

      instance.addListener.mockImplementation(() => ({
        remove,
      }));

      const events = {
        onClick: 'click',
        onMouseMove: 'mousemove',
      };

      const props = {
        onClick,
        onMouseMove,
      };

      const removeEventListeners = utils.registerEvents(
        events,
        props,
        instance
      );

      expect(remove).toHaveBeenCalledTimes(0);

      removeEventListeners();

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

  describe('createListenersPropTypes', () => {
    it('expect to return an object with listeners propType from event types', () => {
      const events = {
        onClick: '',
        onMouseMove: '',
      };

      const expectation = {
        onClick: PropTypes.func,
        onMouseMove: PropTypes.func,
      };

      const actual = utils.createListenersPropTypes(events);

      expect(actual).toEqual(expectation);
    });

    it('expect to return an empty object from empty event types', () => {
      const events = {};

      const expectation = {};
      const actual = utils.createListenersPropTypes(events);

      expect(actual).toEqual(expectation);
    });
  });

  describe('createFilterProps', () => {
    it('expect to return an object without excluded keys', () => {
      const excludes = ['children', 'onClick'];

      const props = {
        label: 'Title',
        onClick: () => {},
        children: '<div />',
      };

      const expectation = {
        label: 'Title',
      };

      const filterProps = utils.createFilterProps(excludes);
      const actual = filterProps(props);

      expect(actual).toEqual(expectation);
    });

    it('expect to return the given props when excluded keys is empty', () => {
      const onClick = () => {};
      const excludes = [];

      const props = {
        children: '<div />',
        onClick,
      };

      const expectation = {
        children: '<div />',
        onClick,
      };

      const filterProps = utils.createFilterProps(excludes);
      const actual = filterProps(props);

      expect(actual).toEqual(expectation);
    });

    it('expect to return an empty object when all keys are excluded', () => {
      const excludes = ['children', 'onClick'];

      const props = {
        onClick: () => {},
        children: '<div />',
      };

      const expectation = {};
      const filterProps = utils.createFilterProps(excludes);
      const actual = filterProps(props);

      expect(actual).toEqual(expectation);
    });
  });
});