Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question]: PagingData<ModelName> instead of List<ModelName> #18

Open
Drjacky opened this issue Feb 15, 2021 · 4 comments
Open

[Question]: PagingData<ModelName> instead of List<ModelName> #18

Drjacky opened this issue Feb 15, 2021 · 4 comments

Comments

@Drjacky
Copy link

Drjacky commented Feb 15, 2021

What if the mocked data is PagingData? What should we write here:

verify { photosListObserver.onChanged(match { it.size == givenPhotos.size }) }

@wajahatkarim3
Copy link
Owner

Are you referring to Paging 2 or 3? I tried this with Paging 2 but my tests couldn't pass. So I will change the implementation to Paging 3 in a separate branch and also update the tests as well.

@Drjacky
Copy link
Author

Drjacky commented Feb 16, 2021

@wajahatkarim3 I'm referring to Paging 3.
This is my test:

@RunWith(JUnit4::class)
class ProductsListViewModelTest {

    private lateinit var viewModel: ProductsListViewModel

    @get:Rule
    var instantExecutorRule = InstantTaskExecutorRule()

    @ExperimentalCoroutinesApi
    @get:Rule
    var coroutineRule = MainCoroutineRule()

    @MockK
    lateinit var getBeersListUseCase: GetBeersListUseCase

    @MockK
    lateinit var getBeersListByCoroutineUseCase: GetBeersListByCoroutineUseCase

    @MockK
    lateinit var savedStateHandle: SavedStateHandle

    @Before
    fun setUp() {
        MockKAnnotations.init(this)
    }

    @After
    fun tearDown() {
    }

    @ExperimentalCoroutinesApi
    @Test
    fun `test when ProductsListViewModel is initialized, products are fetched`() =
        coroutineRule.testDispatcher.runBlockingTest {
            // Given
            val givenProducts = ProductUIFactory.createProducts(3)
            val flow = flow<PagingData<RecyclerItem>> {
//                emit(LoadState.Loading)
                delay(10)
                emit(PagingData.from(givenProducts))
            }
            val productsListObserver = mockk<Observer<PagingData<RecyclerItem>>>(relaxed = true)

            // When
            coEvery {
                getBeersListUseCase.invoke(GetBeersListParams(anyString()))
                getBeersListByCoroutineUseCase.invoke(GetBeersListByCoroutineParams(anyString()))
            }
                .returns(flow)

            // Invoke
            every {
                savedStateHandle.get<ChoosePathType>(CHOOSE_PATH_TYPE) ?: ChoosePathType.COROUTINE
            } returns ChoosePathType.COROUTINE
            viewModel = ProductsListViewModel(
                getBeersUseCase = getBeersListUseCase,
                getBeersListByCoroutineUseCase = getBeersListByCoroutineUseCase,
                savedStateHandle = savedStateHandle
            )
            viewModel.productsListByCoroutine.asLiveData().observeForever(productsListObserver)

            // Then
            coroutineRule.advanceTimeBy(10)
            coVerify(exactly = 1) {
                getBeersListByCoroutineUseCase.invoke(
                    GetBeersListByCoroutineParams(anyString())
                )
            }
            //verify { productsListObserver.onChanged(match { it == PagingData.from(givenProducts) }) }
            verify {
                productsListObserver.onChanged(match { it != null })
            }
            verify(exactly = 1) {
                productsListObserver.onChanged(match { it != null })
            }
            /*val expectedFlow = flow<PagingData<RecyclerItem>> {
                emit(PagingData.from(givenProducts))
            }*/

//            assertNotNull(flow)
//            verify { productsListObserver.onChanged(match { it == expectedFlow }) }
        }

}

But, not sure what to write in the onChanged(match { scope.

@wajahatkarim3
Copy link
Owner

Thank you for sharing the code snippet. I will add the Paging 3 API and see how it works for the tests.

@Drjacky
Copy link
Author

Drjacky commented Feb 1, 2022

@wajahatkarim3 Have you found the solution for this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants