Advertisement
Guest User

Untitled

a guest
Dec 3rd, 2016
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.26 KB | None | 0 0
  1. @Component({
  2. selector: 'text-counter',
  3. templateUrl: 'text-counter.component.html',
  4. styleUrls: ['text-counter.component.css']
  5. })
  6. export class TextCounterComponent implements OnChanges {
  7.  
  8. @Input() inputText: string;
  9. @Input() min: number;
  10. @Input() max: number;
  11. @Input() tolerance: number = 20;
  12.  
  13. message: string;
  14. messageClass: string;
  15.  
  16. constructor() {
  17. }
  18.  
  19. ngOnChanges(changes: SimpleChanges) {
  20.  
  21. const input: string = changes['inputText'].currentValue;
  22.  
  23. function expandMessage(rawMessage, n) {
  24. return rawMessage.replace('%', n);
  25. }
  26.  
  27. const settings = {
  28. initialPrompt: 'saisissez au moins % caractères',
  29. nToGoPrompt: 'il manque % caractères',
  30. nLeftPrompt: 'encore % caractères autorisés',
  31. tooLongByPrompt: 'trop long de % caractères'
  32. };
  33.  
  34. const length = input ? input.length : 0;
  35.  
  36. this.messageClass = 'lightblue';
  37.  
  38. if (length === 0) {
  39. this.message = expandMessage(settings.initialPrompt, this.min);
  40. }
  41. else if (length > 0 && length < this.min) {
  42. this.message = expandMessage(settings.nToGoPrompt, this.min - length);
  43. }
  44. else if (length >= this.min && length <= this.max) {
  45. if (length > this.max - this.tolerance) {
  46. this.messageClass = 'Gold';
  47. }
  48. this.message = expandMessage(settings.nLeftPrompt, this.max - length);
  49. }
  50. else {
  51. this.messageClass = 'Red';
  52. this.message = expandMessage(settings.tooLongByPrompt, length - this.max);
  53. }
  54. }
  55. }
  56.  
  57. <span class="form-text" [ngClass]="messageClass">
  58. {{message}}
  59. </span>
  60.  
  61. @Component({
  62. selector: `test-host-component`,
  63. template: `<div>
  64. <text-counter
  65. [inputText]="valueFromHost"
  66. [min]="2"
  67. [max]="500">
  68. </text-counter>
  69. </div>`
  70. })
  71. export class TestHostComponent {
  72. /* using viewChild we get access to the TestComponent which is a child of TestHostComponent */
  73. @ViewChild(TextCounterComponent)
  74. public textCounterComponent: any;
  75. /* this is the variable which is passed as input to the TestComponent */
  76. public valueFromHost: string;
  77. }
  78.  
  79. describe('Component: TextCounter', () => {
  80.  
  81. beforeEach(async(() => {
  82. TestBed.configureTestingModule({
  83. declarations: [TextCounterComponent, TestHostComponent]
  84. })
  85. .compileComponents();
  86. }));
  87.  
  88. it('should indicate min message', () => {
  89. const hostComponentFixture = TestBed.createComponent(TestHostComponent);
  90. const textCounterComponentFixture = TestBed.createComponent(TextCounterComponent);
  91.  
  92. const de: DebugElement = textCounterComponentFixture.debugElement.query(By.css('span'));
  93. const el: HTMLElement = de.nativeElement;
  94. let testHostComponent = hostComponentFixture.componentInstance;
  95.  
  96. testHostComponent.valueFromHost = 'a';
  97.  
  98. spyOn(testHostComponent.textCounterComponent, 'ngOnChanges').and.callThrough();
  99. hostComponentFixture.detectChanges();
  100. textCounterComponentFixture.detectChanges();
  101. expect(testHostComponent.textCounterComponent.ngOnChanges).toHaveBeenCalled();
  102.  
  103. console.log('el.textContent: ', el.textContent);
  104. expect(el.textContent).toContain('il manque');//This fails!!
  105. });
  106. });
  107.  
  108. Chrome 54.0.2840 (Mac OS X 10.12.1) Component: TextCounter should indicate min message FAILED
  109. Expected '
  110.  
  111. ' to contain 'il manque'.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement