|
| 1 | +import { ComponentFixture, TestBed } from "@angular/core/testing"; |
| 2 | +import { TagComponent, TagType } from "./tag.component"; |
| 3 | +import { TEDI_TRANSLATION_DEFAULT_TOKEN } from "../../../tokens/translation.token"; |
| 4 | + |
| 5 | +describe("TagComponent", () => { |
| 6 | + let component: TagComponent; |
| 7 | + let fixture: ComponentFixture<TagComponent>; |
| 8 | + |
| 9 | + beforeEach(async () => { |
| 10 | + await TestBed.configureTestingModule({ |
| 11 | + imports: [TagComponent], |
| 12 | + providers: [{ provide: TEDI_TRANSLATION_DEFAULT_TOKEN, useValue: "et" }], |
| 13 | + }).compileComponents(); |
| 14 | + |
| 15 | + fixture = TestBed.createComponent(TagComponent); |
| 16 | + component = fixture.componentInstance; |
| 17 | + fixture.detectChanges(); |
| 18 | + }); |
| 19 | + |
| 20 | + it("should create", () => { |
| 21 | + expect(component).toBeTruthy(); |
| 22 | + }); |
| 23 | + |
| 24 | + it("should have default values", () => { |
| 25 | + expect(component.loading()).toBe(false); |
| 26 | + expect(component.closable()).toBe(false); |
| 27 | + expect(component.type()).toBe("primary"); |
| 28 | + }); |
| 29 | + |
| 30 | + it("should have tedi-tag class", () => { |
| 31 | + expect(fixture.nativeElement.classList).toContain("tedi-tag"); |
| 32 | + }); |
| 33 | + |
| 34 | + it("should apply correct type class", () => { |
| 35 | + const types: TagType[] = ["primary", "secondary", "danger"]; |
| 36 | + |
| 37 | + for (const type of types) { |
| 38 | + fixture.componentRef.setInput("type", type); |
| 39 | + fixture.detectChanges(); |
| 40 | + |
| 41 | + expect(fixture.nativeElement.classList).toContain(`tedi-tag--${type}`); |
| 42 | + } |
| 43 | + }); |
| 44 | + |
| 45 | + it("should show danger icon when type is danger", () => { |
| 46 | + fixture.componentRef.setInput("type", "danger"); |
| 47 | + fixture.detectChanges(); |
| 48 | + |
| 49 | + const iconElement = fixture.nativeElement.querySelector("tedi-icon"); |
| 50 | + expect(iconElement).toBeTruthy(); |
| 51 | + expect(iconElement.textContent).toBe("error"); |
| 52 | + }); |
| 53 | + |
| 54 | + it("should not show danger icon when type is not danger", () => { |
| 55 | + fixture.componentRef.setInput("type", "primary"); |
| 56 | + fixture.detectChanges(); |
| 57 | + |
| 58 | + const iconWrapper = fixture.nativeElement.querySelector(".tedi-tag__icon-wrapper"); |
| 59 | + expect(iconWrapper).toBeFalsy(); |
| 60 | + }); |
| 61 | + |
| 62 | + it("should show spinner when loading is true", () => { |
| 63 | + fixture.componentRef.setInput("loading", true); |
| 64 | + fixture.detectChanges(); |
| 65 | + |
| 66 | + const spinner = fixture.nativeElement.querySelector("tedi-spinner"); |
| 67 | + expect(spinner).toBeTruthy(); |
| 68 | + expect(fixture.nativeElement.classList).toContain("tedi-tag--loading"); |
| 69 | + }); |
| 70 | + |
| 71 | + it("should not show spinner when loading is false", () => { |
| 72 | + fixture.componentRef.setInput("loading", false); |
| 73 | + fixture.detectChanges(); |
| 74 | + |
| 75 | + const spinner = fixture.nativeElement.querySelector("tedi-spinner"); |
| 76 | + expect(spinner).toBeFalsy(); |
| 77 | + }); |
| 78 | + |
| 79 | + it("should show close button when closable is true", () => { |
| 80 | + fixture.componentRef.setInput("closable", true); |
| 81 | + fixture.detectChanges(); |
| 82 | + |
| 83 | + const closeButton = fixture.nativeElement.querySelector("[tedi-closing-button]"); |
| 84 | + expect(closeButton).toBeTruthy(); |
| 85 | + expect(fixture.nativeElement.classList).toContain("tedi-tag--closable"); |
| 86 | + }); |
| 87 | + |
| 88 | + it("should not show close button when closable is false", () => { |
| 89 | + fixture.componentRef.setInput("closable", false); |
| 90 | + fixture.detectChanges(); |
| 91 | + |
| 92 | + const closeButton = fixture.nativeElement.querySelector("[tedi-closing-button]"); |
| 93 | + expect(closeButton).toBeFalsy(); |
| 94 | + }); |
| 95 | + |
| 96 | + it("should not show close button when loading is true even if closable is true", () => { |
| 97 | + fixture.componentRef.setInput("closable", true); |
| 98 | + fixture.componentRef.setInput("loading", true); |
| 99 | + fixture.detectChanges(); |
| 100 | + |
| 101 | + const closeButton = fixture.nativeElement.querySelector("[tedi-closing-button]"); |
| 102 | + const spinner = fixture.nativeElement.querySelector("tedi-spinner"); |
| 103 | + |
| 104 | + expect(closeButton).toBeFalsy(); |
| 105 | + expect(spinner).toBeTruthy(); |
| 106 | + }); |
| 107 | + |
| 108 | + it("should emit closed event when close button is clicked", () => { |
| 109 | + fixture.componentRef.setInput("closable", true); |
| 110 | + fixture.detectChanges(); |
| 111 | + |
| 112 | + const closedSpy = jest.fn(); |
| 113 | + component.closed.subscribe(closedSpy); |
| 114 | + |
| 115 | + const closeButton = fixture.nativeElement.querySelector("[tedi-closing-button]"); |
| 116 | + closeButton.click(); |
| 117 | + fixture.detectChanges(); |
| 118 | + |
| 119 | + expect(closedSpy).toHaveBeenCalled(); |
| 120 | + }); |
| 121 | + |
| 122 | + it("should render content", () => { |
| 123 | + const contentElement = fixture.nativeElement.querySelector(".tedi-tag__content"); |
| 124 | + expect(contentElement).toBeTruthy(); |
| 125 | + }); |
| 126 | +}); |
0 commit comments