1
+ import { fireEvent , render } from '@testing-library/react' ;
1
2
import React from 'react' ;
2
- import TestRenderer , { act } from 'react-test-renderer' ;
3
- import { render , fireEvent } from '@testing-library/react' ;
4
3
import { MemoryRouter } from 'react-router-dom' ;
4
+ import TestRenderer , { act } from 'react-test-renderer' ;
5
5
6
6
const { ipcRenderer } = require ( 'electron' ) ;
7
7
8
- import { SettingsRoute } from './Settings' ;
9
- import { AppContext } from '../context/App' ;
8
+ import { AxiosResponse } from 'axios' ;
10
9
import { mockAccounts , mockSettings } from '../__mocks__/mock-state' ;
10
+ import { AppContext } from '../context/App' ;
11
+ import * as apiRequests from '../utils/api-requests' ;
11
12
import Constants from '../utils/constants' ;
13
+ import { SettingsRoute } from './Settings' ;
12
14
13
15
const mockNavigate = jest . fn ( ) ;
14
16
jest . mock ( 'react-router-dom' , ( ) => ( {
15
17
...jest . requireActual ( 'react-router-dom' ) ,
16
18
useNavigate : ( ) => mockNavigate ,
17
19
} ) ) ;
20
+ jest . spyOn ( apiRequests , 'apiRequestAuth' ) . mockResolvedValue ( {
21
+ headers : {
22
+ 'x-oauth-scopes' : Constants . AUTH_SCOPE . join ( ', ' ) ,
23
+ } ,
24
+ } as unknown as AxiosResponse ) ;
18
25
19
26
describe ( 'routes/Settings.tsx' , ( ) => {
20
27
const updateSetting = jest . fn ( ) ;
21
28
22
29
beforeEach ( ( ) => {
23
- mockNavigate . mockReset ( ) ;
24
- updateSetting . mockReset ( ) ;
30
+ jest . clearAllMocks ( ) ;
25
31
} ) ;
26
32
27
33
it ( 'should render itself & its children' , async ( ) => {
@@ -77,7 +83,12 @@ describe('routes/Settings.tsx', () => {
77
83
78
84
await act ( async ( ) => {
79
85
const { getByLabelText : getByLabelTextLocal } = render (
80
- < AppContext . Provider value = { { settings : mockSettings , accounts : mockAccounts } } >
86
+ < AppContext . Provider
87
+ value = { {
88
+ settings : mockSettings ,
89
+ accounts : mockAccounts ,
90
+ } }
91
+ >
81
92
< MemoryRouter >
82
93
< SettingsRoute />
83
94
</ MemoryRouter >
@@ -261,7 +272,12 @@ describe('routes/Settings.tsx', () => {
261
272
262
273
await act ( async ( ) => {
263
274
const { getByLabelText : getByLabelTextLocal } = render (
264
- < AppContext . Provider value = { { settings : mockSettings , accounts : mockAccounts } } >
275
+ < AppContext . Provider
276
+ value = { {
277
+ settings : mockSettings ,
278
+ accounts : mockAccounts ,
279
+ } }
280
+ >
265
281
< MemoryRouter >
266
282
< SettingsRoute />
267
283
</ MemoryRouter >
@@ -298,27 +314,19 @@ describe('routes/Settings.tsx', () => {
298
314
299
315
it ( 'should be able to enable colors' , async ( ) => {
300
316
let getByLabelText ;
301
- jest . mock ( '../utils/api-requests' , ( ) => ( {
302
- ...jest . requireActual ( '../utils/api-requests' ) ,
303
- apiRequestAuth : jest . fn ( ) . mockResolvedValue ( {
304
- headers : {
305
- 'x-oauth-scopes' : Constants . AUTH_SCOPE . join ( ', ' ) ,
306
- 'access-control-allow-headers' : 'Authorization' ,
307
- 'access-control-allow-origin' : '*' ,
308
- 'access-control-expose-headers' : 'X-OAuth-Scopes' ,
309
- 'cache-control' : 'private, max-age=60, s-maxage=60' ,
310
- 'content-encoding' : 'gzip' ,
311
- 'content-security-policy' : "default-src 'none'" ,
312
- 'content-type' : 'application/json; charset=utf-8' ,
313
- server : 'GitHub.com' ,
314
- 'strict-transport-security' :
315
- 'max-age=31536000; includeSubdomains; preload' ,
316
- vary : 'Accept, Authorization, Cookie, X-GitHub-OTP, Accept-Encoding, Accept, X-Requested-With' ,
317
- } ,
318
- } ) ,
319
- } ) ) ;
317
+ let findByLabelText ;
318
+
319
+ jest . spyOn ( apiRequests , 'apiRequestAuth' ) . mockResolvedValue ( {
320
+ headers : {
321
+ 'x-oauth-scopes' : Constants . AUTH_SCOPE . join ( ', ' ) ,
322
+ } ,
323
+ } as unknown as AxiosResponse ) ;
324
+
320
325
await act ( async ( ) => {
321
- const { getByLabelText : getByLabelTextLocal } = render (
326
+ const {
327
+ getByLabelText : getByLabelTextLocal ,
328
+ findByLabelText : findByLabelTextLocal ,
329
+ } = render (
322
330
< AppContext . Provider
323
331
value = { {
324
332
settings : mockSettings ,
@@ -332,34 +340,25 @@ describe('routes/Settings.tsx', () => {
332
340
</ AppContext . Provider > ,
333
341
) ;
334
342
getByLabelText = getByLabelTextLocal ;
343
+ findByLabelText = findByLabelTextLocal ;
335
344
} ) ;
336
345
337
- // await act(async () => {
338
- // expect(getByLabelText('Use GitHub-like state colors')).toBeDefined();
339
- // });
346
+ await findByLabelText ( 'Use GitHub-like state colors' ) ;
340
347
341
- // await act(async () => {
342
- await act (
343
- ( ) =>
344
- // waitFor(() =>
345
- fireEvent . click ( getByLabelText ( 'Use GitHub-like state colors' ) ) ,
346
- // ),
347
- ) ;
348
+ fireEvent . click ( getByLabelText ( 'Use GitHub-like state colors' ) ) ;
348
349
349
350
expect ( updateSetting ) . toHaveBeenCalledTimes ( 1 ) ;
350
351
expect ( updateSetting ) . toHaveBeenCalledWith ( 'colors' , true ) ;
351
352
} ) ;
352
353
353
354
it ( 'should not be able to disable colors' , async ( ) => {
354
355
let queryByLabelText ;
355
- jest . mock ( '../utils/helpers' , ( ) => ( {
356
- ...jest . requireActual ( '../utils/helpers' ) ,
357
- apiRequestAuth : jest . fn ( ) . mockResolvedValue ( {
358
- headers : {
359
- 'x-oauth-scopes' : 'repo, notifications' ,
360
- } ,
361
- } ) ,
362
- } ) ) ;
356
+
357
+ jest . spyOn ( apiRequests , 'apiRequestAuth' ) . mockResolvedValue ( {
358
+ headers : {
359
+ 'x-oauth-scopes' : 'read:user, notifications' ,
360
+ } ,
361
+ } as unknown as AxiosResponse ) ;
363
362
364
363
await act ( async ( ) => {
365
364
const { queryByLabelText : queryByLabelLocal } = render (
@@ -377,10 +376,6 @@ describe('routes/Settings.tsx', () => {
377
376
queryByLabelText = queryByLabelLocal ;
378
377
} ) ;
379
378
380
- console . log (
381
- queryByLabelText ( 'Use GitHub-like state colors (requires re-auth)' ) ,
382
- ) ;
383
-
384
379
expect (
385
380
queryByLabelText ( 'Use GitHub-like state colors (requires re-auth)' ) ,
386
381
) . toBeDefined ( ) ;
0 commit comments