forked from facebook/react-native
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRCTTestModule.mm
More file actions
161 lines (121 loc) · 5.55 KB
/
RCTTestModule.mm
File metadata and controls
161 lines (121 loc) · 5.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
* 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.
*/
#import "RCTTestModule.h"
#import <React/RCTAssert.h>
#import <React/RCTEventDispatcher.h>
#import <React/RCTLog.h>
#import <React/RCTUIManager.h>
#import <ReactCommon/RCTTurboModule.h>
#import "FBSnapshotTestController.h"
#import "RCTTestPlugins.h"
@protocol NativeTestModuleSpec <RCTBridgeModule, RCTTurboModule>
- (void)markTestCompleted;
- (void)markTestPassed:(BOOL)success;
- (void)verifySnapshot:(RCTResponseSenderBlock)callback;
@end
namespace facebook {
namespace react {
/**
* ObjC++ class for module 'TestModule'
*/
class JSI_EXPORT NativeTestModuleSpecJSI : public ObjCTurboModule {
public:
NativeTestModuleSpecJSI(id<RCTTurboModule> instance, std::shared_ptr<CallInvoker> jsInvoker, std::shared_ptr<CallInvoker> nativeInvoker, id<RCTTurboModulePerformanceLogger> perfLogger);
};
} // namespace react
} // namespace facebook
namespace facebook {
namespace react {
static facebook::jsi::Value __hostFunction_NativeTestModuleSpecJSI_markTestCompleted(facebook::jsi::Runtime& rt, TurboModule &turboModule, const facebook::jsi::Value* args, size_t count) {
return static_cast<ObjCTurboModule&>(turboModule).invokeObjCMethod(rt, VoidKind, "markTestCompleted", @selector(markTestCompleted), args, count);
}
static facebook::jsi::Value __hostFunction_NativeTestModuleSpecJSI_markTestPassed(facebook::jsi::Runtime& rt, TurboModule &turboModule, const facebook::jsi::Value* args, size_t count) {
return static_cast<ObjCTurboModule&>(turboModule).invokeObjCMethod(rt, VoidKind, "markTestPassed", @selector(markTestPassed:), args, count);
}
static facebook::jsi::Value __hostFunction_NativeTestModuleSpecJSI_verifySnapshot(facebook::jsi::Runtime& rt, TurboModule &turboModule, const facebook::jsi::Value* args, size_t count) {
return static_cast<ObjCTurboModule&>(turboModule).invokeObjCMethod(rt, VoidKind, "verifySnapshot", @selector(verifySnapshot:), args, count);
}
NativeTestModuleSpecJSI::NativeTestModuleSpecJSI(id<RCTTurboModule> instance, std::shared_ptr<CallInvoker> jsInvoker, std::shared_ptr<CallInvoker> nativeInvoker, id<RCTTurboModulePerformanceLogger> perfLogger)
: ObjCTurboModule("TestModule", instance, jsInvoker, nativeInvoker, perfLogger) {
methodMap_["markTestCompleted"] = MethodMetadata {0, __hostFunction_NativeTestModuleSpecJSI_markTestCompleted};
methodMap_["markTestPassed"] = MethodMetadata {1, __hostFunction_NativeTestModuleSpecJSI_markTestPassed};
methodMap_["verifySnapshot"] = MethodMetadata {1, __hostFunction_NativeTestModuleSpecJSI_verifySnapshot};
}
} // namespace react
} // namespace facebook
@interface RCTTestModule() <NativeTestModuleSpec>
@end
@implementation RCTTestModule {
NSMutableDictionary<NSString *, NSNumber *> *_snapshotCounter;
}
@synthesize bridge = _bridge;
RCT_EXPORT_MODULE()
- (dispatch_queue_t)methodQueue
{
return _bridge.uiManager.methodQueue;
}
RCT_EXPORT_METHOD(verifySnapshot:(RCTResponseSenderBlock)callback)
{
RCTAssert(_controller != nil, @"No snapshot controller configured.");
[_bridge.uiManager addUIBlock:^(RCTUIManager *uiManager, NSDictionary<NSNumber *, UIView *> *viewRegistry) {
NSString *testName = NSStringFromSelector(self->_testSelector);
if (!self->_snapshotCounter) {
self->_snapshotCounter = [NSMutableDictionary new];
}
NSNumber *counter = @([self->_snapshotCounter[testName] integerValue] + 1);
self->_snapshotCounter[testName] = counter;
NSError *error = nil;
NSString *identifier = [counter stringValue];
if (self->_testSuffix) {
identifier = [identifier stringByAppendingString:self->_testSuffix];
}
BOOL success = [self->_controller compareSnapshotOfView:self->_view
selector:self->_testSelector
identifier:identifier
error:&error];
if (!success) {
RCTLogInfo(@"Failed to verify snapshot %@ (error: %@)", identifier, error);
}
callback(@[@(success)]);
}];
}
RCT_EXPORT_METHOD(sendAppEvent:(NSString *)name body:(nullable id)body)
{
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
[_bridge.eventDispatcher sendAppEventWithName:name body:body];
#pragma clang diagnostic pop
}
RCT_REMAP_METHOD(shouldResolve, shouldResolve_resolve:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject)
{
resolve(@1);
}
RCT_REMAP_METHOD(shouldReject, shouldReject_resolve:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject)
{
reject(nil, nil, nil);
}
RCT_EXPORT_METHOD(markTestCompleted)
{
[self markTestPassed:YES];
}
RCT_EXPORT_METHOD(markTestPassed:(BOOL)success)
{
[_bridge.uiManager addUIBlock:^(__unused RCTUIManager *uiManager, __unused NSDictionary<NSNumber *, UIView *> *viewRegistry) {
self->_status = success ? RCTTestStatusPassed : RCTTestStatusFailed;
}];
}
- (std::shared_ptr<facebook::react::TurboModule>)
getTurboModuleWithJsInvoker:(std::shared_ptr<facebook::react::CallInvoker>)jsInvoker
nativeInvoker:(std::shared_ptr<facebook::react::CallInvoker>)nativeInvoker
perfLogger:(id<RCTTurboModulePerformanceLogger>)perfLogger
{
return std::make_shared<facebook::react::NativeTestModuleSpecJSI>(self, jsInvoker, nativeInvoker, perfLogger);
}
@end
Class RCTTestModuleCls(void) {
return RCTTestModule.class;
}