|
1 | 1 | import { createCheckoutService, createLanguageService } from '@bigcommerce/checkout-sdk'; |
2 | 2 | import { createOfflinePaymentStrategy } from '@bigcommerce/checkout-sdk/integrations/offline'; |
| 3 | +import { Formik } from 'formik'; |
3 | 4 | import React from 'react'; |
4 | 5 |
|
5 | 6 | import { type PaymentMethodProps } from '@bigcommerce/checkout/payment-integration-api'; |
6 | | -import { render } from '@bigcommerce/checkout/test-utils'; |
| 7 | +import { fireEvent, render, screen } from '@bigcommerce/checkout/test-utils'; |
7 | 8 |
|
8 | 9 | import OfflinePaymentMethod from './OfflinePaymentMethod'; |
9 | 10 | import { getMethod } from './payment-method.mock'; |
@@ -72,4 +73,102 @@ describe('OfflinePaymentMethod', () => { |
72 | 73 |
|
73 | 74 | await expect(checkoutService.deinitializePayment).rejects.toThrow('test error'); |
74 | 75 | }); |
| 76 | + |
| 77 | + describe('formFieldsData', () => { |
| 78 | + const formFieldsData = [ |
| 79 | + { name: 'Purchase Order', id: 'purchaseOrderNumber', required: true, type: 'number', fieldType: 'text' }, |
| 80 | + { name: 'Reference', id: 'referenceText', required: false, type: 'text', fieldType: 'text' }, |
| 81 | + ]; |
| 82 | + |
| 83 | + const renderWithFormik = (props: PaymentMethodProps) => |
| 84 | + render( |
| 85 | + <Formik initialValues={{}} onSubmit={jest.fn()}> |
| 86 | + <OfflinePaymentMethod {...props} /> |
| 87 | + </Formik>, |
| 88 | + ); |
| 89 | + |
| 90 | + it('returns null when formFieldsData is not present', () => { |
| 91 | + const { container } = render(<OfflinePaymentMethod {...defaultProps} />); |
| 92 | + |
| 93 | + expect(container).toBeEmptyDOMElement(); |
| 94 | + }); |
| 95 | + |
| 96 | + it('returns null when formFieldsData is an empty array', () => { |
| 97 | + const props = { |
| 98 | + ...defaultProps, |
| 99 | + method: { ...defaultProps.method, initializationData: { formFieldsData: [] } }, |
| 100 | + }; |
| 101 | + |
| 102 | + const { container } = render(<OfflinePaymentMethod {...props} />); |
| 103 | + |
| 104 | + expect(container).toBeEmptyDOMElement(); |
| 105 | + }); |
| 106 | + |
| 107 | + it('renders a field for each entry in formFieldsData', () => { |
| 108 | + const props = { |
| 109 | + ...defaultProps, |
| 110 | + method: { ...defaultProps.method, initializationData: { formFieldsData } }, |
| 111 | + }; |
| 112 | + |
| 113 | + renderWithFormik(props); |
| 114 | + |
| 115 | + expect(screen.getByLabelText('Purchase Order')).toBeInTheDocument(); |
| 116 | + expect(screen.getByLabelText('Reference')).toBeInTheDocument(); |
| 117 | + }); |
| 118 | + |
| 119 | + it('strips non-digit characters from a number field on change', () => { |
| 120 | + const props = { |
| 121 | + ...defaultProps, |
| 122 | + method: { ...defaultProps.method, initializationData: { formFieldsData } }, |
| 123 | + }; |
| 124 | + |
| 125 | + renderWithFormik(props); |
| 126 | + |
| 127 | + const input = screen.getByLabelText('Purchase Order'); |
| 128 | + |
| 129 | + fireEvent.change(input, { target: { value: '123abc' } }); |
| 130 | + |
| 131 | + expect((input as HTMLInputElement).value).toBe('123'); |
| 132 | + }); |
| 133 | + |
| 134 | + it('shows a validation error for a required field left empty', async () => { |
| 135 | + const props = { |
| 136 | + ...defaultProps, |
| 137 | + method: { ...defaultProps.method, initializationData: { formFieldsData } }, |
| 138 | + }; |
| 139 | + |
| 140 | + renderWithFormik(props); |
| 141 | + |
| 142 | + const input = screen.getByLabelText('Purchase Order'); |
| 143 | + |
| 144 | + fireEvent.blur(input); |
| 145 | + |
| 146 | + expect(await screen.findByText('Purchase Order is required')).toBeInTheDocument(); |
| 147 | + }); |
| 148 | + |
| 149 | + it('does not show a validation error for an optional field left empty', async () => { |
| 150 | + const props = { |
| 151 | + ...defaultProps, |
| 152 | + method: { ...defaultProps.method, initializationData: { formFieldsData } }, |
| 153 | + }; |
| 154 | + |
| 155 | + renderWithFormik(props); |
| 156 | + |
| 157 | + fireEvent.blur(screen.getByLabelText('Reference')); |
| 158 | + |
| 159 | + expect(screen.queryByText('Reference is required')).not.toBeInTheDocument(); |
| 160 | + }); |
| 161 | + |
| 162 | + // it('uses numeric inputMode for number type fields', () => { |
| 163 | + // const props = { |
| 164 | + // ...defaultProps, |
| 165 | + // method: { ...defaultProps.method, initializationData: { formFieldsData } }, |
| 166 | + // }; |
| 167 | + // |
| 168 | + // renderWithFormik(props); |
| 169 | + // |
| 170 | + // expect(screen.getByLabelText('Purchase Order')).toHaveAttribute('inputmode', 'numeric'); |
| 171 | + // expect(screen.getByLabelText('Reference')).not.toHaveAttribute('inputmode'); |
| 172 | + // }); |
| 173 | + }); |
75 | 174 | }); |
0 commit comments