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' , ( ) => ( {
@@ -20,8 +22,7 @@ describe('routes/Settings.tsx', () => {
20
22
const updateSetting = jest . fn ( ) ;
21
23
22
24
beforeEach ( ( ) => {
23
- mockNavigate . mockReset ( ) ;
24
- updateSetting . mockReset ( ) ;
25
+ jest . clearAllMocks ( ) ;
25
26
} ) ;
26
27
27
28
it ( 'should render itself & its children' , async ( ) => {
@@ -77,7 +78,12 @@ describe('routes/Settings.tsx', () => {
77
78
78
79
await act ( async ( ) => {
79
80
const { getByLabelText : getByLabelTextLocal } = render (
80
- < AppContext . Provider value = { { settings : mockSettings , accounts : mockAccounts } } >
81
+ < AppContext . Provider
82
+ value = { {
83
+ settings : mockSettings ,
84
+ accounts : mockAccounts ,
85
+ } }
86
+ >
81
87
< MemoryRouter >
82
88
< SettingsRoute />
83
89
</ MemoryRouter >
@@ -261,7 +267,12 @@ describe('routes/Settings.tsx', () => {
261
267
262
268
await act ( async ( ) => {
263
269
const { getByLabelText : getByLabelTextLocal } = render (
264
- < AppContext . Provider value = { { settings : mockSettings , accounts : mockAccounts } } >
270
+ < AppContext . Provider
271
+ value = { {
272
+ settings : mockSettings ,
273
+ accounts : mockAccounts ,
274
+ } }
275
+ >
265
276
< MemoryRouter >
266
277
< SettingsRoute />
267
278
</ MemoryRouter >
@@ -298,27 +309,19 @@ describe('routes/Settings.tsx', () => {
298
309
299
310
it ( 'should be able to enable colors' , async ( ) => {
300
311
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
- } ) ) ;
312
+ let findByLabelText ;
313
+
314
+ jest . spyOn ( apiRequests , 'apiRequestAuth' ) . mockResolvedValue ( {
315
+ headers : {
316
+ 'x-oauth-scopes' : Constants . AUTH_SCOPE . join ( ', ' ) ,
317
+ } ,
318
+ } as unknown as AxiosResponse ) ;
319
+
320
320
await act ( async ( ) => {
321
- const { getByLabelText : getByLabelTextLocal } = render (
321
+ const {
322
+ getByLabelText : getByLabelTextLocal ,
323
+ findByLabelText : findByLabelTextLocal ,
324
+ } = render (
322
325
< AppContext . Provider
323
326
value = { {
324
327
settings : mockSettings ,
@@ -332,34 +335,25 @@ describe('routes/Settings.tsx', () => {
332
335
</ AppContext . Provider > ,
333
336
) ;
334
337
getByLabelText = getByLabelTextLocal ;
338
+ findByLabelText = findByLabelTextLocal ;
335
339
} ) ;
336
340
337
- // await act(async () => {
338
- // expect(getByLabelText('Use GitHub-like state colors')).toBeDefined();
339
- // });
341
+ await findByLabelText ( 'Use GitHub-like state colors' ) ;
340
342
341
- // await act(async () => {
342
- await act (
343
- ( ) =>
344
- // waitFor(() =>
345
- fireEvent . click ( getByLabelText ( 'Use GitHub-like state colors' ) ) ,
346
- // ),
347
- ) ;
343
+ fireEvent . click ( getByLabelText ( 'Use GitHub-like state colors' ) ) ;
348
344
349
345
expect ( updateSetting ) . toHaveBeenCalledTimes ( 1 ) ;
350
346
expect ( updateSetting ) . toHaveBeenCalledWith ( 'colors' , true ) ;
351
347
} ) ;
352
348
353
349
it ( 'should not be able to disable colors' , async ( ) => {
354
350
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
- } ) ) ;
351
+
352
+ jest . spyOn ( apiRequests , 'apiRequestAuth' ) . mockResolvedValue ( {
353
+ headers : {
354
+ 'x-oauth-scopes' : 'read:user, notifications' ,
355
+ } ,
356
+ } as unknown as AxiosResponse ) ;
363
357
364
358
await act ( async ( ) => {
365
359
const { queryByLabelText : queryByLabelLocal } = render (
@@ -377,10 +371,6 @@ describe('routes/Settings.tsx', () => {
377
371
queryByLabelText = queryByLabelLocal ;
378
372
} ) ;
379
373
380
- console . log (
381
- queryByLabelText ( 'Use GitHub-like state colors (requires re-auth)' ) ,
382
- ) ;
383
-
384
374
expect (
385
375
queryByLabelText ( 'Use GitHub-like state colors (requires re-auth)' ) ,
386
376
) . toBeDefined ( ) ;
0 commit comments