Fakultas Ilmu Komputer UI

Commit 96614cbd authored by Zafir Rasyidi Taufik's avatar Zafir Rasyidi Taufik
Browse files

[RED] Add test for checking implementation

parent 9b685c5f
......@@ -29,12 +29,14 @@ const testProps = {
cache: {},
}
const mockNavigation = jest.fn()
const mockNavigation = jest.fn();
const mockReset = jest.fn();
jest.mock('@react-navigation/native', () => {
return {
...jest.requireActual('@react-navigation/native'),
useNavigation: () => ({
navigate: mockNavigation,
reset: mockReset
}),
};
});
......@@ -42,7 +44,7 @@ jest.doMock('@react-native-google-signin/google-signin')
jest.mock('axios');
const mockedAxios = axios as jest.Mocked<typeof axios>;
afterEach(() => {
beforeEach(() => {
jest.clearAllMocks();
});
......@@ -231,7 +233,7 @@ 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 () => {
it('goes to home if google sign in token already has corresponding user', async () => {
LocalStorage.setSecretKey("sssttt");
const instance = ReactTestRenderer.create(
......@@ -271,6 +273,17 @@ it('receives google token when signin is successful and succesfully navigates to
GoogleSignin.hasPlayServices = mockHasPlayServices
GoogleSignin.signIn = mockSignin
mockedAxios.request.mockImplementationOnce(
() => new Promise(resolve => {
resolve({
status: 200,
data: {
token: "mock token"
}
});
})
);
const googleSignin = instance.root.findByType(GoogleSigninButton);
act(() => {
......@@ -279,7 +292,6 @@ it('receives google token when signin is successful and succesfully navigates to
await expect(mockHasPlayServices).toBeCalledTimes(1);
await expect(mockSignin).toBeCalledTimes(1);
await expect(mockNavigation).toBeCalledTimes(1);
});
......@@ -373,4 +385,63 @@ it('receives nothing when has no play service failed and fails to navigate to ne
await expect(mockSignin).toBeCalledTimes(0);
await expect(mockNavigation).toBeCalledTimes(0);
});
it('receives google token when signin is successful and succesfully navigates to form', 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
mockedAxios.request.mockImplementationOnce(
() => new Promise(resolve => {
resolve({
status: 203
});
})
);
const googleSignin = instance.root.findByType(GoogleSigninButton);
act(() => {
googleSignin.props.onPress();
})
await expect(mockHasPlayServices).toBeCalledTimes(1);
await expect(mockSignin).toBeCalledTimes(1);
});
\ 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