Skip to content

Instantly share code, notes, and snippets.

@ronlobo
Forked from kkvlk/fancy_test_context.dart
Last active August 29, 2015 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ronlobo/4e374be3afb83d654439 to your computer and use it in GitHub Desktop.
Save ronlobo/4e374be3afb83d654439 to your computer and use it in GitHub Desktop.
// test/fancy_test_context.dart
library fancy_test_context;
import 'dart:html';
import 'package:angular/angular.dart';
import 'package:angular/mock/module.dart';
/**
* Global test context.
*/
_TestContext ctx;
/**
* Fancy testing context with all helpers for angular components.
*/
class _TestContext {
/**
* Injected test bed.
*/
TestBed tb;
/**
* Injected scope.
*/
Scope scope;
// Root directory of target test suite.
//
// Note! No trailing slash!
String _testRoot;
// Root directory of tested templates. Template paths will be all
// combined out of this root path and template file name.
//
// Note! No trailing slash!
String _templatesRoot;
// List of templates to preload.
List<String> _templates;
// Injected angular's template cache.
TemplateCache _templateCache;
_TestContext(this._testRoot, this._templatesRoot, this._templates) {
inject((TestBed _tb, Scope _scope, TemplateCache _cache) {
tb = _tb;
scope = _scope;
_templateCache = _cache;
loadTemplates(_templates, root: _templatesRoot);
});
}
/**
* Compiles given [html] string to DOM element.
*/
Element compileMarkup(String html) {
Element el = tb.compile(html, scope: scope);
microLeap();
tb.rootScope.apply();
return el;
}
/**
* Compiles example file with given [name] to DOM element. Examples
* are loaded from given file in test root directory.
*/
Element compileExample(String name) {
var path = "base/$_testRoot/$name";
loadTemplates([path]);
return compileMarkup(_templateCache.get(path).responseText);
}
/**
* Loads given list of [templates[. You can specify [root] path to not repeat
* yourself too much.
*/
void loadTemplates(List<String> templates, { String root: null}) {
templates.forEach((path) {
if (root != null) {
path = "$root/$path";
}
HttpRequest req = new HttpRequest();
req.open("GET", path, async: false);
req.send();
_templateCache.put(path, new HttpResponse(200, req.responseText));
});
}
}
/**
* Configures global testing context.
*/
setUpContext({String testRoot, String templatesRoot, List<String> templates, Function moduleFn}) {
setUpInjector();
module(moduleFn);
ctx = new _TestContext(testRoot, templatesRoot, templates);
}
/**
* Destroys current global context.
*/
tearDownContext() {
tearDownInjector();
ctx = null;
}
<!-- test/ui/loader/_simple.html -->
<loader>Hello from loader!</loader>
// test/ui/loader/loader_test.dart
library loader_test;
import 'dart:html';
import 'package:guinness/guinness_html.dart';
import 'package:angular/mock/module.dart';
import 'package:angular/angular.dart';
import '../../fancy_test_context.dart';
import 'package:hello/ui/loader/loader.dart';
main() {
guinnessEnableHtmlMatchers();
describe("ui", () {
describe("LoaderComponent", () {
beforeEach(() {
setUpContext(
testRoot: "test/ui/loader",
templatesRoot: "packages/hello/ui/loader",
templates: ["loader.html"], // => will resolve to "packages/hello/ui/loader/loader.html"
moduleFn: (Module m) => m..bind(LoaderComponent)
);
});
afterEach(() {
tearDownContext();
});
it("can be created", async(() {
Element loader = ctx.compileExample('_simple.html');
expect(loader).toBeNotNull();
expect(loader.shadowRoot).toHaveText("Hello from loader!");
}));
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment