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' , ( ) => ( {
1517 ...jest . requireActual ( 'react-router-dom' ) ,
1618 useNavigate : ( ) => mockNavigate ,
1719} ) ) ;
20+ jest . spyOn ( apiRequests , 'apiRequestAuth' ) . mockResolvedValue ( {
21+ headers : {
22+ 'x-oauth-scopes' : Constants . AUTH_SCOPE . join ( ', ' ) ,
23+ } ,
24+ } as unknown as AxiosResponse ) ;
1825
1926describe ( 'routes/Settings.tsx' , ( ) => {
2027 const updateSetting = jest . fn ( ) ;
2128
2229 beforeEach ( ( ) => {
23- mockNavigate . mockReset ( ) ;
24- updateSetting . mockReset ( ) ;
30+ jest . clearAllMocks ( ) ;
2531 } ) ;
2632
2733 it ( 'should render itself & its children' , async ( ) => {
@@ -77,7 +83,12 @@ describe('routes/Settings.tsx', () => {
7783
7884 await act ( async ( ) => {
7985 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+ >
8192 < MemoryRouter >
8293 < SettingsRoute />
8394 </ MemoryRouter >
@@ -261,7 +272,12 @@ describe('routes/Settings.tsx', () => {
261272
262273 await act ( async ( ) => {
263274 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+ >
265281 < MemoryRouter >
266282 < SettingsRoute />
267283 </ MemoryRouter >
@@ -298,27 +314,19 @@ describe('routes/Settings.tsx', () => {
298314
299315 it ( 'should be able to enable colors' , async ( ) => {
300316 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+
320325 await act ( async ( ) => {
321- const { getByLabelText : getByLabelTextLocal } = render (
326+ const {
327+ getByLabelText : getByLabelTextLocal ,
328+ findByLabelText : findByLabelTextLocal ,
329+ } = render (
322330 < AppContext . Provider
323331 value = { {
324332 settings : mockSettings ,
@@ -332,34 +340,25 @@ describe('routes/Settings.tsx', () => {
332340 </ AppContext . Provider > ,
333341 ) ;
334342 getByLabelText = getByLabelTextLocal ;
343+ findByLabelText = findByLabelTextLocal ;
335344 } ) ;
336345
337- // await act(async () => {
338- // expect(getByLabelText('Use GitHub-like state colors')).toBeDefined();
339- // });
346+ await findByLabelText ( 'Use GitHub-like state colors' ) ;
340347
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' ) ) ;
348349
349350 expect ( updateSetting ) . toHaveBeenCalledTimes ( 1 ) ;
350351 expect ( updateSetting ) . toHaveBeenCalledWith ( 'colors' , true ) ;
351352 } ) ;
352353
353354 it ( 'should not be able to disable colors' , async ( ) => {
354355 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 ) ;
363362
364363 await act ( async ( ) => {
365364 const { queryByLabelText : queryByLabelLocal } = render (
@@ -377,10 +376,6 @@ describe('routes/Settings.tsx', () => {
377376 queryByLabelText = queryByLabelLocal ;
378377 } ) ;
379378
380- console . log (
381- queryByLabelText ( 'Use GitHub-like state colors (requires re-auth)' ) ,
382- ) ;
383-
384379 expect (
385380 queryByLabelText ( 'Use GitHub-like state colors (requires re-auth)' ) ,
386381 ) . toBeDefined ( ) ;
0 commit comments