-
Notifications
You must be signed in to change notification settings - Fork 2
/
LoginPresenterTest.java
90 lines (78 loc) · 3.41 KB
/
LoginPresenterTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package com.infinitisuite.surveymobile.presenters;
import com.infinitisuite.surveymobile.services.IUserService;
import com.infinitisuite.surveymobile.services.UserService;
import com.infinitisuite.surveymobile.util.SurveyWebHttpClient;
import com.infinitisuite.surveymobile.views.ILoginView;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
import static org.mockito.Mockito.*;
@RunWith(RobolectricTestRunner.class)
public class LoginPresenterTest {
private LoginPresenter presenter;
private ILoginView loginViewMock;
private IUserService userService;
@Before
public void setUp() throws Exception {
loginViewMock = mock(ILoginView.class);
SurveyWebHttpClient client = new SurveyWebHttpClient();
client.makeAllOperationsSynchronous();
userService = new UserService(client);
presenter = new LoginPresenter(userService, loginViewMock);
}
@Test
public void shows_a_validation_error_if_password_is_empty() throws Exception {
doReturn("foo@bar.com").when(loginViewMock).getEmail();
doReturn(null).when(loginViewMock).getPassword();
presenter.attemptLogin();
verify(loginViewMock, times(1)).setPasswordRequiredError();
}
@Test
public void shows_a_validation_error_if_email_is_empty() throws Exception {
doReturn(null).when(loginViewMock).getEmail();
doReturn("foo").when(loginViewMock).getPassword();
presenter.attemptLogin();
verify(loginViewMock, times(1)).setEmailRequiredError();
}
@Test
public void shows_a_validation_error_if_email_doesnt_have_an_at_sign() throws Exception {
doReturn("abc.com").when(loginViewMock).getEmail();
doReturn("foo").when(loginViewMock).getPassword();
presenter.attemptLogin();
verify(loginViewMock, times(1)).setEmailInvalidError();
}
@Test
public void shows_error_message_if_username_and_password_are_wrong() throws Exception {
Robolectric.addPendingHttpResponse(401, "Unauthorized");
doReturn("foo@bar.com").when(loginViewMock).getEmail();
doReturn("bar").when(loginViewMock).getPassword();
presenter.attemptLogin();
verify(loginViewMock, times(1)).showLoginError();
}
@Test
public void shows_spinner_while_logging_in_if_username_and_password_are_valid() throws Exception {
Robolectric.addPendingHttpResponse(200, "OK");
doReturn("foo@bar.com").when(loginViewMock).getEmail();
doReturn("bar").when(loginViewMock).getPassword();
presenter.attemptLogin();
verify(loginViewMock, times(1)).showSigningIn();
}
@Test
public void hides_spinner_if_login_is_successful() throws Exception {
Robolectric.addPendingHttpResponse(200, "OK");
doReturn("foo@bar.com").when(loginViewMock).getEmail();
doReturn("bar").when(loginViewMock).getPassword();
presenter.attemptLogin();
verify(loginViewMock, times(1)).hideSigningIn();
}
@Test
public void hides_spinner_if_login_is_unsuccessful() throws Exception {
Robolectric.addPendingHttpResponse(401, "Unauthorized");
doReturn("foo@bar.com").when(loginViewMock).getEmail();
doReturn("bar").when(loginViewMock).getPassword();
presenter.attemptLogin();
verify(loginViewMock, times(1)).hideSigningIn();
}
}