Last active
August 29, 2015 14:07
-
-
Save kkvlk/21f8f3f5fe7eeef9139c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- test/ui/loader/_simple.html --> | |
<loader>Hello from loader!</loader> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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