|
| 1 | +import { TestBed, ComponentFixture, fakeAsync, tick } from "@angular/core/testing"; |
| 2 | +import { Component, DebugElement } from "@angular/core"; |
| 3 | +import { By } from "@angular/platform-browser"; |
| 4 | + |
| 5 | +import { TreeViewComponent } from "./treeview.component"; |
| 6 | +import { TreeNodeComponent } from "./tree-node.component"; |
| 7 | + |
| 8 | +@Component({ |
| 9 | + template: ` |
| 10 | + <cds-tree-view |
| 11 | + label="Tree view" |
| 12 | + style="width: 18rem; display: block;" |
| 13 | + [tree]="tree" |
| 14 | + (selected)="onSelect()"> |
| 15 | + </cds-tree-view> |
| 16 | + ` |
| 17 | +}) |
| 18 | +class TreeviewTestComponent { |
| 19 | + onSelect() {} |
| 20 | + |
| 21 | + tree = [ |
| 22 | + { |
| 23 | + id: "1", |
| 24 | + value: "Artificial intelligence", |
| 25 | + label: "Artificial intelligence" |
| 26 | + }, |
| 27 | + { |
| 28 | + id: "2", |
| 29 | + value: "Blockchain", |
| 30 | + label: "Blockchain" |
| 31 | + }, |
| 32 | + { |
| 33 | + id: "bus-auto", |
| 34 | + value: "Business automation", |
| 35 | + label: "Business automation", |
| 36 | + expanded: true, |
| 37 | + disabled: true, |
| 38 | + children: [ |
| 39 | + { |
| 40 | + id: "bus-auto-process", |
| 41 | + value: "Business process automation", |
| 42 | + label: "Business process automation" |
| 43 | + }, |
| 44 | + { |
| 45 | + id: "bus-auto-process-map", |
| 46 | + value: "Business process mapping", |
| 47 | + label: "Business process mapping" |
| 48 | + } |
| 49 | + ] |
| 50 | + } |
| 51 | + ]; |
| 52 | +} |
| 53 | + |
| 54 | + |
| 55 | +describe("Treeview", () => { |
| 56 | + let component: TreeviewTestComponent; |
| 57 | + let fixture: ComponentFixture<TreeviewTestComponent>; |
| 58 | + let nodes: DebugElement[]; |
| 59 | + let tree: DebugElement; |
| 60 | + |
| 61 | + beforeEach(async () => { |
| 62 | + TestBed.configureTestingModule({ |
| 63 | + declarations: [ |
| 64 | + TreeviewTestComponent, |
| 65 | + TreeNodeComponent, |
| 66 | + TreeViewComponent |
| 67 | + ] |
| 68 | + }).compileComponents(); |
| 69 | + |
| 70 | + fixture = TestBed.createComponent(TreeviewTestComponent); |
| 71 | + component = fixture.componentInstance; |
| 72 | + fixture.detectChanges(); |
| 73 | + tree = fixture.debugElement.query(By.directive(TreeViewComponent)); |
| 74 | + nodes = fixture.debugElement.queryAll(By.directive(TreeNodeComponent)); |
| 75 | + }); |
| 76 | + |
| 77 | + it("should create a tree view", () => { |
| 78 | + expect(component).toBeTruthy(); |
| 79 | + }); |
| 80 | + |
| 81 | + it("should indicate if the node has children", () => { |
| 82 | + const parentNode = nodes[2].componentInstance; |
| 83 | + expect(parentNode.expanded).toBeTruthy(); |
| 84 | + expect(nodes[2].nativeElement.querySelector(".cds--tree-node__children")).toBeTruthy(); |
| 85 | + }); |
| 86 | + |
| 87 | + it("should disable the children nodes when parent is disabled", () => { |
| 88 | + const parentNode = nodes[2].componentInstance; |
| 89 | + expect(parentNode.disabled).toBeTruthy(); |
| 90 | + const childNode = nodes[3].componentInstance; |
| 91 | + expect(childNode.disabled).toBeTruthy(); |
| 92 | + expect( |
| 93 | + nodes[3].nativeElement.querySelector("#bus-auto-process") |
| 94 | + .classList |
| 95 | + .contains("cds--tree-node--disabled") |
| 96 | + ).toBeTruthy(); |
| 97 | + }); |
| 98 | + |
| 99 | + it("should set the depth of a node based on the height of the tree", () => { |
| 100 | + const parentNode = nodes[2].componentInstance; |
| 101 | + const childNode = nodes[3].componentInstance; |
| 102 | + expect(parentNode.depth).toEqual(0); |
| 103 | + expect(childNode.depth).toEqual(1); |
| 104 | + }); |
| 105 | + |
| 106 | + it("should expand parent node to show child nodes", () => { |
| 107 | + const parentNode = nodes[2].componentInstance; |
| 108 | + expect(parentNode.expanded).toBeTruthy(); |
| 109 | + expect(nodes[2].nativeElement.querySelector(".cds--tree-parent-node__toggle-icon--expanded")).toBeTruthy(); |
| 110 | + }); |
| 111 | + |
| 112 | + it("should emit 'select' event from the tree wrapper when a node is clicked", fakeAsync(() => { |
| 113 | + const wrapper = tree.componentInstance; |
| 114 | + const selectSpy = spyOn(wrapper.select, 'emit'); |
| 115 | + fixture.detectChanges(); |
| 116 | + nodes[0].nativeElement.querySelector('.cds--tree-node__label').dispatchEvent(new Event('click')); |
| 117 | + tick(); |
| 118 | + expect(selectSpy).toHaveBeenCalled(); |
| 119 | + })) |
| 120 | +}); |
| 121 | + |
0 commit comments