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

[ng] toh-6 comp tests, enabled on Travis; toh-5 test tweaks #567

Merged
merged 6 commits into from Apr 25, 2017
File filter...
Filter file types
Jump to file or symbol
Failed to load files and symbols.
+753 −34
Diff settings

Always

Just for now

Whitespace-only changes.
@@ -73,10 +73,7 @@ void main() {
// #enddocregion title

// #docregion hero
const windstormData = const <String, dynamic>{
'id': 1,
'name': 'Windstorm'
};
const windstormData = const <String, dynamic>{'id': 1, 'name': 'Windstorm'};

test('initial hero properties', () async {
expect(await appPO.heroId, windstormData['id']);
@@ -9,7 +9,7 @@ class Hero {
}

// #docregion hero-array
final List<Hero> mockHeroes = [
final mockHeroes = <Hero>[
new Hero(11, 'Mr. Nice'),
new Hero(12, 'Narco'),
new Hero(13, 'Bombasto'),
@@ -97,7 +97,7 @@ final List<Hero> mockHeroes = [
])
// #enddocregion styles
class AppComponent {
final String title = 'Tour of Heroes';
final title = 'Tour of Heroes';
final List<Hero> heroes = mockHeroes;
// #docregion selected-hero
Hero selectedHero;
@@ -88,11 +88,13 @@ final List<Hero> mockHeroes = [
'''
],
// #docregion directives
directives: const [HeroDetailComponent]
directives: const [
HeroDetailComponent,
]
// #enddocregion directives
)
class AppComponent {
final String title = 'Tour of Heroes';
final title = 'Tour of Heroes';
final List<Hero> heroes = mockHeroes;
Hero selectedHero;

@@ -68,6 +68,5 @@ void main() {
await po.type(newName);
expect(await po.heroFromDetails, updatedHero);
});

});
}
Whitespace-only changes.
Whitespace-only changes.
@@ -23,4 +23,3 @@ class HeroService {
// #docregion
// #docregion just-get-heroes
}

Whitespace-only changes.
Whitespace-only changes.
@@ -26,7 +26,8 @@ class HeroesComponent implements OnInit {
Hero selectedHero;

// #docregion renaming
HeroesComponent(this._heroService,
HeroesComponent(
this._heroService,
// #enddocregion renaming
this._router
// #docregion renaming
@@ -15,7 +15,7 @@ import 'app_po.dart';
NgTestFixture<AppComponent> fixture;
AppPO appPO;

final MockPlatformLocation mockPlatformLocation = new MockPlatformLocation();
final mockPlatformLocation = new MockPlatformLocation();

class MockPlatformLocation extends Mock implements PlatformLocation {}

@@ -17,7 +17,7 @@ import 'dashboard_po.dart';
NgTestFixture<DashboardComponent> fixture;
DashboardPO po;

final MockPlatformLocation mockPlatformLocation = new MockPlatformLocation();
final mockPlatformLocation = new MockPlatformLocation();

class MockPlatformLocation extends Mock implements PlatformLocation {}

@@ -26,8 +26,8 @@ void main() {
final providers = new List.from(ROUTER_PROVIDERS)
..addAll([
provide(APP_BASE_HREF, useValue: '/'),
provide(ROUTER_PRIMARY_COMPONENT, useValue: AppComponent),
provide(PlatformLocation, useValue: mockPlatformLocation),
provide(ROUTER_PRIMARY_COMPONENT, useValue: AppComponent),
HeroService,
]);
final testBed = new NgTestBed<DashboardComponent>().addProviders(providers);
@@ -23,10 +23,12 @@ final mockPlatformLocation = new MockPlatformLocation();
@AngularEntrypoint()
void main() {
final baseProviders = new List.from(ROUTER_PROVIDERS)
..add(provide(APP_BASE_HREF, useValue: '/'))
..add(HeroService)
..add(provide(PlatformLocation, useValue: mockPlatformLocation))
..add(provide(RouteParams, useValue: new RouteParams({})));
..addAll([
provide(APP_BASE_HREF, useValue: '/'),
provide(PlatformLocation, useValue: mockPlatformLocation),
provide(RouteParams, useValue: new RouteParams({})),
HeroService,
]);
final testBed =
new NgTestBed<HeroDetailComponent>().addProviders(baseProviders);

@@ -43,7 +43,6 @@ void basicTests() {
});

test('hero count', () async {
await fixture.update();
expect(po.heroes.length, 10);
});

@@ -71,10 +70,12 @@ void selectedHeroTests() {

test('go to detail', () async {
await po.gotoDetail();
verify(mockRouter.navigate([
final c = verify(mockRouter.navigate(captureAny));
final linkParams = [
'HeroDetail',
{'id': '${targetHero['id']}'}
]));
];
expect(c.captured.single, linkParams);
});

test('select another hero', () async {
@@ -22,7 +22,7 @@ class HeroesPO {

@ByTagName('button')
@optional
PageLoaderElement _button;
PageLoaderElement _gotoDetail;

Future<String> get title => _title.visibleText;

@@ -42,7 +42,7 @@ class HeroesPO {
return matches[1];
}

Future<Null> gotoDetail() => _button.click();
Future<Null> gotoDetail() => _gotoDetail.click();

Map<String, dynamic> _heroDataFromLi(String liText) {
final matches = new RegExp((r'^(\d+) (.*)$')).firstMatch(liText);
Whitespace-only changes.
@@ -12,8 +12,7 @@ import 'hero_service.dart';
@Component(
selector: 'my-hero-detail',
templateUrl: 'hero_detail_component.html',
styleUrls: const ['hero_detail_component.css']
)
styleUrls: const ['hero_detail_component.css'])
class HeroDetailComponent implements OnInit {
Hero hero;
final HeroService _heroService;
@@ -1,7 +1,9 @@
<!-- #docregion -->
<div id="search-component">
<h4>Hero Search</h4>
<input #searchBox id="search-box" (keyup)="search(searchBox.value)" />
<input #searchBox id="search-box"
(change)="search(searchBox.value)"
(keyup)="search(searchBox.value)" />
<div>
<div *ngFor="let hero of heroes | async"
(click)="gotoDetail(hero)" class="search-result" >
@@ -1,3 +1,5 @@
// Note: MockClient constructor API forces all InMemoryDataService members to
// be static.
// #docregion , init
import 'dart:async';
import 'dart:convert';
@@ -18,22 +20,24 @@ class InMemoryDataService extends MockClient {
{'id': 14, 'name': 'Celeritas'},
{'id': 15, 'name': 'Magneta'},
{'id': 16, 'name': 'RubberMan'},
{'id': 17, 'name': 'Dynama2'},
{'id': 17, 'name': 'Dynama'},
{'id': 18, 'name': 'Dr IQ'},
{'id': 19, 'name': 'Magma'},
{'id': 20, 'name': 'Tornado'}
];
static final List<Hero> _heroesDb =
_initialHeroes.map((json) => new Hero.fromJson(json)).toList();
static int _nextId = _heroesDb.map((hero) => hero.id).fold(0, max) + 1;
static List<Hero> _heroesDb;
static int _nextId;

static Future<Response> _handler(Request request) async {
if (_heroesDb == null) resetDb();
var data;
switch (request.method) {
case 'GET':
final id = int.parse(request.url.pathSegments.last, onError: (_) => null);
final id =
int.parse(request.url.pathSegments.last, onError: (_) => null);
if (id != null) {
data = _heroesDb.firstWhere((hero) => hero.id == id); // throws if no match
data = _heroesDb
.firstWhere((hero) => hero.id == id); // throws if no match
} else {
String prefix = request.url.queryParameters['name'] ?? '';
final regExp = new RegExp(prefix, caseSensitive: false);
@@ -66,5 +70,13 @@ class InMemoryDataService extends MockClient {
headers: {'content-type': 'application/json'});
}

static resetDb() {
_heroesDb = _initialHeroes.map((json) => new Hero.fromJson(json)).toList();
_nextId = _heroesDb.map((hero) => hero.id).fold(0, max) + 1;
}

static String lookUpName(int id) =>
_heroesDb.firstWhere((hero) => hero.id == id, orElse: null)?.name;

InMemoryDataService() : super(_handler);
}
@@ -12,8 +12,11 @@ dependencies:
stream_transformers: ^0.3.0
# #enddocregion additions
dev_dependencies:
angular_test: ^1.0.0-beta
browser: ^0.10.0
dart_to_js_script_rewriter: ^1.0.1
mockito: ^2.0.2
test: ^0.12.0
# #docregion additions
transformers:
- angular2:
@@ -27,4 +30,8 @@ transformers:
resolved_identifiers:
BrowserClient: 'package:http/browser_client.dart'
Client: 'package:http/http.dart'
- angular2/transform/reflection_remover:
$include: test/**_test.dart
- test/pub_serve:
$include: test/**_test.dart
- dart_to_js_script_rewriter
@@ -0,0 +1,6 @@
Changes/additions relative to toh-5:

- Test bed: add provider for `Client`.
- Reset (mock) DB during setup.
- Test waiting for element update before proceeding.
- Test interaction with (mock) db backend.
@@ -0,0 +1,20 @@
// #docregion
@Tags(const ['aot'])
@TestOn('browser')
library heroes_test;

import 'package:angular2/angular2.dart';
import 'package:test/test.dart';

import 'dashboard.dart' as dashboard;
import 'heroes.dart' as heroes;
import 'hero_detail.dart' as hero_detail;
import 'hero_search.dart' as hero_search;

@AngularEntrypoint()
void main() {
group('dashboard:', dashboard.main);
group('heroes:', heroes.main);
group('hero detail:', hero_detail.main);
group('hero search:', hero_search.main);
}
@@ -0,0 +1,18 @@
// #docregion
import 'dart:async';

import 'package:pageloader/objects.dart';
import 'utils.dart';

class AppPO {
@ByTagName('h1')
PageLoaderElement _h1;

@ByCss('nav a')
List<PageLoaderElement> _tabLinks;

Future<String> get pageTitle => _h1.visibleText;

Future<List<String>> get tabTitles =>
inIndexOrder(_tabLinks.map((el) => el.visibleText)).toList();
}
@@ -0,0 +1,56 @@
@Skip('AppComponent tests need bootstrap equivalent for the Router init')
@Tags(const ['aot'])
@TestOn('browser')

import 'package:angular2/angular2.dart';
import 'package:angular2/platform/common.dart';
import 'package:angular2/router.dart';
import 'package:angular_test/angular_test.dart';
import 'package:angular_tour_of_heroes/app_component.dart';
import 'package:mockito/mockito.dart';
import 'package:test/test.dart';

import 'app_po.dart';

NgTestFixture<AppComponent> fixture;
AppPO appPO;

final mockPlatformLocation = new MockPlatformLocation();

class MockPlatformLocation extends Mock implements PlatformLocation {}

@AngularEntrypoint()
void main() {
final providers = [
provide(APP_BASE_HREF, useValue: '/'),
provide(ROUTER_PRIMARY_COMPONENT, useValue: AppComponent),
provide(PlatformLocation, useValue: mockPlatformLocation),
];

final testBed = new NgTestBed<AppComponent>().addProviders(providers);

setUpAll(() async {
// Seems like we'd need to do something equivalent to:
// bootstrap(AppComponent);
});

setUp(() async {
fixture = await testBed.create();
appPO = await fixture.resolvePageObject(AppPO);
});

tearDown(disposeAnyRunningTest);

group('Basics:', basicTests);
}

void basicTests() {
test('page title', () async {
expect(await appPO.pageTitle, 'Tour of Heroes');
});

test('tab titles', () async {
final expectTitles = ['Dashboard', 'Heroes'];
expect(await appPO.tabTitles, expectTitles);
});
}
Oops, something went wrong.
ProTip! Use n and p to navigate between commits in a pull request.