2016-06-23 12:47:54 -04:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright Google Inc. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
|
|
* found in the LICENSE file at https://angular.io/license
|
|
|
|
*/
|
|
|
|
|
2016-08-02 18:53:34 -04:00
|
|
|
import {NgSwitch, NgSwitchCase, NgSwitchDefault} from '@angular/common';
|
2016-04-28 20:50:03 -04:00
|
|
|
import {Component} from '@angular/core';
|
2016-07-11 19:04:32 -04:00
|
|
|
import {TestComponentBuilder} from '@angular/core/testing';
|
2016-08-02 18:53:34 -04:00
|
|
|
import {AsyncTestCompleter, beforeEach, ddescribe, describe, iit, inject, it, xit} from '@angular/core/testing/testing_internal';
|
|
|
|
import {expect} from '@angular/platform-browser/testing/matchers';
|
2015-05-26 17:22:35 -04:00
|
|
|
|
|
|
|
export function main() {
|
|
|
|
describe('switch', () => {
|
|
|
|
describe('switch value changes', () => {
|
2015-06-11 21:50:41 -04:00
|
|
|
it('should switch amongst when values',
|
2016-06-10 01:52:30 -04:00
|
|
|
inject(
|
|
|
|
[TestComponentBuilder, AsyncTestCompleter],
|
|
|
|
(tcb: TestComponentBuilder, async: AsyncTestCompleter) => {
|
|
|
|
var template = '<div>' +
|
|
|
|
'<ul [ngSwitch]="switchValue">' +
|
|
|
|
'<template ngSwitchCase="a"><li>when a</li></template>' +
|
|
|
|
'<template ngSwitchCase="b"><li>when b</li></template>' +
|
|
|
|
'</ul></div>';
|
|
|
|
|
|
|
|
tcb.overrideTemplate(TestComponent, template)
|
|
|
|
.createAsync(TestComponent)
|
|
|
|
.then((fixture) => {
|
|
|
|
fixture.detectChanges();
|
|
|
|
expect(fixture.debugElement.nativeElement).toHaveText('');
|
|
|
|
|
|
|
|
fixture.debugElement.componentInstance.switchValue = 'a';
|
|
|
|
fixture.detectChanges();
|
|
|
|
expect(fixture.debugElement.nativeElement).toHaveText('when a');
|
|
|
|
|
|
|
|
fixture.debugElement.componentInstance.switchValue = 'b';
|
|
|
|
fixture.detectChanges();
|
|
|
|
expect(fixture.debugElement.nativeElement).toHaveText('when b');
|
|
|
|
|
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
|
|
|
// TODO(robwormald): deprecate and remove
|
|
|
|
it('should switch amongst when values using switchWhen',
|
2016-06-08 19:38:52 -04:00
|
|
|
inject(
|
|
|
|
[TestComponentBuilder, AsyncTestCompleter],
|
|
|
|
(tcb: TestComponentBuilder, async: AsyncTestCompleter) => {
|
|
|
|
var template = '<div>' +
|
|
|
|
'<ul [ngSwitch]="switchValue">' +
|
|
|
|
'<template ngSwitchWhen="a"><li>when a</li></template>' +
|
|
|
|
'<template ngSwitchWhen="b"><li>when b</li></template>' +
|
|
|
|
'</ul></div>';
|
|
|
|
|
|
|
|
tcb.overrideTemplate(TestComponent, template)
|
|
|
|
.createAsync(TestComponent)
|
|
|
|
.then((fixture) => {
|
|
|
|
fixture.detectChanges();
|
|
|
|
expect(fixture.debugElement.nativeElement).toHaveText('');
|
|
|
|
|
|
|
|
fixture.debugElement.componentInstance.switchValue = 'a';
|
|
|
|
fixture.detectChanges();
|
|
|
|
expect(fixture.debugElement.nativeElement).toHaveText('when a');
|
|
|
|
|
|
|
|
fixture.debugElement.componentInstance.switchValue = 'b';
|
|
|
|
fixture.detectChanges();
|
|
|
|
expect(fixture.debugElement.nativeElement).toHaveText('when b');
|
|
|
|
|
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
2015-05-26 17:22:35 -04:00
|
|
|
|
|
|
|
it('should switch amongst when values with fallback to default',
|
2016-06-08 19:38:52 -04:00
|
|
|
inject(
|
|
|
|
[TestComponentBuilder, AsyncTestCompleter],
|
|
|
|
(tcb: TestComponentBuilder, async: AsyncTestCompleter) => {
|
|
|
|
var template = '<div>' +
|
|
|
|
'<ul [ngSwitch]="switchValue">' +
|
2016-06-10 01:52:30 -04:00
|
|
|
'<li template="ngSwitchCase \'a\'">when a</li>' +
|
2016-06-08 19:38:52 -04:00
|
|
|
'<li template="ngSwitchDefault">when default</li>' +
|
|
|
|
'</ul></div>';
|
|
|
|
|
|
|
|
tcb.overrideTemplate(TestComponent, template)
|
|
|
|
.createAsync(TestComponent)
|
|
|
|
.then((fixture) => {
|
|
|
|
fixture.detectChanges();
|
|
|
|
expect(fixture.debugElement.nativeElement).toHaveText('when default');
|
|
|
|
|
|
|
|
fixture.debugElement.componentInstance.switchValue = 'a';
|
|
|
|
fixture.detectChanges();
|
|
|
|
expect(fixture.debugElement.nativeElement).toHaveText('when a');
|
|
|
|
|
|
|
|
fixture.debugElement.componentInstance.switchValue = 'b';
|
|
|
|
fixture.detectChanges();
|
|
|
|
expect(fixture.debugElement.nativeElement).toHaveText('when default');
|
|
|
|
|
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
2015-05-26 17:22:35 -04:00
|
|
|
|
|
|
|
it('should support multiple whens with the same value',
|
2016-06-08 19:38:52 -04:00
|
|
|
inject(
|
|
|
|
[TestComponentBuilder, AsyncTestCompleter],
|
|
|
|
(tcb: TestComponentBuilder, async: AsyncTestCompleter) => {
|
|
|
|
var template = '<div>' +
|
|
|
|
'<ul [ngSwitch]="switchValue">' +
|
2016-06-10 01:52:30 -04:00
|
|
|
'<template ngSwitchCase="a"><li>when a1;</li></template>' +
|
|
|
|
'<template ngSwitchCase="b"><li>when b1;</li></template>' +
|
|
|
|
'<template ngSwitchCase="a"><li>when a2;</li></template>' +
|
|
|
|
'<template ngSwitchCase="b"><li>when b2;</li></template>' +
|
2016-06-08 19:38:52 -04:00
|
|
|
'<template ngSwitchDefault><li>when default1;</li></template>' +
|
|
|
|
'<template ngSwitchDefault><li>when default2;</li></template>' +
|
|
|
|
'</ul></div>';
|
|
|
|
|
|
|
|
tcb.overrideTemplate(TestComponent, template)
|
|
|
|
.createAsync(TestComponent)
|
|
|
|
.then((fixture) => {
|
|
|
|
fixture.detectChanges();
|
|
|
|
expect(fixture.debugElement.nativeElement)
|
|
|
|
.toHaveText('when default1;when default2;');
|
|
|
|
|
|
|
|
fixture.debugElement.componentInstance.switchValue = 'a';
|
|
|
|
fixture.detectChanges();
|
|
|
|
expect(fixture.debugElement.nativeElement).toHaveText('when a1;when a2;');
|
|
|
|
|
|
|
|
fixture.debugElement.componentInstance.switchValue = 'b';
|
|
|
|
fixture.detectChanges();
|
|
|
|
expect(fixture.debugElement.nativeElement).toHaveText('when b1;when b2;');
|
|
|
|
|
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
2015-05-26 17:22:35 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('when values changes', () => {
|
2015-06-11 21:50:41 -04:00
|
|
|
it('should switch amongst when values',
|
2016-06-08 19:38:52 -04:00
|
|
|
inject(
|
|
|
|
[TestComponentBuilder, AsyncTestCompleter],
|
|
|
|
(tcb: TestComponentBuilder, async: AsyncTestCompleter) => {
|
|
|
|
var template = '<div>' +
|
|
|
|
'<ul [ngSwitch]="switchValue">' +
|
2016-06-10 01:52:30 -04:00
|
|
|
'<template [ngSwitchCase]="when1"><li>when 1;</li></template>' +
|
|
|
|
'<template [ngSwitchCase]="when2"><li>when 2;</li></template>' +
|
2016-06-08 19:38:52 -04:00
|
|
|
'<template ngSwitchDefault><li>when default;</li></template>' +
|
|
|
|
'</ul></div>';
|
|
|
|
|
|
|
|
tcb.overrideTemplate(TestComponent, template)
|
|
|
|
.createAsync(TestComponent)
|
|
|
|
.then((fixture) => {
|
|
|
|
fixture.debugElement.componentInstance.when1 = 'a';
|
|
|
|
fixture.debugElement.componentInstance.when2 = 'b';
|
|
|
|
fixture.debugElement.componentInstance.switchValue = 'a';
|
|
|
|
fixture.detectChanges();
|
|
|
|
expect(fixture.debugElement.nativeElement).toHaveText('when 1;');
|
|
|
|
|
|
|
|
fixture.debugElement.componentInstance.switchValue = 'b';
|
|
|
|
fixture.detectChanges();
|
|
|
|
expect(fixture.debugElement.nativeElement).toHaveText('when 2;');
|
|
|
|
|
|
|
|
fixture.debugElement.componentInstance.switchValue = 'c';
|
|
|
|
fixture.detectChanges();
|
|
|
|
expect(fixture.debugElement.nativeElement).toHaveText('when default;');
|
|
|
|
|
|
|
|
fixture.debugElement.componentInstance.when1 = 'c';
|
|
|
|
fixture.detectChanges();
|
|
|
|
expect(fixture.debugElement.nativeElement).toHaveText('when 1;');
|
|
|
|
|
|
|
|
fixture.debugElement.componentInstance.when1 = 'd';
|
|
|
|
fixture.detectChanges();
|
|
|
|
expect(fixture.debugElement.nativeElement).toHaveText('when default;');
|
|
|
|
|
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
2015-05-26 17:22:35 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-03-08 16:36:48 -05:00
|
|
|
@Component(
|
2016-06-10 01:52:30 -04:00
|
|
|
{selector: 'test-cmp', directives: [NgSwitch, NgSwitchCase, NgSwitchDefault], template: ''})
|
2015-05-26 17:22:35 -04:00
|
|
|
class TestComponent {
|
|
|
|
switchValue: any;
|
|
|
|
when1: any;
|
|
|
|
when2: any;
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
this.switchValue = null;
|
|
|
|
this.when1 = null;
|
|
|
|
this.when2 = null;
|
|
|
|
}
|
|
|
|
}
|