Skip to content

Instantly share code, notes, and snippets.

View PhilippeBoisney's full-sized avatar
🏠
Working from home

Boisney Philippe PhilippeBoisney

🏠
Working from home
View GitHub Profile
class UserRepository(private val service: UserService) {
private suspend fun search(query: String, page: Int, perPage: Int, sort: String) = service.search(query, page, perPage, sort).await()
private suspend fun getDetail(login: String) = service.getDetail(login).await()
private suspend fun getRepos(login: String) = service.getRepos(login).await()
private suspend fun getFollowers(login: String) = service.getFollowers(login).await()
interface UserService {
@GET("search/users")
fun search(@Query("q") query: String,
@Query("page") page: Int,
@Query("per_page") perPage: Int,
@Query("sort") sort: String,
@Query("client_id") clientId: String = BuildConfig.GithubClientId,
@Query("client_secret") clientSecret: String = BuildConfig.GithubClientSecret): Deferred<Result>
class SearchUserViewModel(private val repository: UserRepository): BaseViewModel() {
private val searchQuery = MutableLiveData<String>()
private val listing = map(searchQuery) { repository.searchUsersWithPagination(it) }
val networkState: LiveData<NetworkState>? = switchMap(listing) { it.networkState }
val usersFetched: LiveData<PagedList<User>> = switchMap(listing) { it.pagedList }
override fun onCleared() {
super.onCleared()
/**
* Instrumented tests for [JobListFragment]
*/
@RunWith(AndroidJUnit4::class)
@LargeTest
class TIJobList {
@Rule
@JvmField
val activityRule = ActivityTestRule(SingleFragmentActivity::class.java, true, true)
/**
* Remote [FirebaseFirestore] datasource only used for tests
* @param listApprovedJobs: Mocked [Query] used to return a custom list of jobs.
*/
fun remoteDataSourceTestModule(listApprovedJobs: Query) = module {
factory { mockk<FirebaseFirestore>(relaxed = true) }
single {
object : JobDao(get()) {
override fun listApprovedJobs() = listApprovedJobs
} as JobDao
/**
* We use a separate [Application] for tests to prevent initializing release modules.
* On the contrary, we will provide inside our tests custom [Module].
*
* See [io.nobullshit.nobullshit.TIRunner].
*/
class TIBaseApplication : Application() {
override fun onCreate() {
super.onCreate()
startKoin(this, emptyList())
class JobListFragment : BaseFragment() {
val jobDao: JobDao by inject()
...
}
class BaseApplication: Application() {
override fun onCreate() {
super.onCreate()
startKoin(this, appComponent)
}
}
@Singleton
@Component(
modules = [
AndroidInjectionModule::class,
DataModule::class,
MainActivityModule::class]
)
interface AppComponent {
@Component.Builder
interface Builder {
/**
* App Components
*/
val appComponent: List<Module> = listOf(remoteDataSourceModule)