|
| 1 | +import { subscribeMockFn } from '@paypalcorp/web-sdk-postmessenger/src'; |
1 | 2 | import zoidPolyfill from 'src/components/modal/v2/lib/zoid-polyfill';
|
2 | 3 | import { logger } from 'src/utils';
|
3 | 4 |
|
@@ -25,6 +26,16 @@ jest.mock('@krakenjs/belter/src', () => {
|
25 | 26 | })
|
26 | 27 | };
|
27 | 28 | });
|
| 29 | +jest.mock('@paypalcorp/web-sdk-postmessenger/src', () => { |
| 30 | + const subscribeMock = jest.fn(); |
| 31 | + return { |
| 32 | + __esModule: true, |
| 33 | + subscribeMockFn: subscribeMock, |
| 34 | + default: jest.fn().mockImplementation(() => ({ |
| 35 | + subscribe: subscribeMock |
| 36 | + })) |
| 37 | + }; |
| 38 | +}); |
28 | 39 | jest.mock('src/components/modal/v2/lib/utils', () => ({
|
29 | 40 | isIframe: true
|
30 | 41 | }));
|
@@ -73,7 +84,7 @@ describe('zoidPollyfill', () => {
|
73 | 84 | describe('sets up xprops for browser', () => {
|
74 | 85 | beforeAll(() => {
|
75 | 86 | mockLoadUrl(
|
76 |
| - 'https://localhost.paypal.com:8080/credit-presentment/native/message?client_id=client_1&logo_type=inline&amount=500&devTouchpoint=true' |
| 87 | + 'https://localhost.paypal.com:8080/credit-presentment/lander/modal?client_id=client_1&logo_type=inline&amount=500&devTouchpoint=true' |
77 | 88 | );
|
78 | 89 |
|
79 | 90 | zoidPolyfill();
|
@@ -177,7 +188,7 @@ describe('zoidPollyfill', () => {
|
177 | 188 |
|
178 | 189 | test('sets up xprops for webview', () => {
|
179 | 190 | mockLoadUrl(
|
180 |
| - 'https://localhost.paypal.com:8080/credit-presentment/native/message?client_id=client_1&logo_type=inline&amount=500&dev_touchpoint=true', |
| 191 | + 'https://localhost.paypal.com:8080/credit-presentment/native/modal?client_id=client_1&logo_type=inline&amount=500&dev_touchpoint=true', |
181 | 192 | {
|
182 | 193 | platform: 'ios'
|
183 | 194 | }
|
@@ -316,94 +327,167 @@ describe('zoidPollyfill', () => {
|
316 | 327 | postMessage.mockClear();
|
317 | 328 | });
|
318 | 329 |
|
319 |
| - test('notifies when props update', () => { |
320 |
| - mockLoadUrl( |
321 |
| - 'https://localhost.paypal.com:8080/credit-presentment/native/message?client_id=client_1&logo_type=inline&amount=500&devTouchpoint=true', |
322 |
| - { |
323 |
| - platform: 'android' |
324 |
| - } |
325 |
| - ); |
326 |
| - const postMessage = global.Android.paypalMessageModalCallbackHandler; |
| 330 | + describe('notifies when props update', () => { |
| 331 | + afterEach(() => { |
| 332 | + subscribeMockFn.mockClear(); |
| 333 | + }); |
| 334 | + test('webview', () => { |
| 335 | + mockLoadUrl( |
| 336 | + 'https://localhost.paypal.com:8080/credit-presentment/native/modal?client_id=client_1&logo_type=inline&amount=500&devTouchpoint=true', |
| 337 | + { |
| 338 | + platform: 'android' |
| 339 | + } |
| 340 | + ); |
| 341 | + const postMessage = global.Android.paypalMessageModalCallbackHandler; |
327 | 342 |
|
328 |
| - zoidPolyfill(); |
| 343 | + zoidPolyfill(); |
329 | 344 |
|
330 |
| - expect(window.actions).toEqual( |
331 |
| - expect.objectContaining({ |
332 |
| - updateProps: expect.any(Function) |
333 |
| - }) |
334 |
| - ); |
335 |
| - expect(window.xprops).toEqual( |
336 |
| - expect.objectContaining({ |
337 |
| - onProps: expect.any(Function) |
338 |
| - }) |
339 |
| - ); |
| 345 | + expect(window.actions).toEqual( |
| 346 | + expect.objectContaining({ |
| 347 | + updateProps: expect.any(Function) |
| 348 | + }) |
| 349 | + ); |
| 350 | + expect(window.xprops).toEqual( |
| 351 | + expect.objectContaining({ |
| 352 | + onProps: expect.any(Function) |
| 353 | + }) |
| 354 | + ); |
340 | 355 |
|
341 |
| - const onPropsCallback = jest.fn(); |
| 356 | + const onPropsCallback = jest.fn(); |
342 | 357 |
|
343 |
| - window.xprops.onProps(onPropsCallback); |
344 |
| - window.actions.updateProps({ amount: 1000 }); |
| 358 | + window.xprops.onProps(onPropsCallback); |
| 359 | + window.actions.updateProps({ amount: 1000 }); |
345 | 360 |
|
346 |
| - expect(onPropsCallback).toHaveBeenCalledTimes(1); |
347 |
| - expect(onPropsCallback).toHaveBeenCalledWith( |
348 |
| - expect.objectContaining({ |
349 |
| - clientId: 'client_1', |
350 |
| - logoType: 'inline', |
351 |
| - amount: 1000 |
352 |
| - }) |
353 |
| - ); |
| 361 | + expect(onPropsCallback).toHaveBeenCalledTimes(1); |
| 362 | + expect(onPropsCallback).toHaveBeenCalledWith( |
| 363 | + expect.objectContaining({ |
| 364 | + clientId: 'client_1', |
| 365 | + logoType: 'inline', |
| 366 | + amount: 1000 |
| 367 | + }) |
| 368 | + ); |
354 | 369 |
|
355 |
| - window.actions.updateProps({ offer: 'TEST' }); |
| 370 | + window.actions.updateProps({ offer: 'TEST' }); |
356 | 371 |
|
357 |
| - expect(onPropsCallback).toHaveBeenCalledTimes(2); |
358 |
| - expect(onPropsCallback).toHaveBeenCalledWith( |
359 |
| - expect.objectContaining({ |
360 |
| - clientId: 'client_1', |
361 |
| - logoType: 'inline', |
362 |
| - amount: 1000, |
363 |
| - offer: 'TEST' |
364 |
| - }) |
365 |
| - ); |
| 372 | + expect(onPropsCallback).toHaveBeenCalledTimes(2); |
| 373 | + expect(onPropsCallback).toHaveBeenCalledWith( |
| 374 | + expect.objectContaining({ |
| 375 | + clientId: 'client_1', |
| 376 | + logoType: 'inline', |
| 377 | + amount: 1000, |
| 378 | + offer: 'TEST' |
| 379 | + }) |
| 380 | + ); |
366 | 381 |
|
367 |
| - window.xprops.onReady({ |
368 |
| - products: ['PRODUCT_1', 'PRODUCT_2'], |
369 |
| - meta: { |
370 |
| - trackingDetails: { |
371 |
| - fdata: '123abc', |
372 |
| - credit_product_identifiers: ['PAY_LATER_LONG_TERM_US'], |
373 |
| - offer_country_code: 'US', |
374 |
| - extra_field: 'should not be present' |
| 382 | + window.xprops.onReady({ |
| 383 | + products: ['PRODUCT_1', 'PRODUCT_2'], |
| 384 | + meta: { |
| 385 | + trackingDetails: { |
| 386 | + fdata: '123abc', |
| 387 | + credit_product_identifiers: ['PAY_LATER_LONG_TERM_US'], |
| 388 | + offer_country_code: 'US', |
| 389 | + extra_field: 'should not be present' |
| 390 | + } |
375 | 391 | }
|
376 |
| - } |
377 |
| - }); |
| 392 | + }); |
378 | 393 |
|
379 |
| - expect(postMessage).toHaveBeenCalledTimes(1); |
380 |
| - expect(postMessage.mock.calls[0][0]).toEqual(expect.any(String)); |
381 |
| - expect(JSON.parse(postMessage.mock.calls[0][0])).toMatchInlineSnapshot(` |
382 |
| - Object { |
383 |
| - "args": Array [ |
| 394 | + expect(postMessage).toHaveBeenCalledTimes(1); |
| 395 | + expect(postMessage.mock.calls[0][0]).toEqual(expect.any(String)); |
| 396 | + expect(JSON.parse(postMessage.mock.calls[0][0])).toMatchInlineSnapshot(` |
384 | 397 | Object {
|
385 |
| - "__shared__": Object { |
386 |
| - "credit_product_identifiers": Array [ |
387 |
| - "PAY_LATER_LONG_TERM_US", |
388 |
| - ], |
389 |
| - "fdata": "123abc", |
390 |
| - "offer_country_code": "US", |
391 |
| - }, |
392 |
| - "event_type": "modal_rendered", |
393 |
| - "render_duration": "50", |
394 |
| - "request_duration": "100", |
| 398 | + "args": Array [ |
| 399 | + Object { |
| 400 | + "__shared__": Object { |
| 401 | + "credit_product_identifiers": Array [ |
| 402 | + "PAY_LATER_LONG_TERM_US", |
| 403 | + ], |
| 404 | + "fdata": "123abc", |
| 405 | + "offer_country_code": "US", |
| 406 | + }, |
| 407 | + "event_type": "modal_rendered", |
| 408 | + "render_duration": "50", |
| 409 | + "request_duration": "100", |
| 410 | + }, |
| 411 | + ], |
| 412 | + "name": "onReady", |
| 413 | + } |
| 414 | + `); |
| 415 | + postMessage.mockClear(); |
| 416 | + }); |
| 417 | + test('browser', () => { |
| 418 | + mockLoadUrl( |
| 419 | + 'https://localhost.paypal.com:8080/credit-presentment/lander/modal?client_id=client_1&logo_type=inline&amount=500&devTouchpoint=true' |
| 420 | + ); |
| 421 | + |
| 422 | + zoidPolyfill(); |
| 423 | + |
| 424 | + expect(window.xprops).toEqual( |
| 425 | + expect.objectContaining({ |
| 426 | + onProps: expect.any(Function) |
| 427 | + }) |
| 428 | + ); |
| 429 | + |
| 430 | + const onPropsCallback = jest.fn(); |
| 431 | + |
| 432 | + window.xprops.onProps(onPropsCallback); |
| 433 | + |
| 434 | + expect(subscribeMockFn).toHaveBeenCalledTimes(1); |
| 435 | + expect(subscribeMockFn).toHaveBeenCalledWith('PROPS_UPDATE', expect.any(Function)); |
| 436 | + |
| 437 | + subscribeMockFn( |
| 438 | + { |
| 439 | + amount: 1000 |
395 | 440 | },
|
396 |
| - ], |
397 |
| - "name": "onReady", |
398 |
| - } |
399 |
| - `); |
400 |
| - postMessage.mockClear(); |
| 441 | + window.origin |
| 442 | + ); |
| 443 | + |
| 444 | + const subscribeCallback = subscribeMockFn.mock.calls[0][1]; |
| 445 | + |
| 446 | + subscribeCallback({ |
| 447 | + amount: 1000 |
| 448 | + }); |
| 449 | + |
| 450 | + expect(onPropsCallback).toHaveBeenCalledTimes(1); |
| 451 | + expect(onPropsCallback).toHaveBeenCalledWith( |
| 452 | + expect.objectContaining({ |
| 453 | + clientId: 'client_1', |
| 454 | + logoType: 'inline', |
| 455 | + amount: 1000 |
| 456 | + }) |
| 457 | + ); |
| 458 | + |
| 459 | + subscribeCallback({ |
| 460 | + offerTypes: ['TEST'] |
| 461 | + }); |
| 462 | + |
| 463 | + expect(onPropsCallback).toHaveBeenCalledTimes(2); |
| 464 | + expect(onPropsCallback).toHaveBeenCalledWith( |
| 465 | + expect.objectContaining({ |
| 466 | + clientId: 'client_1', |
| 467 | + logoType: 'inline', |
| 468 | + amount: 1000, |
| 469 | + offerTypes: ['TEST'] |
| 470 | + }) |
| 471 | + ); |
| 472 | + |
| 473 | + window.xprops.onReady({ |
| 474 | + products: ['PRODUCT_1', 'PRODUCT_2'], |
| 475 | + meta: { |
| 476 | + trackingDetails: { |
| 477 | + fdata: '123abc', |
| 478 | + credit_product_identifiers: ['PAY_LATER_LONG_TERM_US'], |
| 479 | + offer_country_code: 'US', |
| 480 | + extra_field: 'should not be present' |
| 481 | + } |
| 482 | + } |
| 483 | + }); |
| 484 | + }); |
401 | 485 | });
|
402 | 486 |
|
403 | 487 | describe('communication with parent window on onClose ', () => {
|
404 | 488 | beforeAll(() => {
|
405 | 489 | mockLoadUrl(
|
406 |
| - 'https://localhost.paypal.com:8080/credit-presentment/native/message?client_id=client_1&logo_type=inline&amount=500&devTouchpoint=true' |
| 490 | + 'https://localhost.paypal.com:8080/credit-presentment/native/modal?client_id=client_1&logo_type=inline&amount=500&devTouchpoint=true' |
407 | 491 | );
|
408 | 492 | zoidPolyfill();
|
409 | 493 | const postMessage = jest.fn();
|
|
0 commit comments