Skip to content

Commit 52ae995

Browse files
committed
[DDW-1076] Improve tests
1 parent b647e76 commit 52ae995

File tree

2 files changed

+172
-99
lines changed

2 files changed

+172
-99
lines changed

source/renderer/app/components/wallet/WalletSendForm.spec.tsx

Lines changed: 154 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,14 @@ describe('wallet/Wallet Send Form', () => {
7878
calculateTransactionFee,
7979
currentNumberFormat = NUMBER_OPTIONS[0].value,
8080
validationDebounceWait,
81+
validateAmount = jest.fn().mockResolvedValue(true),
82+
onTransactionFeeChange = jest.fn(),
8183
}: {
8284
calculateTransactionFee: (...args: Array<any>) => any;
85+
validateAmount?: (amount: string) => Promise<boolean>;
8386
currentNumberFormat?: string;
8487
validationDebounceWait?: number;
88+
onTransactionFeeChange?: () => Promise<void>;
8589
}) {
8690
const [tokenPickerOpen, setTokenPickerOpen] = useState<boolean>(false);
8791
const [state, setState] = useState<{
@@ -98,7 +102,7 @@ describe('wallet/Wallet Send Form', () => {
98102
currencyMaxFractionalDigits={currencyMaxFractionalDigits}
99103
currencyMaxIntegerDigits={11}
100104
currentNumberFormat={currentNumberFormat}
101-
validateAmount={jest.fn().mockResolvedValue(true)}
105+
validateAmount={validateAmount}
102106
validateAssetAmount={jest.fn().mockResolvedValue(true)}
103107
calculateTransactionFee={calculateTransactionFee}
104108
walletAmount={new BigNumber(123)}
@@ -126,6 +130,7 @@ describe('wallet/Wallet Send Form', () => {
126130
onTokenPickerDialogOpen={() => setTokenPickerOpen(true)}
127131
confirmationDialogData={state.formData}
128132
validationDebounceWait={validationDebounceWait}
133+
onTransactionFeeChange={onTransactionFeeChange}
129134
/>
130135
</MobxProvider>
131136
</DiscreetModeFeatureProvider>
@@ -464,87 +469,152 @@ describe('wallet/Wallet Send Form', () => {
464469
assertMinimumAmountNoticeMessage(minimumAda);
465470
});
466471

467-
test('should not allow to submit before fees are calculated', async () => {
468-
expect.assertions(4);
469-
470-
const validationDebounceWait = 0;
471-
472-
const calculateTransactionFeeMock = jest
473-
.fn()
474-
.mockImplementationOnce(
475-
() =>
476-
new Promise(async (resolve) => {
477-
await sleep(5);
478-
479-
return resolve({
480-
fee: new BigNumber(1),
481-
minimumAda: new BigNumber(1),
482-
});
483-
})
484-
)
485-
.mockImplementationOnce(
486-
() =>
487-
new Promise(async (resolve) => {
488-
await sleep(5);
489-
490-
return resolve({
491-
fee: new BigNumber(2),
492-
minimumAda: new BigNumber(2),
493-
});
494-
})
495-
);
496-
497-
render(
498-
<SetupWallet
499-
calculateTransactionFee={calculateTransactionFeeMock}
500-
validationDebounceWait={validationDebounceWait}
501-
/>
502-
);
503-
504-
enterReceiverAddress();
505-
506-
const adaField = await screen.findByLabelText('Ada');
507-
fireEvent.change(adaField, {
508-
target: {
509-
value: 2.5,
510-
},
511-
});
512-
513-
await sleep(validationDebounceWait);
514-
515-
fireEvent.change(adaField, {
516-
target: {
517-
value: 1.5,
518-
},
519-
});
520-
521-
const sendButton: HTMLButtonElement = screen.getByText('Send');
522-
523-
expect(sendButton).not.toBeEnabled();
524-
525-
await waitForTransactionFee();
526-
527-
expect(sendButton).toBeEnabled();
528-
529-
fireEvent.keyPress(adaField, {
530-
key: 'Enter',
531-
code: 13,
532-
charCode: 13,
533-
target: adaField,
534-
});
535-
536-
const adaAmountConfirmation = await screen.findByTestId(
537-
'confirmation-dialog-ada-amount',
538-
{}
539-
);
540-
541-
expect(adaAmountConfirmation).toHaveTextContent('1.500000');
542-
543-
const totalAmountConfirmation = screen.getByTestId(
544-
'confirmation-dialog-total-amount',
545-
{}
546-
);
547-
548-
expect(totalAmountConfirmation).toHaveTextContent('3.500000');
549-
});
472+
type Cases = [
473+
Array<[number, number]>,
474+
[number, number],
475+
Array<[number, number]>,
476+
[string, string, number]
477+
];
478+
479+
const cases: Cases[] = [
480+
[
481+
// ada inputs [delay, value]
482+
[
483+
[0, 2.5],
484+
[0, 1.5],
485+
],
486+
// validate amount [delay]
487+
[50, 5],
488+
// fee calculation [delay, value]
489+
[
490+
[50, 2],
491+
[0, 1],
492+
],
493+
['1.500000', '3.500000', 1],
494+
],
495+
[
496+
[
497+
[0, 2.5],
498+
[0, 1.5],
499+
],
500+
[0, 0],
501+
[
502+
[0, 1],
503+
[0, 2],
504+
],
505+
['1.500000', '3.500000', 1],
506+
],
507+
[
508+
[
509+
[50, 2.5],
510+
[0, 1.5],
511+
],
512+
[0, 0],
513+
[
514+
[0, 1],
515+
[0, 2],
516+
],
517+
['1.500000', '3.500000', 2],
518+
],
519+
];
520+
521+
cases.forEach(
522+
(
523+
[
524+
inputs,
525+
validateAmountParams,
526+
feeCalculationParams,
527+
[expectedAdaAmount, expectedTotalAmount, expectedTimesFeeCalled],
528+
],
529+
idx
530+
) => {
531+
test(`case ${idx}: should not allow to submit before fees are calculated`, async () => {
532+
expect.assertions(5);
533+
534+
const validationDebounceWait = 0;
535+
const onTransactionFeeChangeSpy = jest.fn();
536+
537+
const calculateTransactionFeeMock = jest.fn();
538+
539+
feeCalculationParams.forEach(([delay, value]) => {
540+
calculateTransactionFeeMock.mockImplementationOnce(
541+
() =>
542+
new Promise(async (resolve) => {
543+
await sleep(delay);
544+
return resolve({
545+
fee: new BigNumber(value),
546+
minimumAda: new BigNumber(1),
547+
});
548+
})
549+
);
550+
});
551+
552+
const validateAmountMock = jest.fn();
553+
554+
validateAmountParams.forEach((delay) => {
555+
validateAmountMock.mockImplementationOnce(
556+
() =>
557+
new Promise(async (resolve) => {
558+
await sleep(delay);
559+
return resolve(true);
560+
})
561+
);
562+
});
563+
564+
render(
565+
<SetupWallet
566+
validateAmount={validateAmountMock}
567+
calculateTransactionFee={calculateTransactionFeeMock}
568+
validationDebounceWait={validationDebounceWait}
569+
onTransactionFeeChange={onTransactionFeeChangeSpy}
570+
/>
571+
);
572+
573+
enterReceiverAddress();
574+
575+
const adaField = await screen.findByLabelText('Ada');
576+
577+
for (const [delay, value] of inputs) {
578+
fireEvent.change(adaField, {
579+
target: {
580+
value,
581+
},
582+
});
583+
584+
await sleep(delay);
585+
}
586+
587+
const sendButton: HTMLButtonElement = screen.getByText('Send');
588+
expect(sendButton).not.toBeEnabled();
589+
590+
await waitForTransactionFee();
591+
592+
expect(sendButton).toBeEnabled();
593+
594+
fireEvent.keyPress(adaField, {
595+
key: 'Enter',
596+
code: 13,
597+
charCode: 13,
598+
target: adaField,
599+
});
600+
601+
const adaAmountConfirmation = await screen.findByTestId(
602+
'confirmation-dialog-ada-amount',
603+
{}
604+
);
605+
606+
expect(adaAmountConfirmation).toHaveTextContent(expectedAdaAmount);
607+
608+
const totalAmountConfirmation = screen.getByTestId(
609+
'confirmation-dialog-total-amount',
610+
{}
611+
);
612+
613+
expect(totalAmountConfirmation).toHaveTextContent(expectedTotalAmount);
614+
expect(onTransactionFeeChangeSpy).toHaveBeenCalledTimes(
615+
expectedTimesFeeCalled
616+
);
617+
});
618+
}
619+
);
550620
});

0 commit comments

Comments
 (0)