Skip to content

Commit

Permalink
Tests identify, screen, group, flush, reset (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
ladanazita authored Oct 17, 2017
1 parent b848132 commit 455841b
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 18 deletions.
2 changes: 2 additions & 0 deletions Example/Podfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@ target 'Segment-Amplitude_Tests' do
# Do not include Segment-Amplitude here due to using static lib
pod 'Specta'
pod 'Expecta'
pod 'OCMockito'

end
16 changes: 11 additions & 5 deletions Example/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,33 @@ PODS:
- Amplitude-iOS (4.0.1)
- Analytics (3.6.0)
- Expecta (1.0.5)
- Segment-Amplitude (1.4.3):
- OCHamcrest (7.0.2)
- OCMockito (5.0.1):
- OCHamcrest (~> 7.0)
- Segment-Amplitude (1.4.4):
- Amplitude-iOS (~> 4.0)
- Analytics (~> 3.6)
- Specta (1.0.6)

DEPENDENCIES:
- Expecta
- OCMockito
- Segment-Amplitude (from `../`)
- Specta

EXTERNAL SOURCES:
Segment-Amplitude:
:path: "../"
:path: ../

SPEC CHECKSUMS:
Amplitude-iOS: e511a77d2c05f3cc623a40e4323e65d12ef1c322
Analytics: 15be3e651d22cc811f44df65698538236676437b
Expecta: e1c022fcd33910b6be89c291d2775b3fe27a89fe
Segment-Amplitude: ba21fa153cca4dd8338ddd72d84f506d5109809f
OCHamcrest: 706bfbf69a3df55a873bad096014e80e2e8ca93c
OCMockito: 063837a086c058e764fcd23e771089c1759e7197
Segment-Amplitude: 985f1f4e4e4c3b5bfa28c52995e13fd9ed932e5f
Specta: f506f3a8361de16bc0dcf3b17b75e269072ba465

PODFILE CHECKSUM: 15887ab73967a9c540797944e987a5ee9cc0d7e8
PODFILE CHECKSUM: f3781d85a056f01c90fae0474309638084899ac0

COCOAPODS: 1.2.1
COCOAPODS: 1.3.1
5 changes: 4 additions & 1 deletion Example/Tests/Tests-Prefix.pch
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,8 @@

#import <Specta/Specta.h>
#import <Expecta/Expecta.h>

#import <OCMockito/OCMockito.h>
#import <Analytics/SEGAnalytics.h>
#import <Segment-Amplitude/SEGAmplitudeIntegrationFactory.h>
#import <Segment-Amplitude/SEGAmplitudeIntegration.h>
#endif
109 changes: 101 additions & 8 deletions Example/Tests/Tests.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,112 @@

SpecBegin(InitialSpecs);

describe(@"these will pass", ^{

it(@"can do maths", ^{
expect(1).beLessThan(23);
describe(@"SEGAmplitudeIntegrationFactory", ^{
it(@"factory creates integration with basic settings", ^{
SEGAmplitudeIntegration *integration = [[SEGAmplitudeIntegrationFactory instance] createWithSettings:@{
@"apiKey" : @"1234"
} forAnalytics:nil];
expect(integration.settings).to.equal(@{ @"apiKey" : @"1234" });

});

it(@"factory creates integration with trackSessionEvents", ^{
SEGAmplitudeIntegration *integration = [[SEGAmplitudeIntegrationFactory instance] createWithSettings:@{
@"apiKey" : @"1234",
@"trackSessionEvents" : @true
} forAnalytics:nil];
expect(integration.settings).to.equal(@{ @"apiKey" : @"1234",
@"trackSessionEvents" : @true });
});
});

describe(@"SEGAmplitudeIntegration", ^{

__block Amplitude *amplitude;
__block SEGAmplitudeIntegration *integration;

beforeEach(^{
amplitude = mock([Amplitude class]);
integration = [[SEGAmplitudeIntegration alloc] initWithSettings:@{} andAmplitude:amplitude];
});

describe(@"Identify", ^{

it(@"identify without traits", ^{
SEGIdentifyPayload *payload = [[SEGIdentifyPayload alloc] initWithUserId:@"1111" anonymousId:nil traits:@{} context:@{} integrations:@{}];

[integration identify:payload];
[verify(amplitude) setUserId:@"1111"];
});

it(@"identify with traits", ^{
SEGIdentifyPayload *payload = [[SEGIdentifyPayload alloc] initWithUserId:@"7891" anonymousId:nil traits:@{
@"name" : @"George Costanza",
@"gender" : @"male",
@"quality" : @"unstable",
@"age" : @47
} context:@{}
integrations:@{}];
[integration identify:payload];
[verify(amplitude) setUserProperties:payload.traits];
});

it(@"identify with groups", ^{
SEGIdentifyPayload *payload = [[SEGIdentifyPayload alloc] initWithUserId:@"7891" anonymousId:nil traits:@{
@"name" : @"Elaine Marie Benes",
@"gender" : @"female",
@"quality" : @"assertiveness",
@"age" : @36
} context:@{} integrations:@{ @"Amplitude" : @{
@"groups" : @{
@"jobs" : @[ @"Pendant Publishing" ]
}
} }];
[integration identify:payload];
[verify(amplitude) setGroup:@"jobs" groupName:@[ @"Pendant Publishing" ]];
});

});

it(@"can read", ^{
expect(@"team").toNot.contain(@"I");
describe(@"Screen", ^{
it(@"does not call screen if trackAllPages = false", ^{
integration = [[SEGAmplitudeIntegration alloc] initWithSettings:@{ @"trackAllPages" : @false } andAmplitude:amplitude];

SEGScreenPayload *payload = [[SEGScreenPayload alloc] initWithName:@"Shirts" properties:@{} context:@{} integrations:@{}];
[integration screen:payload];
[verifyCount(amplitude, never()) logEvent:@"Viewed Shirts Screen" withEventProperties:@{}];
});

it(@"calls basic screen", ^{
integration = [[SEGAmplitudeIntegration alloc] initWithSettings:@{ @"trackAllPages" : @true } andAmplitude:amplitude];

SEGScreenPayload *payload = [[SEGScreenPayload alloc] initWithName:@"Shirts" properties:@{} context:@{} integrations:@{}];
[integration screen:payload];
[verify(amplitude) logEvent:@"Viewed Shirts Screen" withEventProperties:@{}];
});

});

describe(@"Group", ^{
it(@"sets groupId", ^{
SEGGroupPayload *payload = [[SEGGroupPayload alloc] initWithGroupId:@"322" traits:@{} context:@{} integrations:@{}];
[integration group:payload];
[verify(amplitude) setGroup:@"[Segment] Group" groupName:@"322"];
});
});

describe(@"Flush", ^{
it(@"calls uploadEvents", ^{
[integration flush];
[verify(amplitude) uploadEvents];
});
});

it(@"will wait and succeed", ^{
waitUntil(^(DoneCallback done) {
done();
describe(@"Reset", ^{
it(@"calls regenerateDeviceId", ^{
[integration reset];
[verify(amplitude) regenerateDeviceId];
});
});
});
Expand Down
1 change: 1 addition & 0 deletions Pod/Classes/SEGAmplitudeIntegration.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@
@property (strong) Amplitude *amplitude;

- (id)initWithSettings:(NSDictionary *)settings;
- (id)initWithSettings:(NSDictionary *)settings andAmplitude:(Amplitude *)amplitude;

@end
16 changes: 12 additions & 4 deletions Pod/Classes/SEGAmplitudeIntegration.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,21 @@ - (id)initWithSettings:(NSDictionary *)settings
if (self = [super init]) {
self.settings = settings;
self.amplitude = [Amplitude instance];
}
return [self initWithSettings:self.settings andAmplitude:self.amplitude];
}

- (id)initWithSettings:(NSDictionary *)settings andAmplitude:(Amplitude *)amplitude
{
if (self = [super init]) {
self.settings = settings;
self.amplitude = amplitude;
NSString *apiKey = [self.settings objectForKey:@"apiKey"];
[self.amplitude initializeApiKey:apiKey];

if ([(NSNumber *)[self.settings objectForKey:@"trackSessionEvents"] boolValue]) {
[Amplitude instance].trackingSessionEvents = true;
self.amplitude.trackingSessionEvents = true;
}

NSString *apiKey = [self.settings objectForKey:@"apiKey"];
[[Amplitude instance] initializeApiKey:apiKey];
}
return self;
}
Expand Down

0 comments on commit 455841b

Please sign in to comment.