Skip to content

Commit

Permalink
feat: support identify user by id (#1115)
Browse files Browse the repository at this point in the history
Jira ID: MOB-13746
  • Loading branch information
abdelhamid-f-nasser authored and ahmedAlaaInstabug committed Feb 20, 2024
1 parent 120563f commit 763f0e5
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 17 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## [Unreleased](https://github.com/Instabug/Instabug-React-Native/compare/v12.6.0...dev)

### Added

- Support user identification using ID ([#1115](https://github.com/Instabug/Instabug-React-Native/pull/1115))

## [12.6.0](https://github.com/Instabug/Instabug-React-Native/compare/v12.5.0...v12.6.0) (January 14, 2024)

### Changed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -294,16 +294,22 @@ public void run() {
* Set the user identity.
* Instabug will pre-fill the user email in reports.
*
* @param userName Username.
* @param userEmail User's default email
* @param userName Username.
* @param userId User's ID
*/
@ReactMethod
public void identifyUser(final String userEmail, final String userName) {
public void identifyUser(
final String userEmail,
final String userName,
@Nullable final String userId
) {
MainThreadHandler.runOnMainThread(new Runnable() {
@Override
public void run() {
try {
Instabug.identifyUser(userName, userEmail);
// The arguments get re-ordered here to match the API signature.
Instabug.identifyUser(userName, userEmail, userId);
} catch (Exception e) {
e.printStackTrace();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,16 +200,29 @@ public void tearDown() {
}

@Test
public void givenArgs$identifyUser_whenQuery_thenShouldCallNativeApiWithArgs() {
public void testIdentifyUserWithNoId() {
// given

String email = "sali@instabug.com";
String userName = "salmaali";
String id = null;
// when
rnModule.identifyUser(email, userName);
rnModule.identifyUser(email, userName, id);
// then
verify(Instabug.class,times(1));
Instabug.identifyUser(userName, email);
mockInstabug.verify(() -> Instabug.identifyUser(userName, email, id));
}

@Test
public void testIdentifyUserWithId() {
// given

String email = "sali@instabug.com";
String userName = "salmaali";
String id = "salmaali";
// when
rnModule.identifyUser(email, userName, id);
// then
mockInstabug.verify(() -> Instabug.identifyUser(userName, email, id));
}

@Test
Expand Down
15 changes: 13 additions & 2 deletions examples/default/ios/InstabugTests/InstabugSampleTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,19 @@ - (void)testIdentifyUser {
NSString *name = @"this is my name";

OCMStub([mock identifyUserWithEmail:email name:name]);
[self.instabugBridge identifyUser:email name:name];
OCMVerify([mock identifyUserWithEmail:email name:name]);
[self.instabugBridge identifyUser:email name:name userId:nil];
OCMVerify([mock identifyUserWithID:nil email:email name:name]);
}

- (void)testIdentifyUserWithID {
id mock = OCMClassMock([Instabug class]);
NSString *email = @"em@il.com";
NSString *name = @"this is my name";
NSString *userId = @"this is my id";

OCMStub([mock identifyUserWithID:userId email:email name:name]);
[self.instabugBridge identifyUser:email name:name userId:userId];
OCMVerify([mock identifyUserWithID:userId email:email name:name]);
}

- (void)testLogOut {
Expand Down
2 changes: 1 addition & 1 deletion ios/RNInstabug/InstabugReactBridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@

- (void)setString:(NSString *)value toKey:(NSString *)key;

- (void)identifyUser:(NSString *)email name:(NSString *)name;
- (void)identifyUser:(NSString *)email name:(NSString *)name userId:(nullable NSString *)userId;

- (void)logOut;

Expand Down
4 changes: 2 additions & 2 deletions ios/RNInstabug/InstabugReactBridge.m
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,8 @@ - (dispatch_queue_t)methodQueue {
[Instabug clearFileAttachments];
}

RCT_EXPORT_METHOD(identifyUser:(NSString *)email name:(NSString *)name) {
[Instabug identifyUserWithEmail:email name:name];
RCT_EXPORT_METHOD(identifyUser:(NSString *)email name:(NSString *)name userId:(nullable NSString *)userId) {
[Instabug identifyUserWithID:userId email:email name:name];
}

RCT_EXPORT_METHOD(logOut) {
Expand Down
7 changes: 4 additions & 3 deletions src/modules/Instabug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,15 +196,16 @@ export const setString = (key: StringKey, string: string) => {
};

/**
* Sets the default value of the user's email and hides the email field from the reporting UI
* Sets the default value of the user's email and ID and hides the email field from the reporting UI
* and set the user's name to be included with all reports.
* It also reset the chats on device to that email and removes user attributes,
* user data and completed surveys.
* @param email Email address to be set as the user's email.
* @param name Name of the user to be set.
* @param [id] ID of the user to be set.
*/
export const identifyUser = (email: string, name: string) => {
NativeInstabug.identifyUser(email, name);
export const identifyUser = (email: string, name: string, id?: string) => {
NativeInstabug.identifyUser(email, name, id);
};

/**
Expand Down
2 changes: 1 addition & 1 deletion src/native/NativeInstabug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export interface InstabugNativeModule extends NativeModule {
clearLogs(): void;

// User APIs //
identifyUser(email: string, name: string): void;
identifyUser(email: string, name: string, id?: string): void;
logOut(): void;
logUserEvent(name: string): void;
setUserData(data: string): void;
Expand Down
12 changes: 11 additions & 1 deletion test/modules/Instabug.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,17 @@ describe('Instabug Module', () => {
Instabug.identifyUser(email, name);

expect(NativeInstabug.identifyUser).toBeCalledTimes(1);
expect(NativeInstabug.identifyUser).toBeCalledWith(email, name);
expect(NativeInstabug.identifyUser).toBeCalledWith(email, name, undefined);
});

it('identifyUser when id is defined should call the native method identifyUser', () => {
const email = 'foo@instabug.com';
const name = 'Instabug';
const id = 'instabug-id';
Instabug.identifyUser(email, name, id);

expect(NativeInstabug.identifyUser).toBeCalledTimes(1);
expect(NativeInstabug.identifyUser).toBeCalledWith(email, name, id);
});

it('should call the native method logOut', () => {
Expand Down

0 comments on commit 763f0e5

Please sign in to comment.