Giter Club home page Giter Club logo

Comments (4)

wajahatkarim3 avatar wajahatkarim3 commented on September 14, 2024

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.

from imagine.

Drjacky avatar Drjacky commented on September 14, 2024

@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.

from imagine.

wajahatkarim3 avatar wajahatkarim3 commented on September 14, 2024

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

from imagine.

Drjacky avatar Drjacky commented on September 14, 2024

@wajahatkarim3 Have you found the solution for this?

from imagine.

Related Issues (7)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.