|
| 1 | +import { ElementRef } from '@angular/core'; |
| 2 | +import { ControlValueAccessor } from '@angular/forms'; |
| 3 | +import { Action, ActionsSubject } from '@ngrx/store'; |
| 4 | +import { Observable } from 'rxjs/Observable'; |
| 5 | +import { BehaviorSubject } from 'rxjs/BehaviorSubject'; |
| 6 | +import 'rxjs/add/operator/first'; |
| 7 | +import 'rxjs/add/operator/count'; |
| 8 | + |
| 9 | +import { SetValueAction } from '../actions'; |
| 10 | +import { createFormControlState } from '../state'; |
| 11 | +import { NgrxFormControlDirective } from './directive'; |
| 12 | + |
| 13 | +describe(NgrxFormControlDirective.name, () => { |
| 14 | + let directive: NgrxFormControlDirective<string>; |
| 15 | + let elementRef: ElementRef; |
| 16 | + let document: Document; |
| 17 | + let actionsSubject: ActionsSubject; |
| 18 | + let actions$: Observable<Action>; |
| 19 | + let valueAccessor: ControlValueAccessor; |
| 20 | + let onChange: (value: string) => void; |
| 21 | + let onTouched: () => void; |
| 22 | + const FORM_CONTROL_ID = 'test ID'; |
| 23 | + const INITIAL_FORM_CONTROL_VALUE = 'value'; |
| 24 | + const INITIAL_STATE = createFormControlState<string>(FORM_CONTROL_ID, INITIAL_FORM_CONTROL_VALUE); |
| 25 | + |
| 26 | + beforeEach(() => { |
| 27 | + elementRef = { nativeElement: { focus: () => void 0, blur: () => void 0 } } as any as ElementRef; |
| 28 | + document = {} as any as Document; |
| 29 | + actionsSubject = new BehaviorSubject<Action>({ type: '' }) as ActionsSubject; |
| 30 | + actions$ = actionsSubject as any; // required due to mismatch of lift() function signature |
| 31 | + valueAccessor = { |
| 32 | + writeValue: () => void 0, |
| 33 | + registerOnChange: fn => onChange = fn, |
| 34 | + registerOnTouched: fn => onTouched = fn, |
| 35 | + setDisabledState: () => void 0, |
| 36 | + }; |
| 37 | + directive = new NgrxFormControlDirective<string>(elementRef, document, actionsSubject, [valueAccessor]); |
| 38 | + directive.ngrxFormControlState = INITIAL_STATE; |
| 39 | + directive.ngOnInit(); |
| 40 | + }); |
| 41 | + |
| 42 | + afterEach(() => { |
| 43 | + directive.ngOnDestroy(); |
| 44 | + }); |
| 45 | + |
| 46 | + it('should write the value when the state changes', () => { |
| 47 | + const newValue = 'new value'; |
| 48 | + const spy = spyOn(valueAccessor, 'writeValue'); |
| 49 | + directive.ngrxFormControlState = { ...INITIAL_STATE, value: newValue }; |
| 50 | + expect(spy).toHaveBeenCalledWith(newValue); |
| 51 | + }); |
| 52 | + |
| 53 | + it('should not write the value when the state value does not change', () => { |
| 54 | + const spy = spyOn(valueAccessor, 'writeValue'); |
| 55 | + directive.ngrxFormControlState = INITIAL_STATE; |
| 56 | + expect(spy).not.toHaveBeenCalled(); |
| 57 | + }); |
| 58 | + |
| 59 | + it('should not write the value when the state value is the same as the view value', () => { |
| 60 | + const newValue = 'new value'; |
| 61 | + onChange(newValue); |
| 62 | + const spy = spyOn(valueAccessor, 'writeValue'); |
| 63 | + directive.ngrxFormControlState = { ...INITIAL_STATE, value: newValue }; |
| 64 | + expect(spy).not.toHaveBeenCalled(); |
| 65 | + }); |
| 66 | + |
| 67 | + it('should dispatch an action if the view value changes', done => { |
| 68 | + const newValue = 'new value'; |
| 69 | + onChange(newValue); |
| 70 | + actions$.first().subscribe(a => { |
| 71 | + expect(a).toEqual(new SetValueAction(INITIAL_STATE.id, newValue)); |
| 72 | + done(); |
| 73 | + }); |
| 74 | + }); |
| 75 | + |
| 76 | + it('should not dispatch an action if the view value is the same as the state', done => { |
| 77 | + onChange(INITIAL_STATE.value); |
| 78 | + actionsSubject.complete(); |
| 79 | + actions$.count().subscribe(c => { |
| 80 | + expect(c).toEqual(0); |
| 81 | + done(); |
| 82 | + }); |
| 83 | + }); |
| 84 | + |
| 85 | + // TODO: throwing error on undefined state |
| 86 | + // TODO: value conversion |
| 87 | + // TODO: mark as touched |
| 88 | + // TODO: disabling and enabling |
| 89 | + // TODO: focus tracking |
| 90 | + // TODO: last keydown code tracking |
| 91 | +}); |
0 commit comments