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