How do I render a Next.js page using React Testing Library? - javascript
I am writing a unit test for Next.js application.
The components side of the 'pages' folder return 'NextPage', in typescript, where
NextPage is imported from 'next'.
For example: import { NextPage } from 'next'; const BenchmarksPage: NextPage = () => {...
There are other components inside the 'pages' folder.
These components do not return 'NextPage', but return 'FC', which is imported from 'react'.
For example: import { FC } from 'react'; const RegisterJWT: FC = (props) => {...
In my Jest/Testing Library, I render these components like this:
render(<Component />)
expect(...)
Is this the correct way to render both the NextPage component and FC component?
Only FC components are rendering, but not NextPage components.
When rendering NextPage components, it returns <body></div></body>, an empty DOM tree.
EDIT:
Here is a NextPage component, that does not render in test:
import {
Box,
capitalize,
Container,
FormControl,
InputLabel,
MenuItem,
Select,
} from '#material-ui/core';
import { NextPage } from 'next';
import React, { useCallback, useEffect, useState } from 'react';
import { Helmet } from 'react-helmet-async';
import RequireScope from 'src/components/authentication/RequireScope';
import BenchmarkTable from 'src/components/benchmark/BenchmarkTable';
import DashboardLayout from 'src/components/dashboard/DashboardLayout';
import Heading from 'src/components/Heading';
import useSettings from 'src/hooks/useSettings';
import gtm from 'src/lib/gtm';
import { useDispatch, useSelector } from 'src/store';
import { getAllRollingTwelveCalcs } from 'src/store/rolling-twelve/rolling-twelve.thunk';
import { Timeframe, timeframeMap } from 'src/types/benchmark';
const BenchmarksPage: NextPage = () => {
const { settings } = useSettings();
const dispatch = useDispatch();
const [selectedTimeframe, setSelectedTimeframe] = useState<Timeframe>(
Timeframe.Monthly,
);
const company = useSelector((state) => state.company.current);
useEffect(() => {
gtm.push({ event: 'page_view' });
}, []);
useEffect(() => {
dispatch(getAllRollingTwelveCalcs());
}, [company]);
const handleTimeframeChange = useCallback(
(
event: React.ChangeEvent<{
name?: string;
value: Timeframe;
event: Event | React.SyntheticEvent<Element, Event>;
}>,
) => {
setSelectedTimeframe(event.target.value);
},
[],
);
return (
<RequireScope scopes={['query:benchmark-calcs']}>
<DashboardLayout>
<Helmet>
<title>Benchmarks</title>
</Helmet>
<Container maxWidth={settings.compact ? 'xl' : false}>
<Box
sx={{
display: 'flex',
alignItems: 'flex-end',
justifyContent: 'space-between',
mb: 4,
}}
>
<Heading>Benchmarks</Heading>
<FormControl sx={{ width: 300 }}>
<InputLabel>Timeframe</InputLabel>
<Select
sx={{ background: '#ffffff', maxWidth: 400 }}
value={selectedTimeframe}
label="Timeframe"
onChange={handleTimeframeChange}
>
{[...timeframeMap.keys()].map((timeframe) => (
<MenuItem key={timeframe} value={timeframe}>
{capitalize(timeframe)}
</MenuItem>
))}
</Select>
</FormControl>
</Box>
<BenchmarkTable
timeframe={selectedTimeframe}
data-testid="benchmark-table"
/>
</Container>
</DashboardLayout>
</RequireScope>
);
};
export default BenchmarksPage;
It uses thunk, which is here:
import { createAsyncThunk } from '#reduxjs/toolkit';
import axios from 'src/lib/axios';
import { RootState } from 'src/store';
import {
RollingTwelveAnalysisCategoryCalculation,
RollingTwelveManualInputLineItemCalculation,
} from 'src/types/rolling-twelve';
export const getAllRollingTwelveCalcs = createAsyncThunk<
{
anaylsisCategoryCalcs: RollingTwelveAnalysisCategoryCalculation[];
manualInputCalcs: RollingTwelveManualInputLineItemCalculation[];
},
undefined,
{
state: RootState;
}
>('rollingTwelve/getAll', async () => {
const response = await axios.get(`/rolling-twelve/all`);
return response.data;
});
Now here is a component, that is 'FC' (not NextPage) that does render. This still is inside pages folder:
import { useAuth0 } from '#auth0/auth0-react';
import {
Box,
Button,
Card,
CardContent,
Container,
Typography,
} from '#material-ui/core';
import Link from 'next/link';
import { useRouter } from 'next/router';
import type { FC } from 'react';
import React, { useEffect } from 'react';
import { Helmet } from 'react-helmet-async';
import Logo from 'src/components/Logo';
import axios from 'src/lib/axios';
import gtm from 'src/lib/gtm';
const Login: FC = () => {
const router = useRouter();
const { isLoading, isAuthenticated, loginWithRedirect } = useAuth0();
useEffect(() => {
gtm.push({ event: 'page_view' });
}, []);
useEffect(() => {
if (!isLoading && isAuthenticated) {
router.push('/recommendations');
}
}, [isLoading, isAuthenticated]);
const handleIntuitLogin = async () => {
try {
const response = await axios.get('/auth/sign-in-with-intuit');
window.location = response.data;
} catch (e) {
throw new Error(e);
}
};
const handleAuth0Login = async () => {
try {
const response = await axios.get('/auth/sign-in-with-auth0');
window.location = response.data;
} catch (e) {
throw new Error(e);
}
};
return (
<>
<Helmet>
<title>Login</title>
</Helmet>
<Box
sx={{
backgroundColor: 'background.default',
display: 'flex',
flexDirection: 'column',
minHeight: '100vh',
}}
>
<Container maxWidth="sm" sx={{ py: '80px' }}>
<Box
sx={{
display: 'flex',
justifyContent: 'center',
mb: 8,
}}
>
<Link href="/login">
<Box>
<Logo height={100} width={300} />
</Box>
</Link>
</Box>
<Card>
<CardContent
sx={{
display: 'flex',
flexDirection: 'column',
p: 4,
}}
>
<Box
sx={{
alignItems: 'center',
display: 'flex',
justifyContent: 'space-between',
mb: 3,
}}
>
<div>
<Typography
color="textPrimary"
gutterBottom
variant="h4"
data-testid="login-title"
>
Log in
</Typography>
</div>
</Box>
<Box
sx={{
flexGrow: 1,
mt: 3,
}}
>
<Button
color="primary"
onClick={handleAuth0Login}
fullWidth
size="large"
type="button"
variant="contained"
>
Sign In
</Button>
</Box>
<Box sx={{ mt: 2 }}>
<Button
color="primary"
onClick={handleIntuitLogin}
fullWidth
size="large"
type="button"
variant="contained"
>
Sign In With Intuit
</Button>
</Box>
</CardContent>
</Card>
</Container>
</Box>
</>
);
};
export default Login;
EDIT 2:
RequireScope:
import React, { useEffect, useState } from 'react';
import useAuth from 'src/hooks/useAuth';
export interface RequireScopeProps {
scopes: string[];
}
const RequireScope: React.FC<RequireScopeProps> = React.memo((props) => {
const { children, scopes } = props;
const { isInitialized, isAuthenticated, permissions } = useAuth();
const [isPermitted, setIsPermitted] = useState(false);
useEffect(() => {
if (isAuthenticated && isInitialized) {
(async () => {
const hasPermissions = scopes
.map((s) => {
return permissions.includes(s);
})
.filter(Boolean);
if (hasPermissions.length === scopes.length) {
setIsPermitted(true);
}
})();
}
}, [isAuthenticated, isInitialized, scopes, permissions]);
if (isPermitted) {
return <>{children}</>;
}
return null;
});
export default RequireScope;
Benchmarks.test.tsx
import '#testing-library/jest-dom';
import { render, screen, waitFor, within } from '#testing-library/react';
import { HelmetProvider } from 'react-helmet-async';
import { Provider } from 'react-redux';
import configureStore from 'redux-mock-store';
import thunk from 'redux-thunk';
import BenchmarksPage from '../src/pages/benchmarks/index';
import { initState } from './mockState'; // this is just an object with nested objects and arrays
test('renders header, timeframe dropdown and the chart image', async () => {
const middlewares = [thunk];
const mockStore = configureStore(middlewares);
render(
<HelmetProvider>
<Provider store={mockStore(initState)}>
<BenchmarksPage />
</Provider>
</HelmetProvider>,
);
// Note: NONE of these 5 queries below work. Component is not rendering in the DOM
const header = screen.getByRole('heading', { name: /benchmarks/i });
const header = screen.getByRole('heading');
const header = await screen.findByRole('heading', { name: /benchmarks/i });
const header = await waitFor(() => screen.getByText(/benchmarks/i));
const header = await waitFor(() => expect(screen.getByText(/benchmarks/i)), {
timeout: 4000,
});
screen.debug(); // doesn't work. <body></div></body>
}
EDIT3:
useAuth in RequireScope
import { useContext } from 'react';
import AuthContext from '../contexts/JWTContext';
const useAuth = () => useContext(AuthContext);
export default useAuth;
JWTContext above:
Just really long, dispatches to /login using JWT token etc.
EDIT 4:
JWTContext component
import { useRouter } from 'next/router';
import PropTypes from 'prop-types';
import {
createContext,
FC,
ReactNode,
useCallback,
useEffect,
useReducer,
} from 'react';
import { useDispatch } from 'src/store';
import { companyActions } from 'src/store/company/company.slice';
import {
getAllCompanies,
getCurrentCompany,
} from 'src/store/company/company.thunk';
import axios from '../lib/axios';
export interface AuthUser {
sub?: string;
nickname?: string;
email?: string;
name?: string;
picture?: string;
locale?: string;
updated_at?: Date;
}
interface State {
isInitialized: boolean;
isAuthenticated: boolean;
permissions: string[];
user: AuthUser;
}
interface AuthContextValue extends State {
platform: 'JWT';
login: () => Promise<void>;
logout: () => Promise<void>;
}
interface AuthProviderProps {
children: ReactNode;
}
type InitializeAction = {
type: 'INITIALIZE';
payload: {
isAuthenticated: boolean;
permissions: string[];
user: AuthUser;
};
};
type LoginAction = {
type: 'LOGIN';
};
type LogoutAction = {
type: 'LOGOUT';
};
type Action = InitializeAction | LoginAction | LogoutAction;
const initialState: State = {
isAuthenticated: false,
isInitialized: false,
permissions: [],
user: undefined,
};
const setSession = (
accessToken?: string,
permissions?: string,
user?: string,
): void => {
if (accessToken && permissions) {
localStorage.setItem('accessToken', accessToken);
localStorage.setItem('perms', permissions);
localStorage.setItem('user', user);
//axios.defaults.headers.common.Authorization = `Bearer ${accessToken}`;
} else {
localStorage.removeItem('accessToken');
localStorage.removeItem('perms');
localStorage.removeItem('user');
//delete axios.defaults.headers.common.Authorization;
}
};
const handlers: Record<string, (state: State, action: Action) => State> = {
INITIALIZE: (state: State, action: InitializeAction): State => {
const { isAuthenticated, permissions, user } = action.payload;
return {
...state,
isAuthenticated,
isInitialized: true,
permissions,
user,
};
},
LOGIN: (state: State): State => {
return {
...state,
isAuthenticated: true,
};
},
LOGOUT: (state: State): State => ({
...state,
isAuthenticated: false,
permissions: [],
}),
};
const reducer = (state: State, action: Action): State =>
handlers[action.type] ? handlers[action.type](state, action) : state;
const AuthContext = createContext<AuthContextValue>({
...initialState,
platform: 'JWT',
login: () => Promise.resolve(),
logout: () => Promise.resolve(),
});
export const AuthProvider: FC<AuthProviderProps> = (props) => {
const { children } = props;
const [state, dispatch] = useReducer(reducer, initialState);
const router = useRouter();
const reduxDispatch = useDispatch();
useEffect(() => {
const initialize = async (): Promise<void> => {
try {
if (router.isReady) {
const { token, permissions, user, companyId } = router.query;
// TODO: Move all of this stuff from query and localstorage into session
const accessToken =
(token as string) || window.localStorage.getItem('accessToken');
const permsStorage = window.localStorage.getItem('perms');
const perms = (permissions as string) || permsStorage;
const userStorage = window.localStorage.getItem('user');
const selectedCompanyId =
(companyId as string) || window.localStorage.getItem('companyId');
const authUser = (user as string) || userStorage;
if (accessToken && perms) {
setSession(accessToken, perms, authUser);
try {
// check if user is admin by this perm, probably want to add a flag later
if (perms.includes('create:calcs')) {
if (!selectedCompanyId) {
const response = await reduxDispatch(getAllCompanies());
const companyId = response.payload[0].id;
reduxDispatch(companyActions.selectCompany(companyId));
reduxDispatch(getCurrentCompany({ companyId }));
} else {
reduxDispatch(
companyActions.selectCompany(selectedCompanyId),
);
await reduxDispatch(
getCurrentCompany({ companyId: selectedCompanyId }),
);
}
} else {
reduxDispatch(companyActions.selectCompany(selectedCompanyId));
await reduxDispatch(
getCurrentCompany({ companyId: selectedCompanyId }),
);
}
} catch (e) {
console.warn(e);
} finally {
dispatch({
type: 'INITIALIZE',
payload: {
isAuthenticated: true,
permissions: JSON.parse(perms),
user: JSON.parse(authUser),
},
});
}
if (token || permissions) {
router.replace(router.pathname, undefined, { shallow: true });
}
} else {
dispatch({
type: 'INITIALIZE',
payload: {
isAuthenticated: false,
permissions: [],
user: undefined,
},
});
setSession(undefined);
if (router.pathname !== '/client-landing') {
router.push('/login');
}
}
}
} catch (err) {
console.error(err);
dispatch({
type: 'INITIALIZE',
payload: {
isAuthenticated: false,
permissions: [],
user: undefined,
},
});
//router.push('/login');
}
};
initialize();
}, [router.isReady]);
const login = useCallback(async (): Promise<void> => {
const response = await axios.get('/auth/sign-in-with-intuit');
window.location = response.data;
}, []);
const logout = useCallback(async (): Promise<void> => {
const token = localStorage.getItem('accessToken');
// only logout if already logged in
if (token) {
dispatch({ type: 'LOGOUT' });
}
setSession(null);
router.push('/login');
}, [dispatch, router]);
return (
<AuthContext.Provider
value={{
...state,
platform: 'JWT',
login,
logout,
}}
>
{state.isInitialized && children}
</AuthContext.Provider>
);
};
AuthProvider.propTypes = {
children: PropTypes.node.isRequired,
};
export default AuthContext;
When I render a component by wrapping it with AuthProvider, I get the error
TypeError: Cannot read property 'isReady' of null
217 |
218 | initialize();
> 219 | }, [router.isReady]);
I searched the whole day how to make the router not to be null, but I could not.
If there are any other suggestions on how to simulate a logged in user given the code above, I would greatly appreciate it. THank you.
Related
useSelector doesn't update value in Next.js
I have a problem, createAsyncThunk function makes request to server (axios) and then get data, after that extraReducers handle builder.addCase in it and makes state.value = action.payload, then console.log(state.value) writes value from server. Great! It works, but when I use useSelector it sees existing value from initialState but get value only when it was first time initialized (null or just []) not updated after dispatch in wrapper.getServerSIdeProps. Same with just reducers and function in it. It works change state (console.log write it) but useSelector doesn't give me updated value. UPDATE: If you have same issue. Just give up, don't use next-redux-wrapper. Context Api. Slice code import { createAsyncThunk, createSlice, PayloadAction } from '#reduxjs/toolkit'; import axios from 'axios'; import { HYDRATE } from 'next-redux-wrapper'; // types import { IInitialStateV1 } from '../types/store'; import { ITrack } from '../types/tracks/track'; export const fetchData = createAsyncThunk('main/fetchData', async (): Promise<ITrack[]> => { const { data } = await axios.get<ITrack[]>('http://localhost:5000/track'); return data; }) const initialState: IInitialStateV1 = { pause: true, currentTime: 0, volume: 0, duration: 0, active: null, tracks: [], } export const mainSlice = createSlice({ name: 'main', initialState, reducers: { setPause(state, action: PayloadAction<boolean>) { state.pause = action.payload; }, setTime(state, action: PayloadAction<number>) { state.currentTime = action.payload; }, setVolume(state, action: PayloadAction<number>) { state.volume = action.payload; }, setDuration(state, action: PayloadAction<number>) { state.duration = action.payload; }, setActive(state, action: PayloadAction<ITrack>) { state.active = action.payload; state.currentTime = 0; state.duration = 0; } }, extraReducers: (builder) => { // [HYDRATE]: (state, action) => { // return { // ...state, // ...action.payload, // } // }, // [fetchData.fulfilled.toString()]: (state, action: PayloadAction<ITrack[]>) => { // state.tracks = action.payload; // } builder.addCase(HYDRATE, (state, action: any) => { return { ...state, ...action.payload, } }).addCase(fetchData.fulfilled, (state, action: PayloadAction<ITrack[]>) => { // return { // ...state, // ...action.payload, // } state.tracks = action.payload; }); } }) export const { setPause, setTime, setVolume, setDuration, setActive } = mainSlice.actions; export default mainSlice.reducer; configurate store import { AnyAction, configureStore, ThunkDispatch } from '#reduxjs/toolkit'; import { createWrapper, MakeStore, Context } from 'next-redux-wrapper'; import mainRed from './index'; const makeStore = () => configureStore({ reducer: { main: mainRed }, }) type AppStore = ReturnType<typeof makeStore>; export type RootState = ReturnType<AppStore['getState']>; export type AppDispatch = AppStore['dispatch']; export type NextThunkDispatch = ThunkDispatch<RootState, void, AnyAction>; export const wrapper = createWrapper<AppStore>(makeStore); hooks for TypeScript import { useDispatch, useSelector, TypedUseSelectorHook } from "react-redux"; import { RootState, AppDispatch } from "../store/reducer"; export const useTypeSelector: TypedUseSelectorHook<RootState> = useSelector; export const useTypeDispath = ()=> useDispatch<AppDispatch>(); getServerSideProps and useSelector (in page) import { Container, ListItem, Stack, Box, Button } from "#mui/material"; import TrackList from "../../components/TrackList"; // interfaces import { ITrack } from "../../types/tracks/track"; // import hooks import { useRouter } from "next/router"; import { useTypeSelector } from "../../hooks/useTypeSelector"; // wrapper import { NextThunkDispatch, wrapper } from "../../store/reducer"; import { fetchData, setVolume } from "../../store"; export default function Index(): JSX.Element { const router = useRouter(); const tracks: ITrack[] = useTypeSelector(state => state.main.tracks); return ( <div className="main"> <Container > <Stack marginTop={20} sx={{ backgroundColor: "#C4C4C4", fontSize: '24px', fontWeight: 'bold' }}> <Box p={5} justifyContent="space-between"> <ListItem>List of Tracks</ListItem> <Button variant="outlined" sx={{ backgroundColor: 'blue', color: 'white' }} onClick={() => router.push('/tracks/create')} >Upload</Button> </Box> <TrackList tracks={tracks} /> </Stack> </Container> </div> ) } export const getServerSideProps = wrapper.getServerSideProps((store) => async () => { const dispatch = store.dispatch as NextThunkDispatch; // dispatch(fetchData()); dispatch(setVolume(2)); return { props: {} } })
why Fetch Api don't post new state updated in useReducer
I'm using useReducer and context to update my state in App.js then sent data to the database but it doesn't Post a new updated state, and always send non-updated state. App.js import AuthContext from './context'; import screenA from './screenA'; export default function App() { const initialLoginState = { email: null, }; const loginReducer = (prevState, action) => { switch (action.type) { case 'Email': return { ...prevState, email: action.Email, }; } }; const authContext = React.useMemo( () => ({ email: emailUser => { dispatch({type: 'Email', Email: emailUser}); }, signIn: async () => { try { fetch('FakeApi', { method: 'POST', headers: { Accept: 'application/json', 'Content-Type': 'application/json', }, body: JSON.stringify({ email: loginState.email, date: '2021-9-20', }), }); } catch (e) { console.log(e); } }, }), [], ); const [loginState, dispatch] = React.useReducer( loginReducer, initialLoginState, ); return ( <AuthContext.Provider value={authContext}> <screenA /> </AuthContext.Provider> Blockquote context I create a separate context component context.js import React from 'react'; export const AuthContext = React.createContext(); Blockquote screenA In screenA I use email and signIn to send data to App.js and then save this data in the database screenA.js import {AuthContext} from './context'; function screenA() { const {email,signIn} = React.useContext(AuthContext); return ( <View style={{marginTop: 150}}> {/* Count: {state.count} */} <Button title="sent email" onPress={() => { email('example#gmail.com'); }} /> <Button title="signIn" onPress={() => { signIn(); }} /> </View> ); } export default screenA;
React test a component with saga
Hllo Guys, I'm having a bit trouble with testing my component The problem is that I would like to test my React Native Component that uses saga to fetch data from server. The Problem is that I do know what I'm supposed to do, I think I should mock my API calls in my test file but I do not know how :/ The component file is really simple, when mounted it dispatches action to fetch list on vehicles, and then it shows them in UI. And until that is fetched it shows loading text Bellow are my current setup of components & test file. Here is a screen component that fetches initial data on screen load Screen Component import React, { useContext, useEffect, useState } from 'react'; import { Platform, FlatList, View, ActivityIndicator, Text } from 'react-native'; import PropTypes from 'prop-types'; import { useDispatch, useSelector } from 'react-redux'; import { vehiclesActions } from '_store/vehicles'; export const MainScreen = ({ navigation }) => { /** * Redux selectors and dispatch */ const { loading = true, vehicles = [], loadMore = false } = useSelector((state) => state.vehicles); /** * Initial effect, fetches all vehicles */ useEffect(() => { dispatch( vehiclesActions.vehicleGet({ page: 1, }) ); }, []); const renderCard = () => { return (<View><Text>Test</Text></View>) } if (loading) { return (<View><Text>App Loading </Text></View> } return ( <View style={styles.wrapper}> <View style={ Platform.OS === 'ios' ? { marginTop: 30 } : { marginTop: 0, flex: 1 } } > {!loading && ( <View style={Platform.OS === 'ios' ? {} : { flex: 1 }}> <FlatList testID={'flat-list'} data={vehicles} renderItem={renderCard} /> </View> )} </View> </View> ); }; MainScreen.propTypes = { navigation: PropTypes.object }; export default MainScreen; My Vehicles Saga: const api = { vehicles: { getVehicles: (page) => { return api.get(`/vehicles/list?page=${page}`, {}); }, } function* getVehicles(action) { try { const { page } = action.payload; const { data } = yield call(api.vehicles.getVehicles, page); yield put({ type: vehiclesConstants.VEHICLE_GET_SUCCESS, payload: data }); } catch (err) { yield call(errorHandler, err); yield put({ type: vehiclesConstants.VEHICLE_GET_FAIL }); } } export function* vehiclesSaga() { yield takeLatest(vehiclesConstants.VEHICLE_GET_REQUEST, getVehicles); } Actions: export const vehiclesActions = { vehicleGet: payload => ({ type: vehiclesConstants.VEHICLE_GET_REQUEST, payload }), vehicleGetSuccess: payload => ({ type: vehiclesConstants.VEHICLE_GET_SUCCESS, payload }), vehicleGetFail: error => ({ type: vehiclesConstants.VEHICLE_GET_FAIL, error }), } Reducer import { vehiclesConstants } from "./constants"; const initialState = { vehicles: [], loading: true, }; export const vehiclesReducer = (state = initialState, action) => { switch (action.type) { case vehiclesConstants.VEHICLE_GET_REQUEST: return { ...state, loading: true, }; case vehiclesConstants.VEHICLE_GET_SUCCESS: return { ...state, loading: false, vehicles: action.payload, }; } } My Test File import 'react-native'; import React from 'react'; import {cleanup, render, fireEvent} from '#testing-library/react-native'; import AppScreen from '../../../../src/screens/App/index'; import {Provider} from 'react-redux'; import {store} from '../../../../src/store/configureStore'; describe('App List Component', () => { beforeEach(() => jest.useFakeTimers()); afterEach(cleanup); it('should render vehicle list page title', async () => { const navigation = { setParams: () => {}, navigate: jest.fn(), }; const route = { } const component = ( <Provider store={store}> <AppScreen route={route} navigation={navigation} /> </Provider>); const {getByText, getByTestId} = render(component); const pageTitle = await getByText('App Loading'); // this works fine expect(pageTitle).toBeDefined(); }); it('should navigate to add vehicle', async () => { const navigation = { setParams: () => {}, navigate: jest.fn(), }; const route = { } const component = ( <Provider store={store}> <AppScreen route={route} navigation={navigation} /> </Provider>); const {getByText, getByTestId} = render(component); const flatList = await getByTestId('flat-list');// this throws error since flat list is still not shown, and loading is showing instead }); Like I see above I cannot find element with testId flat-list, since component AppScreen it always show loading text, is there any way I could mock that API call and make this to work ?
Jest allows you to mock any module using jest.mock. You have to write an alternative to axios.get like this const vehiclesData = [ // ... put default data here ] const delay = (ms, value) => new Promise(res => setTimeout(() => res(value), ms)) const mockAxiosGet = async (path) => { let result = null if (path.includes('vehicles/list') { const query = new URLSearchParams(path.replace(/^[^?]+\?/, '')) const page = + query.get('page') const pageSize = 10 const offset = (page - 1)*pageSize result = vehiclesData.slice(offset, offset + pageSize) } return delay( // simulate 100-500ms latency Math.floor(100 + Math.random()*400), { data: result } ) } Then modify the test file as import 'react-native'; import React from 'react'; import {cleanup, render, fireEvent} from '#testing-library/react-native'; import axios from 'axios' // enable jest mock on 'axios' module jest.mock('axios') import AppScreen from '../../../../src/screens/App/index'; import {Provider} from 'react-redux'; import {store} from '../../../../src/store/configureStore'; describe('App List Component', () => { before(() => { // mock axios implementation axios.get.mockImplementation(mockAxiosGet) }) beforeEach(() => jest.useFakeTimers()); afterEach(cleanup); it('should render vehicle list page title', async () => { const navigation = { setParams: () => {}, navigate: jest.fn(), }; const route = { } const component = ( <Provider store={store}> <AppScreen route={route} navigation={navigation} /> </Provider>); const {getByText, getByTestId} = render(component); const pageTitle = await getByText('App Loading'); // this works fine expect(pageTitle).toBeDefined(); }); it('should navigate to add vehicle', async () => { const navigation = { setParams: () => {}, navigate: jest.fn(), }; const route = { } const component = ( <Provider store={store}> <AppScreen route={route} navigation={navigation} /> </Provider>); const {getByText, getByTestId} = render(component); const flatList = await getByTestId('flat-list');// this throws error since flat list is still not shown, and loading is showing instead }); For your use case, read more at Mocking Implementations
(React and Redux) Error: Objects are not valid as a React child
I am working with React and Redux and I am getting the error mentioned in the title. This is how it looks in the browser, when I try to access the frontend - Here is the code - ProductActions.js - import axios from 'axios' import { PRODUCT_LIST_REQUEST, PRODUCT_LIST_SUCCESS, PRODUCT_LIST_FAIL } from '../constants/productConstants' export const listProducts = () => async (dispatch) => { try{ dispatch({ type: PRODUCT_LIST_REQUEST }) const { data } = await axios.get('/api/products/') dispatch({ type: PRODUCT_LIST_SUCCESS, payload:data }) }catch(error){ dispatch({ type:PRODUCT_LIST_FAIL, payload:error.response && error.response.data.message ? error.response.data.message : error.message, }) } } HomeScreen.js - import React, { useState, useEffect } from "react"; import { useDispatch, useSelector } from 'react-redux' import { Row, Col } from "react-bootstrap"; import Product from "../components/Product"; import { listProducts } from '../actions/productActions'; function HomeScreen() { const dispatch = useDispatch() const productList = useSelector(state => state.productList) const {error, loading, products} = productList useEffect(() => { dispatch(listProducts()) },[dispatch] ) return ( <div> <h1>Latest Products</h1> {loading ? <h2>Loading...</h2> :error ? <h3>{error}</h3> : <Row> {products.map((product) => ( <Col key={product._id} sm={12} md={6} lg={4} xl={3}> <Product product={product} /> </Col> ))} </Row> } </div> ); } export default HomeScreen; ProductReducers.js - import { PRODUCT_LIST_REQUEST, PRODUCT_LIST_SUCCESS, PRODUCT_LIST_FAIL } from '../constants/productConstants' export const productListReducer = (state = { products: [] }, action) => { switch (action.type) { case PRODUCT_LIST_REQUEST: return { loading: true, products: [] } case PRODUCT_LIST_SUCCESS: return { loading:false, error: action.payload } case PRODUCT_LIST_FAIL: return { loading: false, error: action.payload } default: return state } }
React Native - Navigating by ID from api with Redux not working
So, i'am using redux for my application and so far it's good. But recently I just hit a wall that stops me from coding for a while. I'am trying to make a navigation using the ID of every item in my database with redux. let's say I have a "category of food" page that is filled with item from my database and each of those items has their own ID, using those ID I'am trying to navigate to a second page which is the "dishes page". If a user clicks a category, the application should display the dishes that the category contains. As of now, I manage to display the category but I don't know how to apply the navigation. Whenever I try to add a kind of navigation that I know I'am facing an error: Error state_error ( ** UPDATE ** ) You can see all the details in my code. Here is my code: App.js ( ** EDIT ** ) import React, { Component } from 'react'; import { Platform, StyleSheet, Text, View } from 'react-native'; import { StackNavigator } from 'react-navigation'; import { createStore, applyMiddleware } from 'redux'; import { Provider } from 'react-redux'; import thunk from 'redux-thunk'; import Reducer from './app/redux/reducers/Reducer'; import AppContainer from './app/container/AppContainer'; import DishContainer from './app/container/DishContainer'; import Dishes from './app/component/Dishes'; const createStoreWithMiddleware = applyMiddleware(thunk)(createStore); const store = createStoreWithMiddleware(Reducer); export default class App extends Component{ render() { return ( <Provider store = { store }> <Root /> </Provider> ); } } const Root = StackNavigator ({ AppContainer : { screen: AppContainer }, DishContainer : { screen: DishContainer }, Dishes : { screen: Dishes }, }) Action.js import { FETCHING_CATEGORY_REQUEST, FETCHING_CATEGORY_SUCCESS, FETCHING_CATEGORY_FAILURE, FETCHING_DISHES_REQUEST, FETCHING_DISHES_SUCCESS, FETCHING_DISHES_FAILURE, } from "./types"; import axios from 'axios'; /* ---------------------- CATEGORY ---------------------------- */ export const fetchingCategoryRequest = () => ({ type: FETCHING_CATEGORY_REQUEST }); export const fetchingCategorySuccess = (json) => ({ type: FETCHING_CATEGORY_SUCCESS, payload: json, }); export const fetchingCategoryFailure = (error) => ({ type: FETCHING_CATEGORY_FAILURE, payload: error }); export const fetchCategory = () => { return (dispatch) => { axios.get('http://192.168.254.100:3308/categories/') .then(response => { dispatch({ type: FETCHING_CATEGORY_SUCCESS, payload: response.data }) }) .catch(error => console.log(error.response.data)); } } /* ---------------------- DISHES ---------------------------- */ export const fetchingDishesRequest = () => ({ type: FETCHING_DISHES_REQUEST }); export const fetchingDishesSuccess = (json) => ({ type: FETCHING_DISHES_SUCCESS, dish: json, }); export const fetchingDishesFailure = (error) => ({ type: FETCHING_DISHES_FAILURE, dish: error, }); export const fetchDish = () => { return (dispatch) => { const { params } = this.props.navigation.state; axios.get('http://192.168.254.100:3308/categories/' + params.id) .then(response => { dispatch({ type: FETCHING_DISHES_SUCCESS, dish: response.data }) }) .catch(error => console.log(error.response.data)); } } Reducer.js const initialState = { isFetching: false, errorMessage: '', category: [], dishes: [], }; const categoryReducer = (state = initialState, action) => { switch(action.type) { case FETCHING_CATEGORY_REQUEST: return { ...state, isFetching: true }; case FETCHING_CATEGORY_FAILURE: return { ...state, isFetching: false, errorMessage: action.payload }; case FETCHING_CATEGORY_SUCCESS: return { ...state, isFetching: false, category: action.payload }; /* --------------------- DISHES ------------------------- */ case FETCHING_DISHES_REQUEST: return { ...state, isFetching: true }; case FETCHING_DISHES_SUCCESS: return { ...state, isFetching: false, dishes: action.dish }; case FETCHING_DISHES_FAILURE: return { ...state, isFetching: false, errorMessage: action.dish } default: return state; } } export default categoryReducer; CategoryList.js export default class CategoryList extends Component { _renderItem = ({ item }) => { const { cat_name } = item; return ( <View style={styles.cardContainerStyle}> <View style={{ paddingRight: 5 }}> <TouchableOpacity style = { styles.buttonContainer }> <Text style={styles.cardTextStyle} onPress = { () => this.props.navigation.navigate('Dishes', { id: item.cat_id })}> {cat_name} </Text> </TouchableOpacity> </View> </View> ); }; render() { return ( <FlatList style={{ flex: 1 }} data = {this.props.category} keyExtractor={(item, index) => index.toString()} renderItem={this._renderItem} /> ) } } AppContainer.js import CategoryList from "../component/CategoryList"; import { fetchCategory } from '../redux/actions/Actions'; import { connect } from 'react-redux'; class AppContainer extends Component { componentDidMount() { this.props.fetchCategory(); } render() { let content = <CategoryList category = { this.props.randomCategory.category }/>; if (this.props.randomCategory.isFetching) { content = <ActivityIndicator size="large"/>; } return <View style={styles.container}>{content}</View>; } } const mapStateToProps = state => { return { randomCategory: state }; } export default connect(mapStateToProps, { fetchCategory })(AppContainer); Dishes.js (This is where I want to navigate from Category) export default class Dishes extends Component { _renderItem = ({ item }) => { const { cat_desc } = item; return ( <View style={styles.cardContainerStyle}> <View style={{ paddingRight: 5 }}> <TouchableOpacity> <Text style={styles.cardTextStyle}> {cat_desc} </Text> </TouchableOpacity> </View> </View> ); }; render() { const { params } = this.props.navigation.state; return ( <FlatList style={{ flex: 1 }} data = {this.props.dishes} keyExtractor={(item, index) => index.toString()} renderItem={this._renderItem} /> ) } } DishContainer.js import Dishes from "../component/Dishes"; import { fetchDish } from '../redux/actions/Actions'; import { connect } from 'react-redux'; class DishContainer extends Component { componentDidMount() { this.props.fetchDish(); } render() { let content = <Dishes dishes = { this.props.randomDishes.dishes }/>; if (this.props.randomDishes.isFetching) { content = <ActivityIndicator size="large"/>; } return <View style={styles.container}>{content}</View>; } } const mapStateToProps = state => { return { randomDishes: state }; } EDIT: Some how the params is not working or the props.navigation.navigate is not calling the item's id from my api. I'am not sure, please help me.
Edit your AppContainer.js like below : import CategoryList from "../component/CategoryList"; import { fetchCategory } from '../redux/actions/Actions'; import { connect } from 'react-redux'; class AppContainer extends Component { componentDidMount() { this.props.fetchCategory(); } render() { let content = <CategoryList navigation = {this.props.navigation} category = {this.props.randomCategory.category}/>; if (this.props.randomCategory.isFetching) { content = <ActivityIndicator size="large"/>; } return <View style={styles.container}>{content}</View>; } } const mapStateToProps = state => { return { randomCategory: state }; } export default connect(mapStateToProps, { fetchCategory })(AppContainer); The issue is CategoryList can't get navigation props of parent component(AppCountainer), So you have to pass navigation as props.