SchwartzLizer / NimbleSurvey

MIT License
0 stars 0 forks source link

Refactor all unit tests #14

Closed SchwartzLizer closed 11 months ago

SchwartzLizer commented 11 months ago

Refactor Unittest

ForgotPassword

testForgotPassword_Success(): This function is a unit test for the successful scenario of resetting a password. It sets up a mock session with JSON data simulating a successful response from a server. An XCTest expectation is created to wait for the resetSuccess callback to be called. The requestForgotPassword method of the viewModel is called with a valid email address. When the resetSuccess callback is invoked, it fulfills the expectation.

testForgotPassword_Failure(): This function is a unit test for the failure scenario of resetting a password. It sets up a mock session with JSON data simulating a failed response from a server. An XCTest expectation is created to wait for the resetFailed callback to be called. The requestForgotPassword method of the viewModel is called with an invalid email address. When the resetFailed callback is invoked, it sets a boolean flag resetFailedCalled to true and fulfills the expectation. Finally, it asserts that resetFailedCalled is true, indicating that the resetFailed callback was called as expected.

Home

testHome_RequestSurveyList_Success(): This test ensures that the HomeViewModel can successfully request a list of surveys from a remote server. It sets up a mock session with JSON data simulating a successful response from the server. An expectation is created to verify that the completion closure is called. The test waits for the expectation to be fulfilled and then checks if the viewModel's datas array contains the expected number of items (in this case, 1).

testHome_RequestUserProfile_Success(): This test verifies the HomeViewModel's ability to request a user profile from a remote server. It sets up a mock session with JSON data simulating a successful user profile response. An expectation is created to ensure that the completion closure is called. After the completion, it checks if the viewModel's profile data matches the expected values.

testHome_HandleFailureErrorWithLocalData(): This test checks the HomeViewModel's handling of a failure error scenario while also having local data available. It first saves mock survey list data to user defaults. Then, it simulates a network error and calls handleFailureError with the error. The test verifies that the viewModel's datas array is updated with the locally stored data.

testHome_HandleFailureError_ServerError(): This test checks the HomeViewModel's handling of a server error scenario. It sets an expectation to ensure that the onFailed closure is called with the expected error message. It simulates a server error and calls handleFailureError with the error.

testHome_CheckLocalDeviceData_SurveyListData(): This test verifies the HomeViewModel's ability to check if there is locally saved survey list data. It saves mock survey list data to user defaults and checks if checkLocalDeviceData correctly detects the presence of data.

testHome_CheckLocalDeviceDataWithoutSurveyListData(): This test checks if checkLocalDeviceData correctly detects the absence of locally saved survey list data.

testHome_ProcessData(): This test checks the HomeViewModel's ability to process raw survey list data into a different format. It creates mock survey list data, processes it using processData, and then checks if the processed data matches the expected format.

testHome_ScrollViewUpdate(): This test checks the HomeViewModel's ability to update the view model's state based on scroll events. It sets up mock data, triggers a scroll event, and checks if the onScrollUpdated closure is called with the expected data.

testHome_ProcessDate(): This test checks if the HomeViewModel can correctly process the current date into a specific format.

testHome_SaveSurveyData(): This test checks if the HomeViewModel can successfully save survey list data to user defaults. It creates mock survey list data, saves it using save, and then attempts to retrieve it from user defaults to verify successful saving.

Login

testLogin_Success(): This test verifies the successful login scenario. It sets up a mock session with JSON data simulating a successful login response from the server. An expectation is created to ensure that the loginSuccess closure is called. The test calls requestLogin with valid email and password, waits for the expectation to be fulfilled, and checks for the expected access token.

testLogin_Failure(): This test checks the login failure scenario when invalid credentials are provided. It sets up a mock session with JSON data simulating a failed login response from the server. An expectation is created to ensure that the loginFailure closure is called with an error message. The test calls requestLogin with invalid email and password, waits for the expectation to be fulfilled, and verifies the error message.

testLogin_RefreshTokenNotFound(): This test checks the scenario where a refresh token is not found in the server response. It sets up a mock session with JSON data where the refresh token is empty. An expectation is created to ensure that the noRefreshTokenFound closure is called. The test calls requestLogin and waits for the expectation to be fulfilled.

testLogin_RefreshTokenIsEmpty(): This test checks the scenario where the refresh token is empty in the server response. Similar to the previous test, it sets up a mock session with JSON data where the refresh token is missing. An expectation is created to ensure that the noRefreshTokenFound closure is called. The test calls requestLogin and waits for the expectation to be fulfilled.

testLogin_AccessTokenNotFound(): This test checks the scenario where the access token is not found in the server response. It sets up a mock session with JSON data where the access token is missing. An expectation is created to ensure that the noAccessTokenFound closure is called. The test calls requestLogin and waits for the expectation to be fulfilled.

testLogin_AccessTokenIsEmpty(): This test checks the scenario where the access token is empty in the server response. Similar to the previous test, it sets up a mock session with JSON data where the access token is empty. An expectation is created to ensure that the noAccessTokenFound closure is called. The test calls requestLogin and waits for the expectation to be fulfilled.

Notification

testNotificationHandler_HandleRefreshTokenFailure(): This test verifies the behavior of the handleRefreshTokenFailure method of the notificationHandler. It calls the handleRefreshTokenFailure method. Then, it asserts that a method called showAlert in a MockAlertUtility is called with specific arguments (title and message). It also checks if a completion block passed to the showAlert method is triggered and a method called loginScene is called on a mocked appUtility.

testNotificationHandler_ObserverSetup(): This test checks the setup of a notification observer. It posts a notification with the name .refresherTokenOnFailure. It creates an expectation and fulfills it immediately. The purpose of this test seems to be to verify that the observer setup does not result in any errors or crashes.

RightMenu

testRightMenuLogout_Success(): This test verifies the successful logout scenario. It sets up a mock session with empty JSON data to simulate a successful logout response from the server. An expectation is created to ensure that the onLogoutSuccess closure is called. The test calls requestLogout on the viewModel, waits for the expectation to be fulfilled, and confirms that the logout was successful.

testRightMenuLogout_Failure(): This test checks the failure scenario when the logout request fails due to unauthorized access. It sets up a mock session with JSON data simulating a failed logout response from the server with a 403 status code. An expectation is created to ensure that the onLogoutFailed closure is called with an error message. The test calls requestLogout on the viewModel, waits for the expectation to be fulfilled, and verifies the error message.

TokenRefresher

testRefreshToken_NoData(): This test checks the scenario where no data is received in the response. It sets up a mock session with empty JSON data to simulate a response with no refresh token. The refreshToken function is called on a MockTokenRefresher. It expects the onError function to be called, indicating that no refresh token was found.

testRefreshToken_EmptyRefreshToken(): This test checks the scenario where the response contains an empty refresh token. It sets up a mock session with JSON data simulating a response with an empty refresh token. The refreshToken function is called on a MockTokenRefresher. It expects the onError function to be called, indicating that the refresh token is empty.

testRefreshToken_OnSuccess(): This test verifies the successful refresh token scenario. It sets up a mock session with JSON data containing a valid refresh token. The refreshToken function is called on a MockTokenRefresher. It expects the onSuccess function to be called, indicating that the token refresh was successful.

testRefreshToken_OnFailure(): This test checks the scenario where the token refresh request fails with an error response. It sets up a mock session with JSON data simulating a failed refresh token response. The refreshToken function is called on a MockTokenRefresher. It expects the onFailure function to be called, indicating a failure during the token refresh.

testRefreshToken_StartTimer(): This test checks if the timer used for token refresh is started successfully.

testRefreshToken_StopTimer(): This test checks if the timer used for token refresh is stopped successfully.

testRefreshToken_SaveRefreshToken(): This test verifies that a refresh token can be saved securely using Keychain.

testRefreshToken_NotificationPosted(): This test checks if a specific notification is posted correctly when triggered.

testRefreshToken_RemoveRefreshToken(): This test checks if a saved refresh token can be removed from Keychain.

testRefreshToken_FailureNotificationPosted(): This test checks if specific failure notifications are posted correctly when triggered.

testRefreshToken_DidEnterBackground(): This test checks if the appDidEnterBackground notification is correctly handled.

testRefreshToken_WillEnterForeground(): This test checks if the appWillEnterForeground notification is correctly handled.