currency-input.component.spec.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import { Component } from '@angular/core';
  2. import { Type } from '@angular/core/src/type';
  3. import { async, ComponentFixture, fakeAsync, TestBed, tick } from '@angular/core/testing';
  4. import { FormsModule } from '@angular/forms';
  5. import { By } from '@angular/platform-browser';
  6. import { CurrencyInputComponent } from './currency-input.component';
  7. describe('CurrencyInputComponent', () => {
  8. beforeEach(async(() => {
  9. TestBed.configureTestingModule({
  10. imports: [FormsModule],
  11. declarations: [TestControlValueAccessorComponent, TestSimpleComponent, CurrencyInputComponent],
  12. }).compileComponents();
  13. }));
  14. it(
  15. 'should display the price as decimal with a simple binding',
  16. fakeAsync(() => {
  17. const fixture = createAndRunChangeDetection(TestSimpleComponent);
  18. const nativeInput = getNativeInput(fixture);
  19. expect(nativeInput.value).toBe('1.23');
  20. }),
  21. );
  22. it(
  23. 'should display the price as decimal',
  24. fakeAsync(() => {
  25. const fixture = createAndRunChangeDetection(TestControlValueAccessorComponent);
  26. const nativeInput = getNativeInput(fixture);
  27. expect(nativeInput.value).toBe('1.23');
  28. }),
  29. );
  30. it(
  31. 'should display 2 decimal places for multiples of 10',
  32. fakeAsync(() => {
  33. const fixture = createAndRunChangeDetection(TestControlValueAccessorComponent, 120);
  34. const nativeInput = getNativeInput(fixture);
  35. expect(nativeInput.value).toBe('1.20');
  36. }),
  37. );
  38. it(
  39. 'should discard decimal places from input value',
  40. fakeAsync(() => {
  41. const fixture = createAndRunChangeDetection(TestControlValueAccessorComponent, 123.5);
  42. const nativeInput = getNativeInput(fixture);
  43. expect(nativeInput.value).toBe('1.23');
  44. }),
  45. );
  46. it(
  47. 'should correctly round decimal value ',
  48. fakeAsync(() => {
  49. const fixture = createAndRunChangeDetection(TestControlValueAccessorComponent);
  50. const nativeInput = fixture.debugElement.query(By.css('input[type="number"]'));
  51. nativeInput.triggerEventHandler('input', { target: { value: 1.13 } });
  52. tick();
  53. fixture.detectChanges();
  54. expect(fixture.componentInstance.price).toBe(113);
  55. }),
  56. );
  57. it(
  58. 'should update model with integer values for values of more than 2 decimal places',
  59. fakeAsync(() => {
  60. const fixture = createAndRunChangeDetection(TestControlValueAccessorComponent);
  61. const nativeInput = fixture.debugElement.query(By.css('input[type="number"]'));
  62. nativeInput.triggerEventHandler('input', { target: { value: 1.567 } });
  63. tick();
  64. fixture.detectChanges();
  65. expect(fixture.componentInstance.price).toBe(157);
  66. }),
  67. );
  68. function createAndRunChangeDetection<T extends TestControlValueAccessorComponent | TestSimpleComponent>(
  69. component: Type<T>,
  70. priceValue = 123,
  71. ): ComponentFixture<T> {
  72. const fixture = TestBed.createComponent(component);
  73. fixture.componentInstance.price = priceValue;
  74. fixture.detectChanges();
  75. tick();
  76. fixture.detectChanges();
  77. return fixture;
  78. }
  79. function getNativeInput(_fixture: ComponentFixture<TestControlValueAccessorComponent>): HTMLInputElement {
  80. return _fixture.debugElement.query(By.css('input[type="number"]')).nativeElement;
  81. }
  82. });
  83. @Component({
  84. selector: 'vdr-test-component',
  85. template: `<vdr-currency-input [(ngModel)]="price"></vdr-currency-input>`,
  86. })
  87. class TestControlValueAccessorComponent {
  88. price = 123;
  89. }
  90. @Component({
  91. selector: 'vdr-test-component',
  92. template: `<vdr-currency-input [value]="price"></vdr-currency-input>`,
  93. })
  94. class TestSimpleComponent {
  95. price = 123;
  96. }