Skip to content

Commit

Permalink
fix(upi): initial state upi intent (#2921)
Browse files Browse the repository at this point in the history
* fix(upi): initial state

* refactor: fix tests
  • Loading branch information
longyulongyu authored Oct 28, 2024
1 parent 4fb7df4 commit 36f50de
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/lovely-dodos-visit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@adyen/adyen-web": patch
---

Fix `UPIComponent` initial value for `isValid`. It should only be default to `true` for UPI QR.
84 changes: 82 additions & 2 deletions packages/lib/src/components/UPI/UPI.test.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { render, screen } from '@testing-library/preact';
import { render, screen, waitFor } from '@testing-library/preact';
import userEvent from '@testing-library/user-event';
import UPI from './UPI';
import isMobile from '../../utils/isMobile';
import { SRPanel } from '../../core/Errors/SRPanel';
Expand Down Expand Up @@ -102,11 +103,90 @@ describe('UPI', () => {
});
});

describe('isValid', () => {
describe('select the QR code mode', () => {
test('should be valid', async () => {
isMobileMock.mockReturnValue(false);
const upi = new UPI(global.core, { ...props, defaultMode: 'qrCode' });
render(upi.render());
await waitFor(() => {
expect(upi.isValid).toBe(true);
});
});
});

describe('select the vpa mode', () => {
test('should not be valid on init', async () => {
isMobileMock.mockReturnValue(false);
const upi = new UPI(global.core, { ...props, defaultMode: 'vpa' });
render(upi.render());
await waitFor(() => {
expect(upi.isValid).toBe(false);
});
});

test('should be valid when filling in the vpa', async () => {
isMobileMock.mockReturnValue(false);
const upi = new UPI(global.core, { ...props, defaultMode: 'vpa' });
render(upi.render());
const user = userEvent.setup();
const vpaInput = await screen.findByLabelText(/Enter UPI ID \/ VPA/i);
await user.type(vpaInput, 'test@test');
expect(upi.isValid).toBe(true);
});
});

describe('select the intent mode', () => {
beforeEach(() => {
isMobileMock.mockReturnValue(true);
});

test('should not be valid on init', async () => {
const upi = new UPI(global.core, { ...props, apps: [{ id: 'gpay', name: 'Google Pay' }] });
render(upi.render());
await waitFor(() => {
expect(upi.isValid).toBe(false);
});
});

test('should be valid when selecting other apps', async () => {
const upi = new UPI(global.core, { ...props, apps: [{ id: 'gpay', name: 'Google Pay' }] });
render(upi.render());
const user = userEvent.setup();
const radioButton = await screen.findByRole('radio', { name: /google pay/i });
await user.click(radioButton);
expect(upi.isValid).toBe(true);
});

test('should not be valid when selecting upi collect', async () => {
const upi = new UPI(global.core, { ...props, apps: [{ id: 'gpay', name: 'Google Pay' }] });
render(upi.render());
const user = userEvent.setup();
const radioButton = await screen.findByRole('radio', { name: /Enter UPI ID/i });
await user.click(radioButton);
expect(upi.isValid).toBe(false);
});

test('should be valid when filling the vpa', async () => {
const upi = new UPI(global.core, { ...props, apps: [{ id: 'gpay', name: 'Google Pay' }] });
render(upi.render());
const user = userEvent.setup();
const radioButton = await screen.findByRole('radio', { name: /Enter UPI ID/i });
await user.click(radioButton);
expect(upi.isValid).toBe(false);

const vpaInput = await screen.findByLabelText(/Enter UPI ID \/ VPA/i);
await user.type(vpaInput, 'test@test');
expect(upi.isValid).toBe(true);
});
});
});

describe('render', () => {
test('should render the UPI component by default', async () => {
const upi = new UPI(global.core, props);
render(upi.render());
expect(await screen.findByRole('group')).toBeInTheDocument;
expect(await screen.findByRole('group')).toBeInTheDocument();
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export default function UPIComponent({ defaultMode, onChange, onUpdateMode, payB
const { i18n } = useCoreContext();
const getImage = useImage();
const [status, setStatus] = useState<UIElementStatus>('ready');
const [isValid, setIsValid] = useState<boolean>(true);
const [isValid, setIsValid] = useState<boolean>(defaultMode === 'qrCode');
const [mode, setMode] = useState<UpiMode>(defaultMode);
const [vpa, setVpa] = useState<string>('');
const [vpaInputHandlers, setVpaInputHandlers] = useState<VpaInputHandlers>(null);
Expand Down

0 comments on commit 36f50de

Please sign in to comment.