Advertisement
Guest User

Untitled

a guest
Mar 27th, 2017
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.69 KB | None | 0 0
  1. <form [formGroup]="form">
  2. <md-input-container>
  3. <input mdInput formControlName="testControl">
  4. <md-placeholder>A Test Input Control</md-placeholder>
  5. <md-hint id="hint" *ngIf="form.controls.testControl.dirty && form.controls.testControl.errors?.pattern">I am hinting</md-hint>
  6. </md-input-container>
  7. </form>
  8.  
  9. import { Component, OnInit } from '@angular/core';
  10. import { FormGroup, FormControl, Validators } from '@angular/forms';
  11.  
  12. @Component({
  13. selector: 'app-test-md-hint',
  14. templateUrl: './test-md-hint.component.html',
  15. styleUrls: ['./test-md-hint.component.css']
  16. })
  17. export class TestMdHintComponent implements OnInit {
  18.  
  19. form: FormGroup;
  20.  
  21. constructor() { }
  22.  
  23. ngOnInit() {
  24.  
  25. this.form = new FormGroup({
  26. testControl: new FormControl(null, [
  27. Validators.pattern('match-me')
  28. ])
  29. });
  30. }
  31.  
  32. }
  33.  
  34. import { async, ComponentFixture, TestBed } from '@angular/core/testing';
  35. import { NO_ERRORS_SCHEMA, DebugElement } from '@angular/core';
  36. import { By } from '@angular/platform-browser';
  37.  
  38. import { TestMdHintComponent } from './test-md-hint.component';
  39.  
  40. describe('TestMdHintComponent', () => {
  41. let component: TestMdHintComponent;
  42. let fixture: ComponentFixture<TestMdHintComponent>;
  43.  
  44. beforeEach(async(() => {
  45. TestBed.configureTestingModule({
  46. declarations: [TestMdHintComponent],
  47. schemas: [NO_ERRORS_SCHEMA]
  48. })
  49. .compileComponents();
  50. }));
  51.  
  52. beforeEach(() => {
  53. fixture = TestBed.createComponent(TestMdHintComponent);
  54. component = fixture.componentInstance;
  55. fixture.detectChanges();
  56. });
  57.  
  58.  
  59. let form: DebugElement;
  60. let input: DebugElement;
  61. let hint: DebugElement;
  62. beforeEach(() => {
  63. form = fixture.debugElement.query(By.css('form'));
  64. input = fixture.debugElement.query(By.css('input'));
  65. hint = fixture.debugElement.query(By.css('#hint'));
  66. });
  67.  
  68. // passes as expected
  69. it('finds the input', () => {
  70. expect(input).not.toBeNull();
  71. expect(input.nativeElement.getAttribute('formControlName'))
  72. .toBe('testControl');
  73. });
  74.  
  75. // passes as expected
  76. it('starts with no hints', () => {
  77. expect(hint).toBeNull();
  78. });
  79.  
  80. // passes as expected
  81. it('displays no hint when all is okay', () => {
  82. input.nativeElement.value = 'match-me';
  83. input.nativeElement.dispatchEvent(new Event('input'));
  84. fixture.detectChanges();
  85. hint = fixture.debugElement.query(By.css('md-hint'));
  86. expect(hint).toBeNull();
  87. });
  88.  
  89. // fails, not as expected
  90. it('displays a hint when required', () => {
  91. input.nativeElement.value = 'no-match';
  92. input.nativeElement.dispatchEvent(new Event('input'));
  93. fixture.detectChanges();
  94. hint = fixture.debugElement.query(By.css('md-hint'));
  95. expect(hint).not.toBeNull();
  96. });
  97.  
  98. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement