Skip to content

Commit

Permalink
Merge pull request #3 from adrdilauro/feature/export-test-service-to-…
Browse files Browse the repository at this point in the history
…read-chosen-version

Export test service, in order to be able to read the chosen version
  • Loading branch information
Adriano di Lauro committed Jun 1, 2018
2 parents bad3a5b + 5e02b0b commit 5523585
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 2 deletions.
1 change: 1 addition & 0 deletions public_api.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export { AbTestsModule, AbTestOptions } from './src/app/modules/angular-ab-tests/module';
export { AbTestsService } from './src/app/modules/angular-ab-tests/service';
11 changes: 9 additions & 2 deletions src/app/app.component.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Component } from '@angular/core';
import { Component, OnInit } from '@angular/core';
import { AbTestsService } from './modules/angular-ab-tests/service';

@Component({
selector: 'app-root',
Expand All @@ -18,14 +19,20 @@ import { Component } from '@angular/core';
</a>
`
})
export class AppComponent {
export class AppComponent implements OnInit {
public values = ['John Lennon', 'Ringo Starr', 'Paul McCartney', 'George Harrison'];
public randomizedIndex: number = this.getRandomIndex();

constructor(private abTestsService: AbTestsService) {}

randomizeValue() {
this.randomizedIndex = this.getRandomIndex();
}

ngOnInit() {
console.log(this.abTestsService.getVersion());
}

private getRandomIndex() {
return Math.floor(Math.random() * 100) % 4;
}
Expand Down
8 changes: 8 additions & 0 deletions src/app/modules/angular-ab-tests/classes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ export class AbTestForRealUser {
this._chosenVersion = chosenVersion;
}

getVersion(): string {
return this._chosenVersion;
}

shouldRender(versions: string[], forCrawlers: boolean): boolean {
for (let version of versions) {
if (this._versions.indexOf(version) === -1) {
Expand All @@ -28,6 +32,10 @@ export class AbTestForCrawler {
}
}

getVersion(): string {
return '';
}

shouldRender(versions: string[], forCrawlers: boolean): boolean {
return forCrawlers || (!!this._version && versions.indexOf(this._version) !== -1);
}
Expand Down
8 changes: 8 additions & 0 deletions src/app/modules/angular-ab-tests/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ export class AbTestsService {
}
}

getVersion(scope?: string): string {
let scopeOrDefault = scope || this._defaultScope;
if (!this._tests[scopeOrDefault]) {
error('Test with scope <' + scopeOrDefault + '> has not been defined');
}
return this._tests[scopeOrDefault].getVersion();
}

shouldRender(versions: string[], scope: string, forCrawlers: boolean): boolean {
let scopeOrDefault = scope || this._defaultScope;
if (!this._tests[scopeOrDefault]) {
Expand Down

0 comments on commit 5523585

Please sign in to comment.