2015-03-13 06:10:11 -04:00
|
|
|
import {
|
|
|
|
AsyncTestCompleter,
|
|
|
|
beforeEach,
|
|
|
|
ddescribe,
|
|
|
|
describe,
|
|
|
|
el,
|
|
|
|
expect,
|
|
|
|
iit,
|
|
|
|
inject,
|
|
|
|
it,
|
|
|
|
xit,
|
|
|
|
} from 'angular2/test_lib';
|
2015-02-27 17:50:06 -05:00
|
|
|
import {DOM} from 'angular2/src/dom/dom_adapter';
|
2015-02-24 10:05:45 -05:00
|
|
|
|
2015-02-05 16:08:05 -05:00
|
|
|
import {Component} from 'angular2/src/core/annotations/annotations';
|
2015-02-12 08:44:59 -05:00
|
|
|
import {Template} from 'angular2/src/core/annotations/template';
|
2015-03-30 11:11:18 -04:00
|
|
|
|
2015-02-04 16:27:31 -05:00
|
|
|
import {Switch, SwitchWhen, SwitchDefault} from 'angular2/src/directives/switch';
|
2015-03-30 11:11:18 -04:00
|
|
|
|
|
|
|
import {TestBed} from 'angular2/src/test_lib/test_bed';
|
2015-01-09 03:16:12 -05:00
|
|
|
|
|
|
|
export function main() {
|
2015-02-04 16:27:31 -05:00
|
|
|
describe('switch', () => {
|
2015-01-09 03:16:12 -05:00
|
|
|
describe('switch value changes', () => {
|
2015-03-30 11:11:18 -04:00
|
|
|
it('should switch amongst when values', inject([TestBed, AsyncTestCompleter], (tb, async) => {
|
2015-01-09 03:16:12 -05:00
|
|
|
var template = '<div>' +
|
2015-02-04 16:27:31 -05:00
|
|
|
'<ul [switch]="switchValue">' +
|
|
|
|
'<template [switch-when]="\'a\'"><li>when a</li></template>' +
|
|
|
|
'<template [switch-when]="\'b\'"><li>when b</li></template>' +
|
2015-01-09 03:16:12 -05:00
|
|
|
'</ul></div>';
|
2015-03-30 11:11:18 -04:00
|
|
|
|
|
|
|
tb.createView(TestComponent, {html: template}).then((view) => {
|
|
|
|
view.detectChanges();
|
2015-01-09 03:16:12 -05:00
|
|
|
expect(DOM.getText(view.nodes[0])).toEqual('');
|
|
|
|
|
2015-03-30 11:11:18 -04:00
|
|
|
view.context.switchValue = 'a';
|
|
|
|
view.detectChanges();
|
2015-01-09 03:16:12 -05:00
|
|
|
expect(DOM.getText(view.nodes[0])).toEqual('when a');
|
|
|
|
|
2015-03-30 11:11:18 -04:00
|
|
|
view.context.switchValue = 'b';
|
|
|
|
view.detectChanges();
|
2015-01-09 03:16:12 -05:00
|
|
|
expect(DOM.getText(view.nodes[0])).toEqual('when b');
|
|
|
|
|
2015-03-13 06:10:11 -04:00
|
|
|
async.done();
|
2015-01-09 03:16:12 -05:00
|
|
|
});
|
2015-03-13 06:10:11 -04:00
|
|
|
}));
|
2015-01-09 03:16:12 -05:00
|
|
|
|
2015-03-30 11:11:18 -04:00
|
|
|
it('should switch amongst when values with fallback to default',
|
|
|
|
inject([TestBed, AsyncTestCompleter], (tb, async) => {
|
2015-01-09 03:16:12 -05:00
|
|
|
var template = '<div>' +
|
2015-02-04 16:27:31 -05:00
|
|
|
'<ul [switch]="switchValue">' +
|
|
|
|
'<li template="switch-when \'a\'">when a</li>' +
|
|
|
|
'<li template="switch-default">when default</li>' +
|
2015-01-09 03:16:12 -05:00
|
|
|
'</ul></div>';
|
|
|
|
|
2015-03-30 11:11:18 -04:00
|
|
|
tb.createView(TestComponent, {html: template}).then((view) => {
|
|
|
|
view.detectChanges();
|
|
|
|
expect(DOM.getText(view.nodes[0])).toEqual('when default');
|
|
|
|
|
|
|
|
view.context.switchValue = 'a';
|
|
|
|
view.detectChanges();
|
2015-01-09 03:16:12 -05:00
|
|
|
expect(DOM.getText(view.nodes[0])).toEqual('when a');
|
|
|
|
|
2015-03-30 11:11:18 -04:00
|
|
|
view.context.switchValue = 'b';
|
|
|
|
view.detectChanges();
|
|
|
|
expect(DOM.getText(view.nodes[0])).toEqual('when default');
|
2015-01-09 03:16:12 -05:00
|
|
|
|
2015-03-13 06:10:11 -04:00
|
|
|
async.done();
|
2015-01-09 03:16:12 -05:00
|
|
|
});
|
2015-03-13 06:10:11 -04:00
|
|
|
}));
|
2015-01-09 03:16:12 -05:00
|
|
|
|
2015-03-30 11:11:18 -04:00
|
|
|
it('should support multiple whens with the same value',
|
|
|
|
inject([TestBed, AsyncTestCompleter], (tb, async) => {
|
2015-01-09 03:16:12 -05:00
|
|
|
var template = '<div>' +
|
2015-02-04 16:27:31 -05:00
|
|
|
'<ul [switch]="switchValue">' +
|
|
|
|
'<template [switch-when]="\'a\'"><li>when a1;</li></template>' +
|
|
|
|
'<template [switch-when]="\'b\'"><li>when b1;</li></template>' +
|
|
|
|
'<template [switch-when]="\'a\'"><li>when a2;</li></template>' +
|
|
|
|
'<template [switch-when]="\'b\'"><li>when b2;</li></template>' +
|
|
|
|
'<template [switch-default]><li>when default1;</li></template>' +
|
|
|
|
'<template [switch-default]><li>when default2;</li></template>' +
|
2015-01-09 03:16:12 -05:00
|
|
|
'</ul></div>';
|
2015-03-30 11:11:18 -04:00
|
|
|
|
|
|
|
tb.createView(TestComponent, {html: template}).then((view) => {
|
|
|
|
view.detectChanges();
|
2015-01-09 03:16:12 -05:00
|
|
|
expect(DOM.getText(view.nodes[0])).toEqual('when default1;when default2;');
|
|
|
|
|
2015-03-30 11:11:18 -04:00
|
|
|
view.context.switchValue = 'a';
|
|
|
|
view.detectChanges();
|
2015-01-09 03:16:12 -05:00
|
|
|
expect(DOM.getText(view.nodes[0])).toEqual('when a1;when a2;');
|
|
|
|
|
2015-03-30 11:11:18 -04:00
|
|
|
view.context.switchValue = 'b';
|
|
|
|
view.detectChanges();
|
2015-01-09 03:16:12 -05:00
|
|
|
expect(DOM.getText(view.nodes[0])).toEqual('when b1;when b2;');
|
|
|
|
|
2015-03-13 06:10:11 -04:00
|
|
|
async.done();
|
2015-01-09 03:16:12 -05:00
|
|
|
});
|
2015-03-13 06:10:11 -04:00
|
|
|
}));
|
2015-01-09 03:16:12 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('when values changes', () => {
|
2015-03-30 11:11:18 -04:00
|
|
|
it('should switch amongst when values',
|
|
|
|
inject([TestBed, AsyncTestCompleter], (tb, async) => {
|
2015-01-09 03:16:12 -05:00
|
|
|
var template = '<div>' +
|
2015-02-04 16:27:31 -05:00
|
|
|
'<ul [switch]="switchValue">' +
|
|
|
|
'<template [switch-when]="when1"><li>when 1;</li></template>' +
|
|
|
|
'<template [switch-when]="when2"><li>when 2;</li></template>' +
|
|
|
|
'<template [switch-default]><li>when default;</li></template>' +
|
2015-01-09 03:16:12 -05:00
|
|
|
'</ul></div>';
|
|
|
|
|
2015-03-30 11:11:18 -04:00
|
|
|
tb.createView(TestComponent, {html: template}).then((view) => {
|
|
|
|
view.context.when1 = 'a';
|
|
|
|
view.context.when2 = 'b';
|
|
|
|
view.context.switchValue = 'a';
|
|
|
|
view.detectChanges();
|
2015-01-09 03:16:12 -05:00
|
|
|
expect(DOM.getText(view.nodes[0])).toEqual('when 1;');
|
|
|
|
|
2015-03-30 11:11:18 -04:00
|
|
|
view.context.switchValue = 'b';
|
|
|
|
view.detectChanges();
|
2015-01-09 03:16:12 -05:00
|
|
|
expect(DOM.getText(view.nodes[0])).toEqual('when 2;');
|
|
|
|
|
2015-03-30 11:11:18 -04:00
|
|
|
view.context.switchValue = 'c';
|
|
|
|
view.detectChanges();
|
2015-01-09 03:16:12 -05:00
|
|
|
expect(DOM.getText(view.nodes[0])).toEqual('when default;');
|
|
|
|
|
2015-03-30 11:11:18 -04:00
|
|
|
view.context.when1 = 'c';
|
|
|
|
view.detectChanges();
|
2015-01-09 03:16:12 -05:00
|
|
|
expect(DOM.getText(view.nodes[0])).toEqual('when 1;');
|
|
|
|
|
2015-03-30 11:11:18 -04:00
|
|
|
view.context.when1 = 'd';
|
|
|
|
view.detectChanges();
|
2015-01-09 03:16:12 -05:00
|
|
|
expect(DOM.getText(view.nodes[0])).toEqual('when default;');
|
|
|
|
|
2015-03-13 06:10:11 -04:00
|
|
|
async.done();
|
2015-01-09 03:16:12 -05:00
|
|
|
});
|
2015-03-13 06:10:11 -04:00
|
|
|
}));
|
2015-01-09 03:16:12 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-02-12 08:44:59 -05:00
|
|
|
@Component({selector: 'test-cmp'})
|
2015-03-30 11:11:18 -04:00
|
|
|
@Template({directives: [Switch, SwitchWhen, SwitchDefault]})
|
2015-01-09 03:16:12 -05:00
|
|
|
class TestComponent {
|
|
|
|
switchValue: any;
|
|
|
|
when1: any;
|
|
|
|
when2: any;
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
this.switchValue = null;
|
|
|
|
this.when1 = null;
|
|
|
|
this.when2 = null;
|
|
|
|
}
|
|
|
|
}
|