From 0f967e03461b6d37c2218ba555cf23733648906c Mon Sep 17 00:00:00 2001 From: Tim Ebbeke Date: Wed, 31 May 2023 00:54:27 +0200 Subject: [PATCH] Added more stabilize tests. --- .../nui/frontend/utility/stabilize.hpp | 5 +- nui/src/nui/frontend/utility/stabilize.cpp | 5 ++ nui/test/nui/test_render.hpp | 67 +++++++++++++++++-- 3 files changed, 69 insertions(+), 8 deletions(-) diff --git a/nui/include/nui/frontend/utility/stabilize.hpp b/nui/include/nui/frontend/utility/stabilize.hpp index 38fcb6f..def2fe0 100644 --- a/nui/include/nui/frontend/utility/stabilize.hpp +++ b/nui/include/nui/frontend/utility/stabilize.hpp @@ -16,10 +16,7 @@ namespace Nui {} /// Resets the stable element, so that it is re-rendered on the next render. - void reset() - { - stableElement_ = {}; - } + void reset(); friend ElementRenderer stabilize(StableElement& stableElement, ElementRenderer const& encapsulatedRenderer); diff --git a/nui/src/nui/frontend/utility/stabilize.cpp b/nui/src/nui/frontend/utility/stabilize.cpp index f20622e..5098a80 100644 --- a/nui/src/nui/frontend/utility/stabilize.cpp +++ b/nui/src/nui/frontend/utility/stabilize.cpp @@ -5,6 +5,11 @@ namespace Nui { + void StableElement::reset() + { + stableElement_ = {}; + } + ElementRenderer stabilize(StableElement& stableElement, ElementRenderer const& encapsulatedRenderer) { return [encapsulatedRenderer, diff --git a/nui/test/nui/test_render.hpp b/nui/test/nui/test_render.hpp index 5c68e87..2029012 100644 --- a/nui/test/nui/test_render.hpp +++ b/nui/test/nui/test_render.hpp @@ -322,12 +322,13 @@ namespace Nui::Tests { static std::string once = "once"; static std::string onceClass = "onceClass"; - return stabilize( - stable, + const auto s = stabilize( + stable, span{id = once}(button{class_ = onceClass}()) ); once = "X"; onceClass = "Y"; + return s; } } )); @@ -376,7 +377,7 @@ namespace Nui::Tests else { return stabilize( - stable, + stable, span{id = spanId}() ); } @@ -426,7 +427,7 @@ namespace Nui::Tests else { return stabilize( - stable, + stable, // fragment is ignored and forms a div, because StableElements can only be one element fragment(a{}(), span{}()) ); @@ -458,4 +459,62 @@ namespace Nui::Tests ASSERT_EQ(Nui::val::global("document")["body"]["children"]["length"].as(), 1); EXPECT_EQ(Nui::val::global("document")["body"]["children"][0]["tagName"].as(), "div"); } + + TEST_F(TestRender, CanResetStableElement) + { + using Nui::Elements::div; + using Nui::Elements::span; + using Nui::Elements::button; + using namespace Nui::Attributes; + + Nui::Observed toggle = true; + StableElement stable; + + std::string once = "once"; + std::string onceClass = "onceClass"; + + // clang-format off + render(div{}( + observe(toggle), + [&toggle, &stable, &once, &onceClass]() -> Nui::ElementRenderer{ + if (!*toggle) + return nil(); + else + { + const auto s = stabilize( + stable, + span{id = once}(button{class_ = onceClass}()) + ); + once = "X"; + onceClass = "Y"; + return s; + } + } + )); + // clang-format on + + ASSERT_EQ(Nui::val::global("document")["body"]["children"]["length"].as(), 1); + EXPECT_EQ(Nui::val::global("document")["body"]["children"][0]["tagName"].as(), "span"); + EXPECT_EQ(Nui::val::global("document")["body"]["children"][0]["attributes"]["id"].as(), "once"); + EXPECT_EQ( + Nui::val::global("document")["body"]["children"][0]["children"][0]["tagName"].as(), "button"); + EXPECT_EQ( + Nui::val::global("document")["body"]["children"][0]["children"][0]["attributes"]["class"].as(), + "onceClass"); + + toggle = false; + globalEventContext.executeActiveEventsImmediately(); + + stable.reset(); + + toggle = true; + globalEventContext.executeActiveEventsImmediately(); + + ASSERT_EQ(Nui::val::global("document")["body"]["children"]["length"].as(), 1); + EXPECT_EQ(Nui::val::global("document")["body"]["children"][0]["tagName"].as(), "span"); + EXPECT_EQ(Nui::val::global("document")["body"]["children"][0]["attributes"]["id"].as(), "X"); + EXPECT_EQ( + Nui::val::global("document")["body"]["children"][0]["children"][0]["attributes"]["class"].as(), + "Y"); + } } \ No newline at end of file