Skip to content

Commit

Permalink
Implement testMockCallJSFunction.
Browse files Browse the repository at this point in the history
  • Loading branch information
JunielKatarn committed Feb 11, 2019
1 parent 8bb3ff8 commit 572dd57
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 15 deletions.
11 changes: 11 additions & 0 deletions RNTester/RNTesterUnitTests/MockInstance.cpp
Expand Up @@ -8,8 +8,18 @@

#include "MockInstance.hpp"

#include <cxxreact/JsArgumentHelpers.h>

using namespace facebook::react;

using facebook::xplat::jsArgAsInt;
using std::map;

MockInstance::MockInstance(std::shared_ptr<map<int64_t, int64_t>> sumCache)
: sumCache_{sumCache}
{
}

void MockInstance::loadApplication(std::unique_ptr<RAMBundleRegistry> bundleRegistry,
std::unique_ptr<const JSBigString> startupScript,
std::string startupScriptSourceURL)
Expand Down Expand Up @@ -67,6 +77,7 @@ bool MockInstance::isInspectable()
void MockInstance::callJSFunction(std::string &&module, std::string &&method,
folly::dynamic &&params)
{
sumCache_->insert({jsArgAsInt(params, 0), jsArgAsInt(params, 1)});
}

void MockInstance::callJSCallback(uint64_t callbackId, folly::dynamic &&params)
Expand Down
4 changes: 4 additions & 0 deletions RNTester/RNTesterUnitTests/MockInstance.hpp
Expand Up @@ -19,6 +19,8 @@

class MockInstance : public facebook::react::Instance {
private:
std::shared_ptr<std::map<std::int64_t, std::int64_t>> sumCache_;

void loadApplication(std::unique_ptr<facebook::react::RAMBundleRegistry> bundleRegistry,
std::unique_ptr<const facebook::react::JSBigString> startupScript,
std::string startupScriptSourceURL) override;
Expand All @@ -27,6 +29,8 @@ class MockInstance : public facebook::react::Instance {
std::string startupScriptSourceURL) override;

public:
MockInstance(std::shared_ptr<std::map<std::int64_t, std::int64_t>> sumCache);

void initializeBridge(std::unique_ptr<facebook::react::InstanceCallback> callback,
std::shared_ptr<facebook::react::JSExecutorFactory> jsef,
std::shared_ptr<facebook::react::MessageQueueThread> jsQueue,
Expand Down
26 changes: 15 additions & 11 deletions RNTester/RNTesterUnitTests/MockInstanceTests.mm
Expand Up @@ -11,6 +11,10 @@
#import "MockInstance.hpp"
#import "SampleCxxModule.hpp"

using folly::dynamic;
using std::map;
using std::vector;

@interface MockInstanceTests : XCTestCase

@end
Expand All @@ -27,21 +31,21 @@ - (void)tearDown {
[super tearDown];
}

- (void)testExample {
- (void)testMockCallJSFunction {
// This is an example of a functional test case.
// Use XCTAssert and related functions to verify your tests produce the correct results.

std::shared_ptr<MockInstance> instance = std::make_shared<MockInstance>();
std::unique_ptr<SampleCxxModule> module = std::make_unique<SampleCxxModule>();

module->setInstance(instance);
}
auto cache = std::make_shared<map<int64_t, int64_t>>();
auto instance = std::make_shared<MockInstance>(cache);
auto module = std::make_unique<SampleCxxModule>();

- (void)testPerformanceExample {
// This is an example of a performance test case.
[self measureBlock:^{
// Put the code you want to measure the time of here.
}];
module->setInstance(instance);

auto sumMethod = module->getMethods()[0]; // First method: 'sum'.
sumMethod.func(dynamic::array(2, 3), [](vector<dynamic>){}, [](vector<dynamic>){});

auto result = cache->at(2);
XCTAssert(result == 2 + 3);
}

@end
7 changes: 3 additions & 4 deletions RNTester/RNTesterUnitTests/SampleCxxModule.cpp
Expand Up @@ -35,14 +35,13 @@ map<string, dynamic> SampleCxxModule::getConstants()

vector<SampleCxxModule::Method> SampleCxxModule::getMethods()
{
std::weak_ptr<facebook::react::Instance> instance = this->getInstance();

return
{
Method("foo", [instance = this->getInstance().lock()](dynamic args)
Method("sum", [instance = this->getInstance().lock()](dynamic args)
{
auto sum = jsArgAsInt(args, 0) + jsArgAsInt(args, 1);
if (instance)
instance->callJSFunction("RCTDeviceEventEmitter", "emit", dynamic::array(jsArgAsInt(args, 0)));
instance->callJSFunction("MockedModule", "mockedMethod", dynamic::array(jsArgAsInt(args, 0), sum));
})
};
}

0 comments on commit 572dd57

Please sign in to comment.