Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Call react native observer with initial user/context values #768

Merged
merged 1 commit into from
Jul 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Bugsnag/Client/BugsnagClient.m
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,11 @@ - (void)addObserverWithBlock:(BugsnagObserverBlock _Nonnull)observer {
// sync the new observer with changes to metadata so far
BugsnagStateEvent *event = [[BugsnagStateEvent alloc] initWithName:kStateEventMetadata data:self.metadata];
observer(event);

NSDictionary *userJson = [self.user toJson];
observer([[BugsnagStateEvent alloc] initWithName:kStateEventUser data:userJson]);

observer([[BugsnagStateEvent alloc] initWithName:kStateEventContext data:self.context]);
}

- (void)removeObserverWithBlock:(BugsnagObserverBlock _Nonnull)observer {
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ Changelog
## TBD

### Bug fixes

* Call react native observer with initial user/context values
[#768](https://github.com/bugsnag/bugsnag-cocoa/pull/768)

* Respect bundle version set from config
[#762](https://github.com/bugsnag/bugsnag-cocoa/pull/762)

Expand Down
33 changes: 33 additions & 0 deletions Tests/BugsnagStateEventTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ - (void)addObserverWithBlock:(BugsnagObserverBlock _Nonnull)observer;
- (void)removeObserverWithBlock:(BugsnagObserverBlock _Nonnull)observer;
@end

@interface BugsnagMetadata ()
- (NSDictionary *)toDictionary;
@end

@interface BugsnagStateEventTest : XCTestCase
@property BugsnagClient *client;
@property BugsnagStateEvent *event;
Expand Down Expand Up @@ -75,4 +79,33 @@ - (void)testRemoveObserver {
XCTAssertNil(self.event);
}

- (void)testAddObserverTriggersCallback {
[self.client setUser:@"123" withEmail:@"test@example.com" andName:@"Jamie"];
[self.client setContext:@"Foo"];
[self.client addMetadata:@"Bar" withKey:@"Foo" toSection:@"test"];

__block NSDictionary *user;
__block NSString *context;
__block BugsnagMetadata *metadata;

self.block = ^(BugsnagStateEvent *event) {
if ([kStateEventContext isEqualToString:event.type]) {
context = event.data;
} else if ([kStateEventUser isEqualToString:event.type]) {
user = event.data;
} else if ([kStateEventMetadata isEqualToString:event.type]) {
metadata = event.data;
}
};
XCTAssertNil(user);
XCTAssertNil(context);
XCTAssertNil(metadata);
[self.client addObserverWithBlock:self.block];

NSDictionary *expectedUser = @{@"id": @"123", @"email": @"test@example.com", @"name": @"Jamie"};
XCTAssertEqualObjects(expectedUser, user);
XCTAssertEqualObjects(@"Foo", context);
XCTAssertEqualObjects(self.client.metadata, metadata);
}

@end