github.com/status-im/status-go@v1.1.0/services/wallet/activity/session_test.go (about)

     1  package activity
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  
     7  	eth "github.com/ethereum/go-ethereum/common"
     8  
     9  	"github.com/status-im/status-go/services/wallet/transfer"
    10  )
    11  
    12  // TODO #12120: cover missing cases
    13  func TestFindUpdates(t *testing.T) {
    14  	txIds := []transfer.TransactionIdentity{
    15  		transfer.TransactionIdentity{
    16  			ChainID: 1,
    17  			Hash:    eth.HexToHash("0x1234"),
    18  			Address: eth.HexToAddress("0x1234"),
    19  		},
    20  	}
    21  
    22  	type findUpdatesResult struct {
    23  		new     []mixedIdentityResult
    24  		removed []EntryIdentity
    25  	}
    26  
    27  	tests := []struct {
    28  		name       string
    29  		identities []EntryIdentity
    30  		updated    []Entry
    31  		want       findUpdatesResult
    32  	}{
    33  		{
    34  			name:       "Empty to single MT update",
    35  			identities: []EntryIdentity{},
    36  			updated: []Entry{
    37  				{payloadType: MultiTransactionPT, id: 1},
    38  			},
    39  			want: findUpdatesResult{
    40  				new: []mixedIdentityResult{{0, EntryIdentity{payloadType: MultiTransactionPT, id: 1}}},
    41  			},
    42  		},
    43  		{
    44  			name: "No updates",
    45  			identities: []EntryIdentity{
    46  				EntryIdentity{
    47  					payloadType: SimpleTransactionPT, transaction: &txIds[0],
    48  				},
    49  			},
    50  			updated: []Entry{
    51  				{payloadType: SimpleTransactionPT, transaction: &txIds[0]},
    52  			},
    53  			want: findUpdatesResult{},
    54  		},
    55  		{
    56  			name:       "Empty to mixed updates",
    57  			identities: []EntryIdentity{},
    58  			updated: []Entry{
    59  				{payloadType: MultiTransactionPT, id: 1},
    60  				{payloadType: PendingTransactionPT, transaction: &txIds[0]},
    61  			},
    62  			want: findUpdatesResult{
    63  				new: []mixedIdentityResult{{0, EntryIdentity{payloadType: MultiTransactionPT, id: 1}},
    64  					{1, EntryIdentity{payloadType: PendingTransactionPT, transaction: &txIds[0]}},
    65  				},
    66  			},
    67  		},
    68  		{
    69  			name: "Add one on top of one",
    70  			identities: []EntryIdentity{
    71  				EntryIdentity{
    72  					payloadType: MultiTransactionPT, id: 1,
    73  				},
    74  			},
    75  			updated: []Entry{
    76  				{payloadType: PendingTransactionPT, transaction: &txIds[0]},
    77  				{payloadType: MultiTransactionPT, id: 1},
    78  			},
    79  			want: findUpdatesResult{
    80  				new: []mixedIdentityResult{{0, EntryIdentity{payloadType: PendingTransactionPT, transaction: &txIds[0]}}},
    81  			},
    82  		},
    83  		{
    84  			name: "Add one on top keep window",
    85  			identities: []EntryIdentity{
    86  				EntryIdentity{payloadType: MultiTransactionPT, id: 1},
    87  				EntryIdentity{payloadType: PendingTransactionPT, transaction: &txIds[0]},
    88  			},
    89  			updated: []Entry{
    90  				{payloadType: MultiTransactionPT, id: 2},
    91  				{payloadType: MultiTransactionPT, id: 1},
    92  			},
    93  			want: findUpdatesResult{
    94  				new:     []mixedIdentityResult{{0, EntryIdentity{payloadType: MultiTransactionPT, id: 2}}},
    95  				removed: []EntryIdentity{EntryIdentity{payloadType: PendingTransactionPT, transaction: &txIds[0]}},
    96  			},
    97  		},
    98  	}
    99  
   100  	for _, tt := range tests {
   101  		t.Run(tt.name, func(t *testing.T) {
   102  			gotNew, gotRemoved := findUpdates(tt.identities, tt.updated)
   103  			if !reflect.DeepEqual(gotNew, tt.want.new) || !reflect.DeepEqual(gotRemoved, tt.want.removed) {
   104  				t.Errorf("findUpdates() = %v, %v, want %v, %v", gotNew, gotRemoved, tt.want.new, tt.want.removed)
   105  			}
   106  		})
   107  	}
   108  }