-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add counter input component (#61)
- Loading branch information
Showing
10 changed files
with
216 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
src/app/shared/forms/components/counter/counter.component.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<div class="row form-group" [formGroup]="form"> | ||
<label *ngIf="displayLabel" [for]="uuid" class="col-form-label" [ngClass]="labelClass" | ||
>{{ label | translate }}<span *ngIf="required" class="required">*</span> | ||
</label> | ||
<div [ngClass]="inputClass"> | ||
<div class="form-control p-0"> | ||
<div class="d-flex flex-row align-items-center justify-content-around"> | ||
<!-- display: inline --> | ||
<button | ||
class="btn btn-link mr-0" | ||
(click)="decrease()" | ||
[disabled]="cannotDecrease$ | async" | ||
[attr.data-testing-id]="'decrease-' + controlName" | ||
>{{ 'product.quantity.decrease.text' | translate }}</button | ||
> | ||
<span class="text-center w-100" [attr.data-testing-id]="controlName">{{ value$ | async }}</span> | ||
<!-- display: inline --> | ||
<button | ||
class="btn btn-link mr-0" | ||
(click)="increase()" | ||
[disabled]="cannotIncrease$ | async" | ||
[attr.data-testing-id]="'increase-' + controlName" | ||
>{{ 'product.quantity.increase.text' | translate }}</button | ||
> | ||
</div> | ||
</div> | ||
</div> | ||
</div> |
97 changes: 97 additions & 0 deletions
97
src/app/shared/forms/components/counter/counter.component.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import { ComponentFixture, TestBed, async } from '@angular/core/testing'; | ||
import { FormControl, FormGroup, ReactiveFormsModule } from '@angular/forms'; | ||
import { By } from '@angular/platform-browser'; | ||
import { TranslateModule } from '@ngx-translate/core'; | ||
import { spy, verify } from 'ts-mockito'; | ||
|
||
import { CounterComponent } from './counter.component'; | ||
|
||
describe('Counter Component', () => { | ||
let component: CounterComponent; | ||
let fixture: ComponentFixture<CounterComponent>; | ||
let element: HTMLElement; | ||
|
||
const controlName = 'quantity'; | ||
|
||
beforeEach(async(() => { | ||
TestBed.configureTestingModule({ | ||
imports: [ReactiveFormsModule, TranslateModule.forRoot()], | ||
declarations: [CounterComponent], | ||
}).compileComponents(); | ||
})); | ||
|
||
beforeEach(() => { | ||
fixture = TestBed.createComponent(CounterComponent); | ||
component = fixture.componentInstance; | ||
element = fixture.nativeElement; | ||
|
||
component.form = new FormGroup({ | ||
[controlName]: new FormControl(), | ||
}); | ||
component.controlName = controlName; | ||
}); | ||
|
||
it('should be created', () => { | ||
expect(component).toBeTruthy(); | ||
expect(element).toBeTruthy(); | ||
expect(() => fixture.detectChanges()).not.toThrow(); | ||
}); | ||
|
||
describe('with value', () => { | ||
beforeEach(() => { | ||
component.form.get(controlName).setValue(42); | ||
fixture.detectChanges(); | ||
}); | ||
|
||
it('should display value from form when rendered', () => { | ||
const display = element.querySelector(`[data-testing-id=${controlName}]`); | ||
expect(display.textContent).toMatchInlineSnapshot(`""`); | ||
}); | ||
|
||
it('should increase value when increase button was clicked', () => { | ||
const componentSpy = spy(component); | ||
fixture.debugElement.query(By.css(`[data-testing-id=increase-${controlName}]`)).triggerEventHandler('click', {}); | ||
fixture.detectChanges(); | ||
verify(componentSpy.increase()).once(); | ||
|
||
const display = element.querySelector(`[data-testing-id=${controlName}]`); | ||
expect(display.textContent).toMatchInlineSnapshot(`"43"`); | ||
}); | ||
|
||
it('should decrease value when decrease button was clicked', () => { | ||
const componentSpy = spy(component); | ||
fixture.debugElement.query(By.css(`[data-testing-id=decrease-${controlName}]`)).triggerEventHandler('click', {}); | ||
fixture.detectChanges(); | ||
verify(componentSpy.decrease()).once(); | ||
|
||
const display = element.querySelector(`[data-testing-id=${controlName}]`); | ||
expect(display.textContent).toMatchInlineSnapshot(`"41"`); | ||
}); | ||
|
||
describe('with max', () => { | ||
beforeEach(() => { | ||
component.max = 42; | ||
component.ngOnChanges(); | ||
fixture.detectChanges(); | ||
}); | ||
|
||
it('should disable increase button when max is reached', () => { | ||
const increase = element.querySelector(`[data-testing-id=increase-${controlName}]`); | ||
expect(increase.hasAttribute('disabled')).toBeTrue(); | ||
}); | ||
}); | ||
|
||
describe('with min', () => { | ||
beforeEach(() => { | ||
component.min = 42; | ||
component.ngOnChanges(); | ||
fixture.detectChanges(); | ||
}); | ||
|
||
it('should disable decrease button when min is reached', () => { | ||
const decrease = element.querySelector(`[data-testing-id=decrease-${controlName}]`); | ||
expect(decrease.hasAttribute('disabled')).toBeTrue(); | ||
}); | ||
}); | ||
}); | ||
}); |
60 changes: 60 additions & 0 deletions
60
src/app/shared/forms/components/counter/counter.component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { ChangeDetectionStrategy, Component, Input, OnChanges, OnDestroy, OnInit } from '@angular/core'; | ||
import { FormControl } from '@angular/forms'; | ||
import { TranslateService } from '@ngx-translate/core'; | ||
import { Observable, ReplaySubject, Subject } from 'rxjs'; | ||
import { map, takeUntil } from 'rxjs/operators'; | ||
|
||
import { FormElementComponent } from 'ish-shared/forms/components/form-element/form-element.component'; | ||
|
||
@Component({ | ||
selector: 'ish-counter', | ||
templateUrl: './counter.component.html', | ||
changeDetection: ChangeDetectionStrategy.OnPush, | ||
}) | ||
export class CounterComponent extends FormElementComponent implements OnInit, OnDestroy, OnChanges { | ||
@Input() min: number; | ||
@Input() max: number; | ||
|
||
value$ = new ReplaySubject<number>(1); | ||
cannotDecrease$: Observable<boolean>; | ||
cannotIncrease$: Observable<boolean>; | ||
|
||
private destroy$ = new Subject(); | ||
|
||
constructor(protected translate: TranslateService) { | ||
super(translate); | ||
} | ||
|
||
ngOnDestroy() { | ||
this.destroy$.next(); | ||
} | ||
|
||
private get value(): number { | ||
return +this.formControl.value; | ||
} | ||
|
||
ngOnChanges() { | ||
this.value$.next(this.value); | ||
} | ||
|
||
ngOnInit() { | ||
super.init(); | ||
|
||
this.cannotDecrease$ = this.value$.pipe(map(value => this.min !== undefined && value <= this.min)); | ||
this.cannotIncrease$ = this.value$.pipe(map(value => this.max !== undefined && value >= this.max)); | ||
|
||
this.formControl.valueChanges.pipe(takeUntil(this.destroy$)).subscribe(this.value$); | ||
} | ||
|
||
increase() { | ||
(this.formControl as FormControl).setValue(this.value + 1, { emitEvent: true }); | ||
} | ||
|
||
decrease() { | ||
(this.formControl as FormControl).setValue(this.value - 1, { emitEvent: true }); | ||
} | ||
|
||
get displayLabel(): boolean { | ||
return !!this.label && !!this.label.trim(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters