[Proposal] Make Components android independent POJO's for better testability
See original GitHub issueI 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:
- Created 6 years ago
- Reactions:3
- Comments:5 (4 by maintainers)
Top GitHub Comments
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.
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 AndroidContext
. 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.