@@ -4,12 +4,14 @@ import 'dart:html' hide Animation;
44
55import 'package:angular/angular.dart' ;
66import 'package:angular/mock/module.dart' ;
7- import 'package:unittest/unittest.dart' as unit;
87
9- import 'jasmine_syntax .dart' as jasmine_syntax ;
8+ import 'package:guinness/guinness_html .dart' as gns ;
109
1110export 'dart:html' hide Animation;
12- export 'package:unittest/unittest.dart' ;
11+
12+ export 'package:unittest/unittest.dart' hide expect;
13+ export 'package:guinness/guinness_html.dart' ;
14+
1315export 'package:mock/mock.dart' ;
1416export 'package:di/di.dart' ;
1517export 'package:di/dynamic_injector.dart' ;
@@ -37,194 +39,42 @@ es(String html) {
3739
3840e (String html) => es (html).first;
3941
40- Expect expect (actual, [unit.Matcher matcher = null ]) {
41- if (matcher != null ) unit.expect (actual, matcher);
42- return new Expect (actual);
43- }
44-
45- class Expect {
46- var actual;
47- NotExpect not;
48-
49- Expect (this .actual) {
50- not = new NotExpect (this );
51- }
5242
53- toEqual (expected) => unit.expect (actual, unit.equals (expected));
54- toContain (expected) => unit.expect (actual, unit.contains (expected));
55- toBe (expected) => unit.expect (actual,
56- unit.predicate ((actual) => identical (expected, actual), '$expected ' ));
57- toThrow ([exception]) => unit.expect (actual, exception == null ?
58- unit.throws:
59- unit.throwsA (new ExceptionContains (exception)));
60- toBeFalsy () => unit.expect (actual, _isFalsy,
61- reason: '"$actual " is not Falsy' );
62- toBeTruthy () => unit.expect (actual, (v) => ! _isFalsy (v),
63- reason: '"$actual " is not Truthy' );
64- toBeDefined () => unit.expect (actual, unit.isNotNull);
65- toBeNull () => unit.expect (actual, unit.isNull);
66- toBeNotNull () => unit.expect (actual, unit.isNotNull);
67-
68- toHaveHtml (expected) => unit.expect (_toHtml (actual), unit.equals (expected));
69- toHaveText (expected) =>
70- unit.expect (_elementText (actual), unit.equals (expected));
71-
72- toHaveBeenCalled () =>
73- unit.expect (actual.called, true , reason: 'method not called' );
74- toHaveBeenCalledOnce () => unit.expect (actual.count, 1 ,
75- reason: 'method invoked ${actual .count } expected once' );
76- toHaveBeenCalledWith ([a,b,c,d,e,f]) =>
77- unit.expect (actual.firstArgsMatch (a,b,c,d,e,f), true ,
78- reason: 'method invoked with correct arguments' );
79- toHaveBeenCalledOnceWith ([a,b,c,d,e,f]) =>
80- unit.expect (actual.count == 1 && actual.firstArgsMatch (a,b,c,d,e,f),
81- true ,
82- reason: 'method invoked once with correct arguments. '
83- '(Called ${actual .count } times)' );
84-
85- toHaveClass (cls) => unit.expect (actual.classes.contains (cls), true ,
86- reason: ' Expected ${actual } to have css class ${cls }' );
87-
88- toHaveAttribute (name, [value = null ]) {
89- unit.expect (actual.attributes.containsKey (name), true ,
90- reason: 'Epxected $actual to have attribute $name ' );
91- if (value != null ) {
92- unit.expect (actual.attributes[name], value,
93- reason: 'Epxected $actual attribute "$name " to be "$value "' );
94- }
43+ Expect expect (actual, [matcher]) {
44+ final expect = new Expect (actual);
45+ if (matcher != null ) {
46+ expect.to (matcher);
9547 }
48+ return expect;
49+ }
9650
97- toEqualSelect (options) {
98- var actualOptions = [] ;
51+ class Expect extends gns. Expect {
52+ Expect (actual) : super (actual) ;
9953
100- for (var option in actual.querySelectorAll ('option' )) {
101- actualOptions.add (option.selected ? [option.value] : option.value);
102- }
103- return unit.expect (actualOptions, options);
104- }
54+ NotExpect get not => new NotExpect (actual);
10555
106- toBeValid () => unit. expect (actual.valid && ! actual.invalid, true ,
56+ toBeValid () => _expect (actual.valid && ! actual.invalid, true ,
10757 reason: 'Form is not valid' );
108- toBePristine () =>
109- unit.expect (actual.pristine && ! actual.dirty, true ,
110- reason: 'Form is dirty' );
111-
112- _isFalsy (v) => v == null ? true : v is bool ? v == false : false ;
113-
114- _toHtml (node, [bool outer = false ]) {
115- if (node is Comment ) {
116- return '<!--${node .text }-->' ;
117- } else if (node is DocumentFragment ) {
118- var acc = '' ;
119- node.childNodes.forEach ((n) { acc += _toHtml (n, true ); });
120- return acc;
121- } else if (node is List ) {
122- var acc = '' ;
123- node.forEach ((n) { acc += _toHtml (n); });
124- return acc;
125- } else if (node is Element ) {
126- // Remove all the "ng-binding" internal classes
127- node = node.clone (true ) as Element ;
128- node.classes.remove ('ng-binding' );
129- node.querySelectorAll (".ng-binding" ).forEach ((Element e) {
130- e.classes.remove ('ng-binding' );
131- });
132- var htmlString = outer ? node.outerHtml : node.innerHtml;
133- // Strip out empty class attributes. This seems like a Dart bug...
134- return htmlString.replaceAll (' class=""' , '' ).trim ();
135- } else if (node is Text ) {
136- return node.text;
137- } else {
138- throw "JQuery._toHtml not implemented for node type [${node .nodeType }]" ;
139- }
140- }
14158
142- _elementText (n, [bool notShadow = false ]) {
143- if (n is Iterable ) {
144- return n.map ((nn) => _elementText (nn)).join ("" );
145- }
146-
147- if (n is Comment ) return '' ;
148-
149- if (! notShadow && n is Element && n.shadowRoot != null ) {
150- var cShadows = n.shadowRoot.nodes.map ((n) => n.clone (true )).toList ();
151- for (var i = 0 , ii = cShadows.length; i < ii; i++ ) {
152- var n = cShadows[i];
153- if (n is Element ) {
154- var updateElement = (e) {
155- var text = new Text ('SHADOW-CONTENT' );
156- if (e.parent == null ) {
157- cShadows[i] = text;
158- } else {
159- e.parent.insertBefore (text, e);
160- }
161- e.nodes = [];
162- };
163- if (n is ContentElement ) { updateElement (n); }
164- n.querySelectorAll ('content' ).forEach (updateElement);
165- }
166- };
167- var shadowText = _elementText (cShadows, true );
168- var domText = _elementText (n, true );
169-
170- return shadowText.replaceFirst ("SHADOW-CONTENT" , domText);
171- }
172-
173- if (n.nodes == null || n.nodes.length == 0 ) return n.text;
174-
175- return n.nodes.map ((cn) => _elementText (cn)).join ("" );
176- }
177- }
59+ toBePristine () => _expect (actual.pristine && ! actual.dirty, true ,
60+ reason: 'Form is dirty' );
17861
179- class NotExpect {
180- Expect _expect;
181- get actual => _expect.actual;
182-
183- NotExpect (this ._expect);
184-
185- toHaveBeenCalled () =>
186- unit.expect (actual.called, false , reason: 'method called' );
187- toThrow () => actual ();
188-
189- toHaveClass (cls) => unit.expect (actual.classes.contains (cls), false ,
190- reason: ' Expected ${actual } to not have css class ${cls }' );
191- toHaveAttribute (name) => unit.expect (actual.attributes.containsKey (name),
192- false , reason: ' Expected $actual to not have attribute "$name "' );
193- toBe (expected) => unit.expect (actual,
194- unit.predicate ((actual) => ! identical (expected, actual), 'not $expected ' ));
195- toEqual (expected) => unit.expect (actual,
196- unit.predicate ((actual) => expected != actual, 'not $expected ' ));
197- toContain (expected) => unit.expect (actual,
198- unit.predicate ((actual) => ! actual.contains (expected), 'not $expected ' ));
199- toBePristine () => unit.expect (actual.pristine && ! actual.dirty, false ,
200- reason: 'Form is pristine' );
201- toBeValid () => unit.expect (actual.valid && ! actual.invalid, false ,
202- reason: 'Form is valid' );
62+ get _expect => gns.guinness.matchers.expect;
20363}
20464
205- class ExceptionContains extends unit.Matcher {
206-
207- final _expected;
208-
209- const ExceptionContains (this ._expected);
65+ class NotExpect extends gns.NotExpect {
66+ NotExpect (actual) : super (actual);
21067
211- bool matches (item, Map matchState) {
212- if (item is String ) {
213- return item.indexOf (_expected) >= 0 ;
214- }
215- return matches ('$item ' , matchState);
216- }
68+ toBeValid () => _expect (actual.valid && ! actual.invalid, false ,
69+ reason: 'Form is valid' );
21770
218- unit. Description describe (unit. Description description ) =>
219- description. add ( 'exception contains ' ). addDescriptionOf (_expected );
71+ toBePristine ( ) => _expect (actual.pristine && ! actual.dirty, false ,
72+ reason : 'Form is pristine' );
22073
221- unit.Description describeMismatch (item, unit.Description mismatchDescription,
222- Map matchState, bool verbose) {
223- return super .describeMismatch ('$item ' , mismatchDescription, matchState,
224- verbose);
225- }
74+ get _expect => gns.guinness.matchers.expect;
22675}
22776
77+
22878_injectify (fn) {
22979 // The function does two things:
23080 // First: if the it() passed a function, we wrap it in
@@ -236,20 +86,29 @@ _injectify(fn) {
23686 return fn.outer (inject (fn.inner));
23787}
23888
239- // Jasmine syntax
240- beforeEachModule (fn) => jasmine_syntax.beforeEach (module (fn), priority: 1 );
241- beforeEach (fn) => jasmine_syntax.beforeEach (_injectify (fn));
242- afterEach (fn) => jasmine_syntax.afterEach (_injectify (fn));
243- it (name, fn) => jasmine_syntax.it (name, _injectify (fn));
244- iit (name, fn) => jasmine_syntax.iit (name, _injectify (fn));
245- xit (name, fn) => jasmine_syntax.xit (name, fn);
246- xdescribe (name, fn) => jasmine_syntax.xdescribe (name, fn);
247- ddescribe (name, fn) => jasmine_syntax.ddescribe (name, fn);
248- describe (name, fn) => jasmine_syntax.describe (name, fn);
249-
250- var jasmine = jasmine_syntax.jasmine;
89+ // Replace guinness syntax elements to inject dependencies.
90+ beforeEachModule (fn) => gns.beforeEach (module (fn), priority: 1 );
91+ beforeEach (fn) => gns.beforeEach (_injectify (fn));
92+ afterEach (fn) => gns.afterEach (_injectify (fn));
93+ it (name, fn) => gns.it (name, _injectify (fn));
94+ iit (name, fn) => gns.iit (name, _injectify (fn));
95+
96+ _removeNgBinding (node) {
97+ if (node is Element ) {
98+ node = node.clone (true ) as Element ;
99+ node.classes.remove ('ng-binding' );
100+ node.querySelectorAll (".ng-binding" ).forEach ((Element e) {
101+ e.classes.remove ('ng-binding' );
102+ });
103+ return node;
104+ }
105+ return node;
106+ }
251107
252108main () {
253- jasmine_syntax.beforeEach (setUpInjector, priority: 3 );
254- jasmine_syntax.afterEach (tearDownInjector);
109+ gns.beforeEach (setUpInjector, priority: 3 );
110+ gns.afterEach (tearDownInjector);
111+
112+ gns.guinnessEnableHtmlMatchers ();
113+ gns.guinness.matchers.config.preprocessHtml = _removeNgBinding;
255114}
0 commit comments