2016-04-28 20:50:03 -04:00
|
|
|
import {Component, Directive, Output, EventEmitter} from '@angular/core';
|
2016-04-24 13:41:20 -04:00
|
|
|
import {
|
2016-04-12 12:40:37 -04:00
|
|
|
afterEach,
|
|
|
|
beforeEach,
|
|
|
|
ddescribe,
|
|
|
|
describe,
|
|
|
|
expect,
|
|
|
|
it,
|
|
|
|
inject,
|
|
|
|
iit,
|
2016-04-28 20:50:03 -04:00
|
|
|
xit
|
|
|
|
} from '@angular/core/testing/testing_internal';
|
2016-05-02 01:50:37 -04:00
|
|
|
import {fakeAsync, tick, flushMicrotasks} from '@angular/core/testing';
|
2016-04-28 20:50:03 -04:00
|
|
|
import {AsyncTestCompleter} from '@angular/core/testing/testing_internal';
|
|
|
|
import {TestComponentBuilder} from '@angular/compiler/testing';
|
|
|
|
import {ComponentFixture} from '@angular/compiler/testing';
|
|
|
|
import {getDOM} from '@angular/platform-browser/src/dom/dom_adapter';
|
2016-04-12 12:40:37 -04:00
|
|
|
import {
|
|
|
|
Control,
|
|
|
|
ControlGroup,
|
|
|
|
ControlValueAccessor,
|
|
|
|
FORM_DIRECTIVES,
|
|
|
|
NG_VALIDATORS,
|
|
|
|
NG_ASYNC_VALIDATORS,
|
|
|
|
NgControl,
|
|
|
|
NgIf,
|
|
|
|
NgFor,
|
|
|
|
NgForm,
|
|
|
|
Validators,
|
|
|
|
Validator,
|
|
|
|
RadioButtonState
|
2016-04-28 20:50:03 -04:00
|
|
|
} from '@angular/common';
|
|
|
|
import {Provider, forwardRef, Input} from '@angular/core';
|
|
|
|
import {By} from '@angular/platform-browser/src/dom/debug/by';
|
|
|
|
import {ListWrapper} from '../../src/facade/collection';
|
|
|
|
import {ObservableWrapper, TimerWrapper} from '../../src/facade/async';
|
|
|
|
import {PromiseWrapper} from '../../src/facade/promise';
|
|
|
|
import {dispatchEvent} from '@angular/platform-browser/testing';
|
2015-05-22 15:32:49 -04:00
|
|
|
|
|
|
|
export function main() {
|
2016-04-12 12:40:37 -04:00
|
|
|
describe("integration tests", () => {
|
2015-09-30 20:52:33 -04:00
|
|
|
|
2016-04-12 12:40:37 -04:00
|
|
|
it("should initialize DOM elements with the given form object",
|
2015-06-25 00:51:37 -04:00
|
|
|
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
|
2015-11-23 19:02:19 -05:00
|
|
|
var t = `<div [ngFormModel]="form">
|
|
|
|
<input type="text" ngControl="login">
|
2015-05-22 15:32:49 -04:00
|
|
|
</div>`;
|
|
|
|
|
2016-05-02 01:50:37 -04:00
|
|
|
tcb.overrideTemplate(MyComp8, t)
|
|
|
|
.createAsync(MyComp8)
|
|
|
|
.then((fixture) => {
|
|
|
|
fixture.debugElement.componentInstance.form =
|
|
|
|
new ControlGroup({"login": new Control("loginValue")});
|
|
|
|
fixture.detectChanges();
|
2015-05-30 14:56:00 -04:00
|
|
|
|
2016-05-02 01:50:37 -04:00
|
|
|
var input = fixture.debugElement.query(By.css("input"));
|
|
|
|
expect(input.nativeElement.value).toEqual("loginValue");
|
|
|
|
async.done();
|
|
|
|
});
|
2015-05-22 15:32:49 -04:00
|
|
|
}));
|
|
|
|
|
2016-04-20 15:52:35 -04:00
|
|
|
it("should throw if a form isn't passed into ngFormModel",
|
|
|
|
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
|
|
|
|
var t = `<div [ngFormModel]="form">
|
|
|
|
<input type="text" ngControl="login">
|
|
|
|
</div>`;
|
|
|
|
|
2016-05-02 01:50:37 -04:00
|
|
|
tcb.overrideTemplate(MyComp8, t)
|
|
|
|
.createAsync(MyComp8)
|
|
|
|
.then((fixture) => {
|
|
|
|
expect(() => fixture.detectChanges())
|
|
|
|
.toThrowError(new RegExp(`ngFormModel expects a form. Please pass one in.`));
|
|
|
|
async.done();
|
|
|
|
});
|
2016-04-20 15:52:35 -04:00
|
|
|
}));
|
|
|
|
|
2016-04-12 12:40:37 -04:00
|
|
|
it("should update the control group values on DOM change",
|
2015-06-25 00:51:37 -04:00
|
|
|
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
|
2016-04-12 12:40:37 -04:00
|
|
|
var form = new ControlGroup({"login": new Control("oldValue")});
|
2015-05-22 15:32:49 -04:00
|
|
|
|
2015-11-23 19:02:19 -05:00
|
|
|
var t = `<div [ngFormModel]="form">
|
|
|
|
<input type="text" ngControl="login">
|
2015-05-22 15:32:49 -04:00
|
|
|
</div>`;
|
|
|
|
|
2016-05-02 01:50:37 -04:00
|
|
|
tcb.overrideTemplate(MyComp8, t)
|
|
|
|
.createAsync(MyComp8)
|
|
|
|
.then((fixture) => {
|
|
|
|
fixture.debugElement.componentInstance.form = form;
|
|
|
|
fixture.detectChanges();
|
|
|
|
var input = fixture.debugElement.query(By.css("input"));
|
2015-05-22 15:32:49 -04:00
|
|
|
|
2016-05-02 01:50:37 -04:00
|
|
|
input.nativeElement.value = "updatedValue";
|
|
|
|
dispatchEvent(input.nativeElement, "input");
|
2015-05-22 15:32:49 -04:00
|
|
|
|
2016-05-02 01:50:37 -04:00
|
|
|
expect(form.value).toEqual({"login": "updatedValue"});
|
|
|
|
async.done();
|
|
|
|
});
|
2015-05-22 15:32:49 -04:00
|
|
|
}));
|
|
|
|
|
2016-04-12 12:40:37 -04:00
|
|
|
it("should ignore the change event for <input type=text>",
|
2015-11-20 04:19:04 -05:00
|
|
|
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
|
2016-04-12 12:40:37 -04:00
|
|
|
var form = new ControlGroup({"login": new Control("oldValue")});
|
2015-11-20 04:19:04 -05:00
|
|
|
|
2015-11-23 19:02:19 -05:00
|
|
|
var t = `<div [ngFormModel]="form">
|
|
|
|
<input type="text" ngControl="login">
|
2015-11-20 04:19:04 -05:00
|
|
|
</div>`;
|
|
|
|
|
2016-05-02 01:50:37 -04:00
|
|
|
tcb.overrideTemplate(MyComp8, t)
|
|
|
|
.createAsync(MyComp8)
|
|
|
|
.then((fixture) => {
|
|
|
|
fixture.debugElement.componentInstance.form = form;
|
|
|
|
fixture.detectChanges();
|
|
|
|
var input = fixture.debugElement.query(By.css("input"));
|
2015-11-20 04:19:04 -05:00
|
|
|
|
2016-05-02 01:50:37 -04:00
|
|
|
input.nativeElement.value = "updatedValue";
|
2015-11-20 04:19:04 -05:00
|
|
|
|
2016-05-02 01:50:37 -04:00
|
|
|
ObservableWrapper.subscribe(form.valueChanges,
|
|
|
|
(value) => { throw 'Should not happen'; });
|
|
|
|
dispatchEvent(input.nativeElement, "change");
|
2015-11-20 04:19:04 -05:00
|
|
|
|
2016-05-02 01:50:37 -04:00
|
|
|
async.done();
|
|
|
|
});
|
2015-11-20 04:19:04 -05:00
|
|
|
}));
|
|
|
|
|
2016-04-12 12:40:37 -04:00
|
|
|
it("should emit ngSubmit event on submit",
|
2016-04-18 19:04:35 -04:00
|
|
|
fakeAsync(inject([TestComponentBuilder], (tcb: TestComponentBuilder) => {
|
|
|
|
var t = `<div>
|
2015-11-23 19:02:19 -05:00
|
|
|
<form [ngFormModel]="form" (ngSubmit)="name='updated'"></form>
|
2015-10-28 03:59:19 -04:00
|
|
|
<span>{{name}}</span>
|
|
|
|
</div>`;
|
2015-06-05 17:29:50 -04:00
|
|
|
|
2016-04-28 20:50:03 -04:00
|
|
|
let fixture = tcb.overrideTemplate(MyComp8, t).createFakeAsync(MyComp8);
|
2016-04-18 19:04:35 -04:00
|
|
|
tick();
|
2015-08-12 19:26:21 -04:00
|
|
|
|
2016-04-18 19:04:35 -04:00
|
|
|
fixture.debugElement.componentInstance.form = new ControlGroup({});
|
|
|
|
fixture.debugElement.componentInstance.name = 'old';
|
2015-06-05 17:29:50 -04:00
|
|
|
|
2016-04-18 19:04:35 -04:00
|
|
|
tick();
|
2015-06-05 17:29:50 -04:00
|
|
|
|
2016-04-18 19:04:35 -04:00
|
|
|
var form = fixture.debugElement.query(By.css("form"));
|
|
|
|
dispatchEvent(form.nativeElement, "submit");
|
2015-06-25 00:51:37 -04:00
|
|
|
|
2016-04-18 19:04:35 -04:00
|
|
|
tick();
|
|
|
|
expect(fixture.debugElement.componentInstance.name).toEqual('updated');
|
|
|
|
})));
|
2015-06-05 17:29:50 -04:00
|
|
|
|
2015-12-06 14:21:53 -05:00
|
|
|
it("should mark NgForm as submitted on submit event",
|
|
|
|
inject([TestComponentBuilder], fakeAsync((tcb: TestComponentBuilder) => {
|
|
|
|
var t = `<div>
|
|
|
|
<form #f="ngForm" (ngSubmit)="data=f.submitted"></form>
|
|
|
|
<span>{{data}}</span>
|
|
|
|
</div>`;
|
|
|
|
|
|
|
|
var fixture: ComponentFixture<MyComp8>;
|
|
|
|
|
|
|
|
tcb.overrideTemplate(MyComp8, t).createAsync(MyComp8).then(
|
|
|
|
(root) => { fixture = root; });
|
|
|
|
tick();
|
|
|
|
|
|
|
|
fixture.debugElement.componentInstance.data = false;
|
|
|
|
|
|
|
|
tick();
|
|
|
|
|
|
|
|
var form = fixture.debugElement.query(By.css("form"));
|
|
|
|
dispatchEvent(form.nativeElement, "submit");
|
|
|
|
|
|
|
|
tick();
|
|
|
|
expect(fixture.debugElement.componentInstance.data).toEqual(true);
|
|
|
|
})));
|
|
|
|
|
|
|
|
it("should mark NgFormModel as submitted on submit event",
|
|
|
|
inject([TestComponentBuilder], fakeAsync((tcb: TestComponentBuilder) => {
|
|
|
|
var t = `<div>
|
|
|
|
<form #f="ngForm" [ngFormModel]="form" (ngSubmit)="data=f.submitted"></form>
|
|
|
|
<span>{{data}}</span>
|
|
|
|
</div>`;
|
|
|
|
|
|
|
|
var fixture: ComponentFixture<MyComp8>;
|
|
|
|
|
|
|
|
tcb.overrideTemplate(MyComp8, t).createAsync(MyComp8).then(
|
|
|
|
(root) => { fixture = root; });
|
|
|
|
tick();
|
|
|
|
|
|
|
|
fixture.debugElement.componentInstance.form = new ControlGroup({});
|
|
|
|
fixture.debugElement.componentInstance.data = false;
|
|
|
|
|
|
|
|
tick();
|
|
|
|
|
|
|
|
var form = fixture.debugElement.query(By.css("form"));
|
|
|
|
dispatchEvent(form.nativeElement, "submit");
|
|
|
|
|
|
|
|
tick();
|
|
|
|
expect(fixture.debugElement.componentInstance.data).toEqual(true);
|
|
|
|
})));
|
|
|
|
|
2016-04-12 12:40:37 -04:00
|
|
|
it("should work with single controls",
|
2015-06-25 00:51:37 -04:00
|
|
|
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
|
2016-04-12 12:40:37 -04:00
|
|
|
var control = new Control("loginValue");
|
2015-05-22 15:32:49 -04:00
|
|
|
|
2015-11-23 19:02:19 -05:00
|
|
|
var t = `<div><input type="text" [ngFormControl]="form"></div>`;
|
2015-05-22 15:32:49 -04:00
|
|
|
|
2016-05-02 01:50:37 -04:00
|
|
|
tcb.overrideTemplate(MyComp8, t)
|
|
|
|
.createAsync(MyComp8)
|
|
|
|
.then((fixture) => {
|
|
|
|
fixture.debugElement.componentInstance.form = control;
|
|
|
|
fixture.detectChanges();
|
2015-06-25 00:51:37 -04:00
|
|
|
|
2016-05-02 01:50:37 -04:00
|
|
|
var input = fixture.debugElement.query(By.css("input"));
|
|
|
|
expect(input.nativeElement.value).toEqual("loginValue");
|
2015-05-22 15:32:49 -04:00
|
|
|
|
2016-05-02 01:50:37 -04:00
|
|
|
input.nativeElement.value = "updatedValue";
|
|
|
|
dispatchEvent(input.nativeElement, "input");
|
2015-05-22 15:32:49 -04:00
|
|
|
|
2016-05-02 01:50:37 -04:00
|
|
|
expect(control.value).toEqual("updatedValue");
|
|
|
|
async.done();
|
|
|
|
});
|
2015-05-22 15:32:49 -04:00
|
|
|
}));
|
|
|
|
|
2016-04-12 12:40:37 -04:00
|
|
|
it("should update DOM elements when rebinding the control group",
|
2015-06-25 00:51:37 -04:00
|
|
|
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
|
2015-11-23 19:02:19 -05:00
|
|
|
var t = `<div [ngFormModel]="form">
|
|
|
|
<input type="text" ngControl="login">
|
2015-05-30 14:56:00 -04:00
|
|
|
</div>`;
|
2015-05-22 15:32:49 -04:00
|
|
|
|
2016-05-02 01:50:37 -04:00
|
|
|
tcb.overrideTemplate(MyComp8, t)
|
|
|
|
.createAsync(MyComp8)
|
|
|
|
.then((fixture) => {
|
|
|
|
fixture.debugElement.componentInstance.form =
|
|
|
|
new ControlGroup({"login": new Control("oldValue")});
|
|
|
|
fixture.detectChanges();
|
2015-06-25 00:51:37 -04:00
|
|
|
|
2016-05-02 01:50:37 -04:00
|
|
|
fixture.debugElement.componentInstance.form =
|
|
|
|
new ControlGroup({"login": new Control("newValue")});
|
|
|
|
fixture.detectChanges();
|
2015-05-22 15:32:49 -04:00
|
|
|
|
2016-05-02 01:50:37 -04:00
|
|
|
var input = fixture.debugElement.query(By.css("input"));
|
|
|
|
expect(input.nativeElement.value).toEqual("newValue");
|
|
|
|
async.done();
|
|
|
|
});
|
2015-06-01 13:41:50 -04:00
|
|
|
}));
|
|
|
|
|
2016-04-12 12:40:37 -04:00
|
|
|
it("should update DOM elements when updating the value of a control",
|
2015-06-25 00:51:37 -04:00
|
|
|
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
|
2016-04-12 12:40:37 -04:00
|
|
|
var login = new Control("oldValue");
|
|
|
|
var form = new ControlGroup({"login": login});
|
2015-06-01 13:41:50 -04:00
|
|
|
|
2015-11-23 19:02:19 -05:00
|
|
|
var t = `<div [ngFormModel]="form">
|
|
|
|
<input type="text" ngControl="login">
|
2015-06-01 13:41:50 -04:00
|
|
|
</div>`;
|
|
|
|
|
2016-05-02 01:50:37 -04:00
|
|
|
tcb.overrideTemplate(MyComp8, t)
|
|
|
|
.createAsync(MyComp8)
|
|
|
|
.then((fixture) => {
|
|
|
|
fixture.debugElement.componentInstance.form = form;
|
|
|
|
fixture.detectChanges();
|
2015-06-01 13:41:50 -04:00
|
|
|
|
2016-05-02 01:50:37 -04:00
|
|
|
login.updateValue("newValue");
|
2015-06-01 13:41:50 -04:00
|
|
|
|
2016-05-02 01:50:37 -04:00
|
|
|
fixture.detectChanges();
|
2015-06-01 13:41:50 -04:00
|
|
|
|
2016-05-02 01:50:37 -04:00
|
|
|
var input = fixture.debugElement.query(By.css("input"));
|
|
|
|
expect(input.nativeElement.value).toEqual("newValue");
|
|
|
|
async.done();
|
|
|
|
});
|
2015-05-22 15:32:49 -04:00
|
|
|
}));
|
|
|
|
|
2016-04-12 12:40:37 -04:00
|
|
|
it("should mark controls as touched after interacting with the DOM control",
|
2015-06-25 00:51:37 -04:00
|
|
|
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
|
2016-04-12 12:40:37 -04:00
|
|
|
var login = new Control("oldValue");
|
|
|
|
var form = new ControlGroup({"login": login});
|
2015-06-02 11:41:33 -04:00
|
|
|
|
2015-11-23 19:02:19 -05:00
|
|
|
var t = `<div [ngFormModel]="form">
|
|
|
|
<input type="text" ngControl="login">
|
2015-06-02 11:41:33 -04:00
|
|
|
</div>`;
|
|
|
|
|
2016-05-02 01:50:37 -04:00
|
|
|
tcb.overrideTemplate(MyComp8, t)
|
|
|
|
.createAsync(MyComp8)
|
|
|
|
.then((fixture) => {
|
|
|
|
fixture.debugElement.componentInstance.form = form;
|
|
|
|
fixture.detectChanges();
|
2015-06-02 11:41:33 -04:00
|
|
|
|
2016-05-02 01:50:37 -04:00
|
|
|
var loginEl = fixture.debugElement.query(By.css("input"));
|
|
|
|
expect(login.touched).toBe(false);
|
2015-06-02 11:41:33 -04:00
|
|
|
|
2016-05-02 01:50:37 -04:00
|
|
|
dispatchEvent(loginEl.nativeElement, "blur");
|
2015-06-02 11:41:33 -04:00
|
|
|
|
2016-05-02 01:50:37 -04:00
|
|
|
expect(login.touched).toBe(true);
|
2015-06-02 11:41:33 -04:00
|
|
|
|
2016-05-02 01:50:37 -04:00
|
|
|
async.done();
|
|
|
|
});
|
2015-06-02 11:41:33 -04:00
|
|
|
}));
|
|
|
|
|
2016-04-12 12:40:37 -04:00
|
|
|
describe("different control types", () => {
|
|
|
|
it("should support <input type=text>",
|
2015-06-25 00:51:37 -04:00
|
|
|
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
|
2015-11-23 19:02:19 -05:00
|
|
|
var t = `<div [ngFormModel]="form">
|
|
|
|
<input type="text" ngControl="text">
|
2015-05-22 15:32:49 -04:00
|
|
|
</div>`;
|
|
|
|
|
2016-05-02 01:50:37 -04:00
|
|
|
tcb.overrideTemplate(MyComp8, t)
|
|
|
|
.createAsync(MyComp8)
|
|
|
|
.then((fixture) => {
|
|
|
|
fixture.debugElement.componentInstance.form =
|
|
|
|
new ControlGroup({"text": new Control("old")});
|
|
|
|
fixture.detectChanges();
|
2015-05-22 15:32:49 -04:00
|
|
|
|
2016-05-02 01:50:37 -04:00
|
|
|
var input = fixture.debugElement.query(By.css("input"));
|
|
|
|
expect(input.nativeElement.value).toEqual("old");
|
2015-05-22 15:32:49 -04:00
|
|
|
|
2016-05-02 01:50:37 -04:00
|
|
|
input.nativeElement.value = "new";
|
|
|
|
dispatchEvent(input.nativeElement, "input");
|
2015-06-25 00:51:37 -04:00
|
|
|
|
2016-05-02 01:50:37 -04:00
|
|
|
expect(fixture.debugElement.componentInstance.form.value).toEqual({"text": "new"});
|
|
|
|
async.done();
|
|
|
|
});
|
2015-05-22 15:32:49 -04:00
|
|
|
}));
|
|
|
|
|
2016-04-12 12:40:37 -04:00
|
|
|
it("should support <input> without type",
|
2015-06-25 00:51:37 -04:00
|
|
|
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
|
2015-11-23 19:02:19 -05:00
|
|
|
var t = `<div [ngFormModel]="form">
|
|
|
|
<input ngControl="text">
|
2015-05-22 15:32:49 -04:00
|
|
|
</div>`;
|
|
|
|
|
2016-05-02 01:50:37 -04:00
|
|
|
tcb.overrideTemplate(MyComp8, t)
|
|
|
|
.createAsync(MyComp8)
|
|
|
|
.then((fixture) => {
|
|
|
|
fixture.debugElement.componentInstance.form =
|
|
|
|
new ControlGroup({"text": new Control("old")});
|
|
|
|
fixture.detectChanges();
|
|
|
|
var input = fixture.debugElement.query(By.css("input"));
|
|
|
|
expect(input.nativeElement.value).toEqual("old");
|
|
|
|
|
|
|
|
input.nativeElement.value = "new";
|
|
|
|
dispatchEvent(input.nativeElement, "input");
|
|
|
|
|
|
|
|
expect(fixture.debugElement.componentInstance.form.value).toEqual({"text": "new"});
|
|
|
|
async.done();
|
|
|
|
});
|
2015-05-22 15:32:49 -04:00
|
|
|
}));
|
|
|
|
|
2016-04-12 12:40:37 -04:00
|
|
|
it("should support <textarea>",
|
2015-06-25 00:51:37 -04:00
|
|
|
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
|
2015-11-23 19:02:19 -05:00
|
|
|
var t = `<div [ngFormModel]="form">
|
|
|
|
<textarea ngControl="text"></textarea>
|
2015-05-22 15:32:49 -04:00
|
|
|
</div>`;
|
|
|
|
|
2016-05-02 01:50:37 -04:00
|
|
|
tcb.overrideTemplate(MyComp8, t)
|
|
|
|
.createAsync(MyComp8)
|
|
|
|
.then((fixture) => {
|
|
|
|
fixture.debugElement.componentInstance.form =
|
|
|
|
new ControlGroup({"text": new Control('old')});
|
|
|
|
fixture.detectChanges();
|
2015-05-22 15:32:49 -04:00
|
|
|
|
2016-05-02 01:50:37 -04:00
|
|
|
var textarea = fixture.debugElement.query(By.css("textarea"));
|
|
|
|
expect(textarea.nativeElement.value).toEqual("old");
|
2015-05-22 15:32:49 -04:00
|
|
|
|
2016-05-02 01:50:37 -04:00
|
|
|
textarea.nativeElement.value = "new";
|
|
|
|
dispatchEvent(textarea.nativeElement, "input");
|
2015-06-25 00:51:37 -04:00
|
|
|
|
2016-05-02 01:50:37 -04:00
|
|
|
expect(fixture.debugElement.componentInstance.form.value).toEqual({"text": 'new'});
|
|
|
|
async.done();
|
|
|
|
});
|
2015-05-22 15:32:49 -04:00
|
|
|
}));
|
|
|
|
|
2016-04-12 12:40:37 -04:00
|
|
|
it("should support <type=checkbox>",
|
2015-06-25 00:51:37 -04:00
|
|
|
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
|
2015-11-23 19:02:19 -05:00
|
|
|
var t = `<div [ngFormModel]="form">
|
|
|
|
<input type="checkbox" ngControl="checkbox">
|
2015-05-22 15:32:49 -04:00
|
|
|
</div>`;
|
|
|
|
|
2016-05-02 01:50:37 -04:00
|
|
|
tcb.overrideTemplate(MyComp8, t)
|
|
|
|
.createAsync(MyComp8)
|
|
|
|
.then((fixture) => {
|
|
|
|
fixture.debugElement.componentInstance.form =
|
|
|
|
new ControlGroup({"checkbox": new Control(true)});
|
|
|
|
fixture.detectChanges();
|
2015-05-22 15:32:49 -04:00
|
|
|
|
2016-05-02 01:50:37 -04:00
|
|
|
var input = fixture.debugElement.query(By.css("input"));
|
|
|
|
expect(input.nativeElement.checked).toBe(true);
|
2015-05-22 15:32:49 -04:00
|
|
|
|
2016-05-02 01:50:37 -04:00
|
|
|
input.nativeElement.checked = false;
|
|
|
|
dispatchEvent(input.nativeElement, "change");
|
2015-05-22 15:32:49 -04:00
|
|
|
|
2016-05-02 01:50:37 -04:00
|
|
|
expect(fixture.debugElement.componentInstance.form.value)
|
|
|
|
.toEqual({"checkbox": false});
|
|
|
|
async.done();
|
|
|
|
});
|
2015-06-25 00:51:37 -04:00
|
|
|
}));
|
2015-05-22 15:32:49 -04:00
|
|
|
|
2016-04-12 12:40:37 -04:00
|
|
|
it("should support <type=number>",
|
2015-10-15 14:38:34 -04:00
|
|
|
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
|
2015-11-23 19:02:19 -05:00
|
|
|
var t = `<div [ngFormModel]="form">
|
|
|
|
<input type="number" ngControl="num">
|
2015-10-15 14:38:34 -04:00
|
|
|
</div>`;
|
|
|
|
|
2016-05-02 01:50:37 -04:00
|
|
|
tcb.overrideTemplate(MyComp8, t)
|
|
|
|
.createAsync(MyComp8)
|
|
|
|
.then((fixture) => {
|
|
|
|
fixture.debugElement.componentInstance.form =
|
|
|
|
new ControlGroup({"num": new Control(10)});
|
|
|
|
fixture.detectChanges();
|
2015-10-15 14:38:34 -04:00
|
|
|
|
2016-05-02 01:50:37 -04:00
|
|
|
var input = fixture.debugElement.query(By.css("input"));
|
|
|
|
expect(input.nativeElement.value).toEqual("10");
|
2015-10-15 14:38:34 -04:00
|
|
|
|
2016-05-02 01:50:37 -04:00
|
|
|
input.nativeElement.value = "20";
|
|
|
|
dispatchEvent(input.nativeElement, "input");
|
2015-10-15 14:38:34 -04:00
|
|
|
|
2016-05-02 01:50:37 -04:00
|
|
|
expect(fixture.debugElement.componentInstance.form.value).toEqual({"num": 20});
|
|
|
|
async.done();
|
|
|
|
});
|
2015-10-15 14:38:34 -04:00
|
|
|
}));
|
|
|
|
|
2016-04-20 10:13:19 -04:00
|
|
|
it("should support <type=number> when value is cleared in the UI",
|
|
|
|
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
|
|
|
|
var t = `<div [ngFormModel]="form">
|
|
|
|
<input type="number" ngControl="num" required>
|
|
|
|
</div>`;
|
|
|
|
|
2016-05-02 01:50:37 -04:00
|
|
|
tcb.overrideTemplate(MyComp8, t)
|
|
|
|
.createAsync(MyComp8)
|
|
|
|
.then((fixture) => {
|
|
|
|
fixture.debugElement.componentInstance.form =
|
|
|
|
new ControlGroup({"num": new Control(10)});
|
|
|
|
fixture.detectChanges();
|
2016-04-20 10:13:19 -04:00
|
|
|
|
2016-05-02 01:50:37 -04:00
|
|
|
var input = fixture.debugElement.query(By.css("input"));
|
|
|
|
input.nativeElement.value = "";
|
|
|
|
dispatchEvent(input.nativeElement, "input");
|
2016-04-20 10:13:19 -04:00
|
|
|
|
2016-05-02 01:50:37 -04:00
|
|
|
expect(fixture.debugElement.componentInstance.form.valid).toBe(false);
|
|
|
|
expect(fixture.debugElement.componentInstance.form.value).toEqual({"num": null});
|
2016-04-20 10:13:19 -04:00
|
|
|
|
2016-05-02 01:50:37 -04:00
|
|
|
input.nativeElement.value = "0";
|
|
|
|
dispatchEvent(input.nativeElement, "input");
|
2016-04-20 10:13:19 -04:00
|
|
|
|
2016-05-02 01:50:37 -04:00
|
|
|
expect(fixture.debugElement.componentInstance.form.valid).toBe(true);
|
|
|
|
expect(fixture.debugElement.componentInstance.form.value).toEqual({"num": 0});
|
|
|
|
async.done();
|
|
|
|
});
|
2016-04-20 10:13:19 -04:00
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
|
|
it("should support <type=number> when value is cleared programmatically",
|
|
|
|
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
|
|
|
|
var form = new ControlGroup({"num": new Control(10)});
|
|
|
|
var t = `<div [ngFormModel]="form">
|
|
|
|
<input type="number" ngControl="num" [(ngModel)]="data">
|
|
|
|
</div>`;
|
|
|
|
|
2016-05-02 01:50:37 -04:00
|
|
|
tcb.overrideTemplate(MyComp8, t)
|
|
|
|
.createAsync(MyComp8)
|
|
|
|
.then((fixture) => {
|
|
|
|
fixture.debugElement.componentInstance.form = form;
|
|
|
|
fixture.debugElement.componentInstance.data = null;
|
|
|
|
fixture.detectChanges();
|
2016-04-20 10:13:19 -04:00
|
|
|
|
2016-05-02 01:50:37 -04:00
|
|
|
var input = fixture.debugElement.query(By.css("input"));
|
|
|
|
expect(input.nativeElement.value).toEqual("");
|
2016-04-20 10:13:19 -04:00
|
|
|
|
2016-05-02 01:50:37 -04:00
|
|
|
async.done();
|
|
|
|
});
|
2016-04-20 10:13:19 -04:00
|
|
|
}));
|
|
|
|
|
2016-04-12 12:40:37 -04:00
|
|
|
it("should support <type=radio>",
|
2016-02-05 19:08:53 -05:00
|
|
|
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
|
|
|
|
var t = `<form [ngFormModel]="form">
|
|
|
|
<input type="radio" ngControl="foodChicken" name="food">
|
|
|
|
<input type="radio" ngControl="foodFish" name="food">
|
|
|
|
</form>`;
|
|
|
|
|
2016-05-02 01:50:37 -04:00
|
|
|
tcb.overrideTemplate(MyComp8, t)
|
|
|
|
.createAsync(MyComp8)
|
|
|
|
.then((fixture) => {
|
|
|
|
fixture.debugElement.componentInstance.form = new ControlGroup({
|
|
|
|
"foodChicken": new Control(new RadioButtonState(false, 'chicken')),
|
|
|
|
"foodFish": new Control(new RadioButtonState(true, 'fish'))
|
|
|
|
});
|
|
|
|
fixture.detectChanges();
|
|
|
|
|
|
|
|
var input = fixture.debugElement.query(By.css("input"));
|
|
|
|
expect(input.nativeElement.checked).toEqual(false);
|
|
|
|
|
|
|
|
dispatchEvent(input.nativeElement, "change");
|
|
|
|
fixture.detectChanges();
|
|
|
|
|
|
|
|
let value = fixture.debugElement.componentInstance.form.value;
|
|
|
|
expect(value['foodChicken'].checked).toEqual(true);
|
|
|
|
expect(value['foodFish'].checked).toEqual(false);
|
|
|
|
async.done();
|
|
|
|
});
|
2016-02-05 19:08:53 -05:00
|
|
|
}));
|
|
|
|
|
2016-04-12 12:40:37 -04:00
|
|
|
describe("should support <select>", () => {
|
|
|
|
it("with basic selection",
|
|
|
|
inject([TestComponentBuilder, AsyncTestCompleter],
|
|
|
|
(tcb: TestComponentBuilder, async) => {
|
|
|
|
var t = `<select>
|
2016-04-01 22:27:25 -04:00
|
|
|
<option value="SF"></option>
|
|
|
|
<option value="NYC"></option>
|
|
|
|
</select>`;
|
|
|
|
|
2016-05-02 01:50:37 -04:00
|
|
|
tcb.overrideTemplate(MyComp8, t)
|
|
|
|
.createAsync(MyComp8)
|
|
|
|
.then((fixture) => {
|
|
|
|
fixture.detectChanges();
|
2016-04-01 22:27:25 -04:00
|
|
|
|
2016-05-02 01:50:37 -04:00
|
|
|
var select = fixture.debugElement.query(By.css("select"));
|
|
|
|
var sfOption = fixture.debugElement.query(By.css("option"));
|
2016-04-01 22:27:25 -04:00
|
|
|
|
2016-05-02 01:50:37 -04:00
|
|
|
expect(select.nativeElement.value).toEqual("SF");
|
|
|
|
expect(sfOption.nativeElement.selected).toBe(true);
|
|
|
|
async.done();
|
|
|
|
});
|
2016-04-12 12:40:37 -04:00
|
|
|
}));
|
2016-04-01 22:27:25 -04:00
|
|
|
|
2016-04-12 12:40:37 -04:00
|
|
|
it("with basic selection and value bindings",
|
|
|
|
inject([TestComponentBuilder, AsyncTestCompleter],
|
|
|
|
(tcb: TestComponentBuilder, async) => {
|
|
|
|
var t = `<select>
|
2016-04-25 22:52:24 -04:00
|
|
|
<option *ngFor="let city of list" [value]="city['id']">
|
2016-04-08 15:00:04 -04:00
|
|
|
{{ city['name'] }}
|
|
|
|
</option>
|
|
|
|
</select>`;
|
|
|
|
|
2016-05-02 01:50:37 -04:00
|
|
|
tcb.overrideTemplate(MyComp8, t)
|
|
|
|
.createAsync(MyComp8)
|
|
|
|
.then((fixture) => {
|
|
|
|
var testComp = fixture.debugElement.componentInstance;
|
|
|
|
testComp.list = [{"id": "0", "name": "SF"}, {"id": "1", "name": "NYC"}];
|
|
|
|
fixture.detectChanges();
|
|
|
|
|
|
|
|
var sfOption = fixture.debugElement.query(By.css("option"));
|
|
|
|
expect(sfOption.nativeElement.value).toEqual('0');
|
|
|
|
|
|
|
|
testComp.list[0]['id'] = '2';
|
|
|
|
fixture.detectChanges();
|
|
|
|
expect(sfOption.nativeElement.value).toEqual('2');
|
|
|
|
async.done();
|
|
|
|
});
|
2016-04-12 12:40:37 -04:00
|
|
|
}));
|
|
|
|
|
|
|
|
it("with ngControl",
|
2016-05-02 01:50:37 -04:00
|
|
|
inject([TestComponentBuilder, AsyncTestCompleter],
|
|
|
|
(tcb: TestComponentBuilder, async) => {
|
|
|
|
var t = `<div [ngFormModel]="form">
|
2015-11-23 19:02:19 -05:00
|
|
|
<select ngControl="city">
|
2015-05-22 15:32:49 -04:00
|
|
|
<option value="SF"></option>
|
|
|
|
<option value="NYC"></option>
|
|
|
|
</select>
|
|
|
|
</div>`;
|
|
|
|
|
2016-05-02 01:50:37 -04:00
|
|
|
tcb.overrideTemplate(MyComp8, t)
|
|
|
|
.createAsync(MyComp8)
|
|
|
|
.then((fixture) => {
|
|
|
|
fixture.debugElement.componentInstance.form =
|
|
|
|
new ControlGroup({"city": new Control("SF")});
|
|
|
|
fixture.detectChanges();
|
2015-06-25 00:51:37 -04:00
|
|
|
|
2016-05-02 01:50:37 -04:00
|
|
|
var select = fixture.debugElement.query(By.css("select"));
|
|
|
|
var sfOption = fixture.debugElement.query(By.css("option"));
|
2015-06-25 00:51:37 -04:00
|
|
|
|
2016-04-06 17:28:44 -04:00
|
|
|
|
2016-05-02 01:50:37 -04:00
|
|
|
expect(select.nativeElement.value).toEqual("SF");
|
|
|
|
expect(sfOption.nativeElement.selected).toBe(true);
|
2015-06-25 00:51:37 -04:00
|
|
|
|
2016-05-02 01:50:37 -04:00
|
|
|
select.nativeElement.value = "NYC";
|
|
|
|
dispatchEvent(select.nativeElement, "change");
|
2015-05-22 15:32:49 -04:00
|
|
|
|
2016-05-02 01:50:37 -04:00
|
|
|
expect(fixture.debugElement.componentInstance.form.value)
|
|
|
|
.toEqual({"city": 'NYC'});
|
|
|
|
expect(sfOption.nativeElement.selected).toBe(false);
|
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
2016-04-01 22:27:25 -04:00
|
|
|
|
2016-04-12 12:40:37 -04:00
|
|
|
it("with a dynamic list of options",
|
2016-04-18 19:04:35 -04:00
|
|
|
fakeAsync(inject([TestComponentBuilder], (tcb: TestComponentBuilder) => {
|
|
|
|
var t = `<div [ngFormModel]="form">
|
2015-11-23 19:02:19 -05:00
|
|
|
<select ngControl="city">
|
2016-04-25 22:52:24 -04:00
|
|
|
<option *ngFor="let c of data" [value]="c"></option>
|
2015-06-13 15:15:42 -04:00
|
|
|
</select>
|
|
|
|
</div>`;
|
|
|
|
|
2016-04-18 19:04:35 -04:00
|
|
|
var fixture;
|
2016-05-02 01:50:37 -04:00
|
|
|
tcb.overrideTemplate(MyComp8, t)
|
|
|
|
.createAsync(MyComp8)
|
|
|
|
.then((compFixture) => fixture = compFixture);
|
2016-04-18 19:04:35 -04:00
|
|
|
tick();
|
2016-04-01 22:27:25 -04:00
|
|
|
|
2016-04-18 19:04:35 -04:00
|
|
|
fixture.debugElement.componentInstance.form =
|
|
|
|
new ControlGroup({"city": new Control("NYC")});
|
2016-04-06 17:28:44 -04:00
|
|
|
|
2016-04-18 19:04:35 -04:00
|
|
|
fixture.debugElement.componentInstance.data = ['SF', 'NYC'];
|
|
|
|
fixture.detectChanges();
|
|
|
|
tick();
|
2016-04-01 22:27:25 -04:00
|
|
|
|
2016-04-18 19:04:35 -04:00
|
|
|
var select = fixture.debugElement.query(By.css("select"));
|
|
|
|
expect(select.nativeElement.value).toEqual("NYC");
|
|
|
|
})));
|
2016-04-01 22:27:25 -04:00
|
|
|
|
2016-04-12 12:40:37 -04:00
|
|
|
it("with option values that are objects",
|
|
|
|
inject([TestComponentBuilder, AsyncTestCompleter],
|
|
|
|
(tcb: TestComponentBuilder, async) => {
|
|
|
|
var t = `<div>
|
2016-04-01 22:27:25 -04:00
|
|
|
<select [(ngModel)]="selectedCity">
|
2016-04-25 22:52:24 -04:00
|
|
|
<option *ngFor="let c of list" [ngValue]="c">{{c['name']}}</option>
|
2016-04-01 22:27:25 -04:00
|
|
|
</select>
|
|
|
|
</div>`;
|
2015-06-13 15:15:42 -04:00
|
|
|
|
2016-05-02 01:50:37 -04:00
|
|
|
tcb.overrideTemplate(MyComp8, t)
|
|
|
|
.createAsync(MyComp8)
|
|
|
|
.then((fixture) => {
|
|
|
|
|
|
|
|
var testComp = fixture.debugElement.componentInstance;
|
|
|
|
testComp.list = [{"name": "SF"}, {"name": "NYC"}, {"name": "Buffalo"}];
|
|
|
|
testComp.selectedCity = testComp.list[1];
|
|
|
|
fixture.detectChanges();
|
|
|
|
|
|
|
|
var select = fixture.debugElement.query(By.css("select"));
|
|
|
|
var nycOption = fixture.debugElement.queryAll(By.css("option"))[1];
|
|
|
|
|
|
|
|
expect(select.nativeElement.value).toEqual("1: Object");
|
|
|
|
expect(nycOption.nativeElement.selected).toBe(true);
|
|
|
|
|
|
|
|
select.nativeElement.value = "2: Object";
|
|
|
|
dispatchEvent(select.nativeElement, "change");
|
|
|
|
fixture.detectChanges();
|
|
|
|
TimerWrapper.setTimeout(() => {
|
|
|
|
expect(testComp.selectedCity['name']).toEqual("Buffalo");
|
|
|
|
async.done();
|
|
|
|
}, 0);
|
|
|
|
});
|
2016-04-12 12:40:37 -04:00
|
|
|
}));
|
|
|
|
|
2016-05-23 21:43:32 -04:00
|
|
|
it("when new options are added (selection through the model)",
|
2016-04-12 12:40:37 -04:00
|
|
|
inject([TestComponentBuilder, AsyncTestCompleter],
|
|
|
|
(tcb: TestComponentBuilder, async) => {
|
|
|
|
var t = `<div>
|
2016-04-01 22:27:25 -04:00
|
|
|
<select [(ngModel)]="selectedCity">
|
2016-04-25 22:52:24 -04:00
|
|
|
<option *ngFor="let c of list" [ngValue]="c">{{c['name']}}</option>
|
2016-04-01 22:27:25 -04:00
|
|
|
</select>
|
|
|
|
</div>`;
|
2015-09-30 11:41:00 -04:00
|
|
|
|
2016-05-02 01:50:37 -04:00
|
|
|
tcb.overrideTemplate(MyComp8, t)
|
|
|
|
.createAsync(MyComp8)
|
|
|
|
.then((fixture) => {
|
|
|
|
|
|
|
|
var testComp: MyComp8 = fixture.debugElement.componentInstance;
|
|
|
|
testComp.list = [{"name": "SF"}, {"name": "NYC"}];
|
|
|
|
testComp.selectedCity = testComp.list[1];
|
|
|
|
fixture.detectChanges();
|
|
|
|
|
|
|
|
testComp.list.push({"name": "Buffalo"});
|
|
|
|
testComp.selectedCity = testComp.list[2];
|
|
|
|
fixture.detectChanges();
|
|
|
|
|
|
|
|
var select = fixture.debugElement.query(By.css("select"));
|
|
|
|
var buffalo = fixture.debugElement.queryAll(By.css("option"))[2];
|
|
|
|
expect(select.nativeElement.value).toEqual("2: Object");
|
|
|
|
expect(buffalo.nativeElement.selected).toBe(true);
|
|
|
|
async.done();
|
|
|
|
});
|
2016-04-12 12:40:37 -04:00
|
|
|
}));
|
|
|
|
|
2016-05-23 21:43:32 -04:00
|
|
|
it("when new options are added (selection through the UI)",
|
|
|
|
inject([TestComponentBuilder, AsyncTestCompleter],
|
|
|
|
(tcb: TestComponentBuilder, async) => {
|
|
|
|
var t = `<div>
|
|
|
|
<select [(ngModel)]="selectedCity">
|
|
|
|
<option *ngFor="let c of list" [ngValue]="c">{{c['name']}}</option>
|
|
|
|
</select>
|
|
|
|
</div>`;
|
|
|
|
|
|
|
|
tcb.overrideTemplate(MyComp8, t)
|
|
|
|
.createAsync(MyComp8)
|
|
|
|
.then((fixture) => {
|
|
|
|
|
|
|
|
var testComp: MyComp8 = fixture.debugElement.componentInstance;
|
|
|
|
testComp.list = [{"name": "SF"}, {"name": "NYC"}];
|
|
|
|
testComp.selectedCity = testComp.list[0];
|
|
|
|
fixture.detectChanges();
|
|
|
|
|
|
|
|
var select = fixture.debugElement.query(By.css("select"));
|
|
|
|
var ny = fixture.debugElement.queryAll(By.css("option"))[1];
|
|
|
|
|
|
|
|
select.nativeElement.value = "1: Object";
|
|
|
|
dispatchEvent(select.nativeElement, "change");
|
|
|
|
testComp.list.push({"name": "Buffalo"});
|
|
|
|
fixture.detectChanges();
|
|
|
|
|
|
|
|
expect(select.nativeElement.value).toEqual("1: Object");
|
|
|
|
expect(ny.nativeElement.selected).toBe(true);
|
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
2016-04-12 12:40:37 -04:00
|
|
|
it("when options are removed",
|
|
|
|
inject([TestComponentBuilder, AsyncTestCompleter],
|
|
|
|
(tcb: TestComponentBuilder, async) => {
|
|
|
|
var t = `<div>
|
2016-04-01 22:27:25 -04:00
|
|
|
<select [(ngModel)]="selectedCity">
|
2016-04-25 22:52:24 -04:00
|
|
|
<option *ngFor="let c of list" [ngValue]="c">{{c}}</option>
|
2016-04-01 22:27:25 -04:00
|
|
|
</select>
|
|
|
|
</div>`;
|
2016-05-02 01:50:37 -04:00
|
|
|
tcb.overrideTemplate(MyComp8, t)
|
|
|
|
.createAsync(MyComp8)
|
|
|
|
.then((fixture) => {
|
2016-04-01 22:27:25 -04:00
|
|
|
|
2016-05-02 01:50:37 -04:00
|
|
|
var testComp: MyComp8 = fixture.debugElement.componentInstance;
|
|
|
|
testComp.list = [{"name": "SF"}, {"name": "NYC"}];
|
|
|
|
testComp.selectedCity = testComp.list[1];
|
|
|
|
fixture.detectChanges();
|
2016-04-01 22:27:25 -04:00
|
|
|
|
2016-05-02 01:50:37 -04:00
|
|
|
var select = fixture.debugElement.query(By.css("select"));
|
|
|
|
expect(select.nativeElement.value).toEqual("1: Object");
|
2016-04-01 22:27:25 -04:00
|
|
|
|
2016-05-02 01:50:37 -04:00
|
|
|
testComp.list.pop();
|
|
|
|
fixture.detectChanges();
|
2016-04-01 22:27:25 -04:00
|
|
|
|
2016-05-02 01:50:37 -04:00
|
|
|
expect(select.nativeElement.value).not.toEqual("1: Object");
|
|
|
|
async.done();
|
|
|
|
});
|
2016-04-12 12:40:37 -04:00
|
|
|
}));
|
2016-04-01 22:27:25 -04:00
|
|
|
|
2016-04-12 12:40:37 -04:00
|
|
|
it("when option values change identity while tracking by index",
|
|
|
|
inject([TestComponentBuilder, AsyncTestCompleter],
|
|
|
|
(tcb: TestComponentBuilder, async) => {
|
|
|
|
var t = `<div>
|
2016-04-01 22:27:25 -04:00
|
|
|
<select [(ngModel)]="selectedCity">
|
2016-04-25 22:52:24 -04:00
|
|
|
<option *ngFor="let c of list; trackBy:customTrackBy" [ngValue]="c">{{c}}</option>
|
2016-04-01 22:27:25 -04:00
|
|
|
</select>
|
|
|
|
</div>`;
|
|
|
|
|
2016-05-02 01:50:37 -04:00
|
|
|
tcb.overrideTemplate(MyComp8, t)
|
|
|
|
.createAsync(MyComp8)
|
|
|
|
.then((fixture) => {
|
2016-04-01 22:27:25 -04:00
|
|
|
|
2016-05-02 01:50:37 -04:00
|
|
|
var testComp = fixture.debugElement.componentInstance;
|
2016-04-01 22:27:25 -04:00
|
|
|
|
2016-05-02 01:50:37 -04:00
|
|
|
testComp.list = [{"name": "SF"}, {"name": "NYC"}];
|
|
|
|
testComp.selectedCity = testComp.list[0];
|
|
|
|
fixture.detectChanges();
|
2016-04-01 22:27:25 -04:00
|
|
|
|
2016-05-02 01:50:37 -04:00
|
|
|
testComp.list[1] = "Buffalo";
|
|
|
|
testComp.selectedCity = testComp.list[1];
|
|
|
|
fixture.detectChanges();
|
2016-04-01 22:27:25 -04:00
|
|
|
|
2016-05-02 01:50:37 -04:00
|
|
|
var select = fixture.debugElement.query(By.css("select"));
|
|
|
|
var buffalo = fixture.debugElement.queryAll(By.css("option"))[1];
|
2016-04-01 22:27:25 -04:00
|
|
|
|
2016-05-02 01:50:37 -04:00
|
|
|
expect(select.nativeElement.value).toEqual("1: Buffalo");
|
|
|
|
expect(buffalo.nativeElement.selected).toBe(true);
|
|
|
|
async.done();
|
|
|
|
});
|
2016-04-12 12:40:37 -04:00
|
|
|
}));
|
2016-04-01 22:27:25 -04:00
|
|
|
|
2016-04-12 12:40:37 -04:00
|
|
|
it("with duplicate option values",
|
|
|
|
inject([TestComponentBuilder, AsyncTestCompleter],
|
|
|
|
(tcb: TestComponentBuilder, async) => {
|
|
|
|
var t = `<div>
|
2016-04-01 22:27:25 -04:00
|
|
|
<select [(ngModel)]="selectedCity">
|
2016-04-25 22:52:24 -04:00
|
|
|
<option *ngFor="let c of list" [ngValue]="c">{{c}}</option>
|
2016-04-01 22:27:25 -04:00
|
|
|
</select>
|
|
|
|
</div>`;
|
|
|
|
|
2016-05-02 01:50:37 -04:00
|
|
|
tcb.overrideTemplate(MyComp8, t)
|
|
|
|
.createAsync(MyComp8)
|
|
|
|
.then((fixture) => {
|
2016-04-01 22:27:25 -04:00
|
|
|
|
2016-05-02 01:50:37 -04:00
|
|
|
var testComp = fixture.debugElement.componentInstance;
|
2016-04-01 22:27:25 -04:00
|
|
|
|
2016-05-02 01:50:37 -04:00
|
|
|
testComp.list = [{"name": "NYC"}, {"name": "SF"}, {"name": "SF"}];
|
|
|
|
testComp.selectedCity = testComp.list[0];
|
|
|
|
fixture.detectChanges();
|
2016-04-01 22:27:25 -04:00
|
|
|
|
2016-05-02 01:50:37 -04:00
|
|
|
testComp.selectedCity = testComp.list[1];
|
|
|
|
fixture.detectChanges();
|
2016-04-01 22:27:25 -04:00
|
|
|
|
2016-05-02 01:50:37 -04:00
|
|
|
var select = fixture.debugElement.query(By.css("select"));
|
|
|
|
var firstSF = fixture.debugElement.queryAll(By.css("option"))[1];
|
2016-04-01 22:27:25 -04:00
|
|
|
|
2016-05-02 01:50:37 -04:00
|
|
|
expect(select.nativeElement.value).toEqual("1: Object");
|
|
|
|
expect(firstSF.nativeElement.selected).toBe(true);
|
|
|
|
async.done();
|
|
|
|
});
|
2016-04-12 12:40:37 -04:00
|
|
|
}));
|
2016-04-01 22:27:25 -04:00
|
|
|
|
2016-04-12 12:40:37 -04:00
|
|
|
it("when option values have same content, but different identities",
|
|
|
|
inject([TestComponentBuilder, AsyncTestCompleter],
|
|
|
|
(tcb: TestComponentBuilder, async) => {
|
|
|
|
var t = `<div>
|
2016-04-01 22:27:25 -04:00
|
|
|
<select [(ngModel)]="selectedCity">
|
2016-04-25 22:52:24 -04:00
|
|
|
<option *ngFor="let c of list" [ngValue]="c">{{c['name']}}</option>
|
2016-04-01 22:27:25 -04:00
|
|
|
</select>
|
|
|
|
</div>`;
|
|
|
|
|
2016-05-02 01:50:37 -04:00
|
|
|
tcb.overrideTemplate(MyComp8, t)
|
|
|
|
.createAsync(MyComp8)
|
|
|
|
.then((fixture) => {
|
2016-04-01 22:27:25 -04:00
|
|
|
|
2016-05-02 01:50:37 -04:00
|
|
|
var testComp = fixture.debugElement.componentInstance;
|
|
|
|
testComp.list = [{"name": "SF"}, {"name": "NYC"}, {"name": "NYC"}];
|
|
|
|
testComp.selectedCity = testComp.list[0];
|
|
|
|
fixture.detectChanges();
|
2016-04-01 22:27:25 -04:00
|
|
|
|
2016-05-02 01:50:37 -04:00
|
|
|
testComp.selectedCity = testComp.list[2];
|
|
|
|
fixture.detectChanges();
|
2016-04-01 22:27:25 -04:00
|
|
|
|
2016-05-02 01:50:37 -04:00
|
|
|
var select = fixture.debugElement.query(By.css("select"));
|
|
|
|
var secondNYC = fixture.debugElement.queryAll(By.css("option"))[2];
|
2016-04-01 22:27:25 -04:00
|
|
|
|
2016-05-02 01:50:37 -04:00
|
|
|
expect(select.nativeElement.value).toEqual("2: Object");
|
|
|
|
expect(secondNYC.nativeElement.selected).toBe(true);
|
|
|
|
async.done();
|
|
|
|
});
|
2016-04-12 12:40:37 -04:00
|
|
|
}));
|
2016-04-01 22:27:25 -04:00
|
|
|
});
|
2015-06-13 15:15:42 -04:00
|
|
|
|
2016-04-12 12:40:37 -04:00
|
|
|
it("should support custom value accessors",
|
2015-06-25 00:51:37 -04:00
|
|
|
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
|
2015-11-23 19:02:19 -05:00
|
|
|
var t = `<div [ngFormModel]="form">
|
|
|
|
<input type="text" ngControl="name" wrapped-value>
|
2015-05-22 15:32:49 -04:00
|
|
|
</div>`;
|
|
|
|
|
2016-05-02 01:50:37 -04:00
|
|
|
tcb.overrideTemplate(MyComp8, t)
|
|
|
|
.createAsync(MyComp8)
|
|
|
|
.then((fixture) => {
|
|
|
|
fixture.debugElement.componentInstance.form =
|
|
|
|
new ControlGroup({"name": new Control("aa")});
|
|
|
|
fixture.detectChanges();
|
|
|
|
var input = fixture.debugElement.query(By.css("input"));
|
|
|
|
expect(input.nativeElement.value).toEqual("!aa!");
|
|
|
|
|
|
|
|
input.nativeElement.value = "!bb!";
|
|
|
|
dispatchEvent(input.nativeElement, "input");
|
|
|
|
|
|
|
|
expect(fixture.debugElement.componentInstance.form.value).toEqual({"name": "bb"});
|
|
|
|
async.done();
|
|
|
|
});
|
2015-05-22 15:32:49 -04:00
|
|
|
}));
|
2015-10-23 17:06:46 -04:00
|
|
|
|
2016-04-12 12:40:37 -04:00
|
|
|
it("should support custom value accessors on non builtin input elements that fire a change event without a 'target' property",
|
2015-10-23 17:06:46 -04:00
|
|
|
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
|
2015-11-23 19:02:19 -05:00
|
|
|
var t = `<div [ngFormModel]="form">
|
|
|
|
<my-input ngControl="name"></my-input>
|
2015-10-23 17:06:46 -04:00
|
|
|
</div>`;
|
|
|
|
|
2016-05-02 01:50:37 -04:00
|
|
|
tcb.overrideTemplate(MyComp8, t)
|
|
|
|
.createAsync(MyComp8)
|
|
|
|
.then((fixture) => {
|
|
|
|
fixture.debugElement.componentInstance.form =
|
|
|
|
new ControlGroup({"name": new Control("aa")});
|
|
|
|
fixture.detectChanges();
|
|
|
|
var input = fixture.debugElement.query(By.css("my-input"));
|
|
|
|
expect(input.componentInstance.value).toEqual("!aa!");
|
|
|
|
|
|
|
|
input.componentInstance.value = "!bb!";
|
|
|
|
ObservableWrapper.subscribe(input.componentInstance.onInput, (value) => {
|
|
|
|
expect(fixture.debugElement.componentInstance.form.value)
|
|
|
|
.toEqual({"name": "bb"});
|
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
input.componentInstance.dispatchChangeEvent();
|
|
|
|
});
|
2015-10-23 17:06:46 -04:00
|
|
|
}));
|
|
|
|
|
2015-05-22 15:32:49 -04:00
|
|
|
});
|
|
|
|
|
2016-04-12 12:40:37 -04:00
|
|
|
describe("validations", () => {
|
|
|
|
it("should use sync validators defined in html",
|
2015-06-25 00:51:37 -04:00
|
|
|
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
|
2015-10-13 17:38:13 -04:00
|
|
|
var form = new ControlGroup(
|
2016-04-12 12:40:37 -04:00
|
|
|
{"login": new Control(""), "min": new Control(""), "max": new Control("")});
|
2015-05-22 15:32:49 -04:00
|
|
|
|
2015-11-23 19:02:19 -05:00
|
|
|
var t = `<div [ngFormModel]="form" login-is-empty-validator>
|
|
|
|
<input type="text" ngControl="login" required>
|
|
|
|
<input type="text" ngControl="min" minlength="3">
|
|
|
|
<input type="text" ngControl="max" maxlength="3">
|
2015-05-22 15:32:49 -04:00
|
|
|
</div>`;
|
|
|
|
|
2016-05-02 01:50:37 -04:00
|
|
|
tcb.overrideTemplate(MyComp8, t)
|
|
|
|
.createAsync(MyComp8)
|
|
|
|
.then((fixture) => {
|
|
|
|
fixture.debugElement.componentInstance.form = form;
|
|
|
|
fixture.detectChanges();
|
2015-05-22 15:32:49 -04:00
|
|
|
|
2016-05-02 01:50:37 -04:00
|
|
|
var required = fixture.debugElement.query(By.css("[required]"));
|
|
|
|
var minLength = fixture.debugElement.query(By.css("[minlength]"));
|
|
|
|
var maxLength = fixture.debugElement.query(By.css("[maxlength]"));
|
2015-05-22 15:32:49 -04:00
|
|
|
|
2016-05-02 01:50:37 -04:00
|
|
|
required.nativeElement.value = "";
|
|
|
|
minLength.nativeElement.value = "1";
|
|
|
|
maxLength.nativeElement.value = "1234";
|
|
|
|
dispatchEvent(required.nativeElement, "input");
|
|
|
|
dispatchEvent(minLength.nativeElement, "input");
|
|
|
|
dispatchEvent(maxLength.nativeElement, "input");
|
2015-10-13 17:38:13 -04:00
|
|
|
|
2016-05-02 01:50:37 -04:00
|
|
|
expect(form.hasError("required", ["login"])).toEqual(true);
|
|
|
|
expect(form.hasError("minlength", ["min"])).toEqual(true);
|
|
|
|
expect(form.hasError("maxlength", ["max"])).toEqual(true);
|
2015-10-13 17:38:13 -04:00
|
|
|
|
2016-05-02 01:50:37 -04:00
|
|
|
expect(form.hasError("loginIsEmpty")).toEqual(true);
|
2015-10-27 17:36:13 -04:00
|
|
|
|
2016-05-02 01:50:37 -04:00
|
|
|
required.nativeElement.value = "1";
|
|
|
|
minLength.nativeElement.value = "123";
|
|
|
|
maxLength.nativeElement.value = "123";
|
|
|
|
dispatchEvent(required.nativeElement, "input");
|
|
|
|
dispatchEvent(minLength.nativeElement, "input");
|
|
|
|
dispatchEvent(maxLength.nativeElement, "input");
|
2015-10-13 17:38:13 -04:00
|
|
|
|
2016-05-02 01:50:37 -04:00
|
|
|
expect(form.valid).toEqual(true);
|
2015-05-22 15:32:49 -04:00
|
|
|
|
2016-05-02 01:50:37 -04:00
|
|
|
async.done();
|
|
|
|
});
|
2015-05-22 15:32:49 -04:00
|
|
|
}));
|
|
|
|
|
2016-04-12 12:40:37 -04:00
|
|
|
it("should use async validators defined in the html",
|
2016-04-18 19:04:35 -04:00
|
|
|
fakeAsync(inject([TestComponentBuilder], (tcb: TestComponentBuilder) => {
|
|
|
|
var form = new ControlGroup({"login": new Control("")});
|
2015-11-02 13:00:42 -05:00
|
|
|
|
2016-04-18 19:04:35 -04:00
|
|
|
var t = `<div [ngFormModel]="form">
|
2015-11-23 19:02:19 -05:00
|
|
|
<input type="text" ngControl="login" uniq-login-validator="expected">
|
2015-11-02 13:00:42 -05:00
|
|
|
</div>`;
|
|
|
|
|
2016-04-18 19:04:35 -04:00
|
|
|
var rootTC;
|
2016-04-28 20:50:03 -04:00
|
|
|
tcb.overrideTemplate(MyComp8, t).createAsync(MyComp8).then((root) => rootTC = root);
|
2016-04-18 19:04:35 -04:00
|
|
|
tick();
|
2015-11-02 13:00:42 -05:00
|
|
|
|
2016-04-18 19:04:35 -04:00
|
|
|
rootTC.debugElement.componentInstance.form = form;
|
|
|
|
rootTC.detectChanges();
|
2015-11-02 13:00:42 -05:00
|
|
|
|
2016-04-18 19:04:35 -04:00
|
|
|
expect(form.pending).toEqual(true);
|
2015-11-02 13:00:42 -05:00
|
|
|
|
2016-04-18 19:04:35 -04:00
|
|
|
tick(100);
|
2015-11-02 13:00:42 -05:00
|
|
|
|
2016-04-18 19:04:35 -04:00
|
|
|
expect(form.hasError("uniqLogin", ["login"])).toEqual(true);
|
2015-11-02 13:00:42 -05:00
|
|
|
|
2016-04-18 19:04:35 -04:00
|
|
|
var input = rootTC.debugElement.query(By.css("input"));
|
|
|
|
input.nativeElement.value = "expected";
|
|
|
|
dispatchEvent(input.nativeElement, "input");
|
|
|
|
tick(100);
|
2015-11-02 13:00:42 -05:00
|
|
|
|
2016-04-18 19:04:35 -04:00
|
|
|
expect(form.valid).toEqual(true);
|
|
|
|
})));
|
2015-11-02 13:00:42 -05:00
|
|
|
|
2016-04-12 12:40:37 -04:00
|
|
|
it("should use sync validators defined in the model",
|
2015-06-25 00:51:37 -04:00
|
|
|
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
|
2016-04-12 12:40:37 -04:00
|
|
|
var form = new ControlGroup({"login": new Control("aa", Validators.required)});
|
2015-05-22 15:32:49 -04:00
|
|
|
|
2015-11-23 19:02:19 -05:00
|
|
|
var t = `<div [ngFormModel]="form">
|
|
|
|
<input type="text" ngControl="login">
|
2015-05-22 15:32:49 -04:00
|
|
|
</div>`;
|
|
|
|
|
2016-05-02 01:50:37 -04:00
|
|
|
tcb.overrideTemplate(MyComp8, t)
|
|
|
|
.createAsync(MyComp8)
|
|
|
|
.then((fixture) => {
|
|
|
|
fixture.debugElement.componentInstance.form = form;
|
|
|
|
fixture.detectChanges();
|
|
|
|
expect(form.valid).toEqual(true);
|
2015-05-22 15:32:49 -04:00
|
|
|
|
2016-05-02 01:50:37 -04:00
|
|
|
var input = fixture.debugElement.query(By.css("input"));
|
2015-05-22 15:32:49 -04:00
|
|
|
|
2016-05-02 01:50:37 -04:00
|
|
|
input.nativeElement.value = "";
|
|
|
|
dispatchEvent(input.nativeElement, "input");
|
2015-05-22 15:32:49 -04:00
|
|
|
|
2016-05-02 01:50:37 -04:00
|
|
|
expect(form.valid).toEqual(false);
|
|
|
|
async.done();
|
|
|
|
});
|
2015-05-22 15:32:49 -04:00
|
|
|
}));
|
2015-10-29 20:45:15 -04:00
|
|
|
|
2016-04-12 12:40:37 -04:00
|
|
|
it("should use async validators defined in the model",
|
2016-04-18 19:04:35 -04:00
|
|
|
fakeAsync(inject([TestComponentBuilder], (tcb: TestComponentBuilder) => {
|
|
|
|
var control = new Control("", Validators.required, uniqLoginAsyncValidator("expected"));
|
|
|
|
var form = new ControlGroup({"login": control});
|
2015-10-29 20:45:15 -04:00
|
|
|
|
2016-04-18 19:04:35 -04:00
|
|
|
var t = `<div [ngFormModel]="form">
|
2015-11-23 19:02:19 -05:00
|
|
|
<input type="text" ngControl="login">
|
2015-10-29 20:45:15 -04:00
|
|
|
</div>`;
|
|
|
|
|
2016-04-18 19:04:35 -04:00
|
|
|
var fixture;
|
2016-04-28 20:50:03 -04:00
|
|
|
tcb.overrideTemplate(MyComp8, t).createAsync(MyComp8).then((root) => fixture = root);
|
2016-04-18 19:04:35 -04:00
|
|
|
tick();
|
2015-10-29 20:45:15 -04:00
|
|
|
|
2016-04-18 19:04:35 -04:00
|
|
|
fixture.debugElement.componentInstance.form = form;
|
|
|
|
fixture.detectChanges();
|
2015-10-29 20:45:15 -04:00
|
|
|
|
2016-04-18 19:04:35 -04:00
|
|
|
expect(form.hasError("required", ["login"])).toEqual(true);
|
2015-10-29 20:45:15 -04:00
|
|
|
|
2016-04-18 19:04:35 -04:00
|
|
|
var input = fixture.debugElement.query(By.css("input"));
|
|
|
|
input.nativeElement.value = "wrong value";
|
|
|
|
dispatchEvent(input.nativeElement, "input");
|
2015-10-29 20:45:15 -04:00
|
|
|
|
2016-04-18 19:04:35 -04:00
|
|
|
expect(form.pending).toEqual(true);
|
|
|
|
tick();
|
2015-10-29 20:45:15 -04:00
|
|
|
|
2016-04-18 19:04:35 -04:00
|
|
|
expect(form.hasError("uniqLogin", ["login"])).toEqual(true);
|
2015-10-29 20:45:15 -04:00
|
|
|
|
2016-04-18 19:04:35 -04:00
|
|
|
input.nativeElement.value = "expected";
|
|
|
|
dispatchEvent(input.nativeElement, "input");
|
|
|
|
tick();
|
2015-10-29 20:45:15 -04:00
|
|
|
|
2016-04-18 19:04:35 -04:00
|
|
|
expect(form.valid).toEqual(true);
|
|
|
|
})));
|
2015-05-22 15:32:49 -04:00
|
|
|
});
|
|
|
|
|
2016-04-12 12:40:37 -04:00
|
|
|
describe("nested forms", () => {
|
|
|
|
it("should init DOM with the given form object",
|
2015-06-25 00:51:37 -04:00
|
|
|
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
|
2015-05-22 15:32:49 -04:00
|
|
|
var form =
|
2016-04-12 12:40:37 -04:00
|
|
|
new ControlGroup({"nested": new ControlGroup({"login": new Control("value")})});
|
2015-05-22 15:32:49 -04:00
|
|
|
|
2015-11-23 19:02:19 -05:00
|
|
|
var t = `<div [ngFormModel]="form">
|
|
|
|
<div ngControlGroup="nested">
|
|
|
|
<input type="text" ngControl="login">
|
2015-05-22 15:32:49 -04:00
|
|
|
</div>
|
|
|
|
</div>`;
|
|
|
|
|
2016-05-02 01:50:37 -04:00
|
|
|
tcb.overrideTemplate(MyComp8, t)
|
|
|
|
.createAsync(MyComp8)
|
|
|
|
.then((fixture) => {
|
|
|
|
fixture.debugElement.componentInstance.form = form;
|
|
|
|
fixture.detectChanges();
|
2015-06-25 00:51:37 -04:00
|
|
|
|
2016-05-02 01:50:37 -04:00
|
|
|
var input = fixture.debugElement.query(By.css("input"));
|
|
|
|
expect(input.nativeElement.value).toEqual("value");
|
|
|
|
async.done();
|
|
|
|
});
|
2015-05-22 15:32:49 -04:00
|
|
|
}));
|
|
|
|
|
2016-04-12 12:40:37 -04:00
|
|
|
it("should update the control group values on DOM change",
|
2015-06-25 00:51:37 -04:00
|
|
|
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
|
2015-05-22 15:32:49 -04:00
|
|
|
var form =
|
2016-04-12 12:40:37 -04:00
|
|
|
new ControlGroup({"nested": new ControlGroup({"login": new Control("value")})});
|
2015-05-22 15:32:49 -04:00
|
|
|
|
2015-11-23 19:02:19 -05:00
|
|
|
var t = `<div [ngFormModel]="form">
|
|
|
|
<div ngControlGroup="nested">
|
|
|
|
<input type="text" ngControl="login">
|
2015-05-22 15:32:49 -04:00
|
|
|
</div>
|
|
|
|
</div>`;
|
|
|
|
|
2016-05-02 01:50:37 -04:00
|
|
|
tcb.overrideTemplate(MyComp8, t)
|
|
|
|
.createAsync(MyComp8)
|
|
|
|
.then((fixture) => {
|
|
|
|
fixture.debugElement.componentInstance.form = form;
|
|
|
|
fixture.detectChanges();
|
|
|
|
var input = fixture.debugElement.query(By.css("input"));
|
2015-05-22 15:32:49 -04:00
|
|
|
|
2016-05-02 01:50:37 -04:00
|
|
|
input.nativeElement.value = "updatedValue";
|
|
|
|
dispatchEvent(input.nativeElement, "input");
|
2015-05-22 15:32:49 -04:00
|
|
|
|
2016-05-02 01:50:37 -04:00
|
|
|
expect(form.value).toEqual({"nested": {"login": "updatedValue"}});
|
|
|
|
async.done();
|
|
|
|
});
|
2015-05-22 15:32:49 -04:00
|
|
|
}));
|
|
|
|
});
|
2015-05-30 14:56:00 -04:00
|
|
|
|
2016-04-12 12:40:37 -04:00
|
|
|
it("should support ngModel for complex forms",
|
2016-04-18 19:04:35 -04:00
|
|
|
fakeAsync(inject([TestComponentBuilder], (tcb: TestComponentBuilder) => {
|
|
|
|
var form = new ControlGroup({"name": new Control("")});
|
2015-05-31 15:24:34 -04:00
|
|
|
|
2016-04-18 19:04:35 -04:00
|
|
|
var t =
|
|
|
|
`<div [ngFormModel]="form"><input type="text" ngControl="name" [(ngModel)]="name"></div>`;
|
2015-05-31 15:24:34 -04:00
|
|
|
|
2016-04-28 20:50:03 -04:00
|
|
|
let fixture = tcb.overrideTemplate(MyComp8, t).createFakeAsync(MyComp8);
|
2016-04-18 19:04:35 -04:00
|
|
|
tick();
|
2015-05-31 15:24:34 -04:00
|
|
|
|
2016-04-18 19:04:35 -04:00
|
|
|
fixture.debugElement.componentInstance.name = 'oldValue';
|
|
|
|
fixture.debugElement.componentInstance.form = form;
|
|
|
|
fixture.detectChanges();
|
2015-05-31 15:24:34 -04:00
|
|
|
|
2016-04-18 19:04:35 -04:00
|
|
|
var input = fixture.debugElement.query(By.css("input")).nativeElement;
|
|
|
|
expect(input.value).toEqual("oldValue");
|
2015-05-31 15:24:34 -04:00
|
|
|
|
2016-04-18 19:04:35 -04:00
|
|
|
input.value = "updatedValue";
|
|
|
|
dispatchEvent(input, "input");
|
2015-05-31 15:24:34 -04:00
|
|
|
|
2016-04-18 19:04:35 -04:00
|
|
|
tick();
|
|
|
|
expect(fixture.debugElement.componentInstance.name).toEqual("updatedValue");
|
|
|
|
})));
|
2015-05-31 15:24:34 -04:00
|
|
|
|
2016-04-12 12:40:37 -04:00
|
|
|
it("should support ngModel for single fields",
|
2016-04-18 19:04:35 -04:00
|
|
|
fakeAsync(inject([TestComponentBuilder], (tcb: TestComponentBuilder) => {
|
|
|
|
var form = new Control("");
|
2015-05-31 15:24:34 -04:00
|
|
|
|
2016-04-18 19:04:35 -04:00
|
|
|
var t = `<div><input type="text" [ngFormControl]="form" [(ngModel)]="name"></div>`;
|
2015-05-31 15:24:34 -04:00
|
|
|
|
2016-04-28 20:50:03 -04:00
|
|
|
let fixture = tcb.overrideTemplate(MyComp8, t).createFakeAsync(MyComp8);
|
2016-04-18 19:04:35 -04:00
|
|
|
tick();
|
|
|
|
fixture.debugElement.componentInstance.form = form;
|
|
|
|
fixture.debugElement.componentInstance.name = "oldValue";
|
|
|
|
fixture.detectChanges();
|
2015-05-31 15:24:34 -04:00
|
|
|
|
2016-04-18 19:04:35 -04:00
|
|
|
var input = fixture.debugElement.query(By.css("input")).nativeElement;
|
|
|
|
expect(input.value).toEqual("oldValue");
|
2015-05-31 15:24:34 -04:00
|
|
|
|
2016-04-18 19:04:35 -04:00
|
|
|
input.value = "updatedValue";
|
|
|
|
dispatchEvent(input, "input");
|
|
|
|
tick();
|
2015-05-31 15:24:34 -04:00
|
|
|
|
2016-04-18 19:04:35 -04:00
|
|
|
expect(fixture.debugElement.componentInstance.name).toEqual("updatedValue");
|
|
|
|
})));
|
2015-05-31 15:24:34 -04:00
|
|
|
|
2016-04-12 12:40:37 -04:00
|
|
|
describe("template-driven forms", () => {
|
|
|
|
it("should add new controls and control groups",
|
2016-04-18 19:04:35 -04:00
|
|
|
fakeAsync(inject([TestComponentBuilder], (tcb: TestComponentBuilder) => {
|
|
|
|
var t = `<form>
|
2015-11-23 19:02:19 -05:00
|
|
|
<div ngControlGroup="user">
|
|
|
|
<input type="text" ngControl="login">
|
2015-05-30 14:56:00 -04:00
|
|
|
</div>
|
2015-06-01 16:11:34 -04:00
|
|
|
</form>`;
|
2015-05-30 14:56:00 -04:00
|
|
|
|
2016-04-28 20:50:03 -04:00
|
|
|
let fixture = tcb.overrideTemplate(MyComp8, t).createFakeAsync(MyComp8);
|
2016-04-18 19:04:35 -04:00
|
|
|
tick();
|
|
|
|
fixture.debugElement.componentInstance.name = null;
|
|
|
|
fixture.detectChanges();
|
2015-06-25 00:51:37 -04:00
|
|
|
|
2016-04-18 19:04:35 -04:00
|
|
|
var form = fixture.debugElement.children[0].inject(NgForm);
|
|
|
|
expect(form.controls['user']).not.toBeDefined();
|
2015-05-30 14:56:00 -04:00
|
|
|
|
2016-04-18 19:04:35 -04:00
|
|
|
tick();
|
2015-05-30 14:56:00 -04:00
|
|
|
|
2016-04-18 19:04:35 -04:00
|
|
|
expect(form.controls['user']).toBeDefined();
|
|
|
|
expect(form.controls['user'].controls['login']).toBeDefined();
|
|
|
|
})));
|
2015-05-30 14:56:00 -04:00
|
|
|
|
2016-04-12 12:40:37 -04:00
|
|
|
it("should emit ngSubmit event on submit",
|
2016-04-18 19:04:35 -04:00
|
|
|
fakeAsync(inject([TestComponentBuilder], (tcb: TestComponentBuilder) => {
|
|
|
|
var t = `<div><form (ngSubmit)="name='updated'"></form></div>`;
|
2015-06-05 17:29:50 -04:00
|
|
|
|
2016-04-28 20:50:03 -04:00
|
|
|
let fixture = tcb.overrideTemplate(MyComp8, t).createFakeAsync(MyComp8);
|
2016-04-18 19:04:35 -04:00
|
|
|
tick();
|
|
|
|
fixture.debugElement.componentInstance.name = 'old';
|
|
|
|
var form = fixture.debugElement.query(By.css("form"));
|
2015-06-05 17:29:50 -04:00
|
|
|
|
2016-04-18 19:04:35 -04:00
|
|
|
dispatchEvent(form.nativeElement, "submit");
|
|
|
|
tick();
|
2015-06-05 17:29:50 -04:00
|
|
|
|
2016-04-18 19:04:35 -04:00
|
|
|
expect(fixture.debugElement.componentInstance.name).toEqual("updated");
|
|
|
|
})));
|
2015-06-05 17:29:50 -04:00
|
|
|
|
2016-04-12 12:40:37 -04:00
|
|
|
it("should not create a template-driven form when ngNoForm is used",
|
2015-06-25 00:51:37 -04:00
|
|
|
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
|
2015-11-23 19:02:19 -05:00
|
|
|
var t = `<form ngNoForm>
|
2015-06-01 16:11:34 -04:00
|
|
|
</form>`;
|
|
|
|
|
2016-05-02 01:50:37 -04:00
|
|
|
tcb.overrideTemplate(MyComp8, t)
|
|
|
|
.createAsync(MyComp8)
|
|
|
|
.then((fixture) => {
|
|
|
|
fixture.debugElement.componentInstance.name = null;
|
|
|
|
fixture.detectChanges();
|
2015-06-01 16:11:34 -04:00
|
|
|
|
2016-05-02 01:50:37 -04:00
|
|
|
expect(fixture.debugElement.children[0].providerTokens.length).toEqual(0);
|
|
|
|
async.done();
|
|
|
|
});
|
2015-06-25 00:51:37 -04:00
|
|
|
}));
|
2015-05-30 14:56:00 -04:00
|
|
|
|
2016-04-12 12:40:37 -04:00
|
|
|
it("should remove controls",
|
2016-04-18 19:04:35 -04:00
|
|
|
fakeAsync(inject([TestComponentBuilder], (tcb: TestComponentBuilder) => {
|
|
|
|
var t = `<form>
|
2015-11-23 19:02:19 -05:00
|
|
|
<div *ngIf="name == 'show'">
|
|
|
|
<input type="text" ngControl="login">
|
2015-05-30 14:56:00 -04:00
|
|
|
</div>
|
2015-06-01 16:11:34 -04:00
|
|
|
</form>`;
|
2015-05-30 14:56:00 -04:00
|
|
|
|
2016-04-28 20:50:03 -04:00
|
|
|
let fixture = tcb.overrideTemplate(MyComp8, t).createFakeAsync(MyComp8);
|
2016-04-18 19:04:35 -04:00
|
|
|
tick();
|
|
|
|
fixture.debugElement.componentInstance.name = 'show';
|
|
|
|
fixture.detectChanges();
|
|
|
|
tick();
|
|
|
|
var form = fixture.debugElement.children[0].inject(NgForm);
|
2015-05-30 14:56:00 -04:00
|
|
|
|
|
|
|
|
2016-04-18 19:04:35 -04:00
|
|
|
expect(form.controls['login']).toBeDefined();
|
2015-05-30 14:56:00 -04:00
|
|
|
|
2016-04-18 19:04:35 -04:00
|
|
|
fixture.debugElement.componentInstance.name = 'hide';
|
|
|
|
fixture.detectChanges();
|
|
|
|
tick();
|
2015-05-30 14:56:00 -04:00
|
|
|
|
2016-04-18 19:04:35 -04:00
|
|
|
expect(form.controls['login']).not.toBeDefined();
|
|
|
|
})));
|
2015-05-30 14:56:00 -04:00
|
|
|
|
2016-04-12 12:40:37 -04:00
|
|
|
it("should remove control groups",
|
2016-04-18 19:04:35 -04:00
|
|
|
fakeAsync(inject([TestComponentBuilder], (tcb: TestComponentBuilder) => {
|
|
|
|
var t = `<form>
|
2015-11-23 19:02:19 -05:00
|
|
|
<div *ngIf="name=='show'" ngControlGroup="user">
|
|
|
|
<input type="text" ngControl="login">
|
2015-05-30 14:56:00 -04:00
|
|
|
</div>
|
2015-06-01 16:11:34 -04:00
|
|
|
</form>`;
|
2015-05-30 14:56:00 -04:00
|
|
|
|
|
|
|
|
2016-04-28 20:50:03 -04:00
|
|
|
let fixture = tcb.overrideTemplate(MyComp8, t).createFakeAsync(MyComp8);
|
2016-04-18 19:04:35 -04:00
|
|
|
tick();
|
|
|
|
fixture.debugElement.componentInstance.name = 'show';
|
|
|
|
fixture.detectChanges();
|
|
|
|
tick();
|
|
|
|
var form = fixture.debugElement.children[0].inject(NgForm);
|
2015-05-30 14:56:00 -04:00
|
|
|
|
2016-04-18 19:04:35 -04:00
|
|
|
expect(form.controls['user']).toBeDefined();
|
2015-05-30 14:56:00 -04:00
|
|
|
|
2016-04-18 19:04:35 -04:00
|
|
|
fixture.debugElement.componentInstance.name = 'hide';
|
|
|
|
fixture.detectChanges();
|
|
|
|
tick();
|
2015-05-30 14:56:00 -04:00
|
|
|
|
2016-04-18 19:04:35 -04:00
|
|
|
expect(form.controls['user']).not.toBeDefined();
|
|
|
|
})));
|
2015-05-31 15:24:34 -04:00
|
|
|
|
2016-04-12 12:40:37 -04:00
|
|
|
it("should support ngModel for complex forms",
|
2016-04-18 19:04:35 -04:00
|
|
|
fakeAsync(inject([TestComponentBuilder], (tcb: TestComponentBuilder) => {
|
|
|
|
var t = `<form>
|
2015-11-23 19:02:19 -05:00
|
|
|
<input type="text" ngControl="name" [(ngModel)]="name">
|
2015-06-02 17:53:49 -04:00
|
|
|
</form>`;
|
2015-05-31 15:24:34 -04:00
|
|
|
|
2016-04-28 20:50:03 -04:00
|
|
|
let fixture = tcb.overrideTemplate(MyComp8, t).createFakeAsync(MyComp8);
|
2016-04-18 19:04:35 -04:00
|
|
|
tick();
|
|
|
|
fixture.debugElement.componentInstance.name = "oldValue";
|
|
|
|
fixture.detectChanges();
|
|
|
|
tick();
|
2015-05-31 15:24:34 -04:00
|
|
|
|
2016-04-18 19:04:35 -04:00
|
|
|
var input = fixture.debugElement.query(By.css("input")).nativeElement;
|
|
|
|
expect(input.value).toEqual("oldValue");
|
2015-05-31 15:24:34 -04:00
|
|
|
|
2016-04-18 19:04:35 -04:00
|
|
|
input.value = "updatedValue";
|
|
|
|
dispatchEvent(input, "input");
|
|
|
|
tick();
|
2015-05-31 15:24:34 -04:00
|
|
|
|
2016-04-18 19:04:35 -04:00
|
|
|
expect(fixture.debugElement.componentInstance.name).toEqual("updatedValue");
|
|
|
|
})));
|
2015-05-31 15:24:34 -04:00
|
|
|
|
|
|
|
|
2016-04-12 12:40:37 -04:00
|
|
|
it("should support ngModel for single fields",
|
2016-04-18 19:04:35 -04:00
|
|
|
fakeAsync(inject([TestComponentBuilder], (tcb: TestComponentBuilder) => {
|
|
|
|
var t = `<div><input type="text" [(ngModel)]="name"></div>`;
|
2015-05-31 15:24:34 -04:00
|
|
|
|
2016-04-28 20:50:03 -04:00
|
|
|
let fixture = tcb.overrideTemplate(MyComp8, t).createFakeAsync(MyComp8);
|
2016-04-18 19:04:35 -04:00
|
|
|
tick();
|
|
|
|
fixture.debugElement.componentInstance.name = "oldValue";
|
|
|
|
fixture.detectChanges();
|
2015-05-31 15:24:34 -04:00
|
|
|
|
2016-04-18 19:04:35 -04:00
|
|
|
var input = fixture.debugElement.query(By.css("input")).nativeElement;
|
|
|
|
expect(input.value).toEqual("oldValue");
|
2015-05-31 15:24:34 -04:00
|
|
|
|
2016-04-18 19:04:35 -04:00
|
|
|
input.value = "updatedValue";
|
|
|
|
dispatchEvent(input, "input");
|
|
|
|
tick();
|
2015-05-31 15:24:34 -04:00
|
|
|
|
2016-04-18 19:04:35 -04:00
|
|
|
expect(fixture.debugElement.componentInstance.name).toEqual("updatedValue");
|
|
|
|
})));
|
2015-06-03 14:56:01 -04:00
|
|
|
|
|
|
|
|
2016-04-12 12:40:37 -04:00
|
|
|
it("should support <type=radio>",
|
2016-04-18 19:04:35 -04:00
|
|
|
fakeAsync(inject([TestComponentBuilder], (tcb: TestComponentBuilder) => {
|
2016-05-24 21:53:25 -04:00
|
|
|
const t = `<form>
|
|
|
|
<input type="radio" name="food" ngControl="chicken" [(ngModel)]="data['chicken']">
|
|
|
|
<input type="radio" name="food" ngControl="fish" [(ngModel)]="data['fish']">
|
|
|
|
<input type="radio" name="food" ngControl="beef" [(ngModel)]="data['beef']">
|
|
|
|
<input type="radio" name="food" ngControl="pork" [(ngModel)]="data['pork']">
|
2016-02-05 19:08:53 -05:00
|
|
|
</form>`;
|
|
|
|
|
2016-05-24 21:53:25 -04:00
|
|
|
const fixture = tcb.overrideTemplate(MyComp8, t).createFakeAsync(MyComp8);
|
2016-04-18 19:04:35 -04:00
|
|
|
tick();
|
2016-02-05 19:08:53 -05:00
|
|
|
|
2016-04-18 19:04:35 -04:00
|
|
|
fixture.debugElement.componentInstance.data = {
|
2016-05-24 21:53:25 -04:00
|
|
|
'chicken': new RadioButtonState(false, 'chicken'),
|
|
|
|
'fish': new RadioButtonState(true, 'fish'),
|
|
|
|
'beef': new RadioButtonState(false, 'beef'),
|
|
|
|
'pork': new RadioButtonState(true, 'pork')
|
2016-04-18 19:04:35 -04:00
|
|
|
};
|
|
|
|
fixture.detectChanges();
|
|
|
|
tick();
|
2016-02-05 19:08:53 -05:00
|
|
|
|
2016-05-24 21:53:25 -04:00
|
|
|
const input = fixture.debugElement.query(By.css("input"));
|
2016-04-18 19:04:35 -04:00
|
|
|
expect(input.nativeElement.checked).toEqual(false);
|
2016-02-05 19:08:53 -05:00
|
|
|
|
2016-04-18 19:04:35 -04:00
|
|
|
dispatchEvent(input.nativeElement, "change");
|
|
|
|
tick();
|
2016-02-05 19:08:53 -05:00
|
|
|
|
2016-05-24 21:53:25 -04:00
|
|
|
const data = fixture.debugElement.componentInstance.data;
|
2016-02-05 19:08:53 -05:00
|
|
|
|
2016-05-24 21:53:25 -04:00
|
|
|
expect(data['chicken']).toEqual(new RadioButtonState(true, 'chicken'));
|
|
|
|
expect(data['fish']).toEqual(new RadioButtonState(false, 'fish'));
|
|
|
|
expect(data['beef']).toEqual(new RadioButtonState(false, 'beef'));
|
|
|
|
expect(data['pork']).toEqual(new RadioButtonState(false, 'pork'));
|
2016-04-18 19:04:35 -04:00
|
|
|
})));
|
2016-02-05 19:08:53 -05:00
|
|
|
});
|
|
|
|
|
2016-05-24 21:53:25 -04:00
|
|
|
it("should support multiple named <type=radio> groups",
|
|
|
|
fakeAsync(inject([TestComponentBuilder], (tcb: TestComponentBuilder) => {
|
|
|
|
const t = `<form>
|
|
|
|
<input type="radio" name="food" ngControl="chicken" [(ngModel)]="data['chicken']">
|
|
|
|
<input type="radio" name="food" ngControl="fish" [(ngModel)]="data['fish']">
|
|
|
|
<input type="radio" name="drink" ngControl="cola" [(ngModel)]="data['cola']">
|
|
|
|
<input type="radio" name="drink" ngControl="sprite" [(ngModel)]="data['sprite']">
|
|
|
|
</form>`;
|
|
|
|
|
|
|
|
const fixture = tcb.overrideTemplate(MyComp8, t).createFakeAsync(MyComp8);
|
|
|
|
tick();
|
|
|
|
|
|
|
|
fixture.debugElement.componentInstance.data = {
|
|
|
|
'chicken': new RadioButtonState(false, 'chicken'),
|
|
|
|
'fish': new RadioButtonState(true, 'fish'),
|
|
|
|
'cola': new RadioButtonState(false, 'cola'),
|
|
|
|
'sprite': new RadioButtonState(true, 'sprite')
|
|
|
|
};
|
|
|
|
fixture.detectChanges();
|
|
|
|
tick();
|
|
|
|
|
|
|
|
const input = fixture.debugElement.query(By.css("input"));
|
|
|
|
expect(input.nativeElement.checked).toEqual(false);
|
|
|
|
|
|
|
|
dispatchEvent(input.nativeElement, "change");
|
|
|
|
tick();
|
|
|
|
|
|
|
|
const data = fixture.debugElement.componentInstance.data;
|
|
|
|
|
|
|
|
expect(data['chicken']).toEqual(new RadioButtonState(true, 'chicken'));
|
|
|
|
expect(data['fish']).toEqual(new RadioButtonState(false, 'fish'));
|
|
|
|
expect(data['cola']).toEqual(new RadioButtonState(false, 'cola'));
|
|
|
|
expect(data['sprite']).toEqual(new RadioButtonState(true, 'sprite'));
|
|
|
|
})));
|
|
|
|
|
2016-04-12 12:40:37 -04:00
|
|
|
describe("setting status classes", () => {
|
|
|
|
it("should work with single fields",
|
2015-06-25 00:51:37 -04:00
|
|
|
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
|
2016-04-12 12:40:37 -04:00
|
|
|
var form = new Control("", Validators.required);
|
2015-06-03 14:56:01 -04:00
|
|
|
|
2015-11-23 19:02:19 -05:00
|
|
|
var t = `<div><input type="text" [ngFormControl]="form"></div>`;
|
2015-06-03 14:56:01 -04:00
|
|
|
|
2016-05-02 01:50:37 -04:00
|
|
|
tcb.overrideTemplate(MyComp8, t)
|
|
|
|
.createAsync(MyComp8)
|
|
|
|
.then((fixture) => {
|
|
|
|
fixture.debugElement.componentInstance.form = form;
|
|
|
|
fixture.detectChanges();
|
2015-06-03 14:56:01 -04:00
|
|
|
|
2016-05-02 01:50:37 -04:00
|
|
|
var input = fixture.debugElement.query(By.css("input")).nativeElement;
|
|
|
|
expect(sortedClassList(input))
|
|
|
|
.toEqual(['ng-invalid', 'ng-pristine', 'ng-untouched']);
|
2015-06-03 14:56:01 -04:00
|
|
|
|
2016-05-02 01:50:37 -04:00
|
|
|
dispatchEvent(input, "blur");
|
|
|
|
fixture.detectChanges();
|
2015-06-03 14:56:01 -04:00
|
|
|
|
2016-05-02 01:50:37 -04:00
|
|
|
expect(sortedClassList(input))
|
|
|
|
.toEqual(["ng-invalid", "ng-pristine", "ng-touched"]);
|
2015-06-03 14:56:01 -04:00
|
|
|
|
2016-05-02 01:50:37 -04:00
|
|
|
input.value = "updatedValue";
|
|
|
|
dispatchEvent(input, "input");
|
|
|
|
fixture.detectChanges();
|
2015-06-03 14:56:01 -04:00
|
|
|
|
2016-05-02 01:50:37 -04:00
|
|
|
expect(sortedClassList(input)).toEqual(["ng-dirty", "ng-touched", "ng-valid"]);
|
|
|
|
async.done();
|
|
|
|
});
|
2015-06-25 00:51:37 -04:00
|
|
|
}));
|
2015-06-03 14:56:01 -04:00
|
|
|
|
2016-04-12 12:40:37 -04:00
|
|
|
it("should work with complex model-driven forms",
|
2015-06-25 00:51:37 -04:00
|
|
|
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
|
2016-04-12 12:40:37 -04:00
|
|
|
var form = new ControlGroup({"name": new Control("", Validators.required)});
|
2015-06-03 14:56:01 -04:00
|
|
|
|
2015-11-23 19:02:19 -05:00
|
|
|
var t = `<form [ngFormModel]="form"><input type="text" ngControl="name"></form>`;
|
2015-06-03 14:56:01 -04:00
|
|
|
|
2016-05-02 01:50:37 -04:00
|
|
|
tcb.overrideTemplate(MyComp8, t)
|
|
|
|
.createAsync(MyComp8)
|
|
|
|
.then((fixture) => {
|
|
|
|
fixture.debugElement.componentInstance.form = form;
|
|
|
|
fixture.detectChanges();
|
2015-06-03 14:56:01 -04:00
|
|
|
|
2016-05-02 01:50:37 -04:00
|
|
|
var input = fixture.debugElement.query(By.css("input")).nativeElement;
|
|
|
|
expect(sortedClassList(input))
|
|
|
|
.toEqual(["ng-invalid", "ng-pristine", "ng-untouched"]);
|
2015-06-03 14:56:01 -04:00
|
|
|
|
2016-05-02 01:50:37 -04:00
|
|
|
dispatchEvent(input, "blur");
|
|
|
|
fixture.detectChanges();
|
2015-06-03 14:56:01 -04:00
|
|
|
|
2016-05-02 01:50:37 -04:00
|
|
|
expect(sortedClassList(input))
|
|
|
|
.toEqual(["ng-invalid", "ng-pristine", "ng-touched"]);
|
2015-06-03 14:56:01 -04:00
|
|
|
|
2016-05-02 01:50:37 -04:00
|
|
|
input.value = "updatedValue";
|
|
|
|
dispatchEvent(input, "input");
|
|
|
|
fixture.detectChanges();
|
2015-06-03 14:56:01 -04:00
|
|
|
|
2016-05-02 01:50:37 -04:00
|
|
|
expect(sortedClassList(input)).toEqual(["ng-dirty", "ng-touched", "ng-valid"]);
|
|
|
|
async.done();
|
|
|
|
});
|
2015-06-25 00:51:37 -04:00
|
|
|
}));
|
2015-06-03 14:56:01 -04:00
|
|
|
|
2016-04-12 12:40:37 -04:00
|
|
|
it("should work with ngModel",
|
2015-06-25 00:51:37 -04:00
|
|
|
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
|
2015-11-23 19:02:19 -05:00
|
|
|
var t = `<div><input [(ngModel)]="name" required></div>`;
|
2015-06-03 14:56:01 -04:00
|
|
|
|
2016-05-02 01:50:37 -04:00
|
|
|
tcb.overrideTemplate(MyComp8, t)
|
|
|
|
.createAsync(MyComp8)
|
|
|
|
.then((fixture) => {
|
|
|
|
fixture.debugElement.componentInstance.name = "";
|
|
|
|
fixture.detectChanges();
|
2015-06-03 14:56:01 -04:00
|
|
|
|
2016-05-02 01:50:37 -04:00
|
|
|
var input = fixture.debugElement.query(By.css("input")).nativeElement;
|
|
|
|
expect(sortedClassList(input))
|
|
|
|
.toEqual(["ng-invalid", "ng-pristine", "ng-untouched"]);
|
2015-06-03 14:56:01 -04:00
|
|
|
|
2016-05-02 01:50:37 -04:00
|
|
|
dispatchEvent(input, "blur");
|
|
|
|
fixture.detectChanges();
|
2015-06-03 14:56:01 -04:00
|
|
|
|
2016-05-02 01:50:37 -04:00
|
|
|
expect(sortedClassList(input))
|
|
|
|
.toEqual(["ng-invalid", "ng-pristine", "ng-touched"]);
|
2015-06-03 14:56:01 -04:00
|
|
|
|
2016-05-02 01:50:37 -04:00
|
|
|
input.value = "updatedValue";
|
|
|
|
dispatchEvent(input, "input");
|
|
|
|
fixture.detectChanges();
|
2015-06-03 14:56:01 -04:00
|
|
|
|
2016-05-02 01:50:37 -04:00
|
|
|
expect(sortedClassList(input)).toEqual(["ng-dirty", "ng-touched", "ng-valid"]);
|
|
|
|
async.done();
|
|
|
|
});
|
2015-06-25 00:51:37 -04:00
|
|
|
}));
|
2015-06-03 14:56:01 -04:00
|
|
|
});
|
2015-07-15 21:16:50 -04:00
|
|
|
|
2016-04-12 12:40:37 -04:00
|
|
|
describe("ngModel corner cases", () => {
|
|
|
|
it("should not update the view when the value initially came from the view",
|
2016-04-18 19:04:35 -04:00
|
|
|
fakeAsync(inject([TestComponentBuilder], (tcb: TestComponentBuilder) => {
|
|
|
|
var form = new Control("");
|
|
|
|
|
|
|
|
var t = `<div><input type="text" [ngFormControl]="form" [(ngModel)]="name"></div>`;
|
2016-04-28 20:50:03 -04:00
|
|
|
let fixture = tcb.overrideTemplate(MyComp8, t).createFakeAsync(MyComp8);
|
2016-04-18 19:04:35 -04:00
|
|
|
tick();
|
|
|
|
fixture.debugElement.componentInstance.form = form;
|
|
|
|
fixture.detectChanges();
|
|
|
|
|
|
|
|
var input = fixture.debugElement.query(By.css("input")).nativeElement;
|
|
|
|
input.value = "aa";
|
2016-05-02 00:50:23 -04:00
|
|
|
input.setSelectionRange(1, 2);
|
2016-04-18 19:04:35 -04:00
|
|
|
dispatchEvent(input, "input");
|
|
|
|
|
|
|
|
tick();
|
|
|
|
fixture.detectChanges();
|
|
|
|
|
|
|
|
// selection start has not changed because we did not reset the value
|
|
|
|
expect(input.selectionStart).toEqual(1);
|
|
|
|
})));
|
2015-08-19 00:42:11 -04:00
|
|
|
|
2016-04-12 12:40:37 -04:00
|
|
|
it("should update the view when the model is set back to what used to be in the view",
|
2016-04-18 19:04:35 -04:00
|
|
|
fakeAsync(inject([TestComponentBuilder], (tcb: TestComponentBuilder) => {
|
|
|
|
var t = `<input type="text" [(ngModel)]="name">`;
|
2016-04-28 20:50:03 -04:00
|
|
|
let fixture = tcb.overrideTemplate(MyComp8, t).createFakeAsync(MyComp8);
|
2016-04-18 19:04:35 -04:00
|
|
|
tick();
|
|
|
|
fixture.debugElement.componentInstance.name = "";
|
|
|
|
fixture.detectChanges();
|
|
|
|
|
|
|
|
// Type "aa" into the input.
|
|
|
|
var input = fixture.debugElement.query(By.css("input")).nativeElement;
|
|
|
|
input.value = "aa";
|
|
|
|
input.selectionStart = 1;
|
|
|
|
dispatchEvent(input, "input");
|
|
|
|
|
|
|
|
tick();
|
|
|
|
fixture.detectChanges();
|
|
|
|
expect(fixture.debugElement.componentInstance.name).toEqual("aa");
|
|
|
|
|
|
|
|
// Programatically update the input value to be "bb".
|
|
|
|
fixture.debugElement.componentInstance.name = "bb";
|
|
|
|
tick();
|
|
|
|
fixture.detectChanges();
|
|
|
|
expect(input.value).toEqual("bb");
|
|
|
|
|
|
|
|
// Programatically set it back to "aa".
|
|
|
|
fixture.debugElement.componentInstance.name = "aa";
|
|
|
|
tick();
|
|
|
|
fixture.detectChanges();
|
|
|
|
expect(input.value).toEqual("aa");
|
|
|
|
})));
|
2016-04-12 12:40:37 -04:00
|
|
|
it("should not crash when validity is checked from a binding",
|
2016-04-18 19:04:35 -04:00
|
|
|
fakeAsync(inject([TestComponentBuilder], (tcb: TestComponentBuilder) => {
|
|
|
|
// {{x.valid}} used to crash because valid() tried to read a property
|
|
|
|
// from form.control before it was set. This test verifies this bug is
|
|
|
|
// fixed.
|
|
|
|
var t = `<form><div ngControlGroup="x" #x="ngForm">
|
2015-11-23 19:02:19 -05:00
|
|
|
<input type="text" ngControl="test"></div>{{x.valid}}</form>`;
|
2016-04-28 20:50:03 -04:00
|
|
|
let fixture = tcb.overrideTemplate(MyComp8, t).createFakeAsync(MyComp8);
|
2016-04-18 19:04:35 -04:00
|
|
|
tick();
|
|
|
|
fixture.detectChanges();
|
|
|
|
})));
|
2015-07-15 21:16:50 -04:00
|
|
|
});
|
2015-05-22 15:32:49 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
@Directive({
|
|
|
|
selector: '[wrapped-value]',
|
2015-11-20 04:19:04 -05:00
|
|
|
host: {'(input)': 'handleOnInput($event.target.value)', '[value]': 'value'}
|
2015-05-22 15:32:49 -04:00
|
|
|
})
|
2015-05-30 14:56:00 -04:00
|
|
|
class WrappedValue implements ControlValueAccessor {
|
2015-05-22 15:32:49 -04:00
|
|
|
value;
|
|
|
|
onChange: Function;
|
|
|
|
|
2015-06-10 16:51:44 -04:00
|
|
|
constructor(cd: NgControl) { cd.valueAccessor = this; }
|
2015-05-22 15:32:49 -04:00
|
|
|
|
|
|
|
writeValue(value) { this.value = `!${value}!`; }
|
|
|
|
|
2015-05-30 14:56:00 -04:00
|
|
|
registerOnChange(fn) { this.onChange = fn; }
|
2015-06-02 11:41:33 -04:00
|
|
|
registerOnTouched(fn) {}
|
2015-05-30 14:56:00 -04:00
|
|
|
|
2015-11-20 04:19:04 -05:00
|
|
|
handleOnInput(value) { this.onChange(value.substring(1, value.length - 1)); }
|
2015-05-22 15:32:49 -04:00
|
|
|
}
|
|
|
|
|
2016-04-12 12:40:37 -04:00
|
|
|
@Component({selector: "my-input", template: ''})
|
2015-10-23 17:06:46 -04:00
|
|
|
class MyInput implements ControlValueAccessor {
|
2015-11-20 04:19:04 -05:00
|
|
|
@Output('input') onInput: EventEmitter<any> = new EventEmitter();
|
2015-10-23 17:06:46 -04:00
|
|
|
value: string;
|
|
|
|
|
|
|
|
constructor(cd: NgControl) { cd.valueAccessor = this; }
|
|
|
|
|
|
|
|
writeValue(value) { this.value = `!${value}!`; }
|
|
|
|
|
2015-11-20 04:19:04 -05:00
|
|
|
registerOnChange(fn) { ObservableWrapper.subscribe(this.onInput, fn); }
|
2015-10-23 17:06:46 -04:00
|
|
|
|
|
|
|
registerOnTouched(fn) {}
|
|
|
|
|
|
|
|
dispatchChangeEvent() {
|
2015-11-20 04:19:04 -05:00
|
|
|
ObservableWrapper.callEmit(this.onInput, this.value.substring(1, this.value.length - 1));
|
2015-10-23 17:06:46 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-29 20:45:15 -04:00
|
|
|
function uniqLoginAsyncValidator(expectedValue: string) {
|
|
|
|
return (c) => {
|
2015-11-02 13:00:42 -05:00
|
|
|
var completer = PromiseWrapper.completer();
|
2016-04-12 12:40:37 -04:00
|
|
|
var res = (c.value == expectedValue) ? null : {"uniqLogin": true};
|
2015-11-02 13:00:42 -05:00
|
|
|
completer.resolve(res);
|
|
|
|
return completer.promise;
|
2015-10-29 20:45:15 -04:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2015-10-27 17:36:13 -04:00
|
|
|
function loginIsEmptyGroupValidator(c: ControlGroup) {
|
2016-04-12 12:40:37 -04:00
|
|
|
return c.controls["login"].value == "" ? {"loginIsEmpty": true} : null;
|
2015-10-27 17:36:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
@Directive({
|
|
|
|
selector: '[login-is-empty-validator]',
|
2016-04-26 01:25:21 -04:00
|
|
|
providers: [
|
|
|
|
/* @ts2dart_Provider */ {
|
|
|
|
provide: NG_VALIDATORS,
|
|
|
|
useValue: loginIsEmptyGroupValidator,
|
|
|
|
multi: true
|
|
|
|
}
|
|
|
|
]
|
2015-10-27 17:36:13 -04:00
|
|
|
})
|
|
|
|
class LoginIsEmptyValidator {
|
|
|
|
}
|
|
|
|
|
2015-11-02 13:00:42 -05:00
|
|
|
@Directive({
|
|
|
|
selector: '[uniq-login-validator]',
|
2016-04-12 12:40:37 -04:00
|
|
|
providers: [
|
2016-06-02 20:30:40 -04:00
|
|
|
{provide: NG_ASYNC_VALIDATORS, useExisting: forwardRef(() => UniqLoginValidator), multi: true}
|
2016-04-12 12:40:37 -04:00
|
|
|
]
|
2015-11-02 13:00:42 -05:00
|
|
|
})
|
|
|
|
class UniqLoginValidator implements Validator {
|
|
|
|
@Input('uniq-login-validator') expected;
|
|
|
|
|
|
|
|
validate(c) { return uniqLoginAsyncValidator(this.expected)(c); }
|
|
|
|
}
|
|
|
|
|
2015-10-27 17:36:13 -04:00
|
|
|
@Component({
|
2016-04-12 12:40:37 -04:00
|
|
|
selector: "my-comp",
|
2015-10-27 17:36:13 -04:00
|
|
|
template: '',
|
2015-11-02 13:00:42 -05:00
|
|
|
directives: [
|
2016-04-12 12:40:37 -04:00
|
|
|
FORM_DIRECTIVES,
|
|
|
|
WrappedValue,
|
|
|
|
MyInput,
|
|
|
|
NgIf,
|
|
|
|
NgFor,
|
|
|
|
LoginIsEmptyValidator,
|
|
|
|
UniqLoginValidator
|
2015-11-02 13:00:42 -05:00
|
|
|
]
|
2015-10-27 17:36:13 -04:00
|
|
|
})
|
2016-04-28 20:50:03 -04:00
|
|
|
class MyComp8 {
|
2015-05-22 15:32:49 -04:00
|
|
|
form: any;
|
|
|
|
name: string;
|
2015-06-13 15:15:42 -04:00
|
|
|
data: any;
|
2016-04-01 22:27:25 -04:00
|
|
|
list: any[];
|
|
|
|
selectedCity: any;
|
|
|
|
customTrackBy(index: number, obj: any): number { return index; };
|
2015-06-04 03:58:22 -04:00
|
|
|
}
|
2015-10-01 13:07:49 -04:00
|
|
|
|
|
|
|
function sortedClassList(el) {
|
2016-04-28 20:50:03 -04:00
|
|
|
var l = getDOM().classList(el);
|
2015-10-01 13:07:49 -04:00
|
|
|
ListWrapper.sort(l);
|
|
|
|
return l;
|
2015-12-08 14:17:39 -05:00
|
|
|
}
|