@@ -2,9 +2,11 @@ import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
2
2
import { Formik } from "formik" ;
3
3
import { Elements } from "@stripe/react-stripe-js" ;
4
4
import { loadStripe } from "@stripe/stripe-js" ;
5
- import { fireEvent , render , screen } from "@testing-library/react" ;
6
- import { UAProduct } from "../../utils/test/Mocks" ;
5
+ import { render , screen } from "@testing-library/react" ;
6
+ import { DistributorProduct , UAProduct } from "../../utils/test/Mocks" ;
7
7
import Taxes from "./Taxes" ;
8
+ import userEvent from "@testing-library/user-event" ;
9
+ import { UserSubscriptionMarketplace } from "advantage/api/enum" ;
8
10
9
11
describe ( "TaxesTests" , ( ) => {
10
12
let queryClient : QueryClient ;
@@ -14,6 +16,10 @@ describe("TaxesTests", () => {
14
16
queryClient = new QueryClient ( ) ;
15
17
} ) ;
16
18
19
+ afterEach ( ( ) => {
20
+ jest . clearAllMocks ( ) ;
21
+ } ) ;
22
+
17
23
it ( "renders country select correctly" , ( ) => {
18
24
const products = [
19
25
{
@@ -53,12 +59,11 @@ describe("TaxesTests", () => {
53
59
) ;
54
60
55
61
expect ( screen . getByTestId ( "select-country" ) ) . toBeInTheDocument ( ) ;
56
- fireEvent . change ( screen . getByTestId ( "select-country" ) , {
57
- target : { value : "JP" } ,
58
- } ) ;
62
+ userEvent . selectOptions ( screen . getByTestId ( "select-country" ) , "JP" ) ;
63
+ expect ( screen . queryByText ( "VAT number:" ) ) . not . toBeInTheDocument ( ) ;
59
64
} ) ;
60
65
61
- it ( "When VAT country is selected, VAT Number input displays" , ( ) => {
66
+ it ( "When VAT country is selected, VAT Number input displays" , async ( ) => {
62
67
const products = [
63
68
{
64
69
product : UAProduct ,
@@ -76,14 +81,11 @@ describe("TaxesTests", () => {
76
81
) ;
77
82
78
83
expect ( screen . getByTestId ( "select-country" ) ) . toBeInTheDocument ( ) ;
79
- fireEvent . change ( screen . getByTestId ( "select-country" ) , {
80
- target : { value : "FR" } ,
81
- } ) ;
82
-
83
- expect ( screen . getByText ( "VAT number:" ) ) . toBeInTheDocument ( ) ;
84
+ await userEvent . selectOptions ( screen . getByTestId ( "select-country" ) , "FR" ) ;
85
+ expect ( screen . queryByText ( "VAT number:" ) ) . toBeInTheDocument ( ) ;
84
86
} ) ;
85
87
86
- it ( "When USA is selected, State select displays" , ( ) => {
88
+ it ( "When USA is selected, State select displays" , async ( ) => {
87
89
const products = [
88
90
{
89
91
product : UAProduct ,
@@ -99,20 +101,17 @@ describe("TaxesTests", () => {
99
101
</ Formik >
100
102
</ QueryClientProvider > ,
101
103
) ;
102
- fireEvent . change ( getByTestId ( "select-country" ) , {
103
- target : { value : "US" } ,
104
- } ) ;
104
+ await userEvent . selectOptions ( getByTestId ( "select-country" ) , "US" ) ;
105
105
expect ( screen . getByText ( "State:" ) ) . toBeInTheDocument ( ) ;
106
- fireEvent . change ( getByTestId ( "select-state" ) , {
107
- target : { value : "Texas" } ,
108
- } ) ;
106
+ await userEvent . selectOptions ( getByTestId ( "select-state" ) , "Texas" ) ;
107
+ expect ( screen . queryByText ( "VAT number:" ) ) . not . toBeInTheDocument ( ) ;
109
108
} ) ;
110
109
111
110
it ( "sets status right if country is stored" , ( ) => {
112
111
global . window = Object . create ( window ) ;
113
112
Object . defineProperty ( window , "accountId" , { value : "ABCDEF" } ) ;
114
113
115
- const intialValues = {
114
+ const initialValues = {
116
115
country : "GB" ,
117
116
} ;
118
117
const products = [
@@ -123,7 +122,7 @@ describe("TaxesTests", () => {
123
122
] ;
124
123
render (
125
124
< QueryClientProvider client = { queryClient } >
126
- < Formik initialValues = { intialValues } onSubmit = { jest . fn ( ) } >
125
+ < Formik initialValues = { initialValues } onSubmit = { jest . fn ( ) } >
127
126
< Elements stripe = { stripePromise } >
128
127
< Taxes products = { products } setError = { jest . fn ( ) } />
129
128
</ Elements >
@@ -154,11 +153,11 @@ describe("TaxesTests", () => {
154
153
expect ( screen . getByRole ( "button" , { name : "Save" } ) ) . toBeInTheDocument ( ) ;
155
154
} ) ;
156
155
157
- it ( "cancel button resets tax step values" , ( ) => {
156
+ it ( "cancel button resets tax step values" , async ( ) => {
158
157
global . window = Object . create ( window ) ;
159
158
Object . defineProperty ( window , "accountId" , { value : "ABCDEF" } ) ;
160
159
161
- const intialValues = {
160
+ const initialValues = {
162
161
country : "GB" ,
163
162
VATNumber : "GB123123123" ,
164
163
} ;
@@ -171,45 +170,127 @@ describe("TaxesTests", () => {
171
170
] ;
172
171
render (
173
172
< QueryClientProvider client = { queryClient } >
174
- < Formik initialValues = { intialValues } onSubmit = { jest . fn ( ) } >
173
+ < Formik initialValues = { initialValues } onSubmit = { jest . fn ( ) } >
175
174
< Elements stripe = { stripePromise } >
176
175
< Taxes products = { products } setError = { jest . fn ( ) } />
177
176
</ Elements >
178
177
</ Formik >
179
178
</ QueryClientProvider > ,
180
179
) ;
181
-
182
- fireEvent . click ( screen . getByRole ( "button" , { name : "Edit" } ) ) ;
183
- fireEvent . change ( screen . getByTestId ( "select-country" ) , {
184
- target : { value : "FR" } ,
185
- } ) ;
186
- fireEvent . change ( screen . getByTestId ( "field-vat-number" ) , {
187
- target : { value : "FR123123123" } ,
188
- } ) ;
189
- fireEvent . click ( screen . getByRole ( "button" , { name : "Cancel" } ) ) ;
180
+ await userEvent . click ( screen . getByRole ( "button" , { name : "Edit" } ) ) ;
181
+ await userEvent . selectOptions ( screen . getByTestId ( "select-country" ) , "FR" ) ;
182
+ await userEvent . type ( screen . getByTestId ( "field-vat-number" ) , "FR123123123" ) ;
183
+ await userEvent . click ( screen . getByRole ( "button" , { name : "Cancel" } ) ) ;
190
184
expect ( screen . getByTestId ( "country" ) ) . toHaveTextContent ( "United Kingdom" ) ;
191
185
expect ( screen . getByTestId ( "vat-number" ) ) . toHaveTextContent ( "GB123123123" ) ;
192
186
193
- fireEvent . click ( screen . getByRole ( "button" , { name : "Edit" } ) ) ;
194
- fireEvent . change ( screen . getByTestId ( "select-country" ) , {
195
- target : { value : "US" } ,
196
- } ) ;
197
- fireEvent . change ( screen . getByTestId ( "select-state" ) , {
198
- target : { value : "AL" } ,
199
- } ) ;
200
- fireEvent . click ( screen . getByRole ( "button" , { name : "Cancel" } ) ) ;
187
+ await userEvent . click ( screen . getByRole ( "button" , { name : "Edit" } ) ) ;
188
+ await userEvent . selectOptions ( screen . getByTestId ( "select-country" ) , "US" ) ;
189
+ await userEvent . selectOptions ( screen . getByTestId ( "select-state" ) , "AL" ) ;
190
+ await userEvent . click ( screen . getByRole ( "button" , { name : "Cancel" } ) ) ;
201
191
expect ( screen . getByTestId ( "country" ) ) . toHaveTextContent ( "United Kingdom" ) ;
202
192
expect ( screen . getByTestId ( "vat-number" ) ) . toHaveTextContent ( "GB123123123" ) ;
203
193
204
- fireEvent . click ( screen . getByRole ( "button" , { name : "Edit" } ) ) ;
205
- fireEvent . change ( screen . getByTestId ( "select-country" ) , {
206
- target : { value : "CA" } ,
207
- } ) ;
208
- fireEvent . change ( screen . getByTestId ( "select-ca-province" ) , {
209
- target : { value : "AL" } ,
210
- } ) ;
211
- fireEvent . click ( screen . getByRole ( "button" , { name : "Cancel" } ) ) ;
194
+ await userEvent . click ( screen . getByRole ( "button" , { name : "Edit" } ) ) ;
195
+ await userEvent . selectOptions ( screen . getByTestId ( "select-country" ) , "CA" ) ;
196
+ await userEvent . selectOptions (
197
+ screen . getByTestId ( "select-ca-province" ) ,
198
+ "AB" ,
199
+ ) ;
200
+ await userEvent . click ( screen . getByRole ( "button" , { name : "Cancel" } ) ) ;
212
201
expect ( screen . getByTestId ( "country" ) ) . toHaveTextContent ( "United Kingdom" ) ;
213
202
expect ( screen . getByTestId ( "vat-number" ) ) . toHaveTextContent ( "GB123123123" ) ;
214
203
} ) ;
204
+
205
+ it ( "Edit should be available for pro users" , async ( ) => {
206
+ global . window = Object . create ( window ) ;
207
+ Object . defineProperty ( window , "accountId" , { value : "ABCDEF" } ) ;
208
+
209
+ const initialValues = {
210
+ country : "GB" ,
211
+ VATNumber : "GB123123123" ,
212
+ marketPlace : "canonical-ua" ,
213
+ } ;
214
+
215
+ const products = [
216
+ {
217
+ product : UAProduct ,
218
+ quantity : 1 ,
219
+ } ,
220
+ ] ;
221
+ render (
222
+ < QueryClientProvider client = { queryClient } >
223
+ < Formik initialValues = { initialValues } onSubmit = { jest . fn ( ) } >
224
+ < Elements stripe = { stripePromise } >
225
+ < Taxes products = { products } setError = { jest . fn ( ) } />
226
+ </ Elements >
227
+ </ Formik >
228
+ </ QueryClientProvider > ,
229
+ ) ;
230
+
231
+ expect ( screen . queryByRole ( "button" , { name : "Edit" } ) ) . toBeInTheDocument ( ) ;
232
+ await userEvent . click ( screen . getByRole ( "button" , { name : "Edit" } ) ) ;
233
+
234
+ const countryField = await screen . findByTestId ( "select-country" ) ;
235
+ expect ( countryField ) . toBeInTheDocument ( ) ;
236
+ expect ( screen . getByTestId ( "field-vat-number" ) ) . toBeInTheDocument ( ) ;
237
+ expect ( screen . getByRole ( "button" , { name : "Save" } ) ) . toBeInTheDocument ( ) ;
238
+ } ) ;
239
+
240
+ it ( "Edit button should not be displayed for channel users" , ( ) => {
241
+ global . window = Object . create ( window ) ;
242
+ Object . defineProperty ( window , "accountId" , { value : "ABCDEF" } ) ;
243
+ const initialValues = {
244
+ country : "GB" ,
245
+ VATNumber : "GB123123123" ,
246
+ marketPlace : UserSubscriptionMarketplace . CanonicalProChannel ,
247
+ } ;
248
+ const products = [
249
+ {
250
+ product : DistributorProduct ,
251
+ quantity : 1 ,
252
+ } ,
253
+ ] ;
254
+ render (
255
+ < QueryClientProvider client = { queryClient } >
256
+ < Formik initialValues = { initialValues } onSubmit = { jest . fn ( ) } >
257
+ < Elements stripe = { stripePromise } >
258
+ < Taxes products = { products } setError = { jest . fn ( ) } />
259
+ </ Elements >
260
+ </ Formik >
261
+ </ QueryClientProvider > ,
262
+ ) ;
263
+
264
+ expect ( screen . queryByTestId ( "tax-edit-button" ) ) . toBeInTheDocument ( ) ;
265
+ } ) ;
266
+
267
+ it ( "New channel users should be able to add their tax info" , async ( ) => {
268
+ global . window = Object . create ( window ) ;
269
+ Object . defineProperty ( window , "accountId" , { value : "ABCDEF" } ) ;
270
+
271
+ const initialValues = {
272
+ country : "" ,
273
+ VATNumber : "" ,
274
+ marketPlace : UserSubscriptionMarketplace . CanonicalProChannel ,
275
+ } ;
276
+
277
+ const products = [
278
+ {
279
+ product : DistributorProduct ,
280
+ quantity : 1 ,
281
+ } ,
282
+ ] ;
283
+ render (
284
+ < QueryClientProvider client = { queryClient } >
285
+ < Formik initialValues = { initialValues } onSubmit = { jest . fn ( ) } >
286
+ < Elements stripe = { stripePromise } >
287
+ < Taxes products = { products } setError = { jest . fn ( ) } />
288
+ </ Elements >
289
+ </ Formik >
290
+ </ QueryClientProvider > ,
291
+ ) ;
292
+
293
+ expect ( screen . queryByTestId ( "tax-edit-button" ) ) . not . toBeInTheDocument ( ) ;
294
+ expect ( screen . getByRole ( "button" , { name : "Save" } ) ) . toBeInTheDocument ( ) ;
295
+ } ) ;
215
296
} ) ;
0 commit comments