From 3c74b6ea34ce55aba68d86ef154b47abea50140d Mon Sep 17 00:00:00 2001 From: Alex Dvornikov Date: Tue, 12 Feb 2019 04:46:11 -0800 Subject: [PATCH] Bring back the unit tests for the Cxx implementation Reviewed By: jeanlauliac Differential Revision: D14026623 fbshipit-source-id: 76a4089bb09b7e5152e992a91eae0427877767c0 --- ReactCommon/cxxreact/JSDeltaBundleClient.cpp | 2 +- ReactCommon/cxxreact/tests/BUCK | 1 + .../tests/JSDeltaBundleClientTest.cpp | 131 ++++++++++++++++++ 3 files changed, 133 insertions(+), 1 deletion(-) create mode 100644 ReactCommon/cxxreact/tests/JSDeltaBundleClientTest.cpp diff --git a/ReactCommon/cxxreact/JSDeltaBundleClient.cpp b/ReactCommon/cxxreact/JSDeltaBundleClient.cpp index 2f66e861eb40cf..0c48785d0371f4 100644 --- a/ReactCommon/cxxreact/JSDeltaBundleClient.cpp +++ b/ReactCommon/cxxreact/JSDeltaBundleClient.cpp @@ -30,7 +30,7 @@ void JSDeltaBundleClient::patchModules(const folly::dynamic *modules) { for (const folly::dynamic pair : *modules) { auto id = pair[0].getInt(); auto module = pair[1]; - modules_.emplace(id, module.getString()); + modules_[id] = std::move(module.getString()); } } diff --git a/ReactCommon/cxxreact/tests/BUCK b/ReactCommon/cxxreact/tests/BUCK index 25c4d3506cfde8..e25870d3d83b66 100644 --- a/ReactCommon/cxxreact/tests/BUCK +++ b/ReactCommon/cxxreact/tests/BUCK @@ -8,6 +8,7 @@ load( TEST_SRCS = [ "RecoverableErrorTest.cpp", + "JSDeltaBundleClientTest.cpp", "jsarg_helpers.cpp", "jsbigstring.cpp", "methodcall.cpp", diff --git a/ReactCommon/cxxreact/tests/JSDeltaBundleClientTest.cpp b/ReactCommon/cxxreact/tests/JSDeltaBundleClientTest.cpp new file mode 100644 index 00000000000000..c3a70ab221b7c7 --- /dev/null +++ b/ReactCommon/cxxreact/tests/JSDeltaBundleClientTest.cpp @@ -0,0 +1,131 @@ +// Copyright (c) Facebook, Inc. and its affiliates. + +// This source code is licensed under the MIT license found in the +// LICENSE file in the root directory of this source tree. + +#include + +#include +#include +#include + +#include +#include +#include + +using namespace facebook::react; + +TEST(JSDeltaBundleClient, PatchStartupCode) { + JSDeltaBundleClient client; + + folly::dynamic delta1 = folly::parseJson(R"({ + "base": true, + "revisionId": "rev0", + "pre": "pre", + "post": "post", + "modules": [ + [0, "0"], + [1, "1"] + ] + })"); + + client.patch(delta1); + + EXPECT_STREQ(client.getStartupCode()->c_str(), "pre\npost\n"); + + folly::dynamic delta2 = folly::parseJson(R"({ + "base": true, + "revisionId": "rev1", + "pre": "pre2", + "post": "post2", + "modules": [] + })"); + + client.patch(delta2); + + EXPECT_STREQ(client.getStartupCode()->c_str(), "pre2\npost2\n"); +} + +TEST(JSDeltaBundleClient, PatchModule) { + JSDeltaBundleClient client; + + folly::dynamic delta1 = folly::parseJson(R"({ + "base": true, + "revisionId": "rev0", + "pre": "pre", + "post": "post", + "modules": [ + [0, "0"], + [1, "1"] + ] + })"); + + client.patch(delta1); + + EXPECT_EQ(client.getModule(0).code, "0"); + EXPECT_EQ(client.getModule(1).code, "1"); + + ASSERT_THROW(client.getModule(2), JSModulesUnbundle::ModuleNotFound); + + folly::dynamic delta2 = folly::parseJson(R"({ + "base": false, + "revisionId": "rev1", + "added": [ + [2, "2"] + ], + "modified": [ + [0, "0.1"] + ], + "deleted": [1] + })"); + + client.patch(delta2); + + EXPECT_EQ(client.getModule(0).code, "0.1"); + EXPECT_EQ(client.getModule(2).code, "2"); + ASSERT_THROW(client.getModule(1), JSModulesUnbundle::ModuleNotFound); + + folly::dynamic delta3 = folly::parseJson(R"({ + "base": true, + "revisionId": "rev2", + "pre": "pre", + "post": "post", + "modules": [ + [3, "3"], + [4, "4"] + ] + })"); + + client.patch(delta3); + + ASSERT_THROW(client.getModule(0), JSModulesUnbundle::ModuleNotFound); + ASSERT_THROW(client.getModule(1), JSModulesUnbundle::ModuleNotFound); + ASSERT_THROW(client.getModule(2), JSModulesUnbundle::ModuleNotFound); + + EXPECT_EQ(client.getModule(3).code, "3"); + EXPECT_EQ(client.getModule(4).code, "4"); +} + +TEST(JSDeltaBundleClient, Clear) { + JSDeltaBundleClient client; + + folly::dynamic delta1 = folly::parseJson(R"({ + "base": true, + "revisionId": "rev0", + "pre": "pre", + "post": "post", + "modules": [ + [0, "0"], + [1, "1"] + ] + })"); + + client.patch(delta1); + + client.clear(); + + ASSERT_THROW(client.getModule(0), JSModulesUnbundle::ModuleNotFound); + ASSERT_THROW(client.getModule(1), JSModulesUnbundle::ModuleNotFound); + + EXPECT_STREQ(client.getStartupCode()->c_str(), ""); +}