Fakultas Ilmu Komputer UI

Commit 61b6b753 authored by jahnsmichael's avatar jahnsmichael
Browse files

[RED] EditProfileForm scene

parent 7687186b
import 'react-native';
import React from 'react';
import axios from 'axios';
import { NavigationContainer } from '@react-navigation/native';
import { createStackNavigator } from '@react-navigation/stack';
import ReactTestRenderer, { act } from 'react-test-renderer';
import EditProfileForm from '.';
import { AppContext } from 'contexts';
import { useMainService } from 'services';
const Stack = createStackNavigator();
const testProps = {
services: {
main: useMainService('dummyToken'),
},
user: {
name: "test name",
username: "testusername",
email: "test@email.com",
phone_number: "0123456789",
district: "Tapos",
sub_district: "Sukatani",
},
setUser: (data: any) => {
testProps.user = data;
},
cache: {},
}
jest.mock('axios');
jest.useFakeTimers();
const mockedAxios = axios as jest.Mocked<typeof axios>;
it('renders correctly', () => {
const instance = ReactTestRenderer.create(
<NavigationContainer>
<Stack.Navigator
screenOptions={{
header: () => <></>,
}}>
<Stack.Screen name="edit-profile" component={EditProfileForm} />
</Stack.Navigator>
</NavigationContainer>,
);
expect(instance.toJSON()).toMatchSnapshot();
});
\ No newline at end of file
describe("EditProfileForm", () => {
it('renders correctly', () => {
const instance = ReactTestRenderer.create(
<AppContext.Provider value={testProps}>
<NavigationContainer>
<Stack.Navigator
screenOptions={{
header: () => <></>,
}}>
<Stack.Screen name="edit-profile" component={EditProfileForm} />
</Stack.Navigator>
</NavigationContainer>,
</AppContext.Provider>
);
expect(instance.toJSON()).toMatchSnapshot();
});
it('success with insert fields with valid values', async () => {
const formPage = ReactTestRenderer.create(
<AppContext.Provider value={testProps}>
<NavigationContainer>
<Stack.Navigator
screenOptions={{
// Empty header
header: () => <></>,
}}>
<Stack.Screen name="edit-profile" component={EditProfileForm} />
</Stack.Navigator>
</NavigationContainer>
</AppContext.Provider>
);
expect(formPage).toBeTruthy()
const nameField = formPage.root.find(elem => elem.props.id === "new_name")
expect(nameField).toBeTruthy()
const usernameField = formPage.root.find(elem => elem.props.id === "new_username")
expect(usernameField).toBeTruthy()
const passwordField = formPage.root.find(elem => elem.props.id === "new_password")
expect(passwordField).toBeTruthy()
const confirmPasswordField = formPage.root.find(elem => elem.props.id === "confirm_new_password")
expect(confirmPasswordField).toBeTruthy()
const emailField = formPage.root.find(elem => elem.props.id === "new_email")
expect(emailField).toBeTruthy()
const phoneField = formPage.root.find(elem => elem.props.id === "new_phone_number")
expect(phoneField).toBeTruthy()
const districtField = formPage.root.find(elem => elem.props.id === "new_district")
expect(districtField).toBeTruthy()
const sub_districtField = formPage.root.find(elem => elem.props.id === "new_sub_district")
expect(sub_districtField).toBeTruthy()
// Set value
act(() => {
if (nameField && usernameField && passwordField && confirmPasswordField && emailField && phoneField && districtField && sub_districtField) {
nameField.props.updateValue("your name")
usernameField.props.updateValue("username")
passwordField.props.updateValue("asdQWE123")
confirmPasswordField.props.updateValue("asdQWE123")
emailField.props.updateValue("email@gmail.com")
phoneField.props.updateValue("081802769312")
districtField.props.updateValue("districtku")
sub_districtField.props.updateValue("subdistrictku")
}
})
expect(formPage.toJSON()).toMatchSnapshot()
mockedAxios.request.mockImplementationOnce(
() => new Promise(resolve => {
resolve({
status: 200,
data: {
"message": "success edit profile",
},
});
})
);
const submit = formPage.root.find(elem => elem.props.id === "submit")
await act(async () => {
submit.props.onPress()
})
});
})
\ No newline at end of file
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