Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: keep polishing onStatus lifecycle #1055

Merged
merged 11 commits into from
Aug 15, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
dope
Zizzamia committed Aug 15, 2024
commit 42abe375906b81aacaa79e4f5c4293d450b92e43
65 changes: 10 additions & 55 deletions src/transaction/components/TransactionProvider.test.tsx
Original file line number Diff line number Diff line change
@@ -133,18 +133,18 @@ describe('TransactionProvider', () => {
});
});

it('should call onsuccess when receipt exists', async () => {
it('should call onSuccess when one receipt exist', async () => {
const onSuccessMock = vi.fn();
(useWaitForTransactionReceipt as ReturnType<typeof vi.fn>).mockReturnValue({
data: '123',
data: { status: 'success' },
});
(useCallsStatus as ReturnType<typeof vi.fn>).mockReturnValue({
transactionHash: 'hash',
});
render(
<TransactionProvider
address="0x123"
contracts={[]}
contracts={[{ address: '0x123', method: 'method' }]}
onSuccess={onSuccessMock}
>
<TestComponent />
@@ -154,9 +154,16 @@ describe('TransactionProvider', () => {
fireEvent.click(button);
await waitFor(() => {
expect(onSuccessMock).toHaveBeenCalled();
expect(onSuccessMock).toHaveBeenCalledWith({
transactionReceipts: [{ status: 'success' }],
});
});
});

it('should call onSuccess when many receipts are available', async () => {
// todo: implement test
});

it('should handle errors during submission', async () => {
const writeContractsAsyncMock = vi
.fn()
@@ -252,30 +259,6 @@ describe('TransactionProvider', () => {
});
});

it('should call onSuccess when receipts are available', async () => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apperently this test was not testing what was suppose to test 🤦🏻‍♂️

const onSuccessMock = vi.fn();
(useWaitForTransactionReceipt as ReturnType<typeof vi.fn>).mockReturnValue({
data: { status: 'success' },
});
(useCallsStatus as ReturnType<typeof vi.fn>).mockReturnValue({
transactionHash: 'hash',
});
render(
<TransactionProvider
address="0x123"
contracts={[]}
onSuccess={onSuccessMock}
>
<TestComponent />
</TransactionProvider>,
);
await waitFor(() => {
expect(onSuccessMock).toHaveBeenCalledWith({
transactionReceipts: [{ status: 'success' }],
});
});
});

it('should handle chain switching', async () => {
const switchChainAsyncMock = vi.fn();
(useSwitchChain as ReturnType<typeof vi.fn>).mockReturnValue({
@@ -382,34 +365,6 @@ describe('TransactionProvider', () => {
);
});
});

it('should call onSuccess when receiptArray has receipts', async () => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same for this one. I am rewriting the entire logic in a more simple way so we can actually test this.

const onSuccessMock = vi.fn();
const mockReceipt = { status: 'success' };

(useWaitForTransactionReceipt as ReturnType<typeof vi.fn>).mockReturnValue({
data: mockReceipt,
});

render(
<TransactionProvider
address="0x123"
contracts={[]}
onSuccess={onSuccessMock}
>
<TestComponent />
</TransactionProvider>,
);

const button = screen.getByText('Submit');
fireEvent.click(button);

await waitFor(() => {
expect(onSuccessMock).toHaveBeenCalledWith({
transactionReceipts: [mockReceipt],
});
});
});
});

describe('useTransactionContext', () => {
3 changes: 3 additions & 0 deletions src/transaction/components/TransactionProvider.tsx
Original file line number Diff line number Diff line change
@@ -106,6 +106,7 @@ export function TransactionProvider({
transactionReceipts: lifeCycleStatus.statusData.transactionReceipts,
});
}
console.log('lifeCycleStatus', lifeCycleStatus);
// Emit State
onStatus?.(lifeCycleStatus);
}, [
@@ -137,6 +138,7 @@ export function TransactionProvider({
});
}
}
console.log('getTransactionReceipts', receipts?.length);
setLifeCycleStatus({
statusName: 'success',
statusData: {
@@ -147,6 +149,7 @@ export function TransactionProvider({

// Trigger success status when receipts are generated from waitForTransactionReceipt
useEffect(() => {
console.log('transactionHashArray', transactionHashArray?.length);
if (
transactionHashArray.length === contracts.length &&
contracts?.length > 1
Empty file.