File size: 40,854 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 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 |
import {
render,
getAllByLabelText as getAllByLabelTextInNode,
getByText as getByTextInNode,
queryByText as queryByTextInNode,
screen,
waitFor,
} from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { createContext, useState, useContext } from 'react';
import {
CheckoutProvider,
CheckoutStep,
CheckoutStepBody,
useIsStepComplete,
useTransactionStatus,
usePaymentProcessor,
CheckoutFormSubmit,
CheckoutStepGroup,
useSetStepComplete,
useTogglePaymentMethod,
makeErrorResponse,
useMakeStepActive,
} from '../src/public-api';
import { PaymentProcessorFunction, PaymentProcessorResponseType } from '../src/types';
import { DefaultCheckoutSteps } from './utils/default-checkout-steps';
const myContext = createContext( undefined );
const usePaymentData = () => useContext( myContext );
function TogglePaymentMethodBlock( { mockMethod } ) {
const togglePaymentMethod = useTogglePaymentMethod();
return (
<div>
<button onClick={ () => togglePaymentMethod( mockMethod.id, false ) }>
Disable Payment Method
</button>
<button onClick={ () => togglePaymentMethod( mockMethod.id, true ) }>
Enable Payment Method
</button>
</div>
);
}
const createMockMethod = createMockMethodFactory();
describe( 'Checkout', () => {
describe( 'using the default steps', function () {
describe( 'using other defaults', function () {
let MyCheckout;
const mockMethod = createMockMethod();
const mockMethod2 = createMockMethod( {
submitButton: <MockSubmitButton content="Second method" />,
activeContent: <div />,
} );
const { items, total } = createMockItems();
beforeEach( () => {
MyCheckout = () => (
<CheckoutProvider
paymentMethods={ [ mockMethod, mockMethod2 ] }
paymentProcessors={ getMockPaymentProcessors() }
selectFirstAvailablePaymentMethod
>
<DefaultCheckoutSteps items={ items } />
<TogglePaymentMethodBlock mockMethod={ mockMethod } />
</CheckoutProvider>
);
} );
it( 'renders the line items and the total', () => {
const { container } = render( <MyCheckout /> );
// Product line items show the correct price
getAllByLabelTextInNode( container, items[ 0 ].label ).map( ( element ) =>
expect( element ).toHaveTextContent( items[ 0 ].amount )
);
getAllByLabelTextInNode( container, items[ 1 ].label ).map( ( element ) =>
expect( element ).toHaveTextContent( items[ 1 ].amount )
);
// All elements labeled 'Total' show the expected price
getAllByLabelTextInNode( container, total.label ).map( ( element ) =>
expect( element ).toHaveTextContent( total.amount )
);
} );
it( 'renders the payment method label', () => {
const { getAllByText } = render( <MyCheckout /> );
expect( getAllByText( mockMethod.inactiveContent )[ 0 ] ).toBeInTheDocument();
expect( getAllByText( mockMethod2.inactiveContent )[ 0 ] ).toBeInTheDocument();
} );
it( 'renders the payment method label as selected', async () => {
const { getAllByText, findByLabelText } = render( <MyCheckout /> );
const user = userEvent.setup();
await user.click( getAllByText( 'Continue' )[ 0 ] );
const paymentMethod = await findByLabelText( mockMethod.inactiveContent );
expect( paymentMethod ).toBeChecked();
} );
it( 'does not render the payment method label if the payment method is disabled', async () => {
const { queryByText, getByText, getAllByText } = render( <MyCheckout /> );
const user = userEvent.setup();
await user.click( getAllByText( 'Continue' )[ 0 ] );
await user.click( getByText( 'Disable Payment Method' ) );
expect( queryByText( mockMethod.inactiveContent ) ).not.toBeVisible();
} );
it( 'does render the payment method label if the payment method is disabled then enabled', async () => {
const { getAllByText, getByText } = render( <MyCheckout /> );
const user = userEvent.setup();
await user.click( getAllByText( 'Continue' )[ 0 ] );
await user.click( getByText( 'Disable Payment Method' ) );
await user.click( getByText( 'Enable Payment Method' ) );
expect( getAllByText( mockMethod.inactiveContent )[ 0 ] ).toBeVisible();
} );
it( 'renders the second payment method label as selected if the first is disabled', async () => {
const { getByText, getAllByText, findByLabelText } = render( <MyCheckout /> );
const user = userEvent.setup();
await user.click( getAllByText( 'Continue' )[ 0 ] );
await user.click( getByText( 'Disable Payment Method' ) );
const paymentMethod = await findByLabelText( mockMethod2.inactiveContent );
expect( paymentMethod ).toBeChecked();
} );
it( 'renders the payment method activeContent', () => {
const { getByTestId } = render( <MyCheckout /> );
const activeComponent = getByTestId( 'mock-payment-form' );
expect( activeComponent ).toHaveTextContent( 'Cardholder Name' );
} );
it( 'renders the review step', () => {
const { getAllByText } = render( <MyCheckout /> );
expect( getAllByText( items[ 0 ].label ) ).toHaveLength( 2 );
expect( getAllByText( items[ 0 ].amount ) ).toHaveLength( 1 );
expect( getAllByText( items[ 1 ].label ) ).toHaveLength( 2 );
expect( getAllByText( items[ 1 ].amount ) ).toHaveLength( 1 );
} );
it( 'renders the payment method submitButton', () => {
const { getByText } = render( <MyCheckout /> );
expect( getByText( 'Pay Please' ) ).toBeTruthy();
} );
} );
describe( 'before clicking a button', function () {
let container;
beforeEach( () => {
const mockMethod = createMockMethod();
const { items } = createMockItems();
const MyCheckout = () => (
<CheckoutProvider
paymentMethods={ [ mockMethod ] }
paymentProcessors={ getMockPaymentProcessors() }
selectFirstAvailablePaymentMethod
>
<DefaultCheckoutSteps items={ items } />
</CheckoutProvider>
);
const renderResult = render( <MyCheckout /> );
container = renderResult.container;
} );
it( 'renders steps', () => {
const activeSteps = container.querySelectorAll( '.checkout-step' );
expect( activeSteps ).toHaveLength( 2 );
} );
it( 'makes the review step active', () => {
const activeSteps = container.querySelectorAll( '.checkout-step.is-active' );
expect( activeSteps ).toHaveLength( 1 );
expect( activeSteps[ 0 ] ).toHaveTextContent( 'Review your order' );
} );
it( 'makes the payment method step invisible', () => {
const firstStep = container.querySelector( '.checkout__payment-method-step' );
const firstStepContent = firstStep.querySelector( '.checkout-steps__step-content' );
expect( firstStepContent ).toHaveStyle( 'display: none' );
} );
it( 'makes the review step visible', () => {
const reviewStep = container.querySelector( '.checkout__review-order-step' );
expect( reviewStep ).toHaveTextContent( 'Review your order' );
const reviewStepContent = reviewStep.querySelector( '.checkout-steps__step-content' );
expect( reviewStepContent ).toHaveStyle( 'display: block' );
} );
} );
describe( 'when clicking continue from the first step', function () {
let container;
beforeEach( async () => {
const mockMethod = createMockMethod();
const { items } = createMockItems();
const MyCheckout = () => (
<CheckoutProvider
paymentMethods={ [ mockMethod ] }
paymentProcessors={ getMockPaymentProcessors() }
selectFirstAvailablePaymentMethod
>
<DefaultCheckoutSteps items={ items } />
</CheckoutProvider>
);
const renderResult = render( <MyCheckout /> );
container = renderResult.container;
const firstStepContinue = renderResult.getAllByText( 'Continue' )[ 0 ];
const user = userEvent.setup();
await user.click( firstStepContinue );
} );
it( 'makes the first step invisible', async () => {
const firstStep = container.querySelector( '.checkout__review-order-step' );
const firstStepContent = firstStep.querySelector( '.checkout-steps__step-content' );
await waitFor( () => {
expect( firstStepContent ).toHaveStyle( 'display: none' );
} );
} );
it( 'makes the next step visible', async () => {
const reviewStep = container.querySelector( '.checkout__payment-method-step' );
const reviewStepContent = reviewStep.querySelector( '.checkout-steps__step-content' );
await waitFor( () => {
expect( reviewStepContent ).toHaveStyle( 'display: block' );
} );
} );
} );
} );
describe( 'with custom steps', function () {
let MyCheckout;
const mockMethod = createMockMethod();
const steps = createMockStepObjects();
let validateForm;
beforeEach( () => {
MyCheckout = ( props ) => {
const [ paymentData, setPaymentData ] = useState( {} );
const { stepObjectsWithStepNumber, stepObjectsWithoutStepNumber } =
createStepsFromStepObjects( props.steps || steps );
const createStepFromStepObject = createStepObjectConverter( paymentData );
return (
<myContext.Provider value={ [ paymentData, setPaymentData ] }>
<CheckoutProvider
paymentMethods={ [ mockMethod ] }
paymentProcessors={ getMockPaymentProcessors() }
selectFirstAvailablePaymentMethod
>
<CheckoutStepGroup>
{ stepObjectsWithoutStepNumber.map( createStepFromStepObject ) }
{ stepObjectsWithStepNumber.map( createStepFromStepObject ) }
<CheckoutFormSubmit validateForm={ validateForm } />
</CheckoutStepGroup>
</CheckoutProvider>
</myContext.Provider>
);
};
} );
it( 'renders the step className', () => {
const { container } = render( <MyCheckout /> );
expect( container.querySelector( '.' + steps[ 0 ].className ) ).toBeTruthy();
expect( container.querySelector( '.' + steps[ 1 ].className ) ).toBeTruthy();
expect( container.querySelector( '.' + steps[ 2 ].className ) ).toBeTruthy();
expect( container.querySelector( '.' + steps[ 3 ].className ) ).toBeTruthy();
} );
it( 'renders the step titleContent', () => {
const { getByText } = render( <MyCheckout /> );
expect( getByText( 'Custom Step - Summary Title' ) ).toBeInTheDocument();
expect( getByText( 'Custom Step - Contact Title' ) ).toBeInTheDocument();
expect( getByText( 'Custom Step - Review Title' ) ).toBeInTheDocument();
expect( getByText( 'Custom Step - Incomplete Title' ) ).toBeInTheDocument();
} );
it( 'renders the step activeStepContent always', () => {
const { getByText } = render( <MyCheckout /> );
expect( getByText( 'Custom Step - Summary Active' ) ).toBeInTheDocument();
expect( getByText( 'Custom Step - Contact Active' ) ).toBeInTheDocument();
expect( getByText( 'Custom Step - Review Active' ) ).toBeInTheDocument();
expect( getByText( 'Custom Step - Incomplete Active' ) ).toBeInTheDocument();
} );
it( 'renders the activeStepContent as visible when active', () => {
const { container } = render( <MyCheckout /> );
const step = container.querySelector( '.' + steps[ 1 ].className );
const content = step.querySelector( '.checkout-steps__step-content' );
expect( content ).toHaveStyle( 'display: block' );
} );
it( 'renders the activeStepContent as invisible when inactive', () => {
const { container } = render( <MyCheckout /> );
let step = container.querySelector( '.' + steps[ 0 ].className );
let content = step.querySelector( '.checkout-steps__step-content' );
expect( content ).toHaveStyle( 'display: none' );
step = container.querySelector( '.' + steps[ 2 ].className );
content = step.querySelector( '.checkout-steps__step-content' );
expect( content ).toHaveStyle( 'display: none' );
} );
it( 'renders the step completeStepContent immediately for complete ContactStepBody', () => {
const { getByText, queryByText } = render( <MyCheckout /> );
expect( getByText( 'Custom Step - Summary Complete' ) ).toBeInTheDocument();
expect( queryByText( 'Custom Step - Contact Complete' ) ).not.toBeInTheDocument();
expect( queryByText( 'Custom Step - Review Complete' ) ).not.toBeInTheDocument();
expect( queryByText( 'Custom Step - Incomplete Complete' ) ).not.toBeInTheDocument();
} );
it( 'renders the completeStepContent as visible when inactive and complete', () => {
const { container } = render( <MyCheckout /> );
let step = container.querySelector( '.' + steps[ 0 ].className );
let content = step.querySelector( '.checkout-steps__step-complete-content' );
expect( content ).toHaveStyle( 'display: block' );
step = container.querySelector( '.' + steps[ 2 ].className );
content = step.querySelector( '.checkout-steps__step-complete-content' );
expect( content ).toBeNull;
step = container.querySelector( '.' + steps[ 3 ].className );
content = step.querySelector( '.checkout-steps__step-complete-content' );
expect( content ).toBeNull;
} );
it( 'does not render the completeStepContent when active', () => {
const { container } = render( <MyCheckout /> );
const step = container.querySelector( '.' + steps[ 1 ].className );
const content = step.querySelector( '.checkout-steps__step-complete-content' );
expect( content ).toBeNull;
} );
it( 'renders the continue button for the active step', () => {
const { container } = render( <MyCheckout /> );
const step = container.querySelector( '.' + steps[ 1 ].className );
expect( getByTextInNode( step, 'Continue' ) ).toBeInTheDocument();
} );
it( 'does not render the continue button for a step without a number', () => {
const { container } = render( <MyCheckout /> );
const step = container.querySelector( '.' + steps[ 0 ].className );
expect( queryByTextInNode( step, 'Continue' ) ).not.toBeInTheDocument();
} );
it( 'does not render the continue button for an inactive step', () => {
const { container } = render( <MyCheckout /> );
const step = container.querySelector( '.' + steps[ 2 ].className );
expect( queryByTextInNode( step, 'Continue' ) ).not.toBeInTheDocument();
} );
it( 'renders the continue button enabled if the step is active and incomplete', () => {
const { container } = render(
<MyCheckout steps={ [ steps[ 0 ], steps[ 4 ], steps[ 1 ] ] } />
);
const step = container.querySelector( '.' + steps[ 4 ].className );
expect( getByTextInNode( step, 'Continue' ) ).not.toBeDisabled();
} );
it( 'does not change steps if the continue button is clicked when the step is active and incomplete', async () => {
const incompleteStep = {
...steps[ 0 ],
hasStepNumber: true,
isCompleteCallback: () => false,
};
const { container, getAllByText } = render(
<MyCheckout steps={ [ incompleteStep, steps[ 4 ], steps[ 1 ] ] } />
);
const firstStepContinue = getAllByText( 'Continue' )[ 0 ];
const firstStep = container.querySelector( '.' + steps[ 0 ].className );
const firstStepContent = firstStep.querySelector( '.checkout-steps__step-content' );
expect( firstStepContent ).toHaveStyle( 'display: block' );
const user = userEvent.setup();
await user.click( firstStepContinue );
expect( firstStepContent ).toHaveStyle( 'display: block' );
} );
it( 'does change steps if the continue button is clicked when the step is active and complete', async () => {
const completeStep = { ...steps[ 0 ], hasStepNumber: true, isCompleteCallback: () => true };
const { container, getAllByText } = render(
<MyCheckout steps={ [ completeStep, steps[ 4 ], steps[ 1 ] ] } />
);
const firstStepContinue = getAllByText( 'Continue' )[ 0 ];
const firstStep = container.querySelector( '.custom-summary-step-class' );
const firstStepContent = firstStep.querySelector( '.checkout-steps__step-content' );
expect( firstStepContent ).toHaveStyle( 'display: block' );
const user = userEvent.setup();
await user.click( firstStepContinue );
await waitFor( () => {
expect( firstStepContent ).toHaveStyle( 'display: none' );
} );
} );
it( 'disables the continue button while isCompleteCallback resolves a Promise', async () => {
const stepWithAsyncIsComplete = {
...steps[ 1 ],
isCompleteCallback: () => new Promise( () => {} ),
};
const { getAllByText } = render(
<MyCheckout steps={ [ stepWithAsyncIsComplete, steps[ 4 ], steps[ 2 ] ] } />
);
const firstStepContinue = getAllByText( 'Continue' )[ 0 ];
expect( firstStepContinue ).not.toBeDisabled();
const user = userEvent.setup();
await user.click( firstStepContinue );
expect( firstStepContinue ).toBeDisabled();
} );
it( 'does change steps if the continue button is clicked and the step becomes complete after a Promise resolves', async () => {
const stepWithAsyncIsComplete = {
...steps[ 1 ],
isCompleteCallback: () => Promise.resolve( true ),
};
const { container, getAllByText } = render(
<MyCheckout steps={ [ stepWithAsyncIsComplete, steps[ 4 ], steps[ 2 ] ] } />
);
const firstStepContinue = getAllByText( 'Continue' )[ 0 ];
const firstStep = container.querySelector( '.' + steps[ 1 ].className );
const firstStepContent = firstStep.querySelector( '.checkout-steps__step-content' );
expect( firstStepContent ).toHaveStyle( 'display: block' );
const user = userEvent.setup();
await user.click( firstStepContinue );
await waitFor( () => {
expect( firstStepContent ).toHaveStyle( 'display: none' );
} );
} );
it( 'changes steps if useMakeStepActive is used', async () => {
function ManualStepChangeButton( { stepId } ) {
const setActiveStep = useMakeStepActive();
return <button onClick={ () => setActiveStep( stepId ) }>Change step</button>;
}
const stepOne = {
...steps[ 1 ],
id: 'step-will-pass',
className: 'step-will-pass',
};
const stepTwo = {
...steps[ 1 ],
activeStepContent: (
<div>
<span>Custom Step - Summary Active</span>
<ManualStepChangeButton stepId={ steps[ 1 ].id } />
</div>
),
};
const { container, getAllByText } = render( <MyCheckout steps={ [ stepOne, stepTwo ] } /> );
const manualContinue = getAllByText( 'Change step' )[ 0 ];
const firstStep = container.querySelector( '.' + stepOne.className );
const secondStep = container.querySelector( '.' + stepTwo.className );
const firstStepContent = firstStep.querySelector( '.checkout-steps__step-content' );
const secondStepContent = secondStep.querySelector( '.checkout-steps__step-content' );
expect( firstStepContent ).toHaveStyle( 'display: block' );
expect( secondStepContent ).toHaveStyle( 'display: none' );
const user = userEvent.setup();
await user.click( manualContinue );
await waitFor( () => {
expect( firstStepContent ).toHaveStyle( 'display: none' );
} );
expect( secondStepContent ).toHaveStyle( 'display: block' );
} );
it( 'changes steps to the first incomplete step if useMakeStepActive is used', async () => {
function ManualStepChangeButton( { stepId } ) {
const setActiveStep = useMakeStepActive();
return <button onClick={ () => setActiveStep( stepId ) }>Change step</button>;
}
const stepOne = {
...steps[ 1 ],
id: 'step-will-fail',
className: 'step-will-fail',
isCompleteCallback: () => Promise.resolve( false ),
};
const stepTwo = {
...steps[ 1 ],
activeStepContent: (
<div>
<span>Custom Step - Summary Active</span>
<ManualStepChangeButton stepId={ steps[ 1 ].id } />
</div>
),
};
const { container, getAllByText } = render( <MyCheckout steps={ [ stepOne, stepTwo ] } /> );
const manualContinue = getAllByText( 'Change step' )[ 0 ];
const firstStep = container.querySelector( '.' + stepOne.className );
const secondStep = container.querySelector( '.' + stepTwo.className );
const firstStepContent = firstStep.querySelector( '.checkout-steps__step-content' );
const secondStepContent = secondStep.querySelector( '.checkout-steps__step-content' );
expect( firstStepContent ).toHaveStyle( 'display: block' );
expect( secondStepContent ).toHaveStyle( 'display: none' );
const user = userEvent.setup();
await user.click( manualContinue );
expect( firstStepContent ).toHaveStyle( 'display: block' );
expect( secondStepContent ).toHaveStyle( 'display: none' );
} );
it( 'does change steps if useSetStepComplete is used and the step becomes complete after a Promise resolves', async () => {
function ManualStepCompleteButton( { stepId } ) {
const setStepComplete = useSetStepComplete();
return <button onClick={ () => setStepComplete( stepId ) }>Complete manually</button>;
}
const stepWillPass = {
...steps[ 1 ],
id: 'step-will-pass',
className: 'step-will-pass',
isCompleteCallback: () => Promise.resolve( true ),
};
const stepWithAsyncIsComplete = {
...steps[ 1 ],
isCompleteCallback: () => Promise.resolve( true ),
activeStepContent: (
<div>
<span>Custom Step - Summary Active</span>
<ManualStepCompleteButton stepId={ steps[ 1 ].id } />
</div>
),
};
const { container, getAllByText } = render(
<MyCheckout steps={ [ stepWillPass, stepWithAsyncIsComplete, steps[ 4 ], steps[ 2 ] ] } />
);
const manualContinue = getAllByText( 'Complete manually' )[ 0 ];
const firstStep = container.querySelector( '.' + stepWillPass.className );
const secondStep = container.querySelector( '.' + stepWithAsyncIsComplete.className );
const thirdStep = container.querySelector( '.' + steps[ 4 ].className );
const firstStepContent = firstStep.querySelector( '.checkout-steps__step-content' );
const secondStepContent = secondStep.querySelector( '.checkout-steps__step-content' );
const thirdStepContent = thirdStep.querySelector( '.checkout-steps__step-content' );
expect( firstStepContent ).toHaveStyle( 'display: block' );
expect( secondStepContent ).toHaveStyle( 'display: none' );
const user = userEvent.setup();
await user.click( manualContinue );
await waitFor( () => {
expect( firstStepContent ).toHaveStyle( 'display: none' );
} );
expect( secondStepContent ).toHaveStyle( 'display: none' );
expect( thirdStepContent ).toHaveStyle( 'display: block' );
} );
it( 'does not change steps if useSetStepComplete is used and the previous step remains incomplete', async () => {
function ManualStepCompleteButton( { stepId } ) {
const setStepComplete = useSetStepComplete();
return <button onClick={ () => setStepComplete( stepId ) }>Complete manually</button>;
}
const stepWillFail = {
...steps[ 1 ],
id: 'step-will-fail',
className: 'step-will-fail',
isCompleteCallback: () => Promise.resolve( false ),
};
const stepWithAsyncIsComplete = {
...steps[ 1 ],
isCompleteCallback: () => Promise.resolve( true ),
activeStepContent: (
<div>
<span>Custom Step - Summary Active</span>
<ManualStepCompleteButton stepId={ steps[ 1 ].id } />
</div>
),
};
const { container, getAllByText } = render(
<MyCheckout steps={ [ stepWillFail, stepWithAsyncIsComplete, steps[ 4 ], steps[ 2 ] ] } />
);
const manualContinue = getAllByText( 'Complete manually' )[ 0 ];
const firstStep = container.querySelector( '.' + stepWillFail.className );
const secondStep = container.querySelector( '.' + stepWithAsyncIsComplete.className );
const firstStepContent = firstStep.querySelector( '.checkout-steps__step-content' );
const secondStepContent = secondStep.querySelector( '.checkout-steps__step-content' );
expect( firstStepContent ).toHaveStyle( 'display: block' );
expect( secondStepContent ).toHaveStyle( 'display: none' );
const user = userEvent.setup();
await user.click( manualContinue );
expect( firstStepContent ).toHaveStyle( 'display: block' );
expect( secondStepContent ).toHaveStyle( 'display: none' );
} );
it( 'does not change steps if the continue button is clicked and the step remains incomplete after a Promise resolves', async () => {
const stepWithAsyncIsComplete = {
...steps[ 1 ],
isCompleteCallback: () => Promise.resolve( false ),
};
const { container, getAllByText } = render(
<MyCheckout steps={ [ stepWithAsyncIsComplete, steps[ 4 ], steps[ 2 ] ] } />
);
const firstStepContinue = getAllByText( 'Continue' )[ 0 ];
const firstStep = container.querySelector( '.' + steps[ 1 ].className );
const firstStepContent = firstStep.querySelector( '.checkout-steps__step-content' );
expect( firstStepContent ).toHaveStyle( 'display: block' );
const user = userEvent.setup();
await user.click( firstStepContinue );
expect( firstStepContent ).toHaveStyle( 'display: block' );
} );
it( 'does not change steps if useSetStepComplete is used and the step remains incomplete after a Promise resolves', async () => {
function ManualStepCompleteButton( { stepId } ) {
const setStepComplete = useSetStepComplete();
return <button onClick={ () => setStepComplete( stepId ) }>Complete manually</button>;
}
const stepWithAsyncIsComplete = {
...steps[ 1 ],
isCompleteCallback: () => Promise.resolve( false ),
activeStepContent: (
<div>
<span>Custom Step - Summary Active</span>
<ManualStepCompleteButton stepId={ steps[ 1 ].id } />
</div>
),
};
const { container, getAllByText } = render(
<MyCheckout steps={ [ stepWithAsyncIsComplete, steps[ 4 ], steps[ 2 ] ] } />
);
const manualContinue = getAllByText( 'Complete manually' )[ 0 ];
const firstStep = container.querySelector( '.' + steps[ 1 ].className );
const firstStepContent = firstStep.querySelector( '.checkout-steps__step-content' );
expect( firstStepContent ).toHaveStyle( 'display: block' );
const user = userEvent.setup();
await user.click( manualContinue );
expect( firstStepContent ).toHaveStyle( 'display: block' );
} );
it( 'renders the continue button enabled if the step is active and complete', async () => {
const { container, getByLabelText } = render(
<MyCheckout steps={ [ steps[ 0 ], steps[ 4 ], steps[ 1 ] ] } />
);
const user = userEvent.setup();
await user.type( getByLabelText( 'User Name' ), 'Lyra' );
const step = container.querySelector( '.' + steps[ 4 ].className );
expect( getByTextInNode( step, 'Continue' ) ).not.toBeDisabled();
} );
it( 'does not render the edit button for steps without a number', () => {
const { container } = render( <MyCheckout /> );
const step = container.querySelector( '.' + steps[ 0 ].className );
expect( queryByTextInNode( step, 'Edit' ) ).not.toBeInTheDocument();
} );
it( 'does not render the edit button for the active step', () => {
const { container } = render( <MyCheckout /> );
const step = container.querySelector( '.' + steps[ 1 ].className );
expect( queryByTextInNode( step, 'Edit' ) ).not.toBeInTheDocument();
} );
it( 'does not render the edit button for editable steps with a higher index than the active step', () => {
const { container } = render( <MyCheckout /> );
const step = container.querySelector( '.' + steps[ 2 ].className );
expect( queryByTextInNode( step, 'Edit' ) ).not.toBeInTheDocument();
} );
it( 'renders the edit button for editable steps with a lower index than the active step', async () => {
const { container, getAllByText } = render( <MyCheckout /> );
const firstStepContinue = getAllByText( 'Continue' )[ 0 ];
const user = userEvent.setup();
await user.click( firstStepContinue );
const step = container.querySelector( '.' + steps[ 1 ].className );
await waitFor( () => {
expect( getByTextInNode( step, 'Edit' ) ).toBeInTheDocument();
} );
} );
it( 'does not render the edit button if the form status is submitting', async () => {
const { queryByText, getAllByText } = render(
<MyCheckout steps={ [ steps[ 0 ], steps[ 1 ], steps[ 2 ] ] } />
);
const firstStepContinue = getAllByText( 'Continue' )[ 0 ];
const user = userEvent.setup();
await user.click( firstStepContinue );
await waitFor( () => {
expect( queryByText( 'Edit' ) ).toBeInTheDocument();
} );
const submitButton = getAllByText( 'Pay Please' )[ 0 ];
await user.click( submitButton );
expect( queryByText( 'Edit' ) ).not.toBeInTheDocument();
} );
it( 'renders the payment method submitButton', () => {
const { getByText } = render( <MyCheckout /> );
expect( getByText( 'Pay Please' ) ).toBeTruthy();
} );
it( 'renders the payment method submitButton disabled if any steps are incomplete and the last step is not active', () => {
const { getByText } = render(
<MyCheckout steps={ [ steps[ 0 ], steps[ 1 ], steps[ 2 ] ] } />
);
expect( getByText( 'Pay Please' ) ).toBeDisabled();
} );
it( 'renders the payment method submitButton enabled if any steps are incomplete and the last step is active', () => {
const { getByText } = render( <MyCheckout steps={ [ steps[ 0 ], steps[ 3 ] ] } /> );
expect( getByText( 'Pay Please' ) ).not.toBeDisabled();
} );
it( 'renders the payment method submitButton disabled if all steps are complete but the last step is not active', () => {
const { getByText } = render(
<MyCheckout steps={ [ steps[ 0 ], steps[ 1 ], steps[ 2 ] ] } />
);
expect( getByText( 'Pay Please' ) ).toBeDisabled();
} );
it( 'renders the payment method submitButton active if all steps are complete and the last step is active', () => {
const { getByText } = render( <MyCheckout steps={ [ steps[ 0 ], steps[ 1 ] ] } /> );
expect( getByText( 'Pay Please' ) ).not.toBeDisabled();
} );
it( 'provides useIsStepComplete that changes based on isCompleteCallback', async () => {
const { getAllByText, getByText, getByLabelText } = render(
<MyCheckout steps={ [ steps[ 4 ], steps[ 1 ] ] } />
);
expect( getByText( 'Possibly Complete isComplete false' ) ).toBeInTheDocument();
const firstStepContinue = getAllByText( 'Continue' )[ 0 ];
const user = userEvent.setup();
await user.click( firstStepContinue );
await user.type( getByLabelText( 'User Name' ), 'Lyra' );
// isComplete does not update until we press continue
await user.click( firstStepContinue );
await waitFor( () => {
expect( getByText( 'Possibly Complete isComplete true' ) ).toBeInTheDocument();
} );
} );
} );
describe( 'submitting the form', function () {
let MyCheckout;
const submitButtonComponent = <MockSubmitButtonSimple />;
const mockMethod = createMockMethod( { submitButton: submitButtonComponent } );
const steps = createMockStepObjects();
beforeEach( () => {
MyCheckout = ( props ) => {
const [ paymentData, setPaymentData ] = useState( {} );
const { stepObjectsWithStepNumber, stepObjectsWithoutStepNumber } =
createStepsFromStepObjects( props.steps || steps );
const createStepFromStepObject = createStepObjectConverter( paymentData );
return (
<myContext.Provider value={ [ paymentData, setPaymentData ] }>
<CheckoutProvider
paymentMethods={ [ mockMethod ] }
paymentProcessors={ { mock: props.paymentProcessor } }
selectFirstAvailablePaymentMethod
>
<CheckoutStepGroup>
{ stepObjectsWithoutStepNumber.map( createStepFromStepObject ) }
{ stepObjectsWithStepNumber.map( createStepFromStepObject ) }
<CheckoutFormSubmit validateForm={ props.validateForm } />
</CheckoutStepGroup>
</CheckoutProvider>
</myContext.Provider>
);
};
} );
it( 'does not call the payment processor function if the validateForm callback is falsy', async () => {
const validateForm = jest.fn();
validateForm.mockResolvedValue( false );
const processor = jest.fn().mockResolvedValue( makeErrorResponse( 'good' ) );
render(
<MyCheckout
steps={ [ steps[ 0 ] ] }
validateForm={ validateForm }
paymentProcessor={ processor }
/>
);
const submitButton = screen.getByText( 'Pay Please' );
const user = userEvent.setup();
await user.click( submitButton );
expect( processor ).not.toHaveBeenCalled();
} );
it( 'calls the payment processor function if the validateForm callback is truthy', async () => {
const validateForm = jest.fn();
validateForm.mockResolvedValue( true );
const processor = jest.fn().mockResolvedValue( makeErrorResponse( 'good' ) );
render(
<MyCheckout
steps={ [ steps[ 0 ] ] }
validateForm={ validateForm }
paymentProcessor={ processor }
/>
);
const submitButton = screen.getByText( 'Pay Please' );
const user = userEvent.setup();
await user.click( submitButton );
expect( processor ).toHaveBeenCalled();
} );
it( 'calls the payment processor function if the validateForm callback undefined', async () => {
const processor = jest.fn().mockResolvedValue( makeErrorResponse( 'good' ) );
render( <MyCheckout steps={ [ steps[ 0 ] ] } paymentProcessor={ processor } /> );
const submitButton = screen.getByText( 'Pay Please' );
const user = userEvent.setup();
await user.click( submitButton );
expect( processor ).toHaveBeenCalled();
} );
} );
} );
function createMockMethodFactory() {
let mockMethodIdCounter = 0;
return function ( {
submitButton = <MockSubmitButton content="Pay Please" />,
activeContent = <MockPaymentForm />,
} = {} ) {
mockMethodIdCounter = mockMethodIdCounter + 1;
const title = `Mock Payment Method ${ mockMethodIdCounter }`;
return {
id: 'mock' + mockMethodIdCounter,
paymentProcessorId: 'mock',
label: <span data-testid="mock-label">{ title }</span>,
activeContent,
submitButton,
inactiveContent: title,
getAriaLabel: () => title,
};
};
}
function MockSubmitButton( { disabled, content }: { disabled?: boolean; content: string } ) {
const { setTransactionComplete, setTransactionPending } = useTransactionStatus();
const process = usePaymentProcessor( 'mock' );
const onClick = () => {
setTransactionPending();
process( true ).then( ( result ) => setTransactionComplete( result ) );
};
return (
<button disabled={ disabled } onClick={ onClick }>
{ content }
</button>
);
}
function MockPaymentForm( { summary } ) {
const [ cardholderName, changeCardholderName ] = useState( '' );
return (
<div data-testid="mock-payment-form">
<label>
{ summary ? 'Name Summary' : 'Cardholder Name' }
<input
name="cardholderName"
value={ cardholderName }
onChange={ ( event ) => changeCardholderName( event.target.value ) }
/>
</label>
</div>
);
}
function MockSubmitButtonSimple( { disabled, onClick }: { disabled?: boolean; onClick: any } ) {
return (
<button disabled={ disabled } onClick={ onClick }>
Pay Please
</button>
);
}
interface LineItem {
type: string;
id: string;
label: string;
amount: number;
}
function createMockItems(): { items: LineItem[]; total: LineItem } {
const items = [
{
label: 'Illudium Q-36 Explosive Space Modulator',
id: 'space-modulator',
type: 'widget',
amount: 5500,
},
{
label: 'Air Jordans',
id: 'sneakers',
type: 'apparel',
amount: 12000,
},
];
const total = {
label: 'Total',
id: 'total',
type: 'total',
amount: 17500,
};
return { items, total };
}
function createStepsFromStepObjects( stepObjects ) {
const stepObjectsWithoutStepNumber = stepObjects.filter(
( stepObject ) => ! stepObject.hasStepNumber
);
const stepObjectsWithStepNumber = stepObjects.filter(
( stepObject ) => stepObject.hasStepNumber
);
return {
stepObjectsWithStepNumber,
stepObjectsWithoutStepNumber,
};
}
function createStepObjectConverter( paymentData ) {
return function createStepFromStepObject( stepObject ) {
if ( stepObject.hasStepNumber ) {
return (
<CheckoutStep
activeStepContent={ stepObject.activeStepContent }
completeStepContent={ stepObject.completeStepContent }
titleContent={ stepObject.titleContent }
stepId={ stepObject.id }
key={ stepObject.id }
isCompleteCallback={ () => stepObject.isCompleteCallback( { paymentData } ) }
editButtonAriaLabel={ stepObject.getEditButtonAriaLabel() }
nextStepButtonAriaLabel={ stepObject.getNextStepButtonAriaLabel() }
className={ stepObject.className }
/>
);
}
return (
<CheckoutStepBody
errorMessage="error"
editButtonAriaLabel={ stepObject.getEditButtonAriaLabel() }
nextStepButtonAriaLabel={ stepObject.getNextStepButtonAriaLabel() }
isStepActive={ false }
isStepComplete
stepNumber={ 1 }
stepId={ stepObject.id }
key={ stepObject.id }
activeStepContent={ stepObject.activeStepContent }
completeStepContent={ stepObject.completeStepContent }
titleContent={ stepObject.titleContent }
className={ stepObject.className }
/>
);
};
}
function createMockStepObjects() {
return [
{
id: 'custom-summary-step',
className: 'custom-summary-step-class',
hasStepNumber: false,
titleContent: <span>Custom Step - Summary Title</span>,
activeStepContent: <span>Custom Step - Summary Active</span>,
completeStepContent: <span>Custom Step - Summary Complete</span>,
isCompleteCallback: () => true,
getEditButtonAriaLabel: () => 'Custom Step - Summary edit button label',
getNextStepButtonAriaLabel: () => 'Custom Step - Summary next button label',
},
{
id: 'custom-contact-step',
className: 'custom-contact-step-class',
hasStepNumber: true,
titleContent: <span>Custom Step - Contact Title</span>,
activeStepContent: <span>Custom Step - Contact Active</span>,
completeStepContent: <span>Custom Step - Contact Complete</span>,
isCompleteCallback: () => true,
getEditButtonAriaLabel: () => 'Custom Step - Contact edit button label',
getNextStepButtonAriaLabel: () => 'Custom Step - Contact next button label',
},
{
id: 'custom-review-step',
className: 'custom-review-step-class',
hasStepNumber: true,
titleContent: <span>Custom Step - Review Title</span>,
activeStepContent: <span>Custom Step - Review Active</span>,
completeStepContent: <span>Custom Step - Review Complete</span>,
isCompleteCallback: () => true,
getEditButtonAriaLabel: () => 'Custom Step - Review edit button label',
getNextStepButtonAriaLabel: () => 'Custom Step - Review next button label',
},
{
id: 'custom-incomplete-step',
className: 'custom-incomplete-step-class',
hasStepNumber: true,
titleContent: <span>Custom Step - Incomplete Title</span>,
activeStepContent: <span>Custom Step - Incomplete Active</span>,
completeStepContent: <span>Custom Step - Incomplete Complete</span>,
isCompleteCallback: () => false,
getEditButtonAriaLabel: () => 'Custom Step - Incomplete edit button label',
getNextStepButtonAriaLabel: () => 'Custom Step - Incomplete next button label',
},
{
id: 'custom-possibly-complete-step',
className: 'custom-possibly-complete-step-class',
hasStepNumber: true,
titleContent: <PossiblyCompleteTitle />,
activeStepContent: <StepWithEditableField />,
completeStepContent: <span>Custom Step - Possibly Complete Complete</span>,
isCompleteCallback: ( { paymentData } ) => {
return paymentData.userName && paymentData.userName.length > 0 ? true : false;
},
getEditButtonAriaLabel: () => 'Custom Step - Incomplete edit button label',
getNextStepButtonAriaLabel: () => 'Custom Step - Incomplete next button label',
},
{
id: 'custom-uneditable-step',
className: 'custom-uneditable-step-class',
hasStepNumber: true,
titleContent: <span>Custom Step - Uneditable Title</span>,
activeStepContent: <span>Custom Step - Uneditable Active</span>,
completeStepContent: <span>Custom Step - Uneditable Complete</span>,
isCompleteCallback: () => true,
getEditButtonAriaLabel: () => 'Custom Step - Uneditable edit button label',
getNextStepButtonAriaLabel: () => 'Custom Step - Uneditable next button label',
},
];
}
function PossiblyCompleteTitle() {
const isComplete = useIsStepComplete();
const text = `Possibly Complete isComplete ${ isComplete ? 'true' : 'false' }`;
return (
<div>
<span>Custom Step - Possibly Complete Title</span>
<span>{ text }</span>
</div>
);
}
function StepWithEditableField() {
const [ paymentData, setPaymentData ] = usePaymentData();
const onChange = ( event ) => {
setPaymentData( { userName: event.target.value } );
};
const value = paymentData.userName || '';
return (
<div>
<label htmlFor="username-field">User Name</label>
<input id="username-field" type="text" value={ value } onChange={ onChange } />
</div>
);
}
function getMockPaymentProcessors(): Record< string, PaymentProcessorFunction > {
return {
mock: async () => ( { type: PaymentProcessorResponseType.SUCCESS, payload: true } ),
};
}
|