Fakultas Ilmu Komputer UI

Commit 3a3a2623 authored by wulanmantiri's avatar wulanmantiri
Browse files

[REFACTOR] Add handler for newly onboarding users

parent 3b8d062e
......@@ -6,13 +6,43 @@ import ClientNavigation, {
ExtQuestionnaireStackScreen,
} from '.';
import { NavigationContainer } from '@react-navigation/native';
import { mockUserContext } from 'mocks/userContext';
import { UserContext } from 'provider';
describe('ClientNavigation', () => {
it('renders correctly', () => {
it('provides tab if user has finished onboarding', () => {
const userProviderValues = {
...mockUserContext,
user: {
...mockUserContext.user,
is_finished_onboarding: true,
},
};
render(
<UserContext.Provider value={userProviderValues}>
<NavigationContainer>
<ClientNavigation />
</NavigationContainer>,
</NavigationContainer>
</UserContext.Provider>,
);
});
it('does not provide tab if user has not finished onboarding', () => {
const userProviderValues = {
...mockUserContext,
user: {
...mockUserContext.user,
is_finished_onboarding: false,
},
};
render(
<UserContext.Provider value={userProviderValues}>
<NavigationContainer>
<ClientNavigation />
</NavigationContainer>
</UserContext.Provider>,
);
});
......
import React, { FC } from 'react';
import React, { FC, useContext } from 'react';
import { createStackNavigator } from '@react-navigation/stack';
import { createBottomTabNavigator } from '@react-navigation/bottom-tabs';
import { Icon } from 'react-native-elements';
......@@ -21,6 +21,7 @@ import {
import ReadOnlyWeeklyReport from 'scenes/report/ReadOnlyWeeklyReport';
import ChooseWeekForClient from 'scenes/report/ChooseWeekForClient';
import ChatForClient from 'scenes/chat/ChatForClient';
import { UserContext } from 'provider';
interface NavRoute<T = any> {
name: string;
......@@ -107,6 +108,11 @@ export const WeeklyReportStackScreen: FC = () => (
const ClientTab = createBottomTabNavigator();
const ClientNavigation: FC = () => {
const { user } = useContext(UserContext);
if (!user.is_finished_onboarding) {
return <ExtQuestionnaireStackScreen />;
}
return (
<ClientTab.Navigator
initialRouteName={ROUTES.clientTabProfile}
......
......@@ -3,6 +3,8 @@ import { render } from '@testing-library/react-native';
import ConsentForm from '.';
import { mockDietQuestionnaire } from 'mocks/dietQuestionnaire';
import { mockUserContext } from 'mocks/userContext';
import { UserContext } from 'provider';
const mockedNavigate = jest.fn();
......@@ -15,7 +17,15 @@ jest.mock('@react-navigation/native', () => {
});
describe('ConsentForm', () => {
const userProviderValues = {
...mockUserContext,
};
it('renders correctly', () => {
render(<ConsentForm route={{ params: mockDietQuestionnaire }} />);
render(
<UserContext.Provider value={userProviderValues}>
<ConsentForm route={{ params: mockDietQuestionnaire }} />
</UserContext.Provider>,
);
});
});
......@@ -5,6 +5,8 @@ import axios from 'axios';
import Questionnaire1 from '.';
import { mockDietQuestionnaire } from 'mocks/dietQuestionnaire';
import { textFields } from 'constants/questionnaire';
import { mockUserContext } from 'mocks/userContext';
import { UserContext } from 'provider';
const mockedNavigate = jest.fn();
......@@ -27,6 +29,10 @@ describe('Questionnaire1', () => {
waist_size: '765',
};
const userProviderValues = {
...mockUserContext,
};
it('does not redirect to extended questionnaire if form values are invalid', async () => {
const updateDietQuestionnaireApi = () =>
Promise.resolve({
......@@ -36,7 +42,9 @@ describe('Questionnaire1', () => {
mockAxios.request.mockImplementationOnce(updateDietQuestionnaireApi);
const { getByPlaceholderText, getByText } = render(
<Questionnaire1 route={{ params: mockDietQuestionnaire }} />,
<UserContext.Provider value={userProviderValues}>
<Questionnaire1 route={{ params: mockDietQuestionnaire }} />
</UserContext.Provider>,
);
textFields.identity.forEach(({ name, placeholder }) => {
......
......@@ -3,6 +3,8 @@ import { render } from '@testing-library/react-native';
import Questionnaire2 from '.';
import { mockDietQuestionnaire } from 'mocks/dietQuestionnaire';
import { mockUserContext } from 'mocks/userContext';
import { UserContext } from 'provider';
const mockedNavigate = jest.fn();
......@@ -15,7 +17,15 @@ jest.mock('@react-navigation/native', () => {
});
describe('Questionnaire2', () => {
const userProviderValues = {
...mockUserContext,
};
it('renders correctly', () => {
render(<Questionnaire2 route={{ params: mockDietQuestionnaire }} />);
render(
<UserContext.Provider value={userProviderValues}>
<Questionnaire2 route={{ params: mockDietQuestionnaire }} />
</UserContext.Provider>,
);
});
});
......@@ -4,6 +4,8 @@ import axios from 'axios';
import Questionnaire3 from '.';
import { mockDietQuestionnaire } from 'mocks/dietQuestionnaire';
import { mockUserContext } from 'mocks/userContext';
import { UserContext } from 'provider';
const mockedNavigate = jest.fn();
......@@ -19,8 +21,16 @@ jest.mock('axios');
const mockAxios = axios as jest.Mocked<typeof axios>;
describe('Questionnaire3', () => {
const userProviderValues = {
...mockUserContext,
};
it('renders correctly', () => {
render(<Questionnaire3 route={{ params: mockDietQuestionnaire }} />);
render(
<UserContext.Provider value={userProviderValues}>
<Questionnaire3 route={{ params: mockDietQuestionnaire }} />
</UserContext.Provider>,
);
});
it('does not redirect to extended questionnaire if submit fails', async () => {
......@@ -34,7 +44,9 @@ describe('Questionnaire3', () => {
mockAxios.request.mockImplementationOnce(updateDietQuestionnaireApi);
const { getAllByPlaceholderText, getByText } = render(
<Questionnaire3 route={{ params: mockDietQuestionnaire }} />,
<UserContext.Provider value={userProviderValues}>
<Questionnaire3 route={{ params: mockDietQuestionnaire }} />
</UserContext.Provider>,
);
const formFields = getAllByPlaceholderText('Sesuai format jawaban');
......@@ -57,7 +69,9 @@ describe('Questionnaire3', () => {
mockAxios.request.mockImplementationOnce(updateDietQuestionnaireApi);
const { getAllByPlaceholderText, getByText } = render(
<Questionnaire3 route={{ params: mockDietQuestionnaire }} />,
<UserContext.Provider value={userProviderValues}>
<Questionnaire3 route={{ params: mockDietQuestionnaire }} />
</UserContext.Provider>,
);
const formFields = getAllByPlaceholderText('Sesuai format jawaban');
......
......@@ -4,6 +4,8 @@ import axios from 'axios';
import Questionnaire4 from '.';
import { mockDietQuestionnaire } from 'mocks/dietQuestionnaire';
import { mockUserContext } from 'mocks/userContext';
import { UserContext } from 'provider';
const mockedNavigate = jest.fn();
......@@ -19,8 +21,16 @@ jest.mock('axios');
const mockAxios = axios as jest.Mocked<typeof axios>;
describe('Questionnaire4', () => {
const userProviderValues = {
...mockUserContext,
};
it('renders correctly', () => {
render(<Questionnaire4 route={{ params: mockDietQuestionnaire }} />);
render(
<UserContext.Provider value={userProviderValues}>
<Questionnaire4 route={{ params: mockDietQuestionnaire }} />
</UserContext.Provider>,
);
});
it('redirects to extended questionnaire if all form values are valid and submit success', async () => {
......@@ -32,7 +42,9 @@ describe('Questionnaire4', () => {
mockAxios.request.mockImplementationOnce(updateDietQuestionnaireApi);
const { getByText } = render(
<Questionnaire4 route={{ params: mockDietQuestionnaire }} />,
<UserContext.Provider value={userProviderValues}>
<Questionnaire4 route={{ params: mockDietQuestionnaire }} />
</UserContext.Provider>,
);
const submitButton = getByText(/Simpan/i);
......
......@@ -3,6 +3,8 @@ import { render } from '@testing-library/react-native';
import Questionnaire5 from '.';
import { mockDietQuestionnaire } from 'mocks/dietQuestionnaire';
import { mockUserContext } from 'mocks/userContext';
import { UserContext } from 'provider';
const mockedNavigate = jest.fn();
......@@ -16,6 +18,14 @@ jest.mock('@react-navigation/native', () => {
describe('Questionnaire5', () => {
it('renders correctly', () => {
render(<Questionnaire5 route={{ params: mockDietQuestionnaire }} />);
const userProviderValues = {
...mockUserContext,
};
render(
<UserContext.Provider value={userProviderValues}>
<Questionnaire5 route={{ params: mockDietQuestionnaire }} />
</UserContext.Provider>,
);
});
});
import React, { FC } from 'react';
import React, { FC, useContext } from 'react';
import { ScrollView, StyleSheet } from 'react-native';
import { useNavigation } from '@react-navigation/native';
......@@ -10,6 +10,7 @@ import { layoutStyles } from 'styles';
import { generateValidationSchema } from 'utils/form';
import { Props } from './types';
import { updateDietQuestionnaireApi } from 'services/dietQuestionnaire';
import { UserContext } from 'provider';
const QuestionnaireWrapper: FC<Props> = ({
Questionnaire,
......@@ -20,6 +21,7 @@ const QuestionnaireWrapper: FC<Props> = ({
}) => {
const navigation = useNavigation();
const dietQuestionnaireResponse = route.params;
const { user, setUser } = useContext(UserContext);
const getInitialValues = () => {
let defaultValues: typeof initialValues = {};
......@@ -46,6 +48,12 @@ const QuestionnaireWrapper: FC<Props> = ({
payload,
);
if (response.success && response.data) {
if (response.data.finished_steps.length === 6) {
setUser({
...user,
is_finished_onboarding: true,
});
}
navigation.reset({
index: 0,
routes: [{ name: ROUTES.extendedQuestionnaire }],
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment