|
| 1 | +import { Component } from "@angular/core"; |
| 2 | +import { CUSTOM_ELEMENTS_SCHEMA } from "@angular/core"; |
| 3 | +import { async, ComponentFixture, TestBed } from "@angular/core/testing"; |
| 4 | +import { FormsModule } from "@angular/forms"; |
| 5 | +import { By } from "@angular/platform-browser"; |
| 6 | +import { BrowserDynamicTestingModule, platformBrowserDynamicTesting } from "@angular/platform-browser-dynamic/testing"; |
| 7 | + |
| 8 | +import { Breadcrumb } from "./breadcrumb.component"; |
| 9 | +import { BreadcrumbItemComponent } from "./breadcrumb-item.component"; |
| 10 | +import { BreadcrumbItem } from "./breadcrumb-item.interface"; |
| 11 | +import { OverflowMenu } from "../dialog/overflow-menu/overflow-menu.component"; |
| 12 | + |
| 13 | +@Component({ |
| 14 | + selector: "test-breadcrumb", |
| 15 | + template: ` |
| 16 | + <ibm-breadcrumb [noTrailingSlash]="noTrailingSlash"> |
| 17 | + <ibm-breadcrumb-item href="#"> |
| 18 | + Breadcrumb 1 |
| 19 | + </ibm-breadcrumb-item> |
| 20 | + <ibm-breadcrumb-item href="#"> |
| 21 | + Breadcrumb 2 |
| 22 | + </ibm-breadcrumb-item> |
| 23 | + <ibm-breadcrumb-item href="#"> |
| 24 | + Breadcrumb 3 |
| 25 | + </ibm-breadcrumb-item> |
| 26 | + <ibm-breadcrumb-item href="#"> |
| 27 | + Breadcrumb 4 |
| 28 | + </ibm-breadcrumb-item> |
| 29 | + <ibm-breadcrumb-item href="#"> |
| 30 | + Breadcrumb 5 |
| 31 | + </ibm-breadcrumb-item> |
| 32 | + </ibm-breadcrumb>`, |
| 33 | + entryComponents: [Breadcrumb] |
| 34 | +}) |
| 35 | +class TestBreadcrumb { |
| 36 | + noTrailingSlash = true; |
| 37 | +} |
| 38 | + |
| 39 | +@Component({ |
| 40 | + selector: "test-breadcrumb", |
| 41 | + template: ` |
| 42 | + <ibm-breadcrumb |
| 43 | + [noTrailingSlash]="noTrailingSlash" |
| 44 | + [threshold]="threshold" |
| 45 | + [items]="items"> |
| 46 | + </ibm-breadcrumb>`, |
| 47 | + entryComponents: [Breadcrumb] |
| 48 | +}) |
| 49 | +class TestBreadcrumbModel { |
| 50 | + noTrailingSlash = true; |
| 51 | + threshold = 4; |
| 52 | + items = []; |
| 53 | +} |
| 54 | + |
| 55 | +describe("Breadcrumb", () => { |
| 56 | + let component: Breadcrumb; |
| 57 | + let fixture: ComponentFixture<Breadcrumb>; |
| 58 | + |
| 59 | + beforeEach(async(() => { |
| 60 | + TestBed.resetTestEnvironment(); |
| 61 | + TestBed.initTestEnvironment(BrowserDynamicTestingModule, platformBrowserDynamicTesting()); |
| 62 | + |
| 63 | + TestBed.configureTestingModule({ |
| 64 | + imports: [FormsModule], |
| 65 | + schemas: [CUSTOM_ELEMENTS_SCHEMA], |
| 66 | + declarations: [ |
| 67 | + Breadcrumb, |
| 68 | + BreadcrumbItemComponent, |
| 69 | + TestBreadcrumb, |
| 70 | + TestBreadcrumbModel |
| 71 | + ] |
| 72 | + }).compileComponents(); |
| 73 | + })); |
| 74 | + |
| 75 | + beforeEach(() => { |
| 76 | + fixture = TestBed.createComponent(Breadcrumb); |
| 77 | + component = fixture.componentInstance; |
| 78 | + fixture.detectChanges(); |
| 79 | + }); |
| 80 | + |
| 81 | + it("should work", () => { |
| 82 | + expect(component).toBeTruthy(); |
| 83 | + }); |
| 84 | + |
| 85 | + it("should create a breadcrumb with four items and no overflow menu", () => { |
| 86 | + const testFixture: ComponentFixture<TestBreadcrumb> = |
| 87 | + TestBed.createComponent(TestBreadcrumb); |
| 88 | + const testComponent = testFixture.componentInstance; |
| 89 | + testFixture.detectChanges(); |
| 90 | + |
| 91 | + const breadcrumbElement = testFixture.debugElement.query(By.directive(Breadcrumb)); |
| 92 | + expect(breadcrumbElement).not.toBeNull(); |
| 93 | + const breadcrumbItemElements = testFixture.debugElement.queryAll(By.directive(BreadcrumbItemComponent)); |
| 94 | + expect(breadcrumbItemElements).not.toBeNull(); |
| 95 | + expect(breadcrumbItemElements.length).toBe(5); |
| 96 | + const overflowMenuElement = testFixture.debugElement.query(By.directive(OverflowMenu)); |
| 97 | + expect(overflowMenuElement).toBeNull(); |
| 98 | + }); |
| 99 | + |
| 100 | + it("should create a breadcrumb with five items and an overflow menu in second position", () => { |
| 101 | + const testFixture: ComponentFixture<TestBreadcrumbModel> = |
| 102 | + TestBed.createComponent(TestBreadcrumbModel); |
| 103 | + const testComponent = testFixture.componentInstance; |
| 104 | + testComponent.threshold = 4; |
| 105 | + testComponent.items = [ |
| 106 | + { content: "Breadcrumb 1", href: "#1" }, |
| 107 | + { content: "Breadcrumb 2", href: "#2" }, |
| 108 | + { content: "Breadcrumb 3", href: "#3" }, |
| 109 | + { content: "Breadcrumb 4", href: "#4" }, |
| 110 | + { content: "Breadcrumb 5", href: "#5" } |
| 111 | + ]; |
| 112 | + testFixture.detectChanges(); |
| 113 | + |
| 114 | + const breadcrumbElement = testFixture.debugElement.query(By.directive(Breadcrumb)); |
| 115 | + expect(breadcrumbElement).not.toBeNull(); |
| 116 | + const breadcrumbItemElements = testFixture.debugElement.queryAll(By.directive(BreadcrumbItemComponent)); |
| 117 | + expect(breadcrumbItemElements).not.toBeNull(); |
| 118 | + expect(breadcrumbItemElements.length).toBe(4); // 4 because one is created for the overflow menu |
| 119 | + expect(breadcrumbItemElements[1].children[0].name).toEqual("ibm-overflow-menu"); |
| 120 | + }); |
| 121 | +}); |
0 commit comments