Skip to content

Commit

Permalink
test: ajusta erros para funcionamento da esteira
Browse files Browse the repository at this point in the history
Ajusta erros nos testes para correto funcionamento da esteira.

Fixes DTHFUI-9761
  • Loading branch information
anabye authored and bruno-severino committed Aug 30, 2024
1 parent 75b75a7 commit 7942a33
Show file tree
Hide file tree
Showing 14 changed files with 138 additions and 66 deletions.
14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -125,13 +125,13 @@
"gulp": "^4.0.2",
"gulp-tap": "^2.0.0",
"husky": "^8.0.0",
"jasmine-core": "~5.1.1",
"jasmine-spec-reporter": "~7.0.0",
"karma": "~6.4.2",
"karma-chrome-launcher": "~3.2.0",
"karma-coverage": "~2.2.1",
"karma-jasmine": "~5.1.0",
"karma-jasmine-html-reporter": "^2.1.0",
"jasmine-core": "5.1.1",
"jasmine-spec-reporter": "7.0.0",
"karma": "6.4.2",
"karma-chrome-launcher": "3.2.0",
"karma-coverage": "2.2.1",
"karma-jasmine": "5.1.0",
"karma-jasmine-html-reporter": "2.1.0",
"lint-staged": "^15.1.0",
"mkdirp": "3.0.1",
"ng-packagr": "~18.0.0",
Expand Down
6 changes: 5 additions & 1 deletion projects/ui/karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
module.exports = function (config) {
config.set({
basePath: '',
files: [
{ pattern: './src/lib/util-test/util-setup.spec.ts', watched: false, type: 'js' },
{ pattern: './src/**/*.spec.ts', watched: false, type: 'js' }
],
frameworks: ['jasmine', '@angular-devkit/build-angular'],
plugins: [
require('karma-jasmine'),
Expand Down Expand Up @@ -52,7 +56,7 @@ module.exports = function (config) {
flags: ['--no-sandbox', '--headless', '--disable-gpu', '--disable-web-security', '--remote-debugging-port=9222']
}
},
singleRun: false,
singleRun: true,
restartOnFileChange: true,
browserNoActivityTimeout: 50000,
browserDisconnectTimeout: 50000
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,26 @@ describe('PoResizeObserverDirective', () => {

window.ResizeObserver = resizeRef;
}));

it('ngOnInit: should call chartWidthResize$.next when ResizeObserver callback is triggered', fakeAsync(() => {
const mockObserver = {
observe: jasmine.createSpy('observe'),
unobserve: jasmine.createSpy('unobserve')
};

spyOn(window as any, 'ResizeObserver').and.callFake(function (callback) {
this.observe = mockObserver.observe;
this.unobserve = mockObserver.unobserve;
this.callback = callback;
});

const nextSpy = spyOn(directive['chartWidthResize$'], 'next');

directive.ngOnInit();

directive['observer'].callback();
tick(20);

expect(nextSpy).toHaveBeenCalledWith({});
}));
});
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ describe('PoChartTooltipDirective', () => {
directive.createTooltip();
fixture.detectChanges();

spyOnProperty(directive.tooltipElement, 'offsetWidth').and.returnValue(154);
spyOnProperty(directive.tooltipElement, 'offsetHeight').and.returnValue(60);

event = document.createEvent('MouseEvents');
event.initEvent('scroll', false, true);
});
Expand Down Expand Up @@ -112,7 +115,7 @@ describe('PoChartTooltipDirective', () => {

it('calculateTooltipPosition: should return tooltipPosition', () => {
const tooltipEvent = { clientX: 300, clientY: 300 };
const expectedResult = { left: -77, top: 270 };
const expectedResult = { left: 223, top: 228 };
const result = directive.calculateTooltipPosition(tooltipEvent);

expect(result).toEqual(expectedResult);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,25 @@ describe('PoDisclaimerGroupComponent:', () => {
expect(component['focusOnRemoveTag']).toHaveBeenCalled();
});

it('focusOnNextTag: should select attribute index 0 when event is not enter', () => {
const tagsFake = document.createElement('div');
tagsFake.innerHTML = `
<div class="po-tag-remove"></div>
<div class="po-tag-remove"></div>
<div class="po-tag-remove"></div>
`;

document.body.appendChild(tagsFake);

spyOn(component as any, 'handleKeyboardNavigationTag');

component['focusOnNextTag'](null, 'click');

expect(component['handleKeyboardNavigationTag']).toHaveBeenCalledWith(0);

document.body.removeChild(tagsFake);
});

describe('Templates:', () => {
it(`should set tabindex to 0 if have a disclaimer with 'hideClose'.`, () => {
component.disclaimers = [{ value: 'po', hideClose: false }];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,6 @@ describe('PoDropdownComponent: ', () => {
await fixture.whenStable();

const disabledButton = nativeElement.querySelector('.po-dropdown-button-disabled');
console.log('Disabled button:', disabledButton);

expect(disabledButton).toBeTruthy();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { PoComboFilterService } from './po-combo-filter.service';
import { PoComboOption } from './interfaces/po-combo-option.interface';
import { PoCleanComponent } from '../po-clean/po-clean.component';
import { OverlayModule } from '@angular/cdk/overlay';
import { PoControlPositionService } from '../../../services/po-control-position/po-control-position.service';

const eventKeyBoard = document.createEvent('KeyboardEvent');
eventKeyBoard.initEvent('keyup', true, true);
Expand All @@ -29,19 +30,23 @@ eventClick.initEvent('click', false, true);
describe('PoComboComponent:', () => {
let component: PoComboComponent;
let fixture: ComponentFixture<PoComboComponent>;
let controlPositionMock: jasmine.SpyObj<PoControlPositionService>;
let nativeElement: any;

beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [PoLoadingModule, PoIconModule, OverlayModule],
declarations: [PoComboComponent, PoFieldContainerComponent, PoFieldContainerBottomComponent, PoCleanComponent],
providers: [HttpClient, HttpHandler]
providers: [HttpClient, HttpHandler, PoControlPositionService]
}).compileComponents();

fixture = TestBed.createComponent(PoComboComponent);
component = fixture.componentInstance;
component.label = 'Label de teste';
component.help = 'Help de teste';

controlPositionMock = jasmine.createSpyObj('PoControlPositionService', ['adjustPosition', 'setElements']);
component['adjustContainerPosition'] = () => controlPositionMock.adjustPosition('bottom');
});

it('should be created', () => {
Expand Down Expand Up @@ -1180,11 +1185,17 @@ describe('PoComboComponent:', () => {
it('adjustContainerPosition: should call `controlPosition.adjustPosition` with default position of container', () => {
const poComboContainerPositionDefault = 'bottom';

const spyAdjustPosition = spyOn(component['controlPosition'], 'adjustPosition');

component['adjustContainerPosition']();

expect(spyAdjustPosition).toHaveBeenCalledWith(poComboContainerPositionDefault);
expect(controlPositionMock.adjustPosition).toHaveBeenCalledWith(poComboContainerPositionDefault);
});

it('onScroll: should call `adjustContainerPosition` when triggered', () => {
const spy = spyOn(component as any, 'adjustContainerPosition');

component['onScroll']();

expect(spy).toHaveBeenCalled();
});
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { provideHttpClientTesting } from '@angular/common/http/testing';
import { NO_ERRORS_SCHEMA } from '@angular/core';
import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing';
import { ComponentFixture, discardPeriodicTasks, fakeAsync, flush, TestBed, tick } from '@angular/core/testing';
import { RouterTestingModule } from '@angular/router/testing';
import { Observable, of } from 'rxjs';
import { PoLookupFilter } from '../../../../components/po-field/po-lookup/interfaces/po-lookup-filter.interface';
import { PoLookupModalComponent } from '../../../../components/po-field/po-lookup/po-lookup-modal/po-lookup-modal.component';
import { PoModalModule } from '../../../../components/po-modal/po-modal.module';
import { PoComponentInjectorService } from '../../../../services/po-component-injector/po-component-injector.service';
import { changeBrowserInnerHeight } from '../../../../util-test/util-expect.spec';
import { PoDynamicModule } from '../../../po-dynamic/po-dynamic.module';
import { PoTableColumnSortType } from '../../../po-table/enums/po-table-column-sort-type.enum';
import { PoTableColumnSort } from '../../../po-table/interfaces/po-table-column-sort.interface';
Expand All @@ -28,7 +27,7 @@ describe('PoLookupModalComponent', () => {

const advancedFilters = [{ property: 'name', gridColumns: 6, gridSmColumns: 12, order: 1, required: true }];

beforeEach(waitForAsync(() => {
beforeEach(fakeAsync(() => {
TestBed.configureTestingModule({
declarations: [PoLookupModalComponent],
schemas: [NO_ERRORS_SCHEMA],
Expand All @@ -40,9 +39,7 @@ describe('PoLookupModalComponent', () => {
provideHttpClientTesting()
]
}).compileComponents();
}));

beforeEach(() => {
fixture = TestBed.createComponent(PoLookupModalComponent);
component = fixture.componentInstance;
component.infiniteScroll = false;
Expand All @@ -57,7 +54,7 @@ describe('PoLookupModalComponent', () => {
}),
getObjectByValue: () => of({ items: [{ value: 123, label: 'teste' }] })
};
});
}));

afterEach(() => {
component.poModal.close();
Expand Down Expand Up @@ -164,11 +161,16 @@ describe('PoLookupModalComponent', () => {
});

describe('AdvancedSearch: ', () => {
beforeEach(waitForAsync(() => {
beforeEach(fakeAsync(() => {
component.advancedFilters = advancedFilters;
fixture.detectChanges();
component.onAdvancedFilter();

tick(10);
fixture.detectChanges();

flush();
discardPeriodicTasks();
}));

afterEach(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,7 @@ describe('PoLookupComponent:', () => {
expect(spyCallOnChange).toHaveBeenCalled();
});

it('updateVisibleItems: should concat `visibleDisclaimers` with items of disclaimers', () => {
it('updateVisibleItems: should concat `visibleDisclaimers` with items of disclaimers', fakeAsync(() => {
fixture.detectChanges();
const itemsDisclaimers = [
{
Expand All @@ -588,58 +588,57 @@ describe('PoLookupComponent:', () => {
const spyDebounceResize = spyOn(component, 'debounceResize');

component.disclaimers = itemsDisclaimers;

component.updateVisibleItems();

tick();

expect(component.visibleDisclaimers.length).toEqual(2);
expect(spyDebounceResize).toHaveBeenCalled();
});
}));

it(`updateVisibleItems: shouldn't concat 'visibleDisclaimers' with items of disclaimers`, () => {
it(`updateVisibleItems: shouldn't concat 'visibleDisclaimers' with items of disclaimers`, fakeAsync(() => {
fixture.detectChanges();
const spyDebounceResize = spyOn(component, 'debounceResize');

component.disclaimers = [];

component.visibleDisclaimers = [
{
value: 'test',
label: 'test'
}
];
component.visibleDisclaimers = [{ value: 'test', label: 'test' }];
component.updateVisibleItems();

tick();

expect(component.disclaimers.length).toEqual(0);
expect(component.visibleDisclaimers.length).toEqual(1);
expect(spyDebounceResize).toHaveBeenCalled();
});
}));

it('updateVisibleItems: should set true in `isCalculateVisibleItems` if `offsetWidth` is false', () => {
it('updateVisibleItems: should set true in `isCalculateVisibleItems` if `offsetWidth` is false', fakeAsync(() => {
fixture.detectChanges();
const spyDebounceResize = spyOn(component, 'debounceResize');
component.disclaimers = [];

spyOnProperty(component.inputEl.nativeElement, 'offsetWidth').and.returnValue(false);

component.updateVisibleItems();

tick();

expect(component['isCalculateVisibleItems']).toBeTruthy();
expect(spyDebounceResize).toHaveBeenCalled();
});
}));

it('updateVisibleItems: should set false in `isCalculateVisibleItems` if `offsetWidth` is true', () => {
it('updateVisibleItems: should set false in `isCalculateVisibleItems` if `offsetWidth` is true', fakeAsync(() => {
fixture.detectChanges();
component['isCalculateVisibleItems'] = false;
const spyDebounceResize = spyOn(component, 'debounceResize');
component.disclaimers = [];

spyOnProperty(component.inputEl.nativeElement, 'offsetWidth').and.returnValue(true);

component.updateVisibleItems();

tick();

expect(component['isCalculateVisibleItems']).toBeFalsy();
expect(spyDebounceResize).toHaveBeenCalled();
});
}));

it(`debounceResize: should call 'calculateVisibleItems'`, fakeAsync(() => {
component.autoHeight = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,22 @@ import { OverlayModule } from '@angular/cdk/overlay';
import { HttpClient, HttpHandler, provideHttpClient, withInterceptorsFromDi } from '@angular/common/http';
import { HttpTestingController, provideHttpClientTesting } from '@angular/common/http/testing';
import { ComponentFixture, fakeAsync, flush, TestBed, tick } from '@angular/core/testing';
import { Observable, catchError, of, tap, throwError } from 'rxjs';
import { Observable, of, throwError } from 'rxjs';

import * as UtilsFunction from '../../../utils/util';

import { PoTagComponent } from '../../po-tag/po-tag.component';
import { Renderer2 } from '@angular/core';
import { PoKeyCodeEnum } from '../../../enums/po-key-code.enum';
import { PoControlPositionService } from '../../../services/po-control-position/po-control-position.service';
import { PoTagComponent } from '../../po-tag/po-tag.component';
import { PoFieldContainerComponent } from '../po-field-container/po-field-container.component';
import { PoMultiselectBaseComponent } from '../po-multiselect/po-multiselect-base.component';
import { PoFieldContainerBottomComponent } from './../po-field-container/po-field-container-bottom/po-field-container-bottom.component';
import { PoMultiselectDropdownComponent } from './po-multiselect-dropdown/po-multiselect-dropdown.component';
import { PoMultiselectFilter } from './po-multiselect-filter.interface';
import { PoMultiselectFilterService } from './po-multiselect-filter.service';
import { PoMultiselectComponent } from './po-multiselect.component';
import { PoMultiselectOption } from './po-multiselect-option.interface';
import { PoMultiselectComponent } from './po-multiselect.component';

const poMultiselectFilterServiceStub: PoMultiselectFilter = {
getFilteredData: function (params: { property: string; value: string }): Observable<Array<PoMultiselectOption>> {
Expand All @@ -31,6 +32,7 @@ describe('PoMultiselectComponent:', () => {
let fnAdjustContainerPosition;
let component: PoMultiselectComponent;
let fixture: ComponentFixture<PoMultiselectComponent>;
let controlPositionMock: jasmine.SpyObj<PoControlPositionService>;

let multiSelectService: PoMultiselectFilterService;
let httpMock: HttpTestingController;
Expand Down Expand Up @@ -62,6 +64,7 @@ describe('PoMultiselectComponent:', () => {
HttpHandler,
Renderer2,
PoMultiselectFilterService,
PoControlPositionService,
provideHttpClient(withInterceptorsFromDi()),
provideHttpClientTesting()
]
Expand All @@ -71,7 +74,8 @@ describe('PoMultiselectComponent:', () => {
component = fixture.componentInstance;
renderer = TestBed.inject(Renderer2);
fnAdjustContainerPosition = component['adjustContainerPosition'];
component['adjustContainerPosition'] = () => {};
controlPositionMock = jasmine.createSpyObj('PoControlPositionService', ['adjustPosition', 'setElements']);
component['adjustContainerPosition'] = () => controlPositionMock.adjustPosition('bottom');

component.options = [{ label: 'label', value: 1 }];
component.autoHeight = true;
Expand Down Expand Up @@ -157,11 +161,13 @@ describe('PoMultiselectComponent:', () => {
expect(component.initialized).toBeTruthy();
});

it('shouldn`t set focus on input', () => {
it('shouldn`t set focus on input', done => {
component.initialized = false;
component.autoFocus = false;
spyOn(component.inputElement.nativeElement, 'focus');

done();

component.ngAfterViewInit();
expect(component.inputElement.nativeElement.focus).not.toHaveBeenCalled();

Expand Down
Loading

0 comments on commit 7942a33

Please sign in to comment.