Fakultas Ilmu Komputer UI

Commit 6e1bd4b7 authored by jahnsmichael's avatar jahnsmichael
Browse files

[RED] integrate fetching with markup

parent 9bb70b1a
import 'react-native';
import axios from 'axios';
import React from 'react';
import { useMainService } from 'services';
import { useMainService, LocalStorage } from 'services';
import { NavigationContainer } from '@react-navigation/native';
import { createStackNavigator } from '@react-navigation/stack';
import ReactTestRenderer from 'react-test-renderer';
import ReactTestRenderer, { act } from 'react-test-renderer';
import ForgetPasswordPage from '.';
import { AppContext } from 'contexts';
const Stack = createStackNavigator();
const testProps = {
services: {
main: useMainService('dummyToken'),
},
alert: {
illustration: "",
message: "",
},
setAlert: (alert: string) => testProps.alert = alert,
shouldLoading: false,
setShouldLoading: (shouldLoading: boolean) => testProps.shouldLoading = shouldLoading,
cache: {},
}
jest.mock('axios');
const mockedAxios = axios as jest.Mocked<typeof axios>;
it('renders correctly', () => {
const instance = ReactTestRenderer.create(
......@@ -23,4 +40,134 @@ it('renders correctly', () => {
);
expect(instance.toJSON()).toMatchSnapshot();
});
it('insert email correctly', async () => {
LocalStorage.setSecretKey("sssttt");
const instance = ReactTestRenderer.create(
<AppContext.Provider value={testProps}>
<NavigationContainer>
<Stack.Navigator
screenOptions={{
header: () => <></>,
}}>
<Stack.Screen name="forget-password" component={ForgetPasswordPage} />
</Stack.Navigator>
</NavigationContainer>
</AppContext.Provider>,
);
expect(instance.toJSON()).toMatchSnapshot();
const emailField = instance.root.find(elem => elem.props.id === "email");
act(() => {
if (emailField) {
emailField.props.updateValue("email@email.com");
}
})
expect(instance).toBeTruthy();
mockedAxios.request.mockImplementationOnce(
() => new Promise(resolve => {
resolve({
status: 200,
data: [
"Password successfully changed"
],
});
})
);
const submit = instance.root.find(elem => elem.props.id === "submit");
await act(async () => {
submit.props.onPress();
})
expect(testProps.alert.message).toBe("Password berhasil diubah, silakan cek email anda.");
});
it('insert invalid email', async () => {
LocalStorage.setSecretKey("sssttt");
const instance = ReactTestRenderer.create(
<AppContext.Provider value={testProps}>
<NavigationContainer>
<Stack.Navigator
screenOptions={{
header: () => <></>,
}}>
<Stack.Screen name="forget-password" component={ForgetPasswordPage} />
</Stack.Navigator>
</NavigationContainer>
</AppContext.Provider>,
);
expect(instance.toJSON()).toMatchSnapshot();
const emailField = instance.root.find(elem => elem.props.id === "email");
act(() => {
if (emailField) {
emailField.props.updateValue("beneranemail.com");
}
})
expect(instance).toBeTruthy();
const submit = instance.root.find(elem => elem.props.id === "submit");
await act(async () => {
submit.props.onPress();
})
expect(emailField.props.information).toBe("Masukkan email yang valid.");
});
it('insert email that does\'t exists in database', async () => {
LocalStorage.setSecretKey("sssttt");
const instance = ReactTestRenderer.create(
<AppContext.Provider value={testProps}>
<NavigationContainer>
<Stack.Navigator
screenOptions={{
header: () => <></>,
}}>
<Stack.Screen name="forget-password" component={ForgetPasswordPage} />
</Stack.Navigator>
</NavigationContainer>
</AppContext.Provider>,
);
expect(instance.toJSON()).toMatchSnapshot();
const emailField = instance.root.find(elem => elem.props.id === "email");
act(() => {
if (emailField) {
emailField.props.updateValue("email@email.com");
}
})
expect(instance).toBeTruthy();
mockedAxios.request.mockImplementationOnce(
() => new Promise(resolve => {
resolve({
status: 400,
data: {
"error": "Account matching query does not exist."
},
});
})
);
const submit = instance.root.find(elem => elem.props.id === "submit");
await act(async () => {
submit.props.onPress();
})
expect(testProps.alert.message).toBe("Terjadi kesalahan, periksa kembali email anda.");
});
\ 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