Fakultas Ilmu Komputer UI

Commit 126cda42 authored by Josh Sudung's avatar Josh Sudung
Browse files

[RED] Added monitoring selection tests

parent d2215d0f
......@@ -163,7 +163,7 @@ exports[`DatePicker tests renders correctly 1`] = `
]
}
>
4/21/2020
4/30/2020
</Text>
</View>
<View
......
......@@ -880,7 +880,7 @@ exports[`can press previous button 1`] = `
]
}
>
4/21/2020
4/30/2020
</Text>
</View>
<View
......@@ -2307,7 +2307,7 @@ exports[`renders correctly 1`] = `
]
}
>
4/21/2020
4/30/2020
</Text>
</View>
<View
......@@ -3734,7 +3734,7 @@ exports[`renders form with correct fields 1`] = `
]
}
>
4/21/2020
4/30/2020
</Text>
</View>
<View
......@@ -5161,7 +5161,7 @@ exports[`submit data succesfully 1`] = `
]
}
>
4/21/2020
4/30/2020
</Text>
</View>
<View
......
......@@ -634,7 +634,7 @@ exports[`can press previous button 1`] = `
]
}
>
4/21/2020
4/30/2020
</Text>
</View>
<View
......@@ -852,7 +852,7 @@ exports[`can press previous button 1`] = `
]
}
>
4/21/2020
4/30/2020
</Text>
</View>
<View
......@@ -2012,7 +2012,7 @@ exports[`renders correctly 1`] = `
]
}
>
4/21/2020
4/30/2020
</Text>
</View>
<View
......@@ -2230,7 +2230,7 @@ exports[`renders correctly 1`] = `
]
}
>
4/21/2020
4/30/2020
</Text>
</View>
<View
......@@ -3390,7 +3390,7 @@ exports[`renders form with correct fields 1`] = `
]
}
>
4/21/2020
4/30/2020
</Text>
</View>
<View
......@@ -3608,7 +3608,7 @@ exports[`renders form with correct fields 1`] = `
]
}
>
4/21/2020
4/30/2020
</Text>
</View>
<View
......@@ -4768,7 +4768,7 @@ exports[`submit data succesfully 1`] = `
]
}
>
4/21/2020
4/30/2020
</Text>
</View>
<View
......@@ -4986,7 +4986,7 @@ exports[`submit data succesfully 1`] = `
]
}
>
4/21/2020
4/30/2020
</Text>
</View>
<View
......
......@@ -5,9 +5,10 @@ import {createStackNavigator} from '@react-navigation/stack';
import MonitoringSelection from '.';
// Note: test renderer must be required after react-native.
import ReactTestRenderer from 'react-test-renderer';
import ReactTestRenderer, {act} from 'react-test-renderer';
import {useMainService} from 'services';
import {AppContext} from 'contexts';
import axios from 'axios';
const Stack = createStackNavigator();
const testProps = {
......@@ -15,6 +16,40 @@ const testProps = {
main: useMainService('dummyToken'),
},
cache: {},
setCache: jest.fn(),
};
jest.mock('axios');
const mock = axios as jest.Mocked<typeof axios>;
const monitoringFetchData = {
count: 1,
next: true,
previous: true,
results: [
{
id: 'id',
investigation_case: {
id: 'bb6f0e2c-59a4-40d3-942f-db884c64728b',
case_subject: {
id: '198idd8b4b-01dd-448b-8e3d-a52162d657cb',
name: 'case-name',
age: 75,
is_male: true,
address: 'Jl. Adi',
district: 'Beji',
sub_district: 'Beji',
created_at: '2020-04-23T13:13:31.942672+07:00',
},
},
is_referred: true,
is_checked: true,
is_medicated: false,
regular_medicine_intake: [],
treatment_start_date: null,
treatment_end_date: null,
},
],
};
it('renders correctly', () => {
......@@ -37,3 +72,355 @@ it('renders correctly', () => {
expect(instance.toJSON()).toMatchSnapshot();
});
it('successfully fetches monitoring cases', async () => {
mock.request.mockImplementationOnce(
() =>
new Promise(resolve => {
resolve({
status: 200,
data: monitoringFetchData,
});
}),
);
const instance = ReactTestRenderer.create(
<AppContext.Provider value={testProps}>
<NavigationContainer>
<Stack.Navigator
screenOptions={{
// Empty header
header: () => <></>,
}}>
<Stack.Screen
name="monitoring-select"
component={MonitoringSelection}
/>
</Stack.Navigator>
</NavigationContainer>
</AppContext.Provider>,
);
expect(instance.toJSON()).toMatchSnapshot();
expect(mock.request).toBeCalled();
});
it('succesfully searches monitoring case', async () => {
mock.request.mockResolvedValue({
status: 200,
data: monitoringFetchData,
});
const instance = ReactTestRenderer.create(
<AppContext.Provider value={testProps}>
<NavigationContainer>
<Stack.Navigator
screenOptions={{
header: () => <></>,
}}>
<Stack.Screen
name="monitoring-select"
component={MonitoringSelection}
/>
</Stack.Navigator>
</NavigationContainer>
</AppContext.Provider>,
);
expect(instance.toJSON()).toMatchSnapshot();
expect(mock.request).toBeCalled();
const searchField = instance.root.find(
elem => elem.props.name === 'Cari Nama',
);
await act(() => {
searchField.props.updateValue('case-name');
setTimeout(() => {}, 500);
});
expect(searchField.props.value).toBe('case-name');
expect(mock.request).toBeCalled();
});
it('determines state_1 correctly', async () => {
mock.request.mockResolvedValue({
status: 200,
data: monitoringFetchData,
});
const instance = ReactTestRenderer.create(
<AppContext.Provider value={testProps}>
<NavigationContainer>
<Stack.Navigator
screenOptions={{
header: () => <></>,
}}>
<Stack.Screen
name="monitoring-select"
component={MonitoringSelection}
/>
</Stack.Navigator>
</NavigationContainer>
</AppContext.Provider>,
);
expect(instance.toJSON()).toMatchSnapshot();
await act(async () => {
expect(mock.request).toBeCalled();
});
const selectedCase = instance.root.find(elem => elem.props.id === 'case-0');
const nextButton = instance.root.find(
elem => elem.props.id === 'navigate-button',
);
await act(() => {
selectedCase.props.onPress();
});
act(() => {
nextButton.props.onPress();
});
});
it('determines state_2 correctly', async () => {
mock.request.mockResolvedValue({
status: 200,
data: {
count: 1,
next: true,
previous: true,
results: [
{
id: 'id',
investigation_case: {
id: 'bb6f0e2c-59a4-40d3-942f-db884c64728b',
case_subject: {
id: '198idd8b4b-01dd-448b-8e3d-a52162d657cb',
name: 'case-name',
age: 75,
is_male: true,
address: 'Jl. Adi',
district: 'Beji',
sub_district: 'Beji',
created_at: '2020-04-23T13:13:31.942672+07:00',
},
},
is_referred: true,
is_checked: false,
is_medicated: false,
regular_medicine_intake: [],
treatment_start_date: null,
treatment_end_date: null,
},
],
},
});
const instance = ReactTestRenderer.create(
<AppContext.Provider value={testProps}>
<NavigationContainer>
<Stack.Navigator
screenOptions={{
header: () => <></>,
}}>
<Stack.Screen
name="monitoring-select"
component={MonitoringSelection}
/>
</Stack.Navigator>
</NavigationContainer>
</AppContext.Provider>,
);
expect(instance.toJSON()).toMatchSnapshot();
await act(async () => {
expect(mock.request).toBeCalled();
});
const selectedCase = instance.root.find(elem => elem.props.id === 'case-0');
const nextButton = instance.root.find(
elem => elem.props.id === 'navigate-button',
);
await act(() => {
selectedCase.props.onPress();
});
act(() => {
nextButton.props.onPress();
});
});
it('determines state_3 correctly', async () => {
mock.request.mockResolvedValue({
status: 200,
data: {
count: 1,
next: true,
previous: true,
results: [
{
id: 'id',
investigation_case: {
id: 'bb6f0e2c-59a4-40d3-942f-db884c64728b',
case_subject: {
id: '198idd8b4b-01dd-448b-8e3d-a52162d657cb',
name: 'case-name',
age: 75,
is_male: true,
address: 'Jl. Adi',
district: 'Beji',
sub_district: 'Beji',
created_at: '2020-04-23T13:13:31.942672+07:00',
},
},
is_referred: false,
is_checked: false,
is_medicated: false,
regular_medicine_intake: [],
treatment_start_date: null,
treatment_end_date: null,
},
],
},
});
const instance = ReactTestRenderer.create(
<AppContext.Provider value={testProps}>
<NavigationContainer>
<Stack.Navigator
screenOptions={{
header: () => <></>,
}}>
<Stack.Screen
name="monitoring-select"
component={MonitoringSelection}
/>
</Stack.Navigator>
</NavigationContainer>
</AppContext.Provider>,
);
expect(instance.toJSON()).toMatchSnapshot();
await act(async () => {
expect(mock.request).toBeCalled();
});
const selectedCase = instance.root.find(elem => elem.props.id === 'radio-0');
const nextButton = instance.root.find(
elem => elem.props.id === 'navigate-button',
);
await act(() => {
selectedCase.props.onPress();
});
act(() => {
nextButton.props.onPress();
});
});
it('determines state_4 correctly', async () => {
mock.request.mockResolvedValue({
status: 200,
data: {
count: 1,
next: true,
previous: true,
results: [
{
id: 'id',
investigation_case: {
id: 'bb6f0e2c-59a4-40d3-942f-db884c64728b',
case_subject: {
id: '198idd8b4b-01dd-448b-8e3d-a52162d657cb',
name: 'case-name',
age: 75,
is_male: true,
address: 'Jl. Adi',
district: 'Beji',
sub_district: 'Beji',
created_at: '2020-04-23T13:13:31.942672+07:00',
},
},
is_referred: true,
is_checked: true,
is_medicated: true,
regular_medicine_intake: [],
treatment_start_date: null,
treatment_end_date: null,
},
],
},
});
const instance = ReactTestRenderer.create(
<AppContext.Provider value={testProps}>
<NavigationContainer>
<Stack.Navigator
screenOptions={{
header: () => <></>,
}}>
<Stack.Screen
name="monitoring-select"
component={MonitoringSelection}
/>
</Stack.Navigator>
</NavigationContainer>
</AppContext.Provider>,
);
expect(instance.toJSON()).toMatchSnapshot();
await act(async () => {
expect(mock.request).toBeCalled();
});
const selectedCase = instance.root.find(elem => elem.props.id === 'case-0');
const nextButton = instance.root.find(
elem => elem.props.id === 'navigate-button',
);
await act(() => {
selectedCase.props.onPress();
});
act(() => {
nextButton.props.onPress();
});
});
it('changes pages correctly', async () => {
mock.request.mockResolvedValue({
status: 200,
data: monitoringFetchData,
});
const instance = ReactTestRenderer.create(
<AppContext.Provider value={testProps}>
<NavigationContainer>
<Stack.Navigator
screenOptions={{
header: () => <></>,
}}>
<Stack.Screen
name="monitoring-select"
component={MonitoringSelection}
/>
</Stack.Navigator>
</NavigationContainer>
</AppContext.Provider>,
);
expect(instance.toJSON()).toMatchSnapshot();
await act(async () => {
expect(mock.request).toBeCalled();
});
const previous = instance.root.find(elem => elem.props.id === 'prev');
const next = instance.root.find(elem => elem.props.id === 'next');
act(() => {
previous.props.onPress();
next.props.onPress();
});
});
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