test(NgClass): remove code duplication in tests

This commit is contained in:
Pawel Kozlowski 2015-08-07 17:21:57 +02:00
parent 2150a8f9d1
commit 218b037d98
1 changed files with 41 additions and 108 deletions

View File

@ -18,6 +18,11 @@ import {Component, View, NgFor, bind} from 'angular2/angular2';
import {NgClass} from 'angular2/src/directives/ng_class'; import {NgClass} from 'angular2/src/directives/ng_class';
import {APP_VIEW_POOL_CAPACITY} from 'angular2/src/core/compiler/view_pool'; import {APP_VIEW_POOL_CAPACITY} from 'angular2/src/core/compiler/view_pool';
function detectChangesAndCheck(rootTC, classes: string, elIndex: number = 0) {
rootTC.detectChanges();
expect(rootTC.componentViewChildren[elIndex].nativeElement.className).toEqual(classes);
}
export function main() { export function main() {
describe('binding to CSS class list', () => { describe('binding to CSS class list', () => {
@ -33,9 +38,8 @@ export function main() {
rootTC.componentInstance.items = [['0']]; rootTC.componentInstance.items = [['0']];
rootTC.detectChanges(); rootTC.detectChanges();
rootTC.componentInstance.items = [['1']]; rootTC.componentInstance.items = [['1']];
rootTC.detectChanges();
expect(rootTC.componentViewChildren[1].nativeElement.className) detectChangesAndCheck(rootTC, 'ng-binding 1', 1);
.toEqual('ng-binding 1');
async.done(); async.done();
}); });
@ -52,10 +56,7 @@ export function main() {
tcb.overrideTemplate(TestComponent, template) tcb.overrideTemplate(TestComponent, template)
.createAsync(TestComponent) .createAsync(TestComponent)
.then((rootTC) => { .then((rootTC) => {
rootTC.detectChanges(); detectChangesAndCheck(rootTC, 'ng-binding foo');
expect(rootTC.componentViewChildren[0].nativeElement.className)
.toEqual('ng-binding foo');
async.done(); async.done();
}); });
})); }));
@ -68,10 +69,7 @@ export function main() {
tcb.overrideTemplate(TestComponent, template) tcb.overrideTemplate(TestComponent, template)
.createAsync(TestComponent) .createAsync(TestComponent)
.then((rootTC) => { .then((rootTC) => {
rootTC.detectChanges(); detectChangesAndCheck(rootTC, 'ng-binding foo-bar fooBar');
expect(rootTC.componentViewChildren[0].nativeElement.className)
.toEqual('ng-binding foo-bar fooBar');
async.done(); async.done();
}); });
})); }));
@ -83,14 +81,10 @@ export function main() {
tcb.overrideTemplate(TestComponent, template) tcb.overrideTemplate(TestComponent, template)
.createAsync(TestComponent) .createAsync(TestComponent)
.then((rootTC) => { .then((rootTC) => {
rootTC.detectChanges(); detectChangesAndCheck(rootTC, 'ng-binding foo');
expect(rootTC.componentViewChildren[0].nativeElement.className)
.toEqual('ng-binding foo');
rootTC.componentInstance.condition = false; rootTC.componentInstance.condition = false;
rootTC.detectChanges(); detectChangesAndCheck(rootTC, 'ng-binding bar');
expect(rootTC.componentViewChildren[0].nativeElement.className)
.toEqual('ng-binding bar');
async.done(); async.done();
}); });
@ -103,24 +97,16 @@ export function main() {
tcb.overrideTemplate(TestComponent, template) tcb.overrideTemplate(TestComponent, template)
.createAsync(TestComponent) .createAsync(TestComponent)
.then((rootTC) => { .then((rootTC) => {
rootTC.detectChanges(); detectChangesAndCheck(rootTC, 'ng-binding foo');
expect(rootTC.componentViewChildren[0].nativeElement.className)
.toEqual('ng-binding foo');
StringMapWrapper.set(rootTC.componentInstance.objExpr, 'bar', true); StringMapWrapper.set(rootTC.componentInstance.objExpr, 'bar', true);
rootTC.detectChanges(); detectChangesAndCheck(rootTC, 'ng-binding foo bar');
expect(rootTC.componentViewChildren[0].nativeElement.className)
.toEqual('ng-binding foo bar');
StringMapWrapper.set(rootTC.componentInstance.objExpr, 'baz', true); StringMapWrapper.set(rootTC.componentInstance.objExpr, 'baz', true);
rootTC.detectChanges(); detectChangesAndCheck(rootTC, 'ng-binding foo bar baz');
expect(rootTC.componentViewChildren[0].nativeElement.className)
.toEqual('ng-binding foo bar baz');
StringMapWrapper.delete(rootTC.componentInstance.objExpr, 'bar'); StringMapWrapper.delete(rootTC.componentInstance.objExpr, 'bar');
rootTC.detectChanges(); detectChangesAndCheck(rootTC, 'ng-binding foo baz');
expect(rootTC.componentViewChildren[0].nativeElement.className)
.toEqual('ng-binding foo baz');
async.done(); async.done();
}); });
@ -133,19 +119,13 @@ export function main() {
tcb.overrideTemplate(TestComponent, template) tcb.overrideTemplate(TestComponent, template)
.createAsync(TestComponent) .createAsync(TestComponent)
.then((rootTC) => { .then((rootTC) => {
rootTC.detectChanges(); detectChangesAndCheck(rootTC, 'ng-binding foo');
expect(rootTC.componentViewChildren[0].nativeElement.className)
.toEqual('ng-binding foo');
rootTC.componentInstance.objExpr = {foo: true, bar: true}; rootTC.componentInstance.objExpr = {foo: true, bar: true};
rootTC.detectChanges(); detectChangesAndCheck(rootTC, 'ng-binding foo bar');
expect(rootTC.componentViewChildren[0].nativeElement.className)
.toEqual('ng-binding foo bar');
rootTC.componentInstance.objExpr = {baz: true}; rootTC.componentInstance.objExpr = {baz: true};
rootTC.detectChanges(); detectChangesAndCheck(rootTC, 'ng-binding baz');
expect(rootTC.componentViewChildren[0].nativeElement.className)
.toEqual('ng-binding baz');
async.done(); async.done();
}); });
@ -161,10 +141,7 @@ export function main() {
tcb.overrideTemplate(TestComponent, template) tcb.overrideTemplate(TestComponent, template)
.createAsync(TestComponent) .createAsync(TestComponent)
.then((rootTC) => { .then((rootTC) => {
rootTC.detectChanges(); detectChangesAndCheck(rootTC, 'ng-binding foo bar foo-bar fooBar');
expect(rootTC.componentViewChildren[0].nativeElement.className)
.toEqual('ng-binding foo bar foo-bar fooBar');
async.done(); async.done();
}); });
})); }));
@ -176,27 +153,17 @@ export function main() {
tcb.overrideTemplate(TestComponent, template) tcb.overrideTemplate(TestComponent, template)
.createAsync(TestComponent) .createAsync(TestComponent)
.then((rootTC) => { .then((rootTC) => {
var arrExpr: List<string> = rootTC.componentInstance.arrExpr; var arrExpr: List<string> = rootTC.componentInstance.arrExpr;
detectChangesAndCheck(rootTC, 'ng-binding foo');
rootTC.detectChanges();
expect(rootTC.componentViewChildren[0].nativeElement.className)
.toEqual('ng-binding foo');
arrExpr.push('bar'); arrExpr.push('bar');
rootTC.detectChanges(); detectChangesAndCheck(rootTC, 'ng-binding foo bar');
expect(rootTC.componentViewChildren[0].nativeElement.className)
.toEqual('ng-binding foo bar');
arrExpr[1] = 'baz'; arrExpr[1] = 'baz';
rootTC.detectChanges(); detectChangesAndCheck(rootTC, 'ng-binding foo baz');
expect(rootTC.componentViewChildren[0].nativeElement.className)
.toEqual('ng-binding foo baz');
ListWrapper.remove(rootTC.componentInstance.arrExpr, 'baz'); ListWrapper.remove(rootTC.componentInstance.arrExpr, 'baz');
rootTC.detectChanges(); detectChangesAndCheck(rootTC, 'ng-binding foo');
expect(rootTC.componentViewChildren[0].nativeElement.className)
.toEqual('ng-binding foo');
async.done(); async.done();
}); });
@ -209,14 +176,10 @@ export function main() {
tcb.overrideTemplate(TestComponent, template) tcb.overrideTemplate(TestComponent, template)
.createAsync(TestComponent) .createAsync(TestComponent)
.then((rootTC) => { .then((rootTC) => {
rootTC.detectChanges(); detectChangesAndCheck(rootTC, 'ng-binding foo');
expect(rootTC.componentViewChildren[0].nativeElement.className)
.toEqual('ng-binding foo');
rootTC.componentInstance.arrExpr = ['bar']; rootTC.componentInstance.arrExpr = ['bar'];
rootTC.detectChanges(); detectChangesAndCheck(rootTC, 'ng-binding bar');
expect(rootTC.componentViewChildren[0].nativeElement.className)
.toEqual('ng-binding bar');
async.done(); async.done();
}); });
@ -232,10 +195,7 @@ export function main() {
tcb.overrideTemplate(TestComponent, template) tcb.overrideTemplate(TestComponent, template)
.createAsync(TestComponent) .createAsync(TestComponent)
.then((rootTC) => { .then((rootTC) => {
rootTC.detectChanges(); detectChangesAndCheck(rootTC, 'ng-binding foo bar foo-bar fooBar');
expect(rootTC.componentViewChildren[0].nativeElement.className)
.toEqual('ng-binding foo bar foo-bar fooBar');
async.done(); async.done();
}); });
})); }));
@ -247,19 +207,14 @@ export function main() {
tcb.overrideTemplate(TestComponent, template) tcb.overrideTemplate(TestComponent, template)
.createAsync(TestComponent) .createAsync(TestComponent)
.then((rootTC) => { .then((rootTC) => {
rootTC.detectChanges(); detectChangesAndCheck(rootTC, 'ng-binding foo');
expect(rootTC.componentViewChildren[0].nativeElement.className)
.toEqual('ng-binding foo');
rootTC.componentInstance.strExpr = 'foo bar'; rootTC.componentInstance.strExpr = 'foo bar';
rootTC.detectChanges(); detectChangesAndCheck(rootTC, 'ng-binding foo bar');
expect(rootTC.componentViewChildren[0].nativeElement.className)
.toEqual('ng-binding foo bar');
rootTC.componentInstance.strExpr = 'baz'; rootTC.componentInstance.strExpr = 'baz';
rootTC.detectChanges(); detectChangesAndCheck(rootTC, 'ng-binding baz');
expect(rootTC.componentViewChildren[0].nativeElement.className)
.toEqual('ng-binding baz');
async.done(); async.done();
}); });
@ -273,14 +228,10 @@ export function main() {
tcb.overrideTemplate(TestComponent, template) tcb.overrideTemplate(TestComponent, template)
.createAsync(TestComponent) .createAsync(TestComponent)
.then((rootTC) => { .then((rootTC) => {
rootTC.detectChanges(); detectChangesAndCheck(rootTC, 'ng-binding foo');
expect(rootTC.componentViewChildren[0].nativeElement.className)
.toEqual('ng-binding foo');
rootTC.componentInstance.strExpr = null; rootTC.componentInstance.strExpr = null;
rootTC.detectChanges(); detectChangesAndCheck(rootTC, 'ng-binding');
expect(rootTC.componentViewChildren[0].nativeElement.className)
.toEqual('ng-binding');
async.done(); async.done();
}); });
@ -294,19 +245,13 @@ export function main() {
tcb.overrideTemplate(TestComponent, template) tcb.overrideTemplate(TestComponent, template)
.createAsync(TestComponent) .createAsync(TestComponent)
.then((rootTC) => { .then((rootTC) => {
rootTC.detectChanges(); detectChangesAndCheck(rootTC, 'ng-binding foo');
expect(rootTC.componentViewChildren[0].nativeElement.className)
.toEqual('ng-binding foo');
rootTC.componentInstance.objExpr = null; rootTC.componentInstance.objExpr = null;
rootTC.detectChanges(); detectChangesAndCheck(rootTC, 'ng-binding');
expect(rootTC.componentViewChildren[0].nativeElement.className)
.toEqual('ng-binding');
rootTC.componentInstance.objExpr = {'foo': false, 'bar': true}; rootTC.componentInstance.objExpr = {'foo': false, 'bar': true};
rootTC.detectChanges(); detectChangesAndCheck(rootTC, 'ng-binding bar');
expect(rootTC.componentViewChildren[0].nativeElement.className)
.toEqual('ng-binding bar');
async.done(); async.done();
}); });
@ -320,14 +265,10 @@ export function main() {
.createAsync(TestComponent) .createAsync(TestComponent)
.then((rootTC) => { .then((rootTC) => {
StringMapWrapper.set(rootTC.componentInstance.objExpr, 'bar', true); StringMapWrapper.set(rootTC.componentInstance.objExpr, 'bar', true);
rootTC.detectChanges(); detectChangesAndCheck(rootTC, 'init foo ng-binding bar');
expect(rootTC.componentViewChildren[0].nativeElement.className)
.toEqual('init foo ng-binding bar');
StringMapWrapper.set(rootTC.componentInstance.objExpr, 'foo', false); StringMapWrapper.set(rootTC.componentInstance.objExpr, 'foo', false);
rootTC.detectChanges(); detectChangesAndCheck(rootTC, 'init ng-binding bar');
expect(rootTC.componentViewChildren[0].nativeElement.className)
.toEqual('init ng-binding bar');
async.done(); async.done();
}); });
@ -340,24 +281,16 @@ export function main() {
tcb.overrideTemplate(TestComponent, template) tcb.overrideTemplate(TestComponent, template)
.createAsync(TestComponent) .createAsync(TestComponent)
.then((rootTC) => { .then((rootTC) => {
rootTC.detectChanges(); detectChangesAndCheck(rootTC, 'init foo ng-binding baz');
expect(rootTC.componentViewChildren[0].nativeElement.className)
.toEqual('init foo ng-binding baz');
StringMapWrapper.set(rootTC.componentInstance.objExpr, 'bar', true); StringMapWrapper.set(rootTC.componentInstance.objExpr, 'bar', true);
rootTC.detectChanges(); detectChangesAndCheck(rootTC, 'init foo ng-binding baz bar');
expect(rootTC.componentViewChildren[0].nativeElement.className)
.toEqual('init foo ng-binding baz bar');
StringMapWrapper.set(rootTC.componentInstance.objExpr, 'foo', false); StringMapWrapper.set(rootTC.componentInstance.objExpr, 'foo', false);
rootTC.detectChanges(); detectChangesAndCheck(rootTC, 'init ng-binding baz bar');
expect(rootTC.componentViewChildren[0].nativeElement.className)
.toEqual('init ng-binding baz bar');
rootTC.componentInstance.condition = false; rootTC.componentInstance.condition = false;
rootTC.detectChanges(); detectChangesAndCheck(rootTC, 'init ng-binding bar');
expect(rootTC.componentViewChildren[0].nativeElement.className)
.toEqual('init ng-binding bar');
async.done(); async.done();
}); });