Skip to content

Commit

Permalink
[segment] unify native initialization methods (#8046)
Browse files Browse the repository at this point in the history
* [segment] unify native initialization methods

* add handling for empty object argument

* re-mock jest expoModules

* add tests

* revert device-specific changes to mock files
  • Loading branch information
cruzach committed May 21, 2020
1 parent f879204 commit 5766614
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 42 deletions.
Expand Up @@ -112,7 +112,7 @@ public String getName() {
}

@ExpoMethod
public void initializeAndroid(final String writeKey, Promise promise) {
public void initialize(final String writeKey, Promise promise) {
Analytics.Builder builder = new Analytics.Builder(mContext, writeKey);
builder.tag(Integer.toString(sCurrentTag++));
builder.use(FirebaseIntegration.FACTORY);
Expand All @@ -121,12 +121,6 @@ public void initializeAndroid(final String writeKey, Promise promise) {
promise.resolve(null);
}

@ExpoMethod
public void initializeIOS(final String writeKey, Promise promise) {
// NO-OP. Need this here because Segment has different keys for iOS and Android.
promise.reject("E_WRONG_PLATFORM", "Method initializeIOS should not be called on Android, please file an issue on GitHub.");
}

@ExpoMethod
public void identify(final String userId, Promise promise) {
if (mClient != null) {
Expand Down
14 changes: 9 additions & 5 deletions packages/expo-analytics-segment/build/Segment.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/expo-analytics-segment/build/Segment.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 2 additions & 11 deletions packages/expo-analytics-segment/ios/EXSegment/EXSegment.m
Expand Up @@ -16,8 +16,8 @@ @implementation EXSegment

UM_EXPORT_MODULE(ExponentSegment)

UM_EXPORT_METHOD_AS(initializeIOS,
initializeIOS:(NSString *)writeKey
UM_EXPORT_METHOD_AS(initialize,
initialize:(NSString *)writeKey
resolver:(UMPromiseResolveBlock)resolve
rejecter:(UMPromiseRejectBlock)reject)
{
Expand All @@ -30,15 +30,6 @@ @implementation EXSegment
resolve(nil);
}

UM_EXPORT_METHOD_AS(initializeAndroid,
initializeAndroid:(NSString *)writeKey
resolver:(UMPromiseResolveBlock)resolve
rejecter:(UMPromiseRejectBlock)reject)
{
// NO-OP. Need this here because Segment has different keys for iOS and Android.
reject(@"E_WRONG_PLATFORM", @"Method initializeAndroid should not be called on iOS, please file an issue on GitHub.", nil);
}

UM_EXPORT_METHOD_AS(identify,
identify:(NSString *)userId
resolver:(UMPromiseResolveBlock)resolve
Expand Down
15 changes: 10 additions & 5 deletions packages/expo-analytics-segment/src/Segment.ts
Expand Up @@ -9,13 +9,18 @@ export type SegmentOptions = {
};

export function initialize(options: SegmentOptions): void {
if (Platform.OS === 'android') {
ExponentSegment.initializeAndroid(options.androidWriteKey);
} else if (Platform.OS === 'ios') {
ExponentSegment.initializeIOS(options.iosWriteKey);
} else {
if (!ExponentSegment.initialize) {
throw new UnavailabilityError('expo-analytics-segment', 'initialize');
}
const platformWriteKey = Platform.select({
ios: options.iosWriteKey,
android: options.androidWriteKey,
});
if (platformWriteKey) {
ExponentSegment.initialize(platformWriteKey);
} else {
throw new Error('You must provide a platform-specific write key to initialize Segment.');
}
}

export function identify(userId: string): void {
Expand Down
Expand Up @@ -6,9 +6,8 @@ const mockOptions = {
iosWriteKey: 'ios-write-key',
};

it(`initializes`, () => {
it(`initializes once for android`, () => {
Segment.initialize(mockOptions);

expect(ExponentSegment.initializeAndroid).toHaveBeenCalledWith(mockOptions.androidWriteKey);
expect(ExponentSegment.initializeIOS).not.toHaveBeenCalled();
expect(ExponentSegment.initialize).toHaveBeenCalledWith(mockOptions.androidWriteKey);
expect(ExponentSegment.initialize).toHaveBeenCalledTimes(1);
});
16 changes: 13 additions & 3 deletions packages/expo-analytics-segment/src/__tests__/Segment-test.ios.ts
Expand Up @@ -6,8 +6,18 @@ const mockOptions = {
iosWriteKey: 'ios-write-key',
};

it(`initializes`, () => {
it(`initializes once for ios`, () => {
Segment.initialize(mockOptions);
expect(ExponentSegment.initializeIOS).toHaveBeenCalledWith(mockOptions.iosWriteKey);
expect(ExponentSegment.initializeAndroid).not.toHaveBeenCalled();
expect(ExponentSegment.initialize).toHaveBeenCalledWith(mockOptions.iosWriteKey);
expect(ExponentSegment.initialize).toHaveBeenCalledTimes(1);
});

it(`calling with an empty object results in error`, () => {
expect(() => {
Segment.initialize({});
}).toThrowError(
new Error('You must provide a platform-specific write key to initialize Segment.')
);

expect(ExponentSegment.initialize).toHaveBeenCalledTimes(0);
});
11 changes: 5 additions & 6 deletions packages/jest-expo/src/preset/expoModules.js
Expand Up @@ -584,15 +584,14 @@ module.exports = {
{ key: 3, argumentsCount: 1, name: 'screen' },
{ key: 4, argumentsCount: 1, name: 'identify' },
{ key: 5, argumentsCount: 2, name: 'identifyWithTraits' },
{ key: 6, argumentsCount: 1, name: 'initializeAndroid' },
{ key: 7, argumentsCount: 2, name: 'trackWithProperties' },
{ key: 8, argumentsCount: 1, name: 'initializeIOS' },
{ key: 9, argumentsCount: 2, name: 'groupWithTraits' },
{ key: 6, argumentsCount: 2, name: 'trackWithProperties' },
{ key: 7, argumentsCount: 2, name: 'groupWithTraits' },
{ key: 8, argumentsCount: 1, name: 'initialize' },
{ key: 9, argumentsCount: 0, name: 'getEnabledAsync' },
{ key: 10, argumentsCount: 2, name: 'alias' },
{ key: 11, argumentsCount: 1, name: 'group' },
{ key: 12, argumentsCount: 1, name: 'track' },
{ key: 13, argumentsCount: 0, name: 'getEnabledAsync' },
{ key: 14, argumentsCount: 0, name: 'reset' },
{ key: 13, argumentsCount: 0, name: 'reset' },
],
ExponentSpeech: [
{ key: 0, argumentsCount: 3, name: 'speak' },
Expand Down

0 comments on commit 5766614

Please sign in to comment.