|
| 1 | +import { expectPropertiesValues } from '../../../util-test/util-expect.spec'; |
| 2 | + |
| 3 | +import { PoCheckboxBaseComponent } from './po-checkbox-base.component'; |
| 4 | + |
| 5 | +class PoCheckboxComponent extends PoCheckboxBaseComponent { |
| 6 | + protected changeModelValue(value: boolean | null) { } |
| 7 | +} |
| 8 | + |
| 9 | +describe('PoCheckboxBaseComponent:', () => { |
| 10 | + let component: PoCheckboxBaseComponent; |
| 11 | + |
| 12 | + beforeEach((() => { |
| 13 | + component = new PoCheckboxComponent(); |
| 14 | + component.propagateChange = (value: any) => { }; |
| 15 | + })); |
| 16 | + |
| 17 | + it('should be created', () => { |
| 18 | + component.registerOnChange(() => { }); |
| 19 | + component.registerOnTouched(() => { }); |
| 20 | + expect(component instanceof PoCheckboxBaseComponent).toBeTruthy(); |
| 21 | + }); |
| 22 | + |
| 23 | + describe('Properties:', () => { |
| 24 | + |
| 25 | + it('disabled: should update with true value', () => { |
| 26 | + const booleanValidTrueValues = [ true, 'true', 1, '' ]; |
| 27 | + |
| 28 | + expectPropertiesValues(component, 'disabled', booleanValidTrueValues, true); |
| 29 | + }); |
| 30 | + |
| 31 | + it('disabled: should update with false value', () => { |
| 32 | + const booleanInvalidValues = [ undefined, null, 2, 'string', 0, NaN, false ]; |
| 33 | + |
| 34 | + expectPropertiesValues(component, 'disabled', booleanInvalidValues, false); |
| 35 | + }); |
| 36 | + |
| 37 | + }); |
| 38 | + |
| 39 | + describe('Methods:', () => { |
| 40 | + |
| 41 | + it('changeValue: should call `propagateChange` if it is defined and call `change.emit` with `checkboxValue`', () => { |
| 42 | + component.checkboxValue = true; |
| 43 | + |
| 44 | + component.propagateChange = () => {}; |
| 45 | + |
| 46 | + spyOn(component, 'propagateChange'); |
| 47 | + spyOn(component.change, 'emit'); |
| 48 | + |
| 49 | + component.changeValue(); |
| 50 | + |
| 51 | + expect(component.propagateChange).toHaveBeenCalledWith(component.checkboxValue); |
| 52 | + expect(component.change.emit).toHaveBeenCalledWith(component.checkboxValue); |
| 53 | + }); |
| 54 | + |
| 55 | + it('changeValue: should call only `change.emit` with `checkboxValue` if propagateChange is `null`', () => { |
| 56 | + component.checkboxValue = true; |
| 57 | + component.propagateChange = null; |
| 58 | + |
| 59 | + spyOn(component.change, 'emit'); |
| 60 | + |
| 61 | + component.changeValue(); |
| 62 | + |
| 63 | + expect(component.change.emit).toHaveBeenCalledWith(component.checkboxValue); |
| 64 | + }); |
| 65 | + |
| 66 | + it('checkOption: should call `changeModelValue` and `changeValue` if `disabled` is false.', () => { |
| 67 | + component.disabled = false; |
| 68 | + const spyOnChangeValue = spyOn(component, 'changeValue'); |
| 69 | + const spyOnChangeModelValue = spyOn(component, <any>'changeModelValue'); |
| 70 | + |
| 71 | + component.checkOption(true); |
| 72 | + |
| 73 | + expect(spyOnChangeValue).toHaveBeenCalled(); |
| 74 | + expect(spyOnChangeModelValue).toHaveBeenCalled(); |
| 75 | + }); |
| 76 | + |
| 77 | + it('checkOption: shouldn`t call `changeModelValue` and `changeValue` if `disabled` is true.', () => { |
| 78 | + component.disabled = true; |
| 79 | + const spyOnChangeValue = spyOn(component, 'changeValue'); |
| 80 | + const spyOnChangeModelValue = spyOn(component, <any>'changeModelValue'); |
| 81 | + |
| 82 | + component.checkOption(true); |
| 83 | + |
| 84 | + expect(spyOnChangeValue).not.toHaveBeenCalled(); |
| 85 | + expect(spyOnChangeModelValue).not.toHaveBeenCalled(); |
| 86 | + }); |
| 87 | + |
| 88 | + it('registerOnChange: should set `propagateChange` with value of `fnParam`', () => { |
| 89 | + const fnParam = () => {}; |
| 90 | + |
| 91 | + component.registerOnChange(fnParam); |
| 92 | + |
| 93 | + expect(component['propagateChange']).toBe(fnParam); |
| 94 | + }); |
| 95 | + |
| 96 | + it('writeValue: should call `changeModelValue` if value is different from `checkboxValue`', () => { |
| 97 | + const valueParam = true; |
| 98 | + component.checkboxValue = undefined; |
| 99 | + |
| 100 | + spyOn(component, <any>'changeModelValue'); |
| 101 | + |
| 102 | + component.writeValue(valueParam); |
| 103 | + |
| 104 | + expect(component['changeModelValue']).toHaveBeenCalledWith(valueParam); |
| 105 | + }); |
| 106 | + |
| 107 | + it('writeValue: shouldn`t call `changeModelValue` if value is same as `checkboxValue`', () => { |
| 108 | + const valueParam = undefined; |
| 109 | + component.checkboxValue = undefined; |
| 110 | + |
| 111 | + spyOn(component, <any>'changeModelValue'); |
| 112 | + |
| 113 | + component.writeValue(valueParam); |
| 114 | + |
| 115 | + expect(component['changeModelValue']).not.toHaveBeenCalled(); |
| 116 | + }); |
| 117 | + |
| 118 | + }); |
| 119 | + |
| 120 | +}); |
0 commit comments