@@ -4,33 +4,13 @@ import userEvent from '@testing-library/user-event';
4
4
import ApiTokenForm from '../api-token.form' ;
5
5
import useCreateToken from '../../../hooks/useCreateToken' ;
6
6
import useApiToken from '@site/src/hooks/useApiToken' ;
7
- import TokenNameRestrictions from '../../TokenNameRestrictions/TokenNameRestrictions' ;
8
7
9
8
jest . mock ( '@site/src/hooks/useApiToken' ) ;
10
9
11
10
const mockUseApiToken = useApiToken as jest . MockedFunction <
12
11
( ) => Partial < ReturnType < typeof useApiToken > >
13
12
> ;
14
13
15
- mockUseApiToken . mockImplementation ( ( ) => ( {
16
- tokens : [
17
- {
18
- display_name : 'testtoken1' ,
19
- last_used : '' ,
20
- scopes : [ 'read' , 'trade' , 'payments' , 'admin' ] ,
21
- token : 'asdf1234' ,
22
- valid_for_ip : '' ,
23
- } ,
24
- {
25
- display_name : 'testtoken2' ,
26
- last_used : '' ,
27
- scopes : [ 'read' , 'trade' , 'payments' , 'admin' ] ,
28
- token : 'asdf1235' ,
29
- valid_for_ip : '' ,
30
- } ,
31
- ] ,
32
- } ) ) ;
33
-
34
14
jest . mock ( '@site/src/features/dashboard/hooks/useCreateToken' ) ;
35
15
36
16
const mockUseCreateToken = useCreateToken as jest . MockedFunction < typeof useCreateToken > ;
@@ -76,110 +56,156 @@ const scopes = [
76
56
label : 'Admin' ,
77
57
} ,
78
58
] ;
79
- const preventDefault = jest . fn ( ) ;
80
59
81
60
describe ( 'Home Page' , ( ) => {
82
- beforeEach ( ( ) => {
83
- render ( < ApiTokenForm /> ) ;
84
- } ) ;
85
-
86
- afterEach ( ( ) => {
87
- cleanup ( ) ;
88
- jest . clearAllMocks ( ) ;
89
- } ) ;
61
+ describe ( 'General tests' , ( ) => {
62
+ beforeEach ( ( ) => {
63
+ mockUseApiToken . mockImplementation ( ( ) => ( {
64
+ tokens : [
65
+ {
66
+ display_name : 'testtoken1' ,
67
+ last_used : '' ,
68
+ scopes : [ 'read' , 'trade' , 'payments' , 'admin' ] ,
69
+ token : 'asdf1234' ,
70
+ valid_for_ip : '' ,
71
+ } ,
72
+ {
73
+ display_name : 'testtoken2' ,
74
+ last_used : '' ,
75
+ scopes : [ 'read' , 'trade' , 'payments' , 'admin' ] ,
76
+ token : 'asdf1235' ,
77
+ valid_for_ip : '' ,
78
+ } ,
79
+ ] ,
80
+ } ) ) ;
81
+
82
+ render ( < ApiTokenForm /> ) ;
83
+ } ) ;
90
84
91
- it ( 'Should render first step title' , ( ) => {
92
- const firstStep = screen . getByTestId ( 'first-step-title' ) ;
93
- expect ( firstStep ) . toHaveTextContent ( / S e l e c t s c o p e s b a s e d o n t h e a c c e s s y o u n e e d . / i ) ;
94
- } ) ;
85
+ afterEach ( ( ) => {
86
+ cleanup ( ) ;
87
+ jest . clearAllMocks ( ) ;
88
+ } ) ;
95
89
96
- it ( 'Should render all of scopes checkbox cards' , ( ) => {
97
- scopes . forEach ( ( item ) => {
98
- const apiTokenCard = screen . getByTestId ( `api-token-card-${ item . name } ` ) ;
99
- expect ( apiTokenCard ) . toBeInTheDocument ( ) ;
90
+ it ( 'Should render first step title' , ( ) => {
91
+ const firstStep = screen . getByTestId ( 'first-step-title' ) ;
92
+ expect ( firstStep ) . toHaveTextContent ( / S e l e c t s c o p e s b a s e d o n t h e a c c e s s y o u n e e d ./ i) ;
100
93
} ) ;
101
- } ) ;
102
94
103
- it ( 'Should render second step title ' , ( ) => {
104
- const secondStep = screen . getByTestId ( 'second-step-title' ) ;
105
- expect ( secondStep ) . toHaveTextContent (
106
- / N a m e y o u r t o k e n a n d c l i c k o n C r e a t e t o g e n e r a t e y o u r t o k e n . / i ,
107
- ) ;
108
- } ) ;
95
+ it ( 'Should render all of scopes checkbox cards ' , ( ) => {
96
+ scopes . forEach ( ( item ) => {
97
+ const apiTokenCard = screen . getByTestId ( `api-token-card- ${ item . name } ` ) ;
98
+ expect ( apiTokenCard ) . toBeInTheDocument ( ) ;
99
+ } ) ;
100
+ } ) ;
109
101
110
- it ( 'Should check the checkbox when clicked on api token card' , async ( ) => {
111
- const adminTokenCard = screen . getByTestId ( 'api-token-card-admin' ) ;
112
- const withinAdminTokenCard = within ( adminTokenCard ) ;
113
- const adminCheckbox = withinAdminTokenCard . getByRole < HTMLInputElement > ( 'checkbox' ) ;
102
+ it ( 'Should render second step title' , ( ) => {
103
+ const secondStep = screen . getByTestId ( 'second-step-title' ) ;
104
+ expect ( secondStep ) . toHaveTextContent (
105
+ / N a m e y o u r t o k e n a n d c l i c k o n C r e a t e t o g e n e r a t e y o u r t o k e n ./ i,
106
+ ) ;
107
+ } ) ;
114
108
115
- expect ( adminCheckbox . checked ) . toBeFalsy ( ) ;
109
+ it ( 'Should check the checkbox when clicked on api token card' , async ( ) => {
110
+ const adminTokenCard = screen . getByTestId ( 'api-token-card-admin' ) ;
111
+ const withinAdminTokenCard = within ( adminTokenCard ) ;
112
+ const adminCheckbox = withinAdminTokenCard . getByRole < HTMLInputElement > ( 'checkbox' ) ;
116
113
117
- await userEvent . click ( adminTokenCard ) ;
114
+ expect ( adminCheckbox . checked ) . toBeFalsy ( ) ;
118
115
119
- expect ( adminCheckbox . checked ) . toBeTruthy ( ) ;
120
- } ) ;
116
+ await userEvent . click ( adminTokenCard ) ;
121
117
122
- it ( 'Should create token on form submit' , async ( ) => {
123
- const nameInput = screen . getByRole ( 'textbox' ) ;
118
+ expect ( adminCheckbox . checked ) . toBeTruthy ( ) ;
119
+ } ) ;
124
120
125
- await userEvent . type ( nameInput , 'test create token' ) ;
121
+ it ( 'Should create token on form submit' , async ( ) => {
122
+ const nameInput = screen . getByRole ( 'textbox' ) ;
126
123
127
- const submitButton = screen . getByRole ( 'button' , { name : / C r e a t e / i } ) ;
128
- await userEvent . click ( submitButton ) ;
124
+ await userEvent . type ( nameInput , 'test create token' ) ;
129
125
130
- expect ( mockCreateToken ) . toHaveBeenCalledTimes ( 1 ) ;
131
- expect ( mockCreateToken ) . toHaveBeenCalledWith ( 'test create token' , [ ] ) ;
132
- } ) ;
126
+ const submitButton = screen . getByRole ( 'button' , { name : / C r e a t e / i } ) ;
127
+ await userEvent . click ( submitButton ) ;
133
128
134
- it ( 'Should not be able to create a token if name already exists' , async ( ) => {
135
- const nameInput = screen . getByRole ( 'textbox' ) ;
129
+ expect ( mockCreateToken ) . toHaveBeenCalledTimes ( 1 ) ;
130
+ expect ( mockCreateToken ) . toHaveBeenCalledWith ( 'test create token' , [ ] ) ;
131
+ } ) ;
136
132
137
- await userEvent . type ( nameInput , 'testtoken1' ) ;
133
+ it ( 'Should not be able to create a token if name already exists' , async ( ) => {
134
+ const nameInput = screen . getByRole ( 'textbox' ) ;
138
135
139
- const error = screen . getByText ( / T h a t n a m e i s t a k e n . C h o o s e a n o t h e r ./ i) ;
140
- expect ( error ) . toBeVisible ;
141
- } ) ;
136
+ await userEvent . type ( nameInput , 'testtoken1' ) ;
142
137
143
- it ( 'should hide restrictions if error is present' , async ( ) => {
144
- const nameInput = screen . getByRole ( 'textbox' ) ;
145
- const restrictions = screen . getByRole ( 'list' ) ;
146
- expect ( restrictions ) . toBeVisible ( ) ;
147
- await userEvent . type ( nameInput , 'testtoken1' ) ;
148
- expect ( restrictions ) . not . toBeVisible ( ) ;
149
- } ) ;
138
+ const error = screen . getByText ( / T h a t n a m e i s t a k e n . C h o o s e a n o t h e r ./ i) ;
139
+ expect ( error ) . toBeVisible ;
140
+ } ) ;
150
141
151
- it ( 'Should not create token when name input is empty' , async ( ) => {
152
- const nameInput = screen . getByRole ( 'textbox' ) ;
142
+ it ( 'should hide restrictions if error is present' , async ( ) => {
143
+ const nameInput = screen . getByRole ( 'textbox' ) ;
144
+ const restrictions = screen . getByRole ( 'list' ) ;
145
+ expect ( restrictions ) . toBeVisible ( ) ;
146
+ await userEvent . type ( nameInput , 'testtoken1' ) ;
147
+ expect ( restrictions ) . not . toBeVisible ( ) ;
148
+ } ) ;
153
149
154
- await userEvent . clear ( nameInput ) ;
150
+ it ( 'Should not create token when name input is empty' , async ( ) => {
151
+ const nameInput = screen . getByRole ( 'textbox' ) ;
155
152
156
- await userEvent . click ( nameInput ) ;
153
+ await userEvent . clear ( nameInput ) ;
157
154
158
- expect ( mockCreateToken ) . not . toHaveBeenCalled ( ) ;
159
- } ) ;
155
+ await userEvent . click ( nameInput ) ;
160
156
161
- it ( 'Should open success dialog when token is created ' , async ( ) => {
162
- const nameInput = screen . getByRole ( 'textbox' ) ;
157
+ expect ( mockCreateToken ) . not . toHaveBeenCalled ( ) ;
158
+ } ) ;
159
+ it ( 'Should open success dialog when token is created ' , async ( ) => {
160
+ const nameInput = screen . getByRole ( 'textbox' ) ;
163
161
164
- await userEvent . type ( nameInput , 'test create token' ) ;
162
+ await userEvent . type ( nameInput , 'test create token' ) ;
165
163
166
- const submitButton = screen . getByRole ( 'button' , { name : / C r e a t e / i } ) ;
167
- await userEvent . click ( submitButton ) ;
164
+ const submitButton = screen . getByRole ( 'button' , { name : / C r e a t e / i } ) ;
165
+ await userEvent . click ( submitButton ) ;
168
166
169
- const modal = await screen . getByText ( 'Your API token is ready to be used.' ) ;
170
- expect ( modal ) . toBeVisible ( ) ;
171
- } ) ;
167
+ const modal = screen . getByText ( 'Your API token is ready to be used.' ) ;
168
+ expect ( modal ) . toBeVisible ( ) ;
169
+ } ) ;
172
170
173
- it ( 'Should have create button disabled in case of empty input or error message' , async ( ) => {
174
- const submitButton = screen . getByRole ( 'button' , { name : / C r e a t e / i } ) ;
175
- expect ( submitButton ) . toBeDisabled ( ) ;
171
+ it ( 'Should have create button disabled in case of empty input or error message' , async ( ) => {
172
+ const submitButton = screen . getByRole ( 'button' , { name : / C r e a t e / i } ) ;
173
+ expect ( submitButton ) . toBeDisabled ( ) ;
176
174
177
- const nameInput = screen . getByRole ( 'textbox' ) ;
175
+ const nameInput = screen . getByRole ( 'textbox' ) ;
178
176
179
- await userEvent . type ( nameInput , 'token-text' ) ;
180
- expect ( submitButton ) . toBeDisabled ( ) ;
177
+ await userEvent . type ( nameInput , 'token-text' ) ;
178
+ expect ( submitButton ) . toBeDisabled ( ) ;
181
179
182
- await userEvent . clear ( nameInput ) ;
183
- expect ( submitButton ) . toBeDisabled ( ) ;
180
+ await userEvent . clear ( nameInput ) ;
181
+ expect ( submitButton ) . toBeDisabled ( ) ;
182
+ } ) ;
183
+ } ) ;
184
+ describe ( 'Token limit' , ( ) => {
185
+ const createMaxTokens = ( ) => {
186
+ const token_array = [ ] ;
187
+ for ( let i = 0 ; i < 30 ; i ++ ) {
188
+ token_array . push ( {
189
+ display_name : `testtoken${ i } ` ,
190
+ last_used : '' ,
191
+ scopes : [ 'read' , 'trade' , 'payments' , 'admin' ] ,
192
+ token : 'asdf1234' ,
193
+ valid_for_ip : '' ,
194
+ } ) ;
195
+ }
196
+ return token_array ;
197
+ } ;
198
+
199
+ it ( 'Should show an error when the user tries to create more than 30 tokens' , async ( ) => {
200
+ mockUseApiToken . mockImplementation ( ( ) => ( { tokens : createMaxTokens ( ) } ) ) ;
201
+ render ( < ApiTokenForm /> ) ;
202
+
203
+ const nameInput = screen . getByRole ( 'textbox' ) ;
204
+
205
+ await userEvent . type ( nameInput , 'asdf' ) ;
206
+
207
+ const error = screen . getByText ( / c r e a t e d t h e m a x i m u m n u m b e r o f t o k e n s / i) ;
208
+ expect ( error ) . toBeVisible ( ) ;
209
+ } ) ;
184
210
} ) ;
185
211
} ) ;
0 commit comments