|
| 1 | +import { BrowserAnimationsModule } from "@angular/platform-browser/animations"; |
| 2 | +import { ComponentFixture, TestBed, fakeAsync, tick, async } from "@angular/core/testing"; |
| 3 | +import { By } from "@angular/platform-browser"; |
| 4 | +import { DebugElement } from "@angular/core"; |
| 5 | +import { StaticIconModule } from "../icon/static-icon.module"; |
| 6 | + |
| 7 | +import { ToggleComponent } from "./toggle.component"; |
| 8 | +import { CheckboxComponent } from "../checkbox/checkbox.module"; |
| 9 | + |
| 10 | +describe("ToggleComponent", () => { |
| 11 | + let component: ToggleComponent; |
| 12 | + let fixture: ComponentFixture<ToggleComponent>; |
| 13 | + let labelElement: HTMLElement; |
| 14 | + let buttonElement: HTMLElement; |
| 15 | + let svgElement: HTMLElement; |
| 16 | + |
| 17 | + beforeEach(() => { |
| 18 | + TestBed.configureTestingModule({ |
| 19 | + declarations: [ToggleComponent], |
| 20 | + imports: [BrowserAnimationsModule, StaticIconModule], |
| 21 | + providers: [] |
| 22 | + }); |
| 23 | + |
| 24 | + fixture = TestBed.createComponent(ToggleComponent); |
| 25 | + component = fixture.componentInstance; |
| 26 | + fixture.detectChanges(); |
| 27 | + labelElement = fixture.debugElement.query(By.css("label")).nativeElement; |
| 28 | + buttonElement = fixture.debugElement.query(By.css("input")).nativeElement; |
| 29 | + }); |
| 30 | + |
| 31 | + it("should work", () => { |
| 32 | + expect(component instanceof ToggleComponent).toBe(true); |
| 33 | + }); |
| 34 | + |
| 35 | + it("should have input with class 'bx--toggle'", () => { |
| 36 | + expect(buttonElement.className.includes("bx--toggle")).toEqual(true); |
| 37 | + component.size = "sm"; |
| 38 | + fixture.detectChanges(); |
| 39 | + expect(buttonElement.className.includes("bx--toggle")).toEqual(true); |
| 40 | + }); |
| 41 | + |
| 42 | + it("should change state", () => { |
| 43 | + buttonElement.click(); |
| 44 | + fixture.detectChanges(); |
| 45 | + expect(component.checked).toBe(true, "setting to on"); |
| 46 | + |
| 47 | + buttonElement.click(); |
| 48 | + fixture.detectChanges(); |
| 49 | + expect(component.checked).toBe(false, "setting to off"); |
| 50 | + }); |
| 51 | + |
| 52 | + it("should display small version of toggle when size equals sm", () => { |
| 53 | + expect(buttonElement.className.includes("bx--toggle--small")).toEqual(false); |
| 54 | + component.size = "sm"; |
| 55 | + fixture.detectChanges(); |
| 56 | + expect(buttonElement.className.includes("bx--toggle--small")).toEqual(true); |
| 57 | + }); |
| 58 | + |
| 59 | + it("should display SVG in small version of toggle", () => { |
| 60 | + component.size = "sm"; |
| 61 | + fixture.detectChanges(); |
| 62 | + labelElement = fixture.debugElement.query(By.css("label")).nativeElement; |
| 63 | + expect(fixture.debugElement.query(By.css("svg")).nativeElement).not.toBeNull(); |
| 64 | + expect(labelElement.innerHTML).toContain("bx--toggle__check"); |
| 65 | + }); |
| 66 | + |
| 67 | +}); |
0 commit comments