docs: add forms overview example for snippets (#25663)

PR Close #25663
This commit is contained in:
Brandon Roberts
2018-09-05 10:00:14 -05:00
committed by Kara Erickson
parent ebd01e8e79
commit 5649acd03f
21 changed files with 435 additions and 81 deletions

View File

@ -0,0 +1,62 @@
// #docregion
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { ReactiveFormsModule } from '@angular/forms';
import { ReactiveNameComponent } from './name.component';
import { createNewEvent } from '../../shared/utils';
// #docregion tests
describe('Reactive Name Component', () => {
// #enddocregion tests
let component: ReactiveNameComponent;
let fixture: ComponentFixture<ReactiveNameComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [ReactiveFormsModule],
declarations: [ReactiveNameComponent]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(ReactiveNameComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
// #docregion tests
it('should update the value in the control', () => {
// update the control value
component.name.setValue('Nancy');
// query the element
const input = fixture.nativeElement.querySelector('input');
// check its value
expect(input.value).toBe('Nancy');
});
it('should update the value of the input field', () => {
// update the control value
component.name.setValue('Nancy');
// query the element
const input = fixture.nativeElement.querySelector('input');
expect(input.value).toEqual('Nancy');
// update the form field value
input.value = 'Smith';
// Use utility function to create custom event, then dispatch on the input
const event = createNewEvent('input');
input.dispatchEvent(event);
expect(fixture.componentInstance.name.value).toEqual('Smith');
});
});
// #enddocregion

View File

@ -0,0 +1,17 @@
// #docplaster
// #docregion
import { Component } from '@angular/core';
import { FormControl } from '@angular/forms';
@Component({
// #enddocregion
selector: 'app-reactive-name',
// #docregion
template: `
Name: <input type="text" [formControl]="name">
`
})
export class ReactiveNameComponent {
name = new FormControl('');
}
// #enddocregion

View File

@ -0,0 +1,13 @@
import { ReactiveModule } from './reactive.module';
describe('ReactiveModule', () => {
let reactiveModule: ReactiveModule;
beforeEach(() => {
reactiveModule = new ReactiveModule();
});
it('should create an instance', () => {
expect(reactiveModule).toBeTruthy();
});
});

View File

@ -0,0 +1,14 @@
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { ReactiveNameComponent } from './name/name.component';
import { ReactiveFormsModule } from '@angular/forms';
@NgModule({
imports: [
CommonModule,
ReactiveFormsModule
],
declarations: [ReactiveNameComponent],
exports: [ReactiveNameComponent],
})
export class ReactiveModule { }