Skip to content

Commit

Permalink
Added more stabilize tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
5cript committed May 30, 2023
1 parent 977dce0 commit 0f967e0
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 8 deletions.
5 changes: 1 addition & 4 deletions nui/include/nui/frontend/utility/stabilize.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
5 changes: 5 additions & 0 deletions nui/src/nui/frontend/utility/stabilize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@

namespace Nui
{
void StableElement::reset()
{
stableElement_ = {};
}

ElementRenderer stabilize(StableElement& stableElement, ElementRenderer const& encapsulatedRenderer)
{
return [encapsulatedRenderer,
Expand Down
67 changes: 63 additions & 4 deletions nui/test/nui/test_render.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
));
Expand Down Expand Up @@ -376,7 +377,7 @@ namespace Nui::Tests
else
{
return stabilize(
stable,
stable,
span{id = spanId}()
);
}
Expand Down Expand Up @@ -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{}())
);
Expand Down Expand Up @@ -458,4 +459,62 @@ namespace Nui::Tests
ASSERT_EQ(Nui::val::global("document")["body"]["children"]["length"].as<long long>(), 1);
EXPECT_EQ(Nui::val::global("document")["body"]["children"][0]["tagName"].as<std::string>(), "div");
}

TEST_F(TestRender, CanResetStableElement)
{
using Nui::Elements::div;
using Nui::Elements::span;
using Nui::Elements::button;
using namespace Nui::Attributes;

Nui::Observed<bool> 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<long long>(), 1);
EXPECT_EQ(Nui::val::global("document")["body"]["children"][0]["tagName"].as<std::string>(), "span");
EXPECT_EQ(Nui::val::global("document")["body"]["children"][0]["attributes"]["id"].as<std::string>(), "once");
EXPECT_EQ(
Nui::val::global("document")["body"]["children"][0]["children"][0]["tagName"].as<std::string>(), "button");
EXPECT_EQ(
Nui::val::global("document")["body"]["children"][0]["children"][0]["attributes"]["class"].as<std::string>(),
"onceClass");

toggle = false;
globalEventContext.executeActiveEventsImmediately();

stable.reset();

toggle = true;
globalEventContext.executeActiveEventsImmediately();

ASSERT_EQ(Nui::val::global("document")["body"]["children"]["length"].as<long long>(), 1);
EXPECT_EQ(Nui::val::global("document")["body"]["children"][0]["tagName"].as<std::string>(), "span");
EXPECT_EQ(Nui::val::global("document")["body"]["children"][0]["attributes"]["id"].as<std::string>(), "X");
EXPECT_EQ(
Nui::val::global("document")["body"]["children"][0]["children"][0]["attributes"]["class"].as<std::string>(),
"Y");
}
}

0 comments on commit 0f967e0

Please sign in to comment.