Skip to content

Commit

Permalink
feat(tree-view): fixes and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
guilnorth committed Sep 22, 2023
1 parent a9538f0 commit f88f9c3
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ describe('PoTreeViewBaseComponent:', () => {
});

describe('Methods: ', () => {
beforeEach(() => {
component.singleSelect = false;
});

it('emitExpanded: should call collapsed.emit with tree view item if treeViewItem.expanded is false', () => {
const treeViewItem = { label: 'Nível 01', value: 1, expanded: false };

Expand Down Expand Up @@ -112,6 +116,21 @@ describe('PoTreeViewBaseComponent:', () => {
expect(spyUpdateItemsOnSelect).toHaveBeenCalledWith(treeViewItem);
});

it('emitSelected: should emit without treeViewItem.subItems if is `singleSelect`', () => {
const treeViewItem = { label: 'Nível 01', value: 1, selected: true, subItems: [{ label: 'Nivel 02', value: 2 }] };
const expected = { label: 'Nível 01', value: 1, selected: true };

const spyUpdateItemsOnSelect = spyOn(component, <any>'updateItemsOnSelect');
const spySelectedEmit = spyOn(component['selected'], 'emit');

component.singleSelect = true;
component['emitSelected'](treeViewItem);

expect(component.singleSelect).toEqual(true);
expect(spySelectedEmit).toHaveBeenCalledWith(expected);
expect(spyUpdateItemsOnSelect).toHaveBeenCalledWith(expected);
});

it('getItemsByMaxLevel: should return and not call addItem if level is 4', () => {
const items = [];

Expand Down Expand Up @@ -228,6 +247,23 @@ describe('PoTreeViewBaseComponent:', () => {
expect(spyExpandParentItem).not.toHaveBeenCalledWith(childItem, parentItem);
});

it('addItem: shouldn`t call selectItemBySubItems if is `singleSelect`', () => {
const childItem = { label: 'Nível 02', value: 2 };
const parentItem = { label: 'Nível 01', value: 1 };
const items = [];

const expectedValue = [parentItem];

const spySelectItemBySubItems = spyOn(component, <any>'selectItemBySubItems');

component.singleSelect = true;
component['addItem'](items, childItem, parentItem);

expect(items.length).toBe(1);
expect(items).toEqual(expectedValue);
expect(spySelectItemBySubItems).not.toHaveBeenCalled();
});

it('addItem: should add parentItem in items and call expandParentItem, addChildItemInParent and selectItemBySubItems', () => {
const childItem = { label: 'Nível 02', value: 2 };
const parentItem = { label: 'Nível 01', value: 1 };
Expand Down Expand Up @@ -338,7 +374,6 @@ describe('PoTreeViewBaseComponent:', () => {
};
const items = [selectedItem];

component.singleSelect = false;
component.items = items;

const spyGetItemsWithParentSelected = spyOn(component, <any>'getItemsWithParentSelected').and.returnValue(items);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,13 @@ export class PoTreeViewBaseComponent {

this.selectedValue = treeViewItem.value;

this.updateItemsOnSelect(treeViewItem);
// Não emitir subItems quando for singleSelect
const { subItems, ...rest } = treeViewItem;
const treeViewToEmit = this.singleSelect ? { ...rest } : treeViewItem;

this[event].emit({ ...treeViewItem });
this.updateItemsOnSelect(treeViewToEmit);

this[event].emit({ ...treeViewToEmit });
}

private addChildItemInParent(childItem: PoTreeViewItem, parentItem: PoTreeViewItem) {
Expand All @@ -173,8 +177,10 @@ export class PoTreeViewBaseComponent {
if (isNewItem) {
this.expandParentItem(childItem, parentItem);
}

this.addChildItemInParent(childItem, parentItem);
this.selectItemBySubItems(parentItem);

if (!this.singleSelect) this.selectItemBySubItems(parentItem);

Check failure on line 183 in projects/ui/src/lib/components/po-tree-view/po-tree-view-base.component.ts

View workflow job for this annotation

GitHub Actions / lint

Expected { after 'if' condition

items.push(parentItem);
} else {
Expand Down Expand Up @@ -240,7 +246,7 @@ export class PoTreeViewBaseComponent {

if (Array.isArray(subItems)) {
// caso um item pai iniciar selecionado, deve selecionar os filhos.
if (currentItem.selected && !this.singleSelect) {
if (currentItem.selected) {
this.selectAllItems(subItems, currentItem.selected);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,12 @@
<po-info p-label="Event" [p-value]="event"> </po-info>
</div>

<div class="po-row">
<po-switch class="po-md-2" name="selectable" [(ngModel)]="selectable" p-label="Selectable"> </po-switch>
</div>

<po-divider p-label="Po Tree View Config"></po-divider>

<div class="po-row">
<po-input class="po-md-4" name="level" [(ngModel)]="maxLevel" p-label="Nível máximo"> </po-input>
<po-switch
class="po-md-4"
name="singleSelect"
[(ngModel)]="singleSelect"
p-label="Selecionar somente um item na árvore"
>
</po-switch>
<po-input class="po-md-4" name="level" [(ngModel)]="maxLevel" p-label="Max Level"> </po-input>
<po-switch class="po-md-4" name="selectable" [(ngModel)]="selectable" p-label="Selectable"> </po-switch>
<po-switch class="po-md-4" name="singleSelect" [(ngModel)]="singleSelect" p-label="Single Select"> </po-switch>
</div>

<po-divider p-label="Po Tree View Item"></po-divider>
Expand All @@ -54,7 +45,7 @@

<div class="po-row">
<po-checkbox-group
class="po-md-6"
class="po-md-9"
name="itemProperties"
[(ngModel)]="itemProperties"
p-columns="3"
Expand Down

0 comments on commit f88f9c3

Please sign in to comment.