Skip to content

Commit d16cae1

Browse files
committed
test: cleanups and fixes
1 parent a46b876 commit d16cae1

File tree

5 files changed

+55
-26
lines changed

5 files changed

+55
-26
lines changed

projects/coreui-angular/src/lib/navbar/navbar-toggler/navbar-toggler.directive.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class MockElementRef extends ElementRef {}
1111
imports: [NavbarTogglerDirective, CollapseDirective],
1212
template: `
1313
<button [cNavbarToggler]="collapseRef"></button>
14-
<div #collapseRef="cCollapse" navbar cCollapse></div>
14+
<div #collapseRef="cCollapse" navbar cCollapse>test</div>
1515
`
1616
})
1717
class TestComponent {}
Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
import { NgTemplateOutlet } from '@angular/common';
12
import { ComponentFixture, TestBed } from '@angular/core/testing';
2-
import { TemplateIdDirective } from './template-id.directive';
33
import { Component, computed, DebugElement, TemplateRef, viewChild } from '@angular/core';
44
import { By } from '@angular/platform-browser';
5-
import { NgTemplateOutlet } from '@angular/common';
5+
import { TemplateIdDirective } from './template-id.directive';
66

77
@Component({
88
imports: [TemplateIdDirective, NgTemplateOutlet],
@@ -16,7 +16,7 @@ import { NgTemplateOutlet } from '@angular/common';
1616
class TestComponent {
1717
readonly templateId = viewChild(TemplateIdDirective);
1818

19-
readonly id = computed(() => this.templateId()?.templateRef);
19+
readonly id = computed(() => this.templateId()?.templateRef ?? null);
2020
}
2121

2222
describe('TemplateIdDirective', () => {
@@ -26,8 +26,7 @@ describe('TemplateIdDirective', () => {
2626

2727
beforeEach(() => {
2828
TestBed.configureTestingModule({
29-
imports: [TestComponent],
30-
providers: [TemplateRef]
29+
imports: [TestComponent]
3130
}).compileComponents();
3231

3332
fixture = TestBed.createComponent(TestComponent);
@@ -36,19 +35,25 @@ describe('TemplateIdDirective', () => {
3635

3736
fixture.detectChanges();
3837
});
39-
it('should create an instance', () => {
40-
TestBed.runInInjectionContext(() => {
41-
const directive = new TemplateIdDirective();
42-
expect(directive).toBeTruthy();
43-
});
44-
});
4538

4639
it('should create a template with innerText', () => {
4740
expect(debugElement.nativeElement.innerText).toBe('Inner Text');
4841
});
4942

50-
it('should get the template id', () => {
43+
it('should return the correct id when cTemplateId is set', () => {
44+
expect(component.templateId()?.id).toBe('test');
45+
});
46+
47+
it('should correctly bind the templateRef to the directive', () => {
48+
expect(component.templateId()?.templateRef).toBeInstanceOf(TemplateRef);
49+
});
50+
51+
it('should handle multiple instances of the directive with unique ids', () => {
52+
const secondFixture = TestBed.createComponent(TestComponent);
53+
const secondComponent = secondFixture.componentInstance;
54+
secondFixture.detectChanges();
55+
5156
expect(component.templateId()?.id).toBe('test');
52-
expect(component.templateId()?.templateRef).toEqual(jasmine.any(TemplateRef));
57+
expect(secondComponent.templateId()?.id).toBe('test');
5358
});
5459
});

projects/coreui-angular/src/lib/utilities/shadow-on-scroll.directive.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { By } from '@angular/platform-browser';
55

66
@Component({
77
imports: [ShadowOnScrollDirective],
8-
template: '<div cShadowOnScroll></div>'
8+
template: '<div [cShadowOnScroll]="true"></div>'
99
})
1010
class TestComponent {}
1111

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { ComponentFixture, TestBed } from '@angular/core/testing';
2-
import { Component, DebugElement, input, TemplateRef, ViewContainerRef } from '@angular/core';
2+
import { Component, DebugElement, signal } from '@angular/core';
33
import { By } from '@angular/platform-browser';
44
import { VisibleDirective } from './visible.directive';
55

@@ -8,7 +8,7 @@ import { VisibleDirective } from './visible.directive';
88
template: '<ng-template [cVisible]="visible()">Test Node</ng-template>'
99
})
1010
class TestComponent {
11-
readonly visible = input(true);
11+
readonly visible = signal(true);
1212
}
1313

1414
describe('VisibleDirective', () => {
@@ -18,8 +18,7 @@ describe('VisibleDirective', () => {
1818

1919
beforeEach(() => {
2020
TestBed.configureTestingModule({
21-
imports: [TestComponent],
22-
providers: [TemplateRef, ViewContainerRef]
21+
imports: [TestComponent]
2322
}).compileComponents();
2423

2524
fixture = TestBed.createComponent(TestComponent);
@@ -28,10 +27,37 @@ describe('VisibleDirective', () => {
2827
fixture.detectChanges();
2928
});
3029

31-
it('should create an instance', () => {
32-
TestBed.runInInjectionContext(() => {
33-
const directive = new VisibleDirective();
34-
expect(directive).toBeTruthy();
35-
});
30+
it('should display the content when cVisible is true', () => {
31+
component.visible.set(true);
32+
fixture.detectChanges();
33+
const content = fixture.nativeElement.textContent.trim();
34+
expect(content).toBe('Test Node');
35+
});
36+
37+
it('should not display the content when cVisible is false', () => {
38+
component.visible.set(false);
39+
fixture.detectChanges();
40+
const content = fixture.nativeElement.textContent.trim();
41+
expect(content).toBe('');
42+
});
43+
44+
it('should toggle visibility when cVisible changes from true to false', () => {
45+
component.visible.set(true);
46+
fixture.detectChanges();
47+
expect(fixture.nativeElement.textContent.trim()).toBe('Test Node');
48+
49+
component.visible.set(false);
50+
fixture.detectChanges();
51+
expect(fixture.nativeElement.textContent.trim()).toBe('');
52+
});
53+
54+
it('should toggle visibility when cVisible changes from false to true', () => {
55+
component.visible.set(false);
56+
fixture.detectChanges();
57+
expect(fixture.nativeElement.textContent.trim()).toBe('');
58+
59+
component.visible.set(true);
60+
fixture.detectChanges();
61+
expect(fixture.nativeElement.textContent.trim()).toBe('Test Node');
3662
});
3763
});

projects/coreui-angular/src/lib/widget/widget-stat-b/widget-stat-b.component.spec.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@ describe('WidgetStatBComponent', () => {
1010
await TestBed.configureTestingModule({
1111
imports: [WidgetStatBComponent]
1212
}).compileComponents();
13-
});
1413

15-
beforeEach(() => {
1614
fixture = TestBed.createComponent(WidgetStatBComponent);
1715
component = fixture.componentInstance;
1816
fixture.detectChanges();

0 commit comments

Comments
 (0)