1+ import { fireEvent , render } from '@testing-library/react' ;
12import React from 'react' ;
2- import TestRenderer , { act } from 'react-test-renderer' ;
3- import { render , fireEvent } from '@testing-library/react' ;
43import { MemoryRouter } from 'react-router-dom' ;
4+ import TestRenderer , { act } from 'react-test-renderer' ;
55
66const { ipcRenderer } = require ( 'electron' ) ;
77
8- import { SettingsRoute } from './Settings' ;
9- import { AppContext } from '../context/App' ;
8+ import { AxiosResponse } from 'axios' ;
109import { mockAccounts , mockSettings } from '../__mocks__/mock-state' ;
10+ import { AppContext } from '../context/App' ;
11+ import * as apiRequests from '../utils/api-requests' ;
1112import Constants from '../utils/constants' ;
13+ import { SettingsRoute } from './Settings' ;
1214
1315const mockNavigate = jest . fn ( ) ;
1416jest . mock ( 'react-router-dom' , ( ) => ( {
@@ -20,8 +22,7 @@ describe('routes/Settings.tsx', () => {
2022 const updateSetting = jest . fn ( ) ;
2123
2224 beforeEach ( ( ) => {
23- mockNavigate . mockReset ( ) ;
24- updateSetting . mockReset ( ) ;
25+ jest . clearAllMocks ( ) ;
2526 } ) ;
2627
2728 it ( 'should render itself & its children' , async ( ) => {
@@ -77,7 +78,12 @@ describe('routes/Settings.tsx', () => {
7778
7879 await act ( async ( ) => {
7980 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+ >
8187 < MemoryRouter >
8288 < SettingsRoute />
8389 </ MemoryRouter >
@@ -261,7 +267,12 @@ describe('routes/Settings.tsx', () => {
261267
262268 await act ( async ( ) => {
263269 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+ >
265276 < MemoryRouter >
266277 < SettingsRoute />
267278 </ MemoryRouter >
@@ -298,27 +309,19 @@ describe('routes/Settings.tsx', () => {
298309
299310 it ( 'should be able to enable colors' , async ( ) => {
300311 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+
320320 await act ( async ( ) => {
321- const { getByLabelText : getByLabelTextLocal } = render (
321+ const {
322+ getByLabelText : getByLabelTextLocal ,
323+ findByLabelText : findByLabelTextLocal ,
324+ } = render (
322325 < AppContext . Provider
323326 value = { {
324327 settings : mockSettings ,
@@ -332,34 +335,25 @@ describe('routes/Settings.tsx', () => {
332335 </ AppContext . Provider > ,
333336 ) ;
334337 getByLabelText = getByLabelTextLocal ;
338+ findByLabelText = findByLabelTextLocal ;
335339 } ) ;
336340
337- // await act(async () => {
338- // expect(getByLabelText('Use GitHub-like state colors')).toBeDefined();
339- // });
341+ await findByLabelText ( 'Use GitHub-like state colors' ) ;
340342
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' ) ) ;
348344
349345 expect ( updateSetting ) . toHaveBeenCalledTimes ( 1 ) ;
350346 expect ( updateSetting ) . toHaveBeenCalledWith ( 'colors' , true ) ;
351347 } ) ;
352348
353349 it ( 'should not be able to disable colors' , async ( ) => {
354350 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 ) ;
363357
364358 await act ( async ( ) => {
365359 const { queryByLabelText : queryByLabelLocal } = render (
@@ -377,10 +371,6 @@ describe('routes/Settings.tsx', () => {
377371 queryByLabelText = queryByLabelLocal ;
378372 } ) ;
379373
380- console . log (
381- queryByLabelText ( 'Use GitHub-like state colors (requires re-auth)' ) ,
382- ) ;
383-
384374 expect (
385375 queryByLabelText ( 'Use GitHub-like state colors (requires re-auth)' ) ,
386376 ) . toBeDefined ( ) ;
0 commit comments