1- import { comicMockResponse as mockComicData } from '__mocks__/fixtures'
1+ import { comicMockParsed } from '__mocks__/fixtures'
22import React from 'react'
33import { TestIDs } from '@config/testIDs'
44import Routes from '@navigation/routes'
5- import { createNavigationProps , fireEvent , render } from '@utils/testing'
5+ import * as remoteComics from '@remote/comics'
6+ import { act , createNavigationProps , fireEvent , render } from '@utils/testing'
67import DemoScreen from './DemoScreen'
78
89// eslint-disable-next-line @typescript-eslint/no-explicit-any
910const navPropsMock = createNavigationProps ( ) as any
1011
12+ const mockUseLatestComicQuery = jest . spyOn ( remoteComics , 'useLatestComicQuery' ) . mockReturnValue ( {
13+ isLoading : false ,
14+ data : undefined ,
15+ } as ReturnType < typeof remoteComics . useLatestComicQuery > )
16+
17+ jest . mock ( '@remote/auth' , ( ) => ( {
18+ useLogOutMutation : jest . fn ( ) . mockImplementation ( ( ) => ( {
19+ mutate : jest . fn ( ) ,
20+ isPending : false ,
21+ } ) ) ,
22+ } ) )
23+
1124describe ( 'when increment button pressed' , ( ) => {
1225 it ( 'should increment counter by 5' , ( ) => {
1326 const { getByText } = render ( < DemoScreen { ...navPropsMock } /> )
1427 const prevCounterValue = parseInt (
1528 getByText ( / d e m o S c r e e n .c o u n t e r / ) . props . children . split ( ' ' ) [ 1 ] ,
1629 10 ,
1730 )
18- fireEvent . press ( getByText ( / d e m o S c r e e n .i n c r e m e n t B u t t o n / ) )
31+ act ( ( ) => {
32+ fireEvent . press ( getByText ( / d e m o S c r e e n .i n c r e m e n t B u t t o n / ) )
33+ } )
34+
1935 const counterValue = parseInt ( getByText ( / d e m o S c r e e n .c o u n t e r / ) . props . children . split ( ' ' ) [ 1 ] , 10 )
2036
2137 expect ( counterValue ) . toBe ( prevCounterValue + 5 )
@@ -29,7 +45,10 @@ describe('when decrement button pressed', () => {
2945 getByText ( / d e m o S c r e e n .c o u n t e r / ) . props . children . split ( ' ' ) [ 1 ] ,
3046 10 ,
3147 )
32- fireEvent . press ( getByText ( / d e m o S c r e e n .d e c r e m e n t B u t t o n / ) )
48+
49+ act ( ( ) => {
50+ fireEvent . press ( getByText ( / d e m o S c r e e n .d e c r e m e n t B u t t o n / ) )
51+ } )
3352 const counterValue = parseInt ( getByText ( / d e m o S c r e e n .c o u n t e r / ) . props . children . split ( ' ' ) [ 1 ] , 10 )
3453
3554 expect ( counterValue ) . toBe ( prevCounterValue - 15 )
@@ -39,14 +58,11 @@ describe('when decrement button pressed', () => {
3958describe ( 'Comic card' , ( ) => {
4059 describe ( 'when comic is available' , ( ) => {
4160 it ( 'renders the comic' , async ( ) => {
42- fetchMock . mockResponseOnce ( JSON . stringify ( mockComicData ) )
61+ mockUseLatestComicQuery . mockReturnValueOnce ( {
62+ isLoading : false ,
63+ data : comicMockParsed ,
64+ } as ReturnType < typeof remoteComics . useLatestComicQuery > )
4365
44- const comicMock = {
45- id : 1 ,
46- title : 'Some mock title' ,
47- imageUrl : 'http://example.com/test.jpg' ,
48- description : 'Some mock description' ,
49- }
5066 const preloadedState = {
5167 demo : {
5268 counter : 420 ,
@@ -57,13 +73,18 @@ describe('Comic card', () => {
5773 preloadedState,
5874 } )
5975
60- expect ( getByText ( comicMock . title ) ) . toBeDefined ( )
61- expect ( getByText ( comicMock . description ) ) . toBeDefined ( )
76+ expect ( getByText ( comicMockParsed . title ) ) . toBeDefined ( )
77+ expect ( getByText ( comicMockParsed . description ) ) . toBeDefined ( )
6278 expect ( getByTestId ( TestIDs . DEMO_COMIC_IMAGE ) ) . toBeDefined ( )
6379 } )
6480 } )
6581
6682 describe ( 'when NO comic is available' , ( ) => {
83+ mockUseLatestComicQuery . mockReturnValue ( {
84+ isLoading : true ,
85+ data : undefined ,
86+ } as ReturnType < typeof remoteComics . useLatestComicQuery > )
87+
6788 it ( 'renders the loading spinner' , ( ) => {
6889 const preloadedState = {
6990 demo : {
@@ -83,7 +104,9 @@ describe('Comic card', () => {
83104describe ( 'when "go to translations demo" pressed' , ( ) => {
84105 it ( 'should navigate to translations demo screen' , ( ) => {
85106 const { getByText } = render ( < DemoScreen { ...navPropsMock } /> )
86- fireEvent . press ( getByText ( / d e m o S c r e e n .g o T o T r a n s l a t i o n s D e m o / ) )
107+ act ( ( ) => {
108+ fireEvent . press ( getByText ( / d e m o S c r e e n .g o T o T r a n s l a t i o n s D e m o / ) )
109+ } )
87110
88111 expect ( navPropsMock . navigation . navigate ) . toBeCalledWith ( Routes . TRANSLATIONS_DEMO_SCREEN )
89112 } )
0 commit comments