question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Proposal] Make Components android independent POJO's for better testability

See original GitHub issue

I was wondering if the way how Litho works right now could be slightly changed so that components could be instantiated without any Android dependencies (i.e. no context etc).

Something like

Row.create() // no context
       .child (
           Text.create() // no context
               .text("Hello") 
               .build())
      .child(
           Text.create() // no context
               .text("World") 
               .build())
     .build()

So components would be just POJOs describing how the UI should look like. Then LithoView could render this POJOs to real UI widgets (using android dependencies like context etc.)

I think this would be a major improvement regarding testing. Imagine you have a Presenter in MVP (or whatever architectural pattern you prefer) that loads some data from business logic. The Presenter would “map” the data from business logic to LithoComponents and then call view.render(lithoComponentPojos) which then would use LithoView to actually render that components. If we could write litho components without android dependencies, we could easily test this architecture on the jvm with simple unit tests like

  Row expected = Row.create().child(...).child(...).build();
  assertEquals(expected, rowGeneratedByPresenterAndHandOverToView);

I’m sure this is not a simple change and not sure if this is technically possible with the current architecture of Litho, but is it something you would consider while moving towards 1.0?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:3
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
mihaelaocommented, Apr 24, 2017

Hey @sockeqwe, right now the component context is also holding on to information that is needed to perform state updates. Check out these lines: https://github.com/facebook/litho/blob/master/litho-core/src/main/java/com/facebook/litho/ComponentContext.java#L103#L127. We thought about holding onto that information in a separate object, but that would have introduced a new concept in the API and we tried to keep it simple.

1reaction
passycommented, Apr 24, 2017

Thanks @mihaelao! I think the closest we can get to this to enable easier testing is by having a MockComponentContext or similar which can be initialized without access to a real underlying Android Context. We could probably keep the state update logic around in there.

Having something like “stateless” Components similar to how they exist in React may also be something fun to think about for a model like @sockeqwe proposes it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

What I've Learned From Trying to Make An Android App Unit ...
Dependency Injection is really the main “active ingredient” that allows The Square Way enhance the unit testability of our applications.
Read more >
MVP for Android: how to organize the presentation layer
MVP makes views independent from our data source. We divide the application into at least three different layers, which lets us test them...
Read more >
Sample Flutter Clean Architecture on Rorty App focused on the ...
The goal of the project is to demonstrate best practices, provide a set of guidelines, and present modern Flutter application architecture that is...
Read more >
Android-centric-architecture vs. unit testable apps - SlideShare
MVI - Advantages ‣ Separates mutation and asynchronicity ‣ Makes testing much easier assertEquals(expectedModel ... MVI Logging & Timetravel • { " ...
Read more >
10.1: Room, LiveData, and ViewModel · GitBook - GitHub Pages
Architecture Components help you structure your app in a way that is robust, testable, and maintainable with less boilerplate code. Architecture Components ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found