Skip to content

Commit 3c74b6e

Browse files
fromcelticparkfacebook-github-bot
authored andcommitted
Bring back the unit tests for the Cxx implementation
Reviewed By: jeanlauliac Differential Revision: D14026623 fbshipit-source-id: 76a4089bb09b7e5152e992a91eae0427877767c0
1 parent daa79b0 commit 3c74b6e

File tree

3 files changed

+133
-1
lines changed

3 files changed

+133
-1
lines changed

ReactCommon/cxxreact/JSDeltaBundleClient.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ void JSDeltaBundleClient::patchModules(const folly::dynamic *modules) {
3030
for (const folly::dynamic pair : *modules) {
3131
auto id = pair[0].getInt();
3232
auto module = pair[1];
33-
modules_.emplace(id, module.getString());
33+
modules_[id] = std::move(module.getString());
3434
}
3535
}
3636

ReactCommon/cxxreact/tests/BUCK

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ load(
88

99
TEST_SRCS = [
1010
"RecoverableErrorTest.cpp",
11+
"JSDeltaBundleClientTest.cpp",
1112
"jsarg_helpers.cpp",
1213
"jsbigstring.cpp",
1314
"methodcall.cpp",
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
// Copyright (c) Facebook, Inc. and its affiliates.
2+
3+
// This source code is licensed under the MIT license found in the
4+
// LICENSE file in the root directory of this source tree.
5+
6+
#include <gtest/gtest.h>
7+
8+
#include <exception>
9+
#include <iostream>
10+
#include <stdexcept>
11+
12+
#include <cxxreact/JSDeltaBundleClient.h>
13+
#include <folly/dynamic.h>
14+
#include <folly/json.h>
15+
16+
using namespace facebook::react;
17+
18+
TEST(JSDeltaBundleClient, PatchStartupCode) {
19+
JSDeltaBundleClient client;
20+
21+
folly::dynamic delta1 = folly::parseJson(R"({
22+
"base": true,
23+
"revisionId": "rev0",
24+
"pre": "pre",
25+
"post": "post",
26+
"modules": [
27+
[0, "0"],
28+
[1, "1"]
29+
]
30+
})");
31+
32+
client.patch(delta1);
33+
34+
EXPECT_STREQ(client.getStartupCode()->c_str(), "pre\npost\n");
35+
36+
folly::dynamic delta2 = folly::parseJson(R"({
37+
"base": true,
38+
"revisionId": "rev1",
39+
"pre": "pre2",
40+
"post": "post2",
41+
"modules": []
42+
})");
43+
44+
client.patch(delta2);
45+
46+
EXPECT_STREQ(client.getStartupCode()->c_str(), "pre2\npost2\n");
47+
}
48+
49+
TEST(JSDeltaBundleClient, PatchModule) {
50+
JSDeltaBundleClient client;
51+
52+
folly::dynamic delta1 = folly::parseJson(R"({
53+
"base": true,
54+
"revisionId": "rev0",
55+
"pre": "pre",
56+
"post": "post",
57+
"modules": [
58+
[0, "0"],
59+
[1, "1"]
60+
]
61+
})");
62+
63+
client.patch(delta1);
64+
65+
EXPECT_EQ(client.getModule(0).code, "0");
66+
EXPECT_EQ(client.getModule(1).code, "1");
67+
68+
ASSERT_THROW(client.getModule(2), JSModulesUnbundle::ModuleNotFound);
69+
70+
folly::dynamic delta2 = folly::parseJson(R"({
71+
"base": false,
72+
"revisionId": "rev1",
73+
"added": [
74+
[2, "2"]
75+
],
76+
"modified": [
77+
[0, "0.1"]
78+
],
79+
"deleted": [1]
80+
})");
81+
82+
client.patch(delta2);
83+
84+
EXPECT_EQ(client.getModule(0).code, "0.1");
85+
EXPECT_EQ(client.getModule(2).code, "2");
86+
ASSERT_THROW(client.getModule(1), JSModulesUnbundle::ModuleNotFound);
87+
88+
folly::dynamic delta3 = folly::parseJson(R"({
89+
"base": true,
90+
"revisionId": "rev2",
91+
"pre": "pre",
92+
"post": "post",
93+
"modules": [
94+
[3, "3"],
95+
[4, "4"]
96+
]
97+
})");
98+
99+
client.patch(delta3);
100+
101+
ASSERT_THROW(client.getModule(0), JSModulesUnbundle::ModuleNotFound);
102+
ASSERT_THROW(client.getModule(1), JSModulesUnbundle::ModuleNotFound);
103+
ASSERT_THROW(client.getModule(2), JSModulesUnbundle::ModuleNotFound);
104+
105+
EXPECT_EQ(client.getModule(3).code, "3");
106+
EXPECT_EQ(client.getModule(4).code, "4");
107+
}
108+
109+
TEST(JSDeltaBundleClient, Clear) {
110+
JSDeltaBundleClient client;
111+
112+
folly::dynamic delta1 = folly::parseJson(R"({
113+
"base": true,
114+
"revisionId": "rev0",
115+
"pre": "pre",
116+
"post": "post",
117+
"modules": [
118+
[0, "0"],
119+
[1, "1"]
120+
]
121+
})");
122+
123+
client.patch(delta1);
124+
125+
client.clear();
126+
127+
ASSERT_THROW(client.getModule(0), JSModulesUnbundle::ModuleNotFound);
128+
ASSERT_THROW(client.getModule(1), JSModulesUnbundle::ModuleNotFound);
129+
130+
EXPECT_STREQ(client.getStartupCode()->c_str(), "");
131+
}

0 commit comments

Comments
 (0)