Skip to content

Commit 6c1151d

Browse files
committed
add broken tests to chapter 5
1 parent cfebab3 commit 6c1151d

4 files changed

Lines changed: 123 additions & 17 deletions

File tree

Chapter_05/karma.conf.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
module.exports = function(config) {
2+
config.set({
3+
basePath: '.',
4+
frameworks: ['dart-unittest'],
5+
6+
// list of files / patterns to load in the browser
7+
files: [
8+
'main_test.dart',
9+
{pattern: '**/*.dart', watched: false, included: false, served: true},
10+
{pattern: 'packages/browser/dart.js', watched: false, included: true, served: true},
11+
{pattern: 'packages/browser/interop.js', watched: false, included: true, served: true},
12+
],
13+
14+
autoWatch: false,
15+
16+
plugins: [
17+
'karma-dart',
18+
'karma-chrome-launcher'
19+
]
20+
});
21+
};

Chapter_05/main.dart

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -80,19 +80,21 @@ kitchen and took the recipe book with him!""";
8080
}
8181
}
8282

83-
// TODO - Remove the Profiler type. It's only needed to get rid of Misko's spam
84-
main() {
85-
var module = new AngularModule()
86-
..type(RecipeBookController)
87-
..type(RatingComponent)
88-
..type(SearchRecipeComponent)
89-
..type(ViewRecipeComponent)
90-
..type(CategoryFilter)
91-
..type(QueryService)
92-
..type(RouteInitializer, implementedBy: RecipeBookRouteInitializer)
93-
..factory(NgRoutingUsePushState,
94-
(_) => new NgRoutingUsePushState.value(false))
95-
..type(Profiler, implementedBy: Profiler);
83+
class MyAppModule extends Module {
84+
MyAppModule() {
85+
type(RecipeBookController);
86+
type(RatingComponent);
87+
type(CategoryFilter);
88+
type(Profiler, implementedBy: Profiler); // comment out to enable profiling
89+
type(SearchRecipeComponent);
90+
type(ViewRecipeComponent);
91+
type(QueryService);
92+
type(RouteInitializer, implementedBy: RecipeBookRouteInitializer);
93+
factory(NgRoutingUsePushState,
94+
(_) => new NgRoutingUsePushState.value(false));
95+
}
96+
}
9697

97-
ngBootstrap(module: module);
98+
main() {
99+
ngBootstrap(module: new MyAppModule());
98100
}

Chapter_05/main_test.dart

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
library main_test;
2+
3+
import 'package:unittest/unittest.dart';
4+
import 'package:di/di.dart';
5+
import 'package:di/dynamic_injector.dart';
6+
import 'package:angular/angular.dart';
7+
import 'package:angular/mock/module.dart';
8+
9+
import 'main.dart';
10+
11+
main() {
12+
setUp(() {
13+
setUpInjector();
14+
module((Module m) => m.install(new MyAppModule()));
15+
});
16+
tearDown(tearDownInjector);
17+
18+
group('recipe-book', () {
19+
test('should load recipes', async(inject((Injector injector,
20+
MockHttpBackend backend) {
21+
backend.expectGET('recipes.json').respond('[{"name": "test1"}]');
22+
backend.expectGET('categories.json').respond('["c1"]');
23+
24+
var recipesController = injector.get(RecipeBookController);
25+
expect(recipesController.recipes, isEmpty);
26+
27+
microLeap();
28+
backend.flush();
29+
microLeap();
30+
31+
expect(recipesController.recipes, isNot(isEmpty));
32+
})));
33+
34+
test('should select recipe', async(inject((Injector injector,
35+
MockHttpBackend backend) {
36+
backend.expectGET('recipes.json').respond('[{"name": "test1"}]');
37+
backend.expectGET('categories.json').respond('["c1"]');
38+
39+
var recipesController = injector.get(RecipeBookController);
40+
expect(recipesController.recipes, isEmpty);
41+
42+
microLeap();
43+
backend.flush();
44+
microLeap();
45+
46+
var recipe = recipesController.recipes[0];
47+
recipesController.selectRecipe(recipe);
48+
expect(recipesController.selectedRecipe, same(recipe));
49+
})));
50+
});
51+
52+
group('rating component', () {
53+
test('should show the right number of stars', inject((RatingComponent rating) {
54+
rating.maxRating = '5';
55+
expect(rating.stars, equals([1, 2, 3, 4, 5]));
56+
}));
57+
58+
test('should handle click', inject((RatingComponent rating) {
59+
rating.maxRating = '5';
60+
rating.handleClick(3);
61+
expect(rating.rating, equals(3));
62+
63+
rating.handleClick(1);
64+
expect(rating.rating, equals(1));
65+
66+
rating.handleClick(1);
67+
expect(rating.rating, equals(0));
68+
69+
rating.handleClick(1);
70+
expect(rating.rating, equals(1));
71+
}));
72+
});
73+
74+
group('categoryFilter', () {
75+
test('should return subset', inject((CategoryFilter filter) {
76+
var r1 = new Recipe(null, 'C1', null, null, null);
77+
var r2 = new Recipe(null, 'C2', null, null, null);
78+
var list = [r1, r2];
79+
var map = {"C1": false, "C2": true};
80+
expect(filter(list, map), equals([r2]));
81+
}));
82+
});
83+
}

Chapter_05/service/query_service.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
part of recipe_book;
22

33
class QueryService {
4-
String _recipesUrl = '/angular.dart.tutorial/Chapter_05/recipes.json';
5-
String _categoriesUrl = '/angular.dart.tutorial/Chapter_05/categories.json';
4+
String _recipesUrl = 'recipes.json';
5+
String _categoriesUrl = 'categories.json';
66

77
Future _loaded;
88

@@ -68,4 +68,4 @@ class QueryService {
6868
}
6969
return new Future.value(_categoriesCache);
7070
}
71-
}
71+
}

0 commit comments

Comments
 (0)