Skip to content

Commit 89b6e44

Browse files
Samir Hassan Ayoubalinelariguet
authored andcommitted
test(rich-text): adiciona testes unitários no componente
o componente po-rich-text estava sem testes unitários, portanto foi adicionado testes unitarios no componente, aplicado boas praticas onde julgado necessário e resolvido problema sample labs do componente que não passava no AOT. DTHFUI-1174
1 parent ca9b4b4 commit 89b6e44

13 files changed

+691
-56
lines changed

projects/ui/karma.conf.js

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -46,30 +46,6 @@ module.exports = function (config) {
4646
},
4747
'src/lib/components/po-field/po-input/po-input.component.ts': {
4848
branches: 50
49-
},
50-
'src/lib/components/po-field/po-rich-text/po-rich-text.component.ts': {
51-
statements: 0,
52-
lines: 0,
53-
branches: 0,
54-
functions: 0
55-
},
56-
'src/lib/components/po-field/po-rich-text/po-rich-text-base.component.ts': {
57-
statements: 0,
58-
lines: 0,
59-
branches: 0,
60-
functions: 0
61-
},
62-
'src/lib/components/po-field/po-rich-text/po-rich-text-body/po-rich-text-body.component.ts': {
63-
statements: 0,
64-
lines: 0,
65-
branches: 0,
66-
functions: 0
67-
},
68-
'src/lib/components/po-field/po-rich-text/po-rich-text-toolbar/po-rich-text-toolbar.component.ts': {
69-
statements: 0,
70-
lines: 0,
71-
branches: 0,
72-
functions: 0
7349
}
7450
}
7551
}
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
import { FormControl } from '@angular/forms';
2+
3+
import * as ValidatorsFunctions from '../validators';
4+
import { expectPropertiesValues } from '../../../util-test/util-expect.spec';
5+
6+
import { PoRichTextBaseComponent } from './po-rich-text-base.component';
7+
8+
class PoRichTextComponent extends PoRichTextBaseComponent { }
9+
10+
describe('PoRichTextBaseComponent:', () => {
11+
let component: PoRichTextComponent;
12+
13+
beforeEach(() => {
14+
component = new PoRichTextComponent();
15+
});
16+
17+
it('should be created', () => {
18+
expect(component instanceof PoRichTextBaseComponent).toBeTruthy();
19+
});
20+
21+
describe('Properties:', () => {
22+
const booleanInvalidValues = [undefined, null, 2, 'string'];
23+
const booleanValidTrueValues = [true, 'true', 1, ''];
24+
25+
it('p-height: should update property with valid values', () => {
26+
const validValues = [0, 5, 200, 1000];
27+
28+
expectPropertiesValues(component, 'height', validValues, validValues);
29+
});
30+
31+
it('p-placeholder: should update property with valid values', () => {
32+
const validValues = ['value 1', 'value 2'];
33+
34+
expectPropertiesValues(component, 'placeholder', validValues, validValues);
35+
});
36+
37+
it('p-placeholder: property should be an empty string if value is undefined or null', () => {
38+
const invalidValues = [undefined, null];
39+
40+
expectPropertiesValues(component, 'placeholder', invalidValues, '');
41+
});
42+
43+
it('p-readonly: should update property with valid values.', () => {
44+
expectPropertiesValues(component, 'readonly', booleanValidTrueValues, true);
45+
});
46+
47+
it('p-readonly: should update property with invalid values.', () => {
48+
expectPropertiesValues(component, 'readonly', booleanInvalidValues, false);
49+
});
50+
51+
it('p-required: should update property with valid values, invalid values and call `validateModel`', () => {
52+
spyOn(component, <any>'validateModel');
53+
54+
expectPropertiesValues(component, 'required', booleanValidTrueValues, true);
55+
expectPropertiesValues(component, 'required', booleanInvalidValues, false);
56+
57+
expect(component['validateModel']).toHaveBeenCalledWith(component.value);
58+
});
59+
});
60+
61+
describe('Methods:', () => {
62+
63+
it('registerOnChange: should register function onChangeModel function', () => {
64+
const registerOnChangeModelFn = () => { };
65+
66+
component.registerOnChange(registerOnChangeModelFn);
67+
expect(component.onChangeModel).toBe(registerOnChangeModelFn);
68+
});
69+
70+
it('registerOnTouched: should register function onTouched function', () => {
71+
const registeronTouchedFn = () => { };
72+
73+
component.registerOnTouched(registeronTouchedFn);
74+
expect(component['onTouched']).toBe(registeronTouchedFn);
75+
});
76+
77+
it('registerOnValidatorChange: should register function validatorChange function', () => {
78+
const registervalidatorChangeFn = () => { };
79+
80+
component.registerOnValidatorChange(registervalidatorChangeFn);
81+
expect(component['validatorChange']).toBe(registervalidatorChangeFn);
82+
});
83+
84+
it('validate: should return required obj when `requiredFailed` is true', () => {
85+
const validObj = {
86+
required: {
87+
valid: false,
88+
}
89+
};
90+
91+
spyOn(ValidatorsFunctions, 'requiredFailed').and.returnValue(true);
92+
93+
expect(component.validate(new FormControl([]))).toEqual(validObj);
94+
expect(ValidatorsFunctions.requiredFailed).toHaveBeenCalled();
95+
});
96+
97+
it('validate: should return undefined if `requiredFailed` is false', () => {
98+
spyOn(ValidatorsFunctions, 'requiredFailed').and.returnValue(false);
99+
100+
expect(component.validate(new FormControl(null))).toBeUndefined();
101+
expect(ValidatorsFunctions.requiredFailed).toHaveBeenCalled();
102+
});
103+
104+
it('writeValue: should set value with the received param', () => {
105+
component.value = 'value A';
106+
107+
component.writeValue('value B');
108+
109+
expect(component.value).toBe('value B');
110+
});
111+
112+
it('updateModel: should call onChangeModel method if onChangeModel isn`t false', () => {
113+
component.onChangeModel = true;
114+
115+
spyOn(component, <any>'onChangeModel');
116+
117+
component['updateModel']('updated value');
118+
119+
expect(component.onChangeModel).toHaveBeenCalledWith('updated value');
120+
});
121+
122+
it('updateModel: onChangeModel shouldn`t be registered as a function if onChangeModel is undefined', () => {
123+
component.onChangeModel = undefined;
124+
125+
component['updateModel']('updated value');
126+
127+
expect(component.onChangeModel).toBeUndefined();
128+
});
129+
130+
it('validateModel: should call validatorChange method if validatorChange isn`t false', () => {
131+
component['validatorChange'] = true;
132+
133+
spyOn(component, <any>'validatorChange');
134+
135+
component['validateModel']('updated value');
136+
137+
expect(component['validatorChange']).toHaveBeenCalledWith('updated value');
138+
});
139+
140+
it('validateModel: validatorChange shouldn`t be registered as a function if validatorChange is undefined', () => {
141+
component['validatorChange'] = undefined;
142+
143+
component['validateModel']('updated value');
144+
145+
expect(component['validatorChange']).toBeUndefined();
146+
});
147+
});
148+
});

projects/ui/src/lib/components/po-field/po-rich-text/po-rich-text-base.component.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@ export abstract class PoRichTextBaseComponent implements ControlValueAccessor, V
1616
private _height?: number;
1717
private _placeholder: string;
1818
private _readonly: boolean;
19+
private _required: boolean;
1920

20-
onChangeModel: any = null;
2121
invalid: boolean = false;
22+
onChangeModel: any = null;
2223
value: string;
2324

2425
// tslint:disable-next-line
@@ -120,19 +121,24 @@ export abstract class PoRichTextBaseComponent implements ControlValueAccessor, V
120121
}
121122

122123
/**
124+
* @optional
125+
*
123126
* @description
124127
*
125128
* Indica que o campo será obrigatório.
126129
*
127130
* @default `false`
128131
*/
129-
required?: boolean = false;
130-
@Input('p-required') set setRequired(required: string) {
131-
this.required = convertToBoolean(required);
132+
@Input('p-required') set required(value: boolean) {
133+
this._required = convertToBoolean(value);
132134

133135
this.validateModel(this.value);
134136
}
135137

138+
get required() {
139+
return this._required;
140+
}
141+
136142
// Função implementada do ControlValueAccessor
137143
// Usada para interceptar as mudanças e não atualizar automaticamente o Model
138144
registerOnChange(func: any): void {
Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
import { ComponentFixture, TestBed, fakeAsync, tick } from '@angular/core/testing';
2+
3+
import { configureTestSuite } from './../../../../util-test/util-expect.spec';
4+
5+
import { PoRichTextBodyComponent } from './po-rich-text-body.component';
6+
7+
describe('PoRichTextBodyComponent:', () => {
8+
let component: PoRichTextBodyComponent;
9+
let fixture: ComponentFixture<PoRichTextBodyComponent>;
10+
let nativeElement: any;
11+
12+
configureTestSuite(() => {
13+
TestBed.configureTestingModule({
14+
declarations: [
15+
PoRichTextBodyComponent
16+
]
17+
});
18+
});
19+
20+
beforeEach(() => {
21+
fixture = TestBed.createComponent(PoRichTextBodyComponent);
22+
component = fixture.componentInstance;
23+
fixture.detectChanges();
24+
nativeElement = fixture.debugElement.nativeElement;
25+
});
26+
27+
it('should be created', () => {
28+
expect(component).toBeTruthy();
29+
});
30+
31+
describe('Methods: ', () => {
32+
33+
it('onInit: should update `bodyElement`', () => {
34+
const expectedValue = 'on';
35+
component.ngOnInit();
36+
37+
expect(component.bodyElement.nativeElement.designMode).toEqual(expectedValue);
38+
});
39+
40+
it('onInit: should call `updateValueWithModelValue`', fakeAsync(() => {
41+
spyOn(component, <any>'updateValueWithModelValue');
42+
43+
component.ngOnInit();
44+
tick(50);
45+
46+
expect(component['updateValueWithModelValue']).toHaveBeenCalled();
47+
}));
48+
49+
it('executeCommand: should call `focus`', () => {
50+
const spyFocus = spyOn(component.bodyElement.nativeElement, <any> 'focus');
51+
const fakeValue = 'p';
52+
53+
component.executeCommand(fakeValue);
54+
55+
expect(spyFocus).toHaveBeenCalled();
56+
});
57+
58+
it('executeCommand: should call `execCommand`', () => {
59+
const spyExecCommand = spyOn(component.bodyElement.nativeElement, <any> 'focus');
60+
const fakeValue = 'p';
61+
62+
component.executeCommand(fakeValue);
63+
64+
expect(spyExecCommand).toHaveBeenCalled();
65+
});
66+
67+
it('executeCommand: should call `updateModel`', () => {
68+
const fakeValue = 'p';
69+
spyOn(component, <any>'updateModel');
70+
71+
component.executeCommand(fakeValue);
72+
73+
expect(component['updateModel']).toHaveBeenCalled();
74+
});
75+
76+
it('executeCommand: should call `value.emit` with `modelValue`', () => {
77+
component.modelValue = 'teste';
78+
const fakeValue = 'p';
79+
80+
spyOn(component.value, 'emit');
81+
component.executeCommand(fakeValue);
82+
83+
expect(component.value.emit).toHaveBeenCalledWith(component.modelValue);
84+
});
85+
86+
it('onClick: should call `emitSelectionCommands`', () => {
87+
spyOn(component, <any>'emitSelectionCommands');
88+
component.onClick();
89+
90+
expect(component['emitSelectionCommands']).toHaveBeenCalled();
91+
});
92+
93+
it('onKeyUp: should call `updateModel`', () => {
94+
const element = document.createElement('div');
95+
element.classList.add('teste');
96+
component.bodyElement.nativeElement.appendChild(element);
97+
component.onKeyUp();
98+
expect(nativeElement.querySelector('.teste')).toBeFalsy();
99+
});
100+
101+
it('onKeyUp: should call `updateModel`', () => {
102+
spyOn(component, <any>'updateModel');
103+
component.onKeyUp();
104+
105+
expect(component['updateModel']).toHaveBeenCalled();
106+
});
107+
108+
it('onKeyUp: should call `emitSelectionCommands`', () => {
109+
spyOn(component, <any>'emitSelectionCommands');
110+
component.onKeyUp();
111+
112+
expect(component['emitSelectionCommands']).toHaveBeenCalled();
113+
});
114+
115+
it('update: should call `updateModel`', fakeAsync(() => {
116+
spyOn(component, <any>'updateModel');
117+
118+
component.update();
119+
tick(50);
120+
121+
expect(component['updateModel']).toHaveBeenCalled();
122+
}));
123+
124+
it('update: should call `onKeyUp`', fakeAsync(() => {
125+
spyOn(component, <any>'onKeyUp');
126+
127+
component.update();
128+
tick(50);
129+
130+
expect(component['onKeyUp']).toHaveBeenCalled();
131+
}));
132+
133+
it('emitSelectionCommands: should call `commands.emit`', () => {
134+
spyOn(component.commands, 'emit');
135+
component['emitSelectionCommands']();
136+
137+
expect(component.commands.emit).toHaveBeenCalled();
138+
});
139+
140+
it('updateModel: should update `modelValue`', () => {
141+
component.bodyElement.nativeElement.innerHTML = 'teste';
142+
component['updateModel']();
143+
fixture.detectChanges();
144+
expect(component.modelValue).toContain('teste');
145+
});
146+
147+
it('updateModel: should call `value.emit` with `modelValue`', () => {
148+
component.modelValue = 'teste';
149+
150+
spyOn(component.value, 'emit');
151+
component['updateModel']();
152+
153+
expect(component.value.emit).toHaveBeenCalledWith(component.modelValue);
154+
});
155+
156+
it('updateValueWithModelValue: should call `bodyElement.nativeElement.insertAdjacentHTML`', () => {
157+
component.modelValue = 'teste';
158+
159+
spyOn(component.bodyElement.nativeElement, 'insertAdjacentHTML');
160+
component['updateValueWithModelValue']();
161+
162+
expect(component.bodyElement.nativeElement.insertAdjacentHTML).toHaveBeenCalledWith('afterbegin', component.modelValue);
163+
});
164+
165+
});
166+
167+
describe('Templates: ', () => {
168+
169+
it('should contain `po-rich-text-body`', () => {
170+
171+
expect(nativeElement.querySelector('.po-rich-text-body')).toBeTruthy();
172+
});
173+
174+
});
175+
176+
});

0 commit comments

Comments
 (0)