Refactor student selection screen (#2087)

This commit is contained in:
Mikołaj Pich 2023-01-01 20:26:32 +01:00 committed by GitHub
parent 83974b6550
commit 897eac050a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
29 changed files with 1052 additions and 349 deletions

View file

@ -1,9 +1,9 @@
package io.github.wulkanowy.ui.modules.login.form
import io.github.wulkanowy.MainCoroutineRule
import io.github.wulkanowy.data.db.entities.Student
import io.github.wulkanowy.data.db.entities.StudentWithSemesters
import io.github.wulkanowy.data.pojos.RegisterUser
import io.github.wulkanowy.data.repositories.StudentRepository
import io.github.wulkanowy.sdk.scrapper.Scrapper
import io.github.wulkanowy.ui.modules.login.LoginErrorHandler
import io.github.wulkanowy.utils.AnalyticsHelper
import io.mockk.*
@ -12,7 +12,6 @@ import org.junit.Before
import org.junit.Rule
import org.junit.Test
import java.io.IOException
import java.time.Instant
class LoginFormPresenterTest {
@ -33,6 +32,15 @@ class LoginFormPresenterTest {
private lateinit var presenter: LoginFormPresenter
private val registerUser = RegisterUser(
email = "",
password = "",
login = "",
baseUrl = "",
loginType = Scrapper.LoginType.AUTO,
symbols = listOf(),
)
@Before
fun setUp() {
MockKAnnotations.init(this)
@ -104,32 +112,9 @@ class LoginFormPresenterTest {
@Test
fun loginTest() {
val studentTest = Student(
email = "test@",
password = "123",
scrapperBaseUrl = "https://fakelog.cf/?email",
loginType = "AUTO",
studentName = "",
schoolSymbol = "",
schoolName = "",
studentId = 0,
classId = 1,
isCurrent = false,
symbol = "",
registrationDate = Instant.now(),
className = "",
mobileBaseUrl = "",
privateKey = "",
certificateKey = "",
loginMode = "",
userLoginId = 0,
schoolShortName = "",
isParent = false,
userName = ""
)
coEvery { repository.getStudentsScrapper(any(), any(), any(), any()) } returns listOf(
StudentWithSemesters(studentTest, emptyList())
)
coEvery {
repository.getUserSubjectsFromScrapper(any(), any(), any(), any())
} returns registerUser
every { loginFormView.formUsernameValue } returns "@"
every { loginFormView.formPassValue } returns "123456"
@ -146,7 +131,9 @@ class LoginFormPresenterTest {
@Test
fun loginEmptyTest() {
coEvery { repository.getStudentsScrapper(any(), any(), any(), any()) } returns listOf()
coEvery {
repository.getUserSubjectsFromScrapper(any(), any(), any(), any())
} returns registerUser
every { loginFormView.formUsernameValue } returns "@"
every { loginFormView.formPassValue } returns "123456"
every { loginFormView.formHostValue } returns "https://fakelog.cf/?email"
@ -162,7 +149,9 @@ class LoginFormPresenterTest {
@Test
fun loginEmptyTwiceTest() {
coEvery { repository.getStudentsScrapper(any(), any(), any(), any()) } returns listOf()
coEvery {
repository.getUserSubjectsFromScrapper(any(), any(), any(), any())
} returns registerUser
every { loginFormView.formUsernameValue } returns "@"
every { loginFormView.formPassValue } returns "123456"
every { loginFormView.formHostValue } returns "https://fakelog.cf/?email"
@ -180,7 +169,14 @@ class LoginFormPresenterTest {
@Test
fun loginErrorTest() {
val testException = IOException("test")
coEvery { repository.getStudentsScrapper(any(), any(), any(), any()) } throws testException
coEvery {
repository.getUserSubjectsFromScrapper(
any(),
any(),
any(),
any()
)
} throws testException
every { loginFormView.formUsernameValue } returns "@"
every { loginFormView.formPassValue } returns "123456"
every { loginFormView.formHostValue } returns "https://fakelog.cf/?email"

View file

@ -1,18 +1,22 @@
package io.github.wulkanowy.ui.modules.login.studentselect
import io.github.wulkanowy.MainCoroutineRule
import io.github.wulkanowy.data.db.entities.Student
import io.github.wulkanowy.data.db.entities.StudentWithSemesters
import io.github.wulkanowy.data.pojos.RegisterStudent
import io.github.wulkanowy.data.pojos.RegisterSymbol
import io.github.wulkanowy.data.pojos.RegisterUnit
import io.github.wulkanowy.data.pojos.RegisterUser
import io.github.wulkanowy.data.repositories.StudentRepository
import io.github.wulkanowy.sdk.scrapper.Scrapper
import io.github.wulkanowy.services.sync.SyncManager
import io.github.wulkanowy.ui.modules.login.LoginData
import io.github.wulkanowy.ui.modules.login.LoginErrorHandler
import io.github.wulkanowy.utils.AnalyticsHelper
import io.github.wulkanowy.utils.AppInfo
import io.mockk.*
import io.mockk.impl.annotations.MockK
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import java.time.Instant
class LoginStudentSelectPresenterTest {
@ -22,7 +26,7 @@ class LoginStudentSelectPresenterTest {
@MockK(relaxed = true)
lateinit var errorHandler: LoginErrorHandler
@MockK(relaxed = true)
@MockK
lateinit var loginStudentSelectView: LoginStudentSelectView
@MockK
@ -34,33 +38,55 @@ class LoginStudentSelectPresenterTest {
@MockK(relaxed = true)
lateinit var syncManager: SyncManager
private val appInfo = AppInfo()
private lateinit var presenter: LoginStudentSelectPresenter
private val testStudent by lazy {
Student(
email = "test",
password = "test123",
scrapperBaseUrl = "https://fakelog.cf",
loginType = "AUTO",
symbol = "",
isCurrent = false,
studentId = 0,
schoolName = "",
schoolSymbol = "",
classId = 1,
studentName = "",
registrationDate = Instant.now(),
className = "",
loginMode = "",
certificateKey = "",
privateKey = "",
mobileBaseUrl = "",
schoolShortName = "",
userLoginId = 1,
isParent = false,
userName = ""
)
}
private val loginData = LoginData(
login = "",
password = "",
baseUrl = "",
symbol = null,
)
private val subject = RegisterStudent(
studentId = 0,
studentName = "",
studentSecondName = "",
studentSurname = "",
className = "",
classId = 0,
isParent = false,
semesters = listOf(),
)
private val school = RegisterUnit(
userLoginId = 0,
schoolId = "",
schoolName = "",
schoolShortName = "",
parentIds = listOf(),
studentIds = listOf(),
employeeIds = listOf(),
error = null,
students = listOf(subject)
)
private val symbol = RegisterSymbol(
symbol = "",
error = null,
userName = "",
schools = listOf(school),
)
private val registerUser = RegisterUser(
email = "",
password = "",
login = "",
baseUrl = "",
loginType = Scrapper.LoginType.AUTO,
symbols = listOf(symbol),
)
private val testException by lazy { RuntimeException("Problem") }
@ -69,30 +95,44 @@ class LoginStudentSelectPresenterTest {
MockKAnnotations.init(this)
clearMocks(studentRepository, loginStudentSelectView)
coEvery { studentRepository.getSavedStudents(false) } returns emptyList()
every { loginStudentSelectView.initView() } just Runs
every { loginStudentSelectView.showContact(any()) } just Runs
every { loginStudentSelectView.symbols } returns emptyMap()
every { loginStudentSelectView.enableSignIn(any()) } just Runs
every { loginStudentSelectView.showProgress(any()) } just Runs
every { loginStudentSelectView.showContent(any()) } just Runs
presenter = LoginStudentSelectPresenter(studentRepository, errorHandler, syncManager, analytics)
presenter.onAttachView(loginStudentSelectView, emptyList())
presenter = LoginStudentSelectPresenter(
studentRepository = studentRepository,
loginErrorHandler = errorHandler,
syncManager = syncManager,
analytics = analytics,
appInfo = appInfo,
)
}
@Test
fun initViewTest() {
presenter.onAttachView(loginStudentSelectView, loginData, registerUser)
verify { loginStudentSelectView.initView() }
}
@Test
fun onSelectedStudentTest() {
coEvery {
studentRepository.saveStudents(listOf(StudentWithSemesters(testStudent, emptyList())))
} just Runs
val itemsSlot = slot<List<LoginStudentSelectItem>>()
every { loginStudentSelectView.updateData(capture(itemsSlot)) } just Runs
presenter.onAttachView(loginStudentSelectView, loginData, registerUser)
coEvery { studentRepository.saveStudents(any()) } just Runs
every { loginStudentSelectView.navigateToNext() } just Runs
presenter.onItemSelected(StudentWithSemesters(testStudent, emptyList()), false)
itemsSlot.captured.filterIsInstance<LoginStudentSelectItem.Student>().first().let {
it.onClick(it)
}
presenter.onSignIn()
verify { loginStudentSelectView.showContent(false) }
@ -102,13 +142,15 @@ class LoginStudentSelectPresenterTest {
@Test
fun onSelectedStudentErrorTest() {
coEvery {
studentRepository.saveStudents(listOf(StudentWithSemesters(testStudent, emptyList())))
} throws testException
val itemsSlot = slot<List<LoginStudentSelectItem>>()
every { loginStudentSelectView.updateData(capture(itemsSlot)) } just Runs
presenter.onAttachView(loginStudentSelectView, loginData, registerUser)
coEvery { studentRepository.logoutStudent(testStudent) } just Runs
coEvery { studentRepository.saveStudents(any()) } throws testException
presenter.onItemSelected(StudentWithSemesters(testStudent, emptyList()), false)
itemsSlot.captured.filterIsInstance<LoginStudentSelectItem.Student>().first().let {
it.onClick(it)
}
presenter.onSignIn()
verify { loginStudentSelectView.showContent(false) }