|
| 1 | +import { Component } from "@angular/core"; |
| 2 | +import { ComponentFixture, TestBed } from "@angular/core/testing"; |
| 3 | +import { By } from "@angular/platform-browser"; |
| 4 | +import { ButtonModule } from "../button"; |
| 5 | +import { IconModule, IconService } from "../icon"; |
| 6 | +import { ContainedListItem } from "./contained-list-item.component"; |
| 7 | +import Apple16 from "@carbon/icons/es/apple/16"; |
| 8 | +import Fish16 from "@carbon/icons/es/fish/16"; |
| 9 | +import { ContainedList } from "./contained-list.component"; |
| 10 | +import { ContainedListKind, ContainedListSize } from "./contained-list.enums"; |
| 11 | + |
| 12 | +@Component({ |
| 13 | + template: ` |
| 14 | + <ng-template #label> |
| 15 | + <h1>My contained list</h1> |
| 16 | + </ng-template> |
| 17 | +
|
| 18 | + <ng-template #action> |
| 19 | + <ibm-icon-button |
| 20 | + type="button" |
| 21 | + kind="primary" |
| 22 | + align="left" |
| 23 | + description="Add"> |
| 24 | + <svg class="cds--btn__icon" ibmIcon="add" size="16"></svg> |
| 25 | + </ibm-icon-button> |
| 26 | + </ng-template> |
| 27 | +
|
| 28 | + <ng-template #icon> |
| 29 | + <svg ibmIcon="fish" size="16"></svg> |
| 30 | + </ng-template> |
| 31 | +
|
| 32 | + <cds-contained-list [label]="label" [action]="action"> |
| 33 | + <cds-contained-list-item>List item</cds-contained-list-item> |
| 34 | + <cds-contained-list-item [icon]="icon">List item with icon</cds-contained-list-item> |
| 35 | + <cds-contained-list-item icon="apple">List item with string ref icon</cds-contained-list-item> |
| 36 | + <cds-contained-list-item [action]="action">List item with action</cds-contained-list-item> |
| 37 | + <cds-contained-list-item #clickableListItem [clickable]="true"> |
| 38 | + <ng-container ibmContainedListItemButton>Clickable list item</ng-container> |
| 39 | + </cds-contained-list-item> |
| 40 | + </cds-contained-list> |
| 41 | + ` |
| 42 | +}) |
| 43 | +class WrapperComponent { |
| 44 | + constructor(private iconService: IconService) { |
| 45 | + this.iconService.registerAll([Apple16, Fish16]); |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +describe("ContainedList", () => { |
| 50 | + let component: ContainedList; |
| 51 | + let fixture: ComponentFixture<ContainedList>; |
| 52 | + |
| 53 | + beforeEach(async () => { |
| 54 | + await TestBed.configureTestingModule({ |
| 55 | + declarations: [ContainedList, ContainedListItem, WrapperComponent], |
| 56 | + imports: [IconModule, ButtonModule] |
| 57 | + }).compileComponents(); |
| 58 | + |
| 59 | + fixture = TestBed.createComponent(ContainedList); |
| 60 | + component = fixture.componentInstance; |
| 61 | + }); |
| 62 | + |
| 63 | + it("should set default inputs", () => { |
| 64 | + fixture.detectChanges(); |
| 65 | + expect(component.action).toBeUndefined(); |
| 66 | + expect(component.isInset).toBeFalsy(); |
| 67 | + expect(component.kind).toBe(ContainedListKind.OnPage); |
| 68 | + expect(component.label).toBeUndefined(); |
| 69 | + expect(component.size).toBe(ContainedListSize.Large); |
| 70 | + }); |
| 71 | + |
| 72 | + it("should display the label when a string is provided", () => { |
| 73 | + const label = "My contained list"; |
| 74 | + component.label = label; |
| 75 | + fixture.detectChanges(); |
| 76 | + |
| 77 | + const labelElement = fixture.nativeElement.querySelector(".cds--contained-list__label"); |
| 78 | + expect(labelElement.textContent.trim()).toEqual(label); |
| 79 | + }); |
| 80 | + |
| 81 | + it("should have the correct isInset class", () => { |
| 82 | + component.isInset = true; |
| 83 | + fixture.detectChanges(); |
| 84 | + |
| 85 | + const listElement: HTMLElement = fixture.nativeElement.querySelector(".cds--contained-list"); |
| 86 | + expect(listElement).toHaveClass("cds--contained-list--inset-rulers"); |
| 87 | + }); |
| 88 | + |
| 89 | + it("should have the correct size class", () => { |
| 90 | + component.size = ContainedListSize.Small; |
| 91 | + fixture.detectChanges(); |
| 92 | + |
| 93 | + const listElement: HTMLElement = fixture.nativeElement.querySelector(".cds--contained-list"); |
| 94 | + expect(listElement).toHaveClass("cds--contained-list--sm"); |
| 95 | + }); |
| 96 | + |
| 97 | + it("should have the correct kind class", () => { |
| 98 | + component.kind = ContainedListKind.Disclosed; |
| 99 | + fixture.detectChanges(); |
| 100 | + |
| 101 | + const listElement: HTMLElement = fixture.nativeElement.querySelector(".cds--contained-list"); |
| 102 | + expect(listElement).toHaveClass("cds--contained-list--disclosed"); |
| 103 | + }); |
| 104 | + |
| 105 | + describe("TemplateRefs", () => { |
| 106 | + it("should render the label if it is a TemplateRef", () => { |
| 107 | + const wrapperFixture: ComponentFixture<WrapperComponent> = TestBed.createComponent(WrapperComponent); |
| 108 | + wrapperFixture.detectChanges(); |
| 109 | + |
| 110 | + const labelRefElement = wrapperFixture.nativeElement.querySelector(".cds--contained-list .cds--contained-list__label h1"); |
| 111 | + expect(labelRefElement.textContent.trim()).toBe("My contained list"); |
| 112 | + }); |
| 113 | + |
| 114 | + it("should render the action if it is a TemplateRef", () => { |
| 115 | + const wrapperFixture: ComponentFixture<WrapperComponent> = TestBed.createComponent(WrapperComponent); |
| 116 | + wrapperFixture.detectChanges(); |
| 117 | + |
| 118 | + const actionElementRef = wrapperFixture.nativeElement.querySelector(".cds--contained-list .cds--contained-list__action ibm-icon-button"); |
| 119 | + expect(actionElementRef).toBeTruthy(); |
| 120 | + }); |
| 121 | + }); |
| 122 | + |
| 123 | + describe("ContainedListItem", () => { |
| 124 | + it("should render the content", () => { |
| 125 | + const wrapperFixture: ComponentFixture<WrapperComponent> = TestBed.createComponent(WrapperComponent); |
| 126 | + wrapperFixture.detectChanges(); |
| 127 | + |
| 128 | + const listItemElement = wrapperFixture.debugElement.query(By.css(".cds--contained-list-item:nth-child(1)")); |
| 129 | + expect(listItemElement.nativeElement.textContent.trim()).toBe("List item"); |
| 130 | + }); |
| 131 | + |
| 132 | + it("should render the icon", () => { |
| 133 | + const wrapperFixture: ComponentFixture<WrapperComponent> = TestBed.createComponent(WrapperComponent); |
| 134 | + wrapperFixture.detectChanges(); |
| 135 | + |
| 136 | + const iconElement = wrapperFixture.debugElement.query(By.css(".cds--contained-list-item:nth-child(2) svg[ibmIcon='fish']")); |
| 137 | + expect(iconElement).toBeTruthy(); |
| 138 | + }); |
| 139 | + |
| 140 | + it("should render the icon", () => { |
| 141 | + const wrapperFixture: ComponentFixture<WrapperComponent> = TestBed.createComponent(WrapperComponent); |
| 142 | + wrapperFixture.detectChanges(); |
| 143 | + |
| 144 | + const iconElement = wrapperFixture.debugElement.query(By.css(".cds--contained-list-item:nth-child(3) svg[ng-reflect-ibm-icon='apple']")); |
| 145 | + expect(iconElement).toBeTruthy(); |
| 146 | + }); |
| 147 | + |
| 148 | + it("should render the action", () => { |
| 149 | + const wrapperFixture: ComponentFixture<WrapperComponent> = TestBed.createComponent(WrapperComponent); |
| 150 | + wrapperFixture.detectChanges(); |
| 151 | + |
| 152 | + const actionElement = wrapperFixture.debugElement.query(By.css(".cds--contained-list-item:nth-child(4) ibm-icon-button")); |
| 153 | + expect(actionElement).toBeTruthy(); |
| 154 | + }); |
| 155 | + |
| 156 | + it("should render with the clickable state", () => { |
| 157 | + const wrapperFixture: ComponentFixture<WrapperComponent> = TestBed.createComponent(WrapperComponent); |
| 158 | + wrapperFixture.detectChanges(); |
| 159 | + |
| 160 | + const clickableListItemElement = wrapperFixture.debugElement.query(By.css(".cds--contained-list-item:nth-child(5)")); |
| 161 | + expect(clickableListItemElement.nativeElement).toHaveClass("cds--contained-list-item--clickable"); |
| 162 | + |
| 163 | + const buttonElement = clickableListItemElement.nativeElement.querySelector("button"); |
| 164 | + expect(buttonElement.textContent.trim()).toBe("Clickable list item"); |
| 165 | + }); |
| 166 | + }); |
| 167 | +}); |
0 commit comments