|
1 | 1 | import { Messenger } from '@metamask/base-controller';
|
2 |
| -import type { CaipAssetType, Transaction } from '@metamask/keyring-api'; |
| 2 | +import type { |
| 3 | + AccountTransactionsUpdatedEventPayload, |
| 4 | + CaipAssetType, |
| 5 | + TransactionsPage, |
| 6 | +} from '@metamask/keyring-api'; |
3 | 7 | import {
|
4 | 8 | BtcAccountType,
|
5 | 9 | BtcMethod,
|
@@ -130,7 +134,7 @@ const setupController = ({
|
130 | 134 | state?: MultichainTransactionsControllerState;
|
131 | 135 | mocks?: {
|
132 | 136 | listMultichainAccounts?: InternalAccount[];
|
133 |
| - handleRequestReturnValue?: Record<CaipAssetType, Transaction>; |
| 137 | + handleRequestReturnValue?: TransactionsPage; |
134 | 138 | };
|
135 | 139 | } = {}) => {
|
136 | 140 | const messenger = new Messenger<AllowedActions, AllowedEvents>();
|
@@ -192,6 +196,9 @@ async function waitForAllPromises(): Promise<void> {
|
192 | 196 | await new Promise(process.nextTick);
|
193 | 197 | }
|
194 | 198 |
|
| 199 | +const NEW_ACCOUNT_ID = 'new-account-id'; |
| 200 | +const TEST_ACCOUNT_ID = 'test-account-id'; |
| 201 | + |
195 | 202 | describe('MultichainTransactionsController', () => {
|
196 | 203 | it('initialize with default state', () => {
|
197 | 204 | const { controller } = setupController({});
|
@@ -433,36 +440,225 @@ describe('MultichainTransactionsController', () => {
|
433 | 440 | });
|
434 | 441 |
|
435 | 442 | it('updates transactions when receiving "AccountsController:accountTransactionsUpdated" event', async () => {
|
| 443 | + const mockSolAccountWithId = { |
| 444 | + ...mockSolAccount, |
| 445 | + id: TEST_ACCOUNT_ID, |
| 446 | + }; |
| 447 | + |
| 448 | + const existingTransaction = { |
| 449 | + ...mockTransactionResult.data[0], |
| 450 | + id: '123', |
| 451 | + status: 'confirmed' as const, |
| 452 | + }; |
| 453 | + |
| 454 | + const newTransaction = { |
| 455 | + ...mockTransactionResult.data[0], |
| 456 | + id: '456', |
| 457 | + status: 'submitted' as const, |
| 458 | + }; |
| 459 | + |
| 460 | + const updatedExistingTransaction = { |
| 461 | + ...mockTransactionResult.data[0], |
| 462 | + id: '123', |
| 463 | + status: 'failed' as const, |
| 464 | + }; |
| 465 | + |
| 466 | + const { controller, messenger } = setupController({ |
| 467 | + state: { |
| 468 | + nonEvmTransactions: { |
| 469 | + [mockSolAccountWithId.id]: { |
| 470 | + transactions: [existingTransaction], |
| 471 | + next: null, |
| 472 | + lastUpdated: Date.now(), |
| 473 | + }, |
| 474 | + }, |
| 475 | + }, |
| 476 | + }); |
| 477 | + |
| 478 | + messenger.publish('AccountsController:accountTransactionsUpdated', { |
| 479 | + transactions: { |
| 480 | + [mockSolAccountWithId.id]: [updatedExistingTransaction, newTransaction], |
| 481 | + }, |
| 482 | + }); |
| 483 | + |
| 484 | + await waitForAllPromises(); |
| 485 | + |
| 486 | + const finalTransactions = |
| 487 | + controller.state.nonEvmTransactions[mockSolAccountWithId.id].transactions; |
| 488 | + expect(finalTransactions).toStrictEqual([ |
| 489 | + updatedExistingTransaction, |
| 490 | + newTransaction, |
| 491 | + ]); |
| 492 | + }); |
| 493 | + |
| 494 | + it('handles empty transaction updates gracefully', async () => { |
436 | 495 | const { controller, messenger } = setupController({
|
437 | 496 | state: {
|
438 | 497 | nonEvmTransactions: {
|
439 |
| - [mockBtcAccount.id]: { |
| 498 | + [TEST_ACCOUNT_ID]: { |
440 | 499 | transactions: [],
|
441 | 500 | next: null,
|
442 | 501 | lastUpdated: Date.now(),
|
443 | 502 | },
|
444 | 503 | },
|
445 | 504 | },
|
446 | 505 | });
|
447 |
| - const transactionUpdate = { |
| 506 | + |
| 507 | + messenger.publish('AccountsController:accountTransactionsUpdated', { |
| 508 | + transactions: {}, |
| 509 | + }); |
| 510 | + |
| 511 | + await waitForAllPromises(); |
| 512 | + |
| 513 | + expect(controller.state.nonEvmTransactions[TEST_ACCOUNT_ID]).toStrictEqual({ |
| 514 | + transactions: [], |
| 515 | + next: null, |
| 516 | + lastUpdated: expect.any(Number), |
| 517 | + }); |
| 518 | + }); |
| 519 | + |
| 520 | + it('initializes new accounts with empty transactions array when receiving updates', async () => { |
| 521 | + const { controller, messenger } = setupController({ |
| 522 | + state: { |
| 523 | + nonEvmTransactions: {}, |
| 524 | + }, |
| 525 | + }); |
| 526 | + |
| 527 | + messenger.publish('AccountsController:accountTransactionsUpdated', { |
448 | 528 | transactions: {
|
449 |
| - [mockBtcAccount.id]: mockTransactionResult.data, |
| 529 | + [NEW_ACCOUNT_ID]: mockTransactionResult.data, |
| 530 | + }, |
| 531 | + }); |
| 532 | + |
| 533 | + await waitForAllPromises(); |
| 534 | + |
| 535 | + expect(controller.state.nonEvmTransactions[NEW_ACCOUNT_ID]).toStrictEqual({ |
| 536 | + transactions: mockTransactionResult.data, |
| 537 | + lastUpdated: expect.any(Number), |
| 538 | + }); |
| 539 | + }); |
| 540 | + |
| 541 | + it('handles undefined transactions in update payload', async () => { |
| 542 | + const { controller, messenger } = setupController({ |
| 543 | + state: { |
| 544 | + nonEvmTransactions: { |
| 545 | + [TEST_ACCOUNT_ID]: { |
| 546 | + transactions: [], |
| 547 | + next: null, |
| 548 | + lastUpdated: Date.now(), |
| 549 | + }, |
| 550 | + }, |
| 551 | + }, |
| 552 | + mocks: { |
| 553 | + listMultichainAccounts: [], |
| 554 | + handleRequestReturnValue: { |
| 555 | + data: [], |
| 556 | + next: null, |
| 557 | + }, |
| 558 | + }, |
| 559 | + }); |
| 560 | + |
| 561 | + const initialStateSnapshot = { |
| 562 | + [TEST_ACCOUNT_ID]: { |
| 563 | + ...controller.state.nonEvmTransactions[TEST_ACCOUNT_ID], |
| 564 | + lastUpdated: expect.any(Number), |
450 | 565 | },
|
451 | 566 | };
|
452 | 567 |
|
453 |
| - messenger.publish( |
454 |
| - 'AccountsController:accountTransactionsUpdated', |
455 |
| - transactionUpdate, |
| 568 | + messenger.publish('AccountsController:accountTransactionsUpdated', { |
| 569 | + transactions: undefined, |
| 570 | + } as unknown as AccountTransactionsUpdatedEventPayload); |
| 571 | + |
| 572 | + await waitForAllPromises(); |
| 573 | + |
| 574 | + expect(controller.state.nonEvmTransactions).toStrictEqual( |
| 575 | + initialStateSnapshot, |
456 | 576 | );
|
| 577 | + }); |
| 578 | + |
| 579 | + it('sorts transactions by timestamp (newest first)', async () => { |
| 580 | + const olderTransaction = { |
| 581 | + ...mockTransactionResult.data[0], |
| 582 | + id: '123', |
| 583 | + timestamp: 1000, |
| 584 | + }; |
| 585 | + const newerTransaction = { |
| 586 | + ...mockTransactionResult.data[0], |
| 587 | + id: '456', |
| 588 | + timestamp: 2000, |
| 589 | + }; |
| 590 | + |
| 591 | + const { controller, messenger } = setupController({ |
| 592 | + state: { |
| 593 | + nonEvmTransactions: { |
| 594 | + [TEST_ACCOUNT_ID]: { |
| 595 | + transactions: [olderTransaction], |
| 596 | + next: null, |
| 597 | + lastUpdated: Date.now(), |
| 598 | + }, |
| 599 | + }, |
| 600 | + }, |
| 601 | + }); |
| 602 | + |
| 603 | + messenger.publish('AccountsController:accountTransactionsUpdated', { |
| 604 | + transactions: { |
| 605 | + [TEST_ACCOUNT_ID]: [newerTransaction], |
| 606 | + }, |
| 607 | + }); |
457 | 608 |
|
458 | 609 | await waitForAllPromises();
|
459 | 610 |
|
460 |
| - expect( |
461 |
| - controller.state.nonEvmTransactions[mockBtcAccount.id], |
462 |
| - ).toStrictEqual({ |
463 |
| - transactions: mockTransactionResult.data, |
464 |
| - next: null, |
465 |
| - lastUpdated: expect.any(Number), |
| 611 | + const finalTransactions = |
| 612 | + controller.state.nonEvmTransactions[TEST_ACCOUNT_ID].transactions; |
| 613 | + expect(finalTransactions).toStrictEqual([ |
| 614 | + newerTransaction, |
| 615 | + olderTransaction, |
| 616 | + ]); |
| 617 | + }); |
| 618 | + |
| 619 | + it('sorts transactions by timestamp and handles null timestamps', async () => { |
| 620 | + const nullTimestampTx1 = { |
| 621 | + ...mockTransactionResult.data[0], |
| 622 | + id: '123', |
| 623 | + timestamp: null, |
| 624 | + }; |
| 625 | + const nullTimestampTx2 = { |
| 626 | + ...mockTransactionResult.data[0], |
| 627 | + id: '456', |
| 628 | + timestamp: null, |
| 629 | + }; |
| 630 | + const withTimestampTx = { |
| 631 | + ...mockTransactionResult.data[0], |
| 632 | + id: '789', |
| 633 | + timestamp: 1000, |
| 634 | + }; |
| 635 | + |
| 636 | + const { controller, messenger } = setupController({ |
| 637 | + state: { |
| 638 | + nonEvmTransactions: { |
| 639 | + [TEST_ACCOUNT_ID]: { |
| 640 | + transactions: [nullTimestampTx1], |
| 641 | + next: null, |
| 642 | + lastUpdated: Date.now(), |
| 643 | + }, |
| 644 | + }, |
| 645 | + }, |
466 | 646 | });
|
| 647 | + |
| 648 | + messenger.publish('AccountsController:accountTransactionsUpdated', { |
| 649 | + transactions: { |
| 650 | + [TEST_ACCOUNT_ID]: [withTimestampTx, nullTimestampTx2], |
| 651 | + }, |
| 652 | + }); |
| 653 | + |
| 654 | + await waitForAllPromises(); |
| 655 | + |
| 656 | + const finalTransactions = |
| 657 | + controller.state.nonEvmTransactions[TEST_ACCOUNT_ID].transactions; |
| 658 | + expect(finalTransactions).toStrictEqual([ |
| 659 | + withTimestampTx, |
| 660 | + nullTimestampTx1, |
| 661 | + nullTimestampTx2, |
| 662 | + ]); |
467 | 663 | });
|
468 | 664 | });
|
0 commit comments