1
1
import React from 'react' ;
2
- import { screen , fireEvent } from '@testing-library/react' ;
2
+ import { screen , fireEvent , within } from '@testing-library/react' ;
3
3
4
4
import { renderWithProvider } from '../../../../test/lib/render-helpers' ;
5
5
import mockState from '../../../../test/data/mock-state.json' ;
@@ -17,6 +17,10 @@ jest.mock('react-router-dom', () => ({
17
17
} ) ,
18
18
} ) ) ;
19
19
20
+ const searchContainerTestId = 'multichain-account-list-search' ;
21
+ const searchClearButtonTestId = 'text-field-search-clear-button' ;
22
+ const walletHeaderTestId = 'multichain-account-tree-wallet-header' ;
23
+
20
24
describe ( 'AccountList' , ( ) => {
21
25
beforeEach ( ( ) => {
22
26
jest . clearAllMocks ( ) ;
@@ -26,44 +30,6 @@ describe('AccountList', () => {
26
30
const store = configureStore ( {
27
31
metamask : {
28
32
...mockState . metamask ,
29
- accountTree : {
30
- selectedAccountGroup : '01JKAF3DSGM3AB87EM9N0K41AJ:default' ,
31
- wallets : {
32
- '01JKAF3DSGM3AB87EM9N0K41AJ' : {
33
- id : '01JKAF3DSGM3AB87EM9N0K41AJ' ,
34
- metadata : {
35
- name : 'Wallet 1' ,
36
- } ,
37
- groups : {
38
- '01JKAF3DSGM3AB87EM9N0K41AJ:default' : {
39
- id : '01JKAF3DSGM3AB87EM9N0K41AJ:default' ,
40
- metadata : {
41
- name : 'Account 1 from wallet 1' ,
42
- } ,
43
- accounts : [
44
- 'cf8dace4-9439-4bd4-b3a8-88c821c8fcb3' ,
45
- '07c2cfec-36c9-46c4-8115-3836d3ac9047' ,
46
- ] ,
47
- } ,
48
- } ,
49
- } ,
50
- '01JKAF3PJ247KAM6C03G5Q0NP8' : {
51
- id : '01JKAF3PJ247KAM6C03G5Q0NP8' ,
52
- metadata : {
53
- name : 'Wallet 2' ,
54
- } ,
55
- groups : {
56
- '01JKAF3PJ247KAM6C03G5Q0NP8:default' : {
57
- id : '01JKAF3PJ247KAM6C03G5Q0NP8:default' ,
58
- metadata : {
59
- name : 'Account 1 from wallet 2' ,
60
- } ,
61
- accounts : [ '784225f4-d30b-4e77-a900-c8bbce735b88' ] ,
62
- } ,
63
- } ,
64
- } ,
65
- } ,
66
- } ,
67
33
} ,
68
34
} ) ;
69
35
@@ -76,15 +42,13 @@ describe('AccountList', () => {
76
42
expect ( screen . getByText ( 'Accounts' ) ) . toBeInTheDocument ( ) ;
77
43
expect ( screen . getByLabelText ( 'Back' ) ) . toBeInTheDocument ( ) ;
78
44
79
- const walletHeaders = screen . getAllByTestId (
80
- 'multichain-account-tree-wallet-header' ,
81
- ) ;
45
+ const walletHeaders = screen . getAllByTestId ( walletHeaderTestId ) ;
82
46
83
- expect ( walletHeaders . length ) . toBe ( 2 ) ;
47
+ expect ( walletHeaders . length ) . toBe ( 5 ) ;
84
48
expect ( screen . getByText ( 'Wallet 1' ) ) . toBeInTheDocument ( ) ;
85
49
expect ( screen . getByText ( 'Wallet 2' ) ) . toBeInTheDocument ( ) ;
86
- expect ( screen . getByText ( 'Account 1 from wallet 1 ' ) ) . toBeInTheDocument ( ) ;
87
- expect ( screen . getByText ( 'Account 1 from wallet 2' ) ) . toBeInTheDocument ( ) ;
50
+ expect ( screen . getByText ( 'Account 1' ) ) . toBeInTheDocument ( ) ;
51
+ expect ( screen . getByText ( 'Account 2' ) ) . toBeInTheDocument ( ) ;
88
52
} ) ;
89
53
90
54
it ( 'calls history.goBack when back button is clicked' , ( ) => {
@@ -110,4 +74,85 @@ describe('AccountList', () => {
110
74
expect ( screen . getByText ( 'Import an account' ) ) . toBeInTheDocument ( ) ;
111
75
expect ( screen . getByText ( 'Add a hardware wallet' ) ) . toBeInTheDocument ( ) ;
112
76
} ) ;
77
+
78
+ it ( 'displays the search field with correct placeholder' , ( ) => {
79
+ renderComponent ( ) ;
80
+
81
+ const searchContainer = screen . getByTestId ( searchContainerTestId ) ;
82
+
83
+ expect ( searchContainer ) . toBeInTheDocument ( ) ;
84
+
85
+ const searchInput = within ( searchContainer ) . getByPlaceholderText (
86
+ 'Search your accounts' ,
87
+ ) ;
88
+
89
+ expect ( searchInput ) . toBeInTheDocument ( ) ;
90
+ } ) ;
91
+
92
+ it ( 'updates search value when typing in the search field' , ( ) => {
93
+ renderComponent ( ) ;
94
+
95
+ const searchContainer = screen . getByTestId ( searchContainerTestId ) ;
96
+ const searchInput = within ( searchContainer ) . getByRole ( 'searchbox' ) ;
97
+ fireEvent . change ( searchInput , { target : { value : 'Account 2' } } ) ;
98
+
99
+ // @ts -expect-error Values does exist on the search input
100
+ expect ( searchInput ?. value ) . toBe ( 'Account 2' ) ;
101
+ } ) ;
102
+
103
+ it ( 'filters accounts when search text is entered' , ( ) => {
104
+ renderComponent ( ) ;
105
+
106
+ // Verify all accounts are shown initially
107
+ const walletHeaders = screen . getAllByTestId ( walletHeaderTestId ) ;
108
+ expect ( walletHeaders . length ) . toBe ( 5 ) ;
109
+ expect ( screen . getByText ( 'Account 1' ) ) . toBeInTheDocument ( ) ;
110
+ expect ( screen . getByText ( 'Account 2' ) ) . toBeInTheDocument ( ) ;
111
+
112
+ const searchContainer = screen . getByTestId ( searchContainerTestId ) ;
113
+ const searchInput = within ( searchContainer ) . getByRole ( 'searchbox' ) ;
114
+ fireEvent . change ( searchInput , { target : { value : 'Account 2' } } ) ;
115
+
116
+ expect ( screen . queryByText ( 'Account 1' ) ) . not . toBeInTheDocument ( ) ;
117
+ expect ( screen . getByText ( 'Account 2' ) ) . toBeInTheDocument ( ) ;
118
+ } ) ;
119
+
120
+ it ( 'shows "No accounts found" message when no accounts match search criteria' , ( ) => {
121
+ renderComponent ( ) ;
122
+
123
+ const searchContainer = screen . getByTestId ( searchContainerTestId ) ;
124
+ const searchInput = within ( searchContainer ) . getByRole ( 'searchbox' ) ;
125
+ fireEvent . change ( searchInput , { target : { value : 'nonexistent account' } } ) ;
126
+
127
+ expect (
128
+ screen . getByText ( 'No accounts found for the given search query' ) ,
129
+ ) . toBeInTheDocument ( ) ;
130
+ } ) ;
131
+
132
+ it ( 'clears search when clear button is clicked' , ( ) => {
133
+ renderComponent ( ) ;
134
+
135
+ const searchContainer = screen . getByTestId ( searchContainerTestId ) ;
136
+ const searchInput = within ( searchContainer ) . getByRole ( 'searchbox' ) ;
137
+ fireEvent . change ( searchInput , { target : { value : 'Account 2' } } ) ;
138
+
139
+ const clearButton = screen . getByTestId ( searchClearButtonTestId ) ;
140
+ fireEvent . click ( clearButton ) ;
141
+
142
+ // @ts -expect-error Value does exist on search input
143
+ expect ( searchInput ?. value ) . toBe ( '' ) ;
144
+ expect ( screen . getByText ( 'Account 1' ) ) . toBeInTheDocument ( ) ;
145
+ expect ( screen . getByText ( 'Account 2' ) ) . toBeInTheDocument ( ) ;
146
+ } ) ;
147
+
148
+ it ( 'performs case-insensitive search' , ( ) => {
149
+ renderComponent ( ) ;
150
+
151
+ const searchContainer = screen . getByTestId ( searchContainerTestId ) ;
152
+ const searchInput = within ( searchContainer ) . getByRole ( 'searchbox' ) ;
153
+ fireEvent . change ( searchInput , { target : { value : 'account 2' } } ) ;
154
+
155
+ expect ( screen . queryByText ( 'Account 1' ) ) . not . toBeInTheDocument ( ) ;
156
+ expect ( screen . getByText ( 'Account 2' ) ) . toBeInTheDocument ( ) ;
157
+ } ) ;
113
158
} ) ;
0 commit comments