UNPKG

11.7 kBJavaScriptView Raw
1import React from 'react';
2import { mount } from 'enzyme';
3import { PSClickWrap } from '../src';
4
5describe('_ps initialization', () => {
6 it('Creates _ps runner global', () => {
7 mount(<PSClickWrap accessId="0000000-000000-0000-0000000" signerIdSelector="email" groupKey="example-clickwrap" displayAll testMode />);
8 expect(_ps).toBeDefined();
9 });
10});
11
12describe('PSClickWrap _ps interface tests', () => {
13 beforeEach(() => {
14 window._ps = jest.fn();
15 });
16
17 it('Renders a ps-clickwrap wrapper div', () => {
18 const wrapper = mount(<PSClickWrap accessId="0000000-000000-0000-0000000" signerIdSelector="email" groupKey="example-clickwrap" />);
19 expect(wrapper.containsMatchingElement(<div id="ps-clickwrap" />)).toBeTruthy();
20 });
21
22 it('Renders a container div with specified container Id if passed as a prop', () => {
23 const wrapper = mount(<PSClickWrap accessId="0000000-000000-0000-0000000" containerId="test" signerIdSelector="email" groupKey="example-clickwrap" />);
24 expect(wrapper.containsMatchingElement(<div id="test" />)).toBeTruthy();
25 });
26
27 it('calls _ps with create and proper access ID', () => {
28 const passedAccessId = '29ea80d9-d386-4cfd-a280-505e802ee732';
29 mount(<PSClickWrap accessId={passedAccessId} signerIdSelector="email" groupKey="example-clickwrap" displayAll testMode />);
30 expect(_ps.mock.calls[0][0]).toBe('create');
31 expect(_ps.mock.calls[0][1]).toBe(passedAccessId);
32 });
33
34 it('calls _ps create with test_mode if passed as a prop', () => {
35 mount(<PSClickWrap accessId="0000000-000000-0000-0000000" signerIdSelector="email" groupKey="example-clickwrap" testMode />);
36 expect(_ps.mock.calls[0][2].test_mode).toBe(true);
37 });
38
39 it('calls _ps create with disable_sending if passed as a prop', () => {
40 mount(<PSClickWrap accessId="0000000-000000-0000-0000000" signerIdSelector="email" groupKey="example-clickwrap" disableSending />);
41 expect(_ps.mock.calls[0][2].disable_sending).toBe(true);
42 });
43
44 it('calls _ps create with dynamic in payload if passed as a prop', () => {
45 mount(<PSClickWrap accessId="0000000-000000-0000-0000000" signerIdSelector="email" groupKey="example-clickwrap" dynamic renderData={{}} />);
46 expect(_ps.mock.calls[0][2].dynamic).toBe(true);
47 });
48
49 it('calls _ps with signer id specified in create payload if passed as a prop', () => {
50 mount(<PSClickWrap accessId="0000000-000000-0000-0000000" groupKey="example-clickwrap" signerId="test@abc.com" />);
51 expect(_ps.mock.calls[0][2].signer_id).toBe('test@abc.com');
52 });
53
54 it('_ps create passes in test_mode, disable_sending, dynamic, and signer_id as options', () => {
55 mount(<PSClickWrap accessId="0000000-000000-0000-0000000" groupKey="example-clickwrap" signerId="test@abc.com" />);
56 expect(_ps.mock.calls[0][2]).toHaveProperty('test_mode');
57 expect(_ps.mock.calls[0][2]).toHaveProperty('disable_sending');
58 expect(_ps.mock.calls[0][2]).toHaveProperty('dynamic');
59 expect(_ps.mock.calls[0][2]).toHaveProperty('signer_id');
60 });
61
62 it('calls _ps load with specified group key', () => {
63 mount(<PSClickWrap accessId="0000000-000000-0000-0000000" groupKey="example-clickwrap" signerId="test@abc.com" />);
64 expect(_ps.mock.calls[1][0]).toBe('load');
65 expect(_ps.mock.calls[1][1]).toBe('example-clickwrap');
66 });
67
68 it('calls _ps with options as second parameter if filter is specified, makes sure filter is passed propertly', () => {
69 mount(<PSClickWrap accessId="0000000-000000-0000-0000000" filter="id==12345 and tags==tag1,tag2" signerId="test@abc.com" />);
70 expect(_ps.mock.calls[1][0]).toBe('load');
71 expect(typeof _ps.mock.calls[1][1]).toBe('object');
72 expect(_ps.mock.calls[1][1]).toHaveProperty('filter');
73 expect(_ps.mock.calls[1][1].filter).toBe('id==12345 and tags==tag1,tag2');
74 expect(_ps.mock.calls[1][1]).toHaveProperty('container_selector');
75 expect(_ps.mock.calls[1][1]).toHaveProperty('confirmation_email');
76 expect(_ps.mock.calls[1][1]).toHaveProperty('signer_id_selector');
77 expect(_ps.mock.calls[1][1]).toHaveProperty('style');
78 expect(_ps.mock.calls[1][1]).toHaveProperty('display_all');
79 expect(_ps.mock.calls[1][1]).toHaveProperty('render_data');
80 expect(_ps.mock.calls[1][1]).toHaveProperty('auto_run');
81 expect(_ps.mock.calls[1][1]).toHaveProperty('force_scroll');
82 });
83
84 it('calls _ps with options as third parameter if groupKey is specified', () => {
85 mount(<PSClickWrap accessId="0000000-000000-0000-0000000" groupKey="example-clickwrap" signerId="test@abc.com" />);
86 expect(_ps.mock.calls[1][0]).toBe('load');
87 expect(typeof _ps.mock.calls[1][2]).toBe('object');
88 expect(_ps.mock.calls[1][2]).toHaveProperty('filter');
89 expect(_ps.mock.calls[1][2]).toHaveProperty('container_selector');
90 expect(_ps.mock.calls[1][2]).toHaveProperty('confirmation_email');
91 expect(_ps.mock.calls[1][2]).toHaveProperty('signer_id_selector');
92 expect(_ps.mock.calls[1][2]).toHaveProperty('style');
93 expect(_ps.mock.calls[1][2]).toHaveProperty('display_all');
94 expect(_ps.mock.calls[1][2]).toHaveProperty('render_data');
95 expect(_ps.mock.calls[1][2]).toHaveProperty('auto_run');
96 expect(_ps.mock.calls[1][2]).toHaveProperty('force_scroll');
97 });
98
99 it('sets clickwrapStyle properly on payload if passed as a prop', () => {
100 mount(<PSClickWrap accessId="0000000-000000-0000-0000000" groupKey="example-clickwrap" signerId="test@abc.com" clickWrapStyle="scroll" />);
101 expect(_ps.mock.calls[1][2].style).toBe('scroll');
102 });
103
104 it('leaves clickwrapStyle to be undefined if not passed prop', () => {
105 mount(<PSClickWrap accessId="0000000-000000-0000-0000000" groupKey="example-clickwrap" signerId="test@abc.com" />);
106 expect(_ps.mock.calls[1][2].style).toBeUndefined();
107 });
108
109 it('sets confirmationEmail property to true on payload if passed as a prop', () => {
110 mount(<PSClickWrap accessId="0000000-000000-0000-0000000" groupKey="example-clickwrap" signerId="test@abc.com" confirmationEmail />);
111 expect(_ps.mock.calls[1][2].confirmation_email).toBe(true);
112 });
113
114 it('leaves confirmationEmail as undefined if not passed as prop', () => {
115 mount(<PSClickWrap accessId="0000000-000000-0000-0000000" groupKey="example-clickwrap" signerId="test@abc.com" />);
116 expect(_ps.mock.calls[1][2].confirmation_email).toBeUndefined();
117 });
118
119 it('sets disableSending to true if passed as a prop', () => {
120 mount(<PSClickWrap accessId="0000000-000000-0000-0000000" groupKey="example-clickwrap" signerId="test@abc.com" disableSending />);
121 expect(_ps.mock.calls[0][2].disable_sending).toBe(true);
122 });
123
124 it('defaults disableSending to false if not passed as a prop', () => {
125 mount(<PSClickWrap accessId="0000000-000000-0000-0000000" groupKey="example-clickwrap" signerId="test@abc.com" />);
126 expect(_ps.mock.calls[0][2].disable_sending).toBe(false);
127 });
128
129 it('defaults displayAll to true if not passed as a prop', () => {
130 mount(<PSClickWrap accessId="0000000-000000-0000-0000000" groupKey="example-clickwrap" signerId="test@abc.com" />);
131 expect(_ps.mock.calls[1][2].display_all).toBe(true);
132 });
133
134 it('sets displayAll to value passed as prop', () => {
135 mount(<PSClickWrap accessId="0000000-000000-0000-0000000" groupKey="example-clickwrap" signerId="test@abc.com" displayAll={false} />);
136 expect(_ps.mock.calls[1][2].display_all).toBe(false);
137 });
138
139 it('sets auto_run to true by default if not passing displayImmediately to false', () => {
140 mount(<PSClickWrap accessId="0000000-000000-0000-0000000" groupKey="example-clickwrap" signerId="test@abc.com" />);
141 expect(_ps.mock.calls[1][2].auto_run).toBe(true);
142 });
143
144 it('sets auto_run to false if passing displayImmediately=false as a prop', () => {
145 mount(<PSClickWrap accessId="0000000-000000-0000-0000000" groupKey="example-clickwrap" signerId="test@abc.com" displayImmediately={false} />);
146 expect(_ps.mock.calls[1][2].auto_run).toBe(false);
147 });
148
149 it('sets dynamic to true in create call if passed as a prop', () => {
150 mount(<PSClickWrap accessId="0000000-000000-0000-0000000" groupKey="example-clickwrap" signerId="test@abc.com" dynamic renderData={{}} />);
151 expect(_ps.mock.calls[0][2].dynamic).toBe(true);
152 });
153
154 it('defaults to dynamic as false if not passed as a prop', () => {
155 mount(<PSClickWrap accessId="0000000-000000-0000-0000000" groupKey="example-clickwrap" signerId="test@abc.com" />);
156 expect(_ps.mock.calls[0][2].dynamic).toBe(false);
157 });
158
159 it('defaults force_scroll to undefined if not passed as a prop', () => {
160 mount(<PSClickWrap accessId="0000000-000000-0000-0000000" groupKey="example-clickwrap" signerId="test@abc.com" />);
161 expect(_ps.mock.calls[1][2].force_scroll).toBe(undefined);
162 });
163
164 it('ensures renderData is passed properly if passed as a prop', () => {
165 mount(<PSClickWrap accessId="0000000-000000-0000-0000000" groupKey="example-clickwrap" signerId="test@abc.com" renderData={{ test: 'abc' }} />);
166 expect(_ps.mock.calls[1][2].render_data).toMatchObject({ test: 'abc' });
167 });
168
169 it('sets renderData to undefined if it is not passed as a prop', () => {
170 mount(<PSClickWrap accessId="0000000-000000-0000-0000000" groupKey="example-clickwrap" signerId="test@abc.com" />);
171 expect(_ps.mock.calls[1][2].render_data).toBeUndefined();
172 });
173
174 it('sets testMode to true if passed as a prop', () => {
175 mount(<PSClickWrap accessId="0000000-000000-0000-0000000" groupKey="example-clickwrap" signerId="test@abc.com" testMode />);
176 expect(_ps.mock.calls[0][2].test_mode).toBe(true);
177 });
178
179 it('sets testMode to false if not passed as a prop', () => {
180 mount(<PSClickWrap accessId="0000000-000000-0000-0000000" groupKey="example-clickwrap" signerId="test@abc.com" />);
181 expect(_ps.mock.calls[0][2].test_mode).toBe(false);
182 });
183});
184
185describe('PSClickWrap _ps event prop tests', () => {
186 const propsEventMap = {
187 onAll: 'all',
188 onSent: 'sent',
189 onRetrieved: 'retrieved',
190 onSet: 'set',
191 onSetSignerId: 'set:signer_id',
192 onValid: 'valid',
193 onInvalid: 'invalid',
194 onRendered: 'rendered',
195 onDisplayed: 'displayed',
196 onScrolledContract: 'scrolled:contract',
197 onScrolled: 'scrolled',
198 onError: 'error',
199 };
200
201 beforeEach(() => {
202 const fakeGroupObj = { render: () => {} };
203 window._ps = jest.fn((...args) => {
204 args.forEach((arg) => {
205 if (typeof arg === 'object' && arg.hasOwnProperty('event_callback') && typeof arg.event_callback === 'function') {
206 arg.event_callback(null, fakeGroupObj);
207 }
208 });
209 });
210 window._ps.on = jest.fn();
211 });
212
213 function testPassedEventListenerCalled(psEvent) {
214 const testingGroupKey = 'example-clickwrap';
215 const eventCallback = {
216 [psEvent]: jest.fn(),
217 };
218 mount(<PSClickWrap accessId="0000000-000000-0000-0000000" groupKey={testingGroupKey} signerId="test@abc.com" {...eventCallback} />);
219 const callbackRegistered = _ps.on.mock.calls[0][1];
220 callbackRegistered({ get: () => testingGroupKey });
221 expect(eventCallback[psEvent]).toHaveBeenCalled();
222 }
223
224 function testPSOnParameter(psEvent) {
225 const callbackProp = { [psEvent]: () => `${psEvent} dummy callback event` };
226 mount(<PSClickWrap accessId="0000000-000000-0000-0000000" groupKey="example-clickwrap" signerId="test@abc.com" {...callbackProp} />);
227 expect(_ps.on.mock.calls[0][0]).toBe(propsEventMap[psEvent]);
228 }
229
230 Object.keys(propsEventMap).forEach((prop) => {
231 it(`passed ${prop} event prop fires a _ps.on("${propsEventMap[prop]}",...) function call`, () => {
232 testPSOnParameter(prop);
233 });
234
235 it(`passed ${prop} event listener is called by _ps callback function`, () => {
236 testPassedEventListenerCalled(prop);
237 });
238 });
239});