import {
AsyncTestCompleter,
TestComponentBuilder,
beforeEach,
ddescribe,
describe,
el,
expect,
iit,
inject,
it,
xit,
} from 'angular2/test_lib';
import {Component, View} from 'angular2/angular2';
import {NgSwitch, NgSwitchWhen, NgSwitchDefault} from 'angular2/src/directives/ng_switch';
export function main() {
describe('switch', () => {
describe('switch value changes', () => {
it('should switch amongst when values',
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
var template = '
' +
'
' +
'- when a
' +
'- when b
' +
'
';
tcb.overrideTemplate(TestComponent, template)
.createAsync(TestComponent)
.then((rootTC) => {
rootTC.detectChanges();
expect(rootTC.nativeElement).toHaveText('');
rootTC.componentInstance.switchValue = 'a';
rootTC.detectChanges();
expect(rootTC.nativeElement).toHaveText('when a');
rootTC.componentInstance.switchValue = 'b';
rootTC.detectChanges();
expect(rootTC.nativeElement).toHaveText('when b');
async.done();
});
}));
it('should switch amongst when values with fallback to default',
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
var template = '' +
'
' +
'- when a
' +
'- when default
' +
'
';
tcb.overrideTemplate(TestComponent, template)
.createAsync(TestComponent)
.then((rootTC) => {
rootTC.detectChanges();
expect(rootTC.nativeElement).toHaveText('when default');
rootTC.componentInstance.switchValue = 'a';
rootTC.detectChanges();
expect(rootTC.nativeElement).toHaveText('when a');
rootTC.componentInstance.switchValue = 'b';
rootTC.detectChanges();
expect(rootTC.nativeElement).toHaveText('when default');
async.done();
});
}));
it('should support multiple whens with the same value',
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
var template = '' +
'
' +
'- when a1;
' +
'- when b1;
' +
'- when a2;
' +
'- when b2;
' +
'- when default1;
' +
'- when default2;
' +
'
';
tcb.overrideTemplate(TestComponent, template)
.createAsync(TestComponent)
.then((rootTC) => {
rootTC.detectChanges();
expect(rootTC.nativeElement).toHaveText('when default1;when default2;');
rootTC.componentInstance.switchValue = 'a';
rootTC.detectChanges();
expect(rootTC.nativeElement).toHaveText('when a1;when a2;');
rootTC.componentInstance.switchValue = 'b';
rootTC.detectChanges();
expect(rootTC.nativeElement).toHaveText('when b1;when b2;');
async.done();
});
}));
});
describe('when values changes', () => {
it('should switch amongst when values',
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
var template = '' +
'
' +
'- when 1;
' +
'- when 2;
' +
'- when default;
' +
'
';
tcb.overrideTemplate(TestComponent, template)
.createAsync(TestComponent)
.then((rootTC) => {
rootTC.componentInstance.when1 = 'a';
rootTC.componentInstance.when2 = 'b';
rootTC.componentInstance.switchValue = 'a';
rootTC.detectChanges();
expect(rootTC.nativeElement).toHaveText('when 1;');
rootTC.componentInstance.switchValue = 'b';
rootTC.detectChanges();
expect(rootTC.nativeElement).toHaveText('when 2;');
rootTC.componentInstance.switchValue = 'c';
rootTC.detectChanges();
expect(rootTC.nativeElement).toHaveText('when default;');
rootTC.componentInstance.when1 = 'c';
rootTC.detectChanges();
expect(rootTC.nativeElement).toHaveText('when 1;');
rootTC.componentInstance.when1 = 'd';
rootTC.detectChanges();
expect(rootTC.nativeElement).toHaveText('when default;');
async.done();
});
}));
});
});
}
@Component({selector: 'test-cmp'})
@View({directives: [NgSwitch, NgSwitchWhen, NgSwitchDefault]})
class TestComponent {
switchValue: any;
when1: any;
when2: any;
constructor() {
this.switchValue = null;
this.when1 = null;
this.when2 = null;
}
}