Fakultas Ilmu Komputer UI

Commit ff372eec authored by Zafir Rasyidi Taufik's avatar Zafir Rasyidi Taufik
Browse files

[RED] Add tests for login with google

parent addc93cb
......@@ -163,7 +163,7 @@ exports[`DatePicker tests renders correctly 1`] = `
]
}
>
27/5/2021
28/5/2021
</Text>
</View>
<View
......
......@@ -8,6 +8,7 @@ import LoginPage from '.';
import ReactTestRenderer, { act } from 'react-test-renderer';
import { AppContext } from 'contexts';
import { GoogleSignin, GoogleSigninButton } from '@react-native-google-signin/google-signin';
const Stack = createStackNavigator();
const testProps = {
......@@ -28,9 +29,23 @@ const testProps = {
cache: {},
}
const mockNavigation = jest.fn()
jest.mock('@react-navigation/native', () => {
return {
...jest.requireActual('@react-navigation/native'),
useNavigation: () => ({
navigate: mockNavigation,
}),
};
});
jest.doMock('@react-native-google-signin/google-signin')
jest.mock('axios');
const mockedAxios = axios as jest.Mocked<typeof axios>;
afterEach(() => {
jest.clearAllMocks();
});
it('renders correctly', () => {
const instance = ReactTestRenderer.create(
<NavigationContainer>
......@@ -214,4 +229,149 @@ it('inserts invalid (length<8) password', async () => {
});
expect(testProps.alert.message).toBe("Unknown Server Error");
});
it('receives google token when signin is successful and succesfully navigates to next scene', async () => {
LocalStorage.setSecretKey("sssttt");
const instance = ReactTestRenderer.create(
<AppContext.Provider value={testProps}>
<NavigationContainer>
<Stack.Navigator
screenOptions={{
header: () => <></>,
}}>
<Stack.Screen name="login" component={LoginPage} />
</Stack.Navigator>
</NavigationContainer>
</AppContext.Provider>,
);
expect(instance.toJSON()).toMatchSnapshot();
const mockHasPlayServices = jest.fn().mockImplementationOnce(() => new Promise(resolve => {
resolve(true);
}));
const mockSignin = jest.fn().mockImplementationOnce(() => new Promise(resolve => {
const data = {
idToken: "1234",
serverAuthCode: "4321",
user: {
email: "test@gmail.com",
id: "1234",
givenName: "test",
familyName: "test",
photo: "test.com", // url
name: "test" // full name
}
}
resolve(data);
}));
GoogleSignin.hasPlayServices = mockHasPlayServices
GoogleSignin.signIn = mockSignin
const googleSignin = instance.root.findByType(GoogleSigninButton);
act(() => {
googleSignin.props.onPress();
})
await expect(mockHasPlayServices).toBeCalledTimes(1);
await expect(mockSignin).toBeCalledTimes(1);
await expect(mockNavigation).toBeCalledTimes(1);
await expect(mockNavigation).toBeCalledWith('officer-signup-form-google-signin');
});
it('receives nothing when signin failed and fails to navigate to next scene', async () => {
LocalStorage.setSecretKey("sssttt");
const instance = ReactTestRenderer.create(
<AppContext.Provider value={testProps}>
<NavigationContainer>
<Stack.Navigator
screenOptions={{
header: () => <></>,
}}>
<Stack.Screen name="login" component={LoginPage} />
</Stack.Navigator>
</NavigationContainer>
</AppContext.Provider>,
);
expect(instance.toJSON()).toMatchSnapshot();
const mockHasPlayServices = jest.fn().mockImplementationOnce(() => new Promise(resolve => {
resolve(true);
}));
const mockSignin = jest.fn().mockImplementationOnce(() => {
throw new Error("signin error");
});
GoogleSignin.hasPlayServices = mockHasPlayServices
GoogleSignin.signIn = mockSignin
const googleSignin = instance.root.findByType(GoogleSigninButton);
act(() => {
googleSignin.props.onPress();
})
await expect(mockHasPlayServices).toBeCalledTimes(1);
await expect(mockSignin).toBeCalledTimes(1);
await expect(mockNavigation).toBeCalledTimes(0);
});
it('receives nothing when has no play service failed and fails to navigate to next scene', async () => {
LocalStorage.setSecretKey("sssttt");
const instance = ReactTestRenderer.create(
<AppContext.Provider value={testProps}>
<NavigationContainer>
<Stack.Navigator
screenOptions={{
header: () => <></>,
}}>
<Stack.Screen name="login" component={LoginPage} />
</Stack.Navigator>
</NavigationContainer>
</AppContext.Provider>,
);
expect(instance.toJSON()).toMatchSnapshot();
const mockHasPlayServices = jest.fn().mockImplementationOnce(() => {
throw new Error("has no play service error");
});
const mockSignin = jest.fn().mockImplementationOnce(() => new Promise(resolve => {
const data = {
idToken: "1234",
serverAuthCode: "4321",
user: {
email: "test@gmail.com",
id: "1234",
givenName: "test",
familyName: "test",
photo: "test.com", // url
name: "test" // full name
}
}
resolve(data);
}));
GoogleSignin.hasPlayServices = mockHasPlayServices
GoogleSignin.signIn = mockSignin
const googleSignin = instance.root.findByType(GoogleSigninButton);
act(() => {
googleSignin.props.onPress();
})
await expect(mockHasPlayServices).toBeCalledTimes(1);
await expect(mockSignin).toBeCalledTimes(0);
await expect(mockNavigation).toBeCalledTimes(0);
});
\ No newline at end of file
......@@ -909,7 +909,7 @@ exports[`can press previous button 1`] = `
]
}
>
27/5/2021
28/5/2021
</Text>
</View>
<View
......@@ -2365,7 +2365,7 @@ exports[`renders correctly 1`] = `
]
}
>
27/5/2021
28/5/2021
</Text>
</View>
<View
......@@ -3821,7 +3821,7 @@ exports[`renders form with correct fields 1`] = `
]
}
>
27/5/2021
28/5/2021
</Text>
</View>
<View
......@@ -5277,7 +5277,7 @@ exports[`submit data succesfully 1`] = `
]
}
>
27/5/2021
28/5/2021
</Text>
</View>
<View
......
......@@ -663,7 +663,7 @@ exports[`can press previous button 1`] = `
]
}
>
27/5/2021
28/5/2021
</Text>
</View>
<View
......@@ -881,7 +881,7 @@ exports[`can press previous button 1`] = `
]
}
>
27/5/2021
28/5/2021
</Text>
</View>
<View
......@@ -2070,7 +2070,7 @@ exports[`renders correctly 1`] = `
]
}
>
27/5/2021
28/5/2021
</Text>
</View>
<View
......@@ -2288,7 +2288,7 @@ exports[`renders correctly 1`] = `
]
}
>
27/5/2021
28/5/2021
</Text>
</View>
<View
......@@ -3477,7 +3477,7 @@ exports[`renders form with correct fields 1`] = `
]
}
>
27/5/2021
28/5/2021
</Text>
</View>
<View
......@@ -3695,7 +3695,7 @@ exports[`renders form with correct fields 1`] = `
]
}
>
27/5/2021
28/5/2021
</Text>
</View>
<View
......@@ -4884,7 +4884,7 @@ exports[`submit data succesfully 1`] = `
]
}
>
27/5/2021
28/5/2021
</Text>
</View>
<View
......@@ -5102,7 +5102,7 @@ exports[`submit data succesfully 1`] = `
]
}
>
27/5/2021
28/5/2021
</Text>
</View>
<View
......
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