Skip to content

Commit be49782

Browse files
authored
[iOS native tests] - BugReporting Module (#337)
1 parent 25e35fe commit be49782

File tree

2 files changed

+240
-2
lines changed

2 files changed

+240
-2
lines changed

InstabugSample/ios/InstabugSampleTests/InstabugSampleTests.m

Lines changed: 199 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ @interface InstabugSampleTests : XCTestCase
1717

1818
@implementation InstabugSampleTests
1919

20+
NSTimeInterval EXPECTATION_TIMEOUT = 10;
21+
2022
- (void)setUp {
2123
// Put setup code here. This method is called before the invocation of each test method in the class.
2224
self.instabugBridge = [[InstabugReactBridge alloc] init];
@@ -26,9 +28,204 @@ - (void)testShowingSurveyWithToken {
2628
NSString *token = @"token";
2729
id mock = OCMClassMock([IBGSurveys class]);
2830

29-
[[[mock stub] classMethod] showSurveyWithToken:token];
31+
OCMStub([mock showSurveyWithToken:token]);
3032
[self.instabugBridge showSurveyWithToken:token];
31-
[[[mock verify] classMethod] showSurveyWithToken:token];
33+
OCMVerify([mock showSurveyWithToken:token]);
34+
}
35+
36+
37+
/***********Bug Reporting*****************/
38+
39+
- (void) testgivenBoolean$setBugReportingEnabled_whenQuery_thenShouldCallNativeApi {
40+
BOOL enabled = false;
41+
[self.instabugBridge setBugReportingEnabled:enabled];
42+
XCTAssertFalse(IBGBugReporting.enabled);
43+
}
44+
45+
- (void) testgivenInvocationEvent$setInvocationEvents_whenQuery_thenShouldCallNativeApiWithArgs {
46+
NSArray *invocationEventsArr;
47+
invocationEventsArr = [NSArray arrayWithObjects: @(IBGInvocationEventScreenshot), nil];
48+
49+
[self.instabugBridge setInvocationEvents:invocationEventsArr];
50+
IBGInvocationEvent invocationEvents = 0;
51+
for (NSNumber *boxedValue in invocationEventsArr) {
52+
invocationEvents |= [boxedValue intValue];
53+
}
54+
XCTAssertEqual(IBGBugReporting.invocationEvents, invocationEvents);
3255
}
3356

57+
- (void)testgiven$invoke_whenQuery_thenShouldCallNativeApi {
58+
id mock = OCMClassMock([IBGBugReporting class]);
59+
60+
OCMStub([mock invoke]);
61+
[self.instabugBridge invoke];
62+
XCTestExpectation *expectation = [self expectationWithDescription:@"Test ME PLX"];
63+
64+
[[NSRunLoop mainRunLoop] performBlock:^{
65+
OCMVerify([mock invoke]);
66+
[expectation fulfill];
67+
}];
68+
69+
[self waitForExpectationsWithTimeout:EXPECTATION_TIMEOUT handler:nil];
70+
}
71+
72+
- (void) testgivenOptions$setInvocationOptions_whenQuery_thenShouldCallNativeApiWithArgs {
73+
NSArray *invocationOptionsArr = [NSArray arrayWithObjects: @(IBGBugReportingInvocationOptionEmailFieldHidden), nil];
74+
75+
[self.instabugBridge setInvocationOptions:invocationOptionsArr];
76+
IBGBugReportingInvocationOption invocationOptions = 0;
77+
for (NSNumber *boxedValue in invocationOptionsArr) {
78+
invocationOptions |= [boxedValue intValue];
79+
}
80+
XCTAssertEqual(IBGBugReporting.invocationOptions, invocationOptions);
81+
}
82+
83+
- (void) testgivenInvocationModeAndOptiond$invokeWithModeOptions_whenQuery_thenShouldCallNativeApiWithArgs {
84+
NSArray *invocationOptionsArr = [NSArray arrayWithObjects: @(IBGBugReportingInvocationOptionEmailFieldHidden), nil];
85+
IBGBugReportingInvocationOption invocationOptions = 0;
86+
for (NSNumber *boxedValue in invocationOptionsArr) {
87+
invocationOptions |= [boxedValue intValue];
88+
}
89+
IBGInvocationMode invocationMode = IBGInvocationModeNewBug;
90+
id mock = OCMClassMock([IBGBugReporting class]);
91+
OCMStub([mock invokeWithMode:invocationMode options:invocationOptions]);
92+
[self.instabugBridge invokeWithInvocationModeAndOptions:invocationMode options:invocationOptionsArr];
93+
XCTestExpectation *expectation = [self expectationWithDescription:@"Test ME PLX"];
94+
95+
[[NSRunLoop mainRunLoop] performBlock:^{
96+
OCMVerify([mock invokeWithMode:invocationMode options:invocationOptions]);
97+
[expectation fulfill];
98+
}];
99+
100+
[self waitForExpectationsWithTimeout:EXPECTATION_TIMEOUT handler:nil];
101+
}
102+
103+
- (void) testgivenPreInvocationHandler$setPreInvocationHandler_whenQuery_thenShouldCallNativeApi {
104+
id partialMock = OCMPartialMock(self.instabugBridge);
105+
RCTResponseSenderBlock callback = ^(NSArray *response) {};
106+
[partialMock setPreInvocationHandler:callback];
107+
XCTAssertNotNil(IBGBugReporting.willInvokeHandler);
108+
OCMStub([partialMock sendEventWithName:@"IBGpreInvocationHandler" body:nil]);
109+
IBGBugReporting.willInvokeHandler();
110+
OCMVerify([partialMock sendEventWithName:@"IBGpreInvocationHandler" body:nil]);
111+
}
112+
113+
114+
- (void) testgivenPostInvocationHandlerCANCEL$setPostInvocationHandler_whenQuery_thenShouldCallNativeApi {
115+
id partialMock = OCMPartialMock(self.instabugBridge);
116+
RCTResponseSenderBlock callback = ^(NSArray *response) {};
117+
[partialMock setPostInvocationHandler:callback];
118+
XCTAssertNotNil(IBGBugReporting.didDismissHandler);
119+
NSDictionary *result = @{ @"dismissType": @"CANCEL",
120+
@"reportType": @"bug"};
121+
OCMStub([partialMock sendEventWithName:@"IBGpostInvocationHandler" body:result]);
122+
IBGBugReporting.didDismissHandler(IBGDismissTypeCancel,IBGReportTypeBug);
123+
OCMVerify([partialMock sendEventWithName:@"IBGpostInvocationHandler" body:result]);
124+
}
125+
126+
- (void) testgivenPostInvocationHandlerSUBMIT$setPostInvocationHandler_whenQuery_thenShouldCallNativeApi {
127+
id partialMock = OCMPartialMock(self.instabugBridge);
128+
RCTResponseSenderBlock callback = ^(NSArray *response) {};
129+
[partialMock setPostInvocationHandler:callback];
130+
XCTAssertNotNil(IBGBugReporting.didDismissHandler);
131+
132+
NSDictionary *result = @{ @"dismissType": @"SUBMIT",
133+
@"reportType": @"feedback"};
134+
OCMStub([partialMock sendEventWithName:@"IBGpostInvocationHandler" body:result]);
135+
IBGBugReporting.didDismissHandler(IBGDismissTypeSubmit,IBGReportTypeFeedback);
136+
OCMVerify([partialMock sendEventWithName:@"IBGpostInvocationHandler" body:result]);
137+
}
138+
139+
- (void) testgivenPostInvocationHandlerADD_ATTACHMENT$setPostInvocationHandler_whenQuery_thenShouldCallNativeApi {
140+
id partialMock = OCMPartialMock(self.instabugBridge);
141+
RCTResponseSenderBlock callback = ^(NSArray *response) {};
142+
[partialMock setPostInvocationHandler:callback];
143+
XCTAssertNotNil(IBGBugReporting.didDismissHandler);
144+
NSDictionary *result = @{ @"dismissType": @"ADD_ATTACHMENT",
145+
@"reportType": @"feedback"};
146+
OCMStub([partialMock sendEventWithName:@"IBGpostInvocationHandler" body:result]);
147+
IBGBugReporting.didDismissHandler(IBGDismissTypeAddAttachment,IBGReportTypeFeedback);
148+
OCMVerify([partialMock sendEventWithName:@"IBGpostInvocationHandler" body:result]);
149+
}
150+
151+
- (void) testgivenBooleans$setPromptOptionsEnabled_whenQuery_thenShouldCallNativeApi {
152+
id mock = OCMClassMock([IBGBugReporting class]);
153+
BOOL enabled = true;
154+
IBGPromptOption promptOption = IBGPromptOptionNone + IBGPromptOptionChat + IBGPromptOptionBug + IBGPromptOptionFeedback;
155+
OCMStub([mock setPromptOptions:promptOption]);
156+
[self.instabugBridge setPromptOptionsEnabled:enabled feedback:enabled chat:enabled];
157+
OCMVerify([mock setPromptOptions:promptOption]);
158+
}
159+
160+
- (void) testgivenDouble$setShakingThresholdForiPhone_whenQuery_thenShouldCallNativeApi {
161+
double threshold = 12;
162+
[self.instabugBridge setShakingThresholdForiPhone:threshold];
163+
XCTAssertEqual(IBGBugReporting.shakingThresholdForiPhone, threshold);
164+
}
165+
166+
- (void) testgivenDouble$setShakingThresholdForiPad_whenQuery_thenShouldCallNativeApi {
167+
double threshold = 12;
168+
[self.instabugBridge setShakingThresholdForiPad:threshold];
169+
XCTAssertEqual(IBGBugReporting.shakingThresholdForiPad, threshold);
170+
}
171+
172+
- (void) testgivenExtendedBugReportMode$setExtendedBugReportMode_whenQuery_thenShouldCallNativeApi {
173+
IBGExtendedBugReportMode extendedBugReportMode = IBGExtendedBugReportModeEnabledWithOptionalFields;
174+
[self.instabugBridge setExtendedBugReportMode:extendedBugReportMode];
175+
XCTAssertEqual(IBGBugReporting.extendedBugReportMode, extendedBugReportMode);
176+
}
177+
178+
- (void) testgivenArray$setReportTypes_whenQuery_thenShouldCallNativeApi {
179+
id mock = OCMClassMock([IBGBugReporting class]);
180+
NSArray *reportTypesArr = [NSArray arrayWithObjects: @(IBGReportTypeBug), nil];
181+
IBGBugReportingReportType reportTypes = 0;
182+
for (NSNumber *boxedValue in reportTypesArr) {
183+
reportTypes |= [boxedValue intValue];
184+
}
185+
OCMStub([mock setPromptOptionsEnabledReportTypes:reportTypes]);
186+
[self.instabugBridge setReportTypes:reportTypesArr];
187+
OCMVerify([mock setPromptOptionsEnabledReportTypes:reportTypes]);
188+
}
189+
190+
191+
- (void) testgivenArgs$showBugReportingWithReportTypeAndOptions_whenQuery_thenShouldCallNativeApi {
192+
id mock = OCMClassMock([IBGBugReporting class]);
193+
IBGBugReportingReportType reportType = IBGBugReportingReportTypeBug;
194+
NSArray *options = [NSArray arrayWithObjects: @(IBGBugReportingOptionEmailFieldOptional), nil];
195+
IBGBugReportingOption parsedOptions = 0;
196+
for (NSNumber *boxedValue in options) {
197+
parsedOptions |= [boxedValue intValue];
198+
}
199+
OCMStub([mock showWithReportType:reportType options:parsedOptions]);
200+
[self.instabugBridge showBugReportingWithReportTypeAndOptions:reportType :options];
201+
202+
XCTestExpectation *expectation = [self expectationWithDescription:@"Test ME PLX"];
203+
204+
[[NSRunLoop mainRunLoop] performBlock:^{
205+
OCMVerify([mock showWithReportType:reportType options:parsedOptions]);
206+
[expectation fulfill];
207+
}];
208+
209+
[self waitForExpectationsWithTimeout:EXPECTATION_TIMEOUT handler:nil];
210+
}
211+
212+
- (void) testgivenBoolean$setAutoScreenRecordingEnabled_whenQuery_thenShouldCallNativeApi {
213+
BOOL enabled = false;
214+
[self.instabugBridge setAutoScreenRecordingEnabled:enabled];
215+
XCTAssertFalse(IBGBugReporting.autoScreenRecordingEnabled);
216+
}
217+
218+
- (void) testgivenArgs$setAutoScreenRecordingMaxDuration_whenQuery_thenShouldCallNativeApi {
219+
CGFloat duration = 12.3;
220+
[self.instabugBridge setAutoScreenRecordingMaxDuration:duration];
221+
XCTAssertEqual(IBGBugReporting.autoScreenRecordingDuration, duration);
222+
}
223+
224+
- (void) testgivenBoolean$setViewHierarchyEnabled_whenQuery_thenShouldCallNativeApi {
225+
BOOL enabled = false;
226+
[self.instabugBridge setViewHierarchyEnabled:enabled];
227+
XCTAssertFalse(IBGBugReporting.shouldCaptureViewHierarchy);
228+
}
229+
230+
34231
@end

ios/RNInstabug/InstabugReactBridge.h

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,50 @@
99
#import <Foundation/Foundation.h>
1010
#import <React/RCTBridgeModule.h>
1111
#import <React/RCTEventEmitter.h>
12+
#import <Instabug/Instabug.h>
13+
#import <Instabug/IBGBugReporting.h>
14+
#import <Instabug/IBGCrashReporting.h>
15+
#import <Instabug/IBGSurveys.h>
16+
#import <Instabug/IBGLog.h>
1217

1318
@interface InstabugReactBridge : RCTEventEmitter <RCTBridgeModule>
1419

1520
- (void)showSurveyWithToken:(NSString *)surveyToken;
1621

22+
23+
/***********Bug Reporting*****************/
24+
25+
26+
- (void)setBugReportingEnabled:(BOOL) isEnabled;
27+
28+
- (void)setInvocationEvents:(NSArray*)invocationEventsArray;
29+
30+
- (void)invoke;
31+
32+
- (void)setInvocationOptions:(NSArray*)invocationOptionsArray;
33+
34+
- (void)invokeWithInvocationModeAndOptions:(IBGInvocationMode)invocationMode options:(NSArray*)options;
35+
36+
- (void)setPreInvocationHandler:(RCTResponseSenderBlock)callBack;
37+
38+
- (void)setPostInvocationHandler:(RCTResponseSenderBlock)callBack;
39+
40+
- (void)setPromptOptionsEnabled:(BOOL)chatEnabled feedback:(BOOL)bugReportEnabled chat:(BOOL)feedbackEnabled;
41+
42+
- (void)setShakingThresholdForiPhone:(double)iPhoneShakingThreshold;
43+
44+
- (void)setShakingThresholdForiPad:(double)iPadShakingThreshold;
45+
46+
- (void)setExtendedBugReportMode:(IBGExtendedBugReportMode)extendedBugReportMode;
47+
48+
- (void)setReportTypes:(NSArray*)types;
49+
50+
- (void)showBugReportingWithReportTypeAndOptions:(IBGBugReportingReportType) type: (NSArray*) options;
51+
52+
- (void)setAutoScreenRecordingEnabled:(BOOL) enabled;
53+
54+
- (void)setAutoScreenRecordingMaxDuration:(CGFloat) duration;
55+
56+
- (void)setViewHierarchyEnabled:(BOOL) viewHierarchyEnabled;
57+
1758
@end

0 commit comments

Comments
 (0)