Skip to content

Commit 0e819c0

Browse files
committed
test: updates, cleanups and fixes
1 parent 7fda57c commit 0e819c0

File tree

15 files changed

+118
-117
lines changed

15 files changed

+118
-117
lines changed

projects/coreui-angular-chartjs/src/test.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,9 @@
33
import 'zone.js';
44
import 'zone.js/testing';
55
import { getTestBed } from '@angular/core/testing';
6-
import {
7-
BrowserDynamicTestingModule,
8-
platformBrowserDynamicTesting
9-
} from '@angular/platform-browser-dynamic/testing';
6+
import { BrowserTestingModule, platformBrowserTesting } from '@angular/platform-browser/testing';
107

118
// First, initialize the Angular testing environment.
12-
getTestBed().initTestEnvironment(
13-
BrowserDynamicTestingModule,
14-
platformBrowserDynamicTesting(),
15-
{ teardown: { destroyAfterEach: true }},
16-
);
9+
getTestBed().initTestEnvironment(BrowserTestingModule, platformBrowserTesting(), {
10+
teardown: { destroyAfterEach: true }
11+
});

projects/coreui-angular/src/lib/card/card-img.directive.spec.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
import { Component, DebugElement } from '@angular/core';
1+
import { Component, DebugElement, input } from '@angular/core';
22
import { ComponentFixture, TestBed } from '@angular/core/testing';
33
import { CardImgDirective } from './card-img.directive';
44
import { By } from '@angular/platform-browser';
55

66
@Component({
77
imports: [CardImgDirective],
8-
template: ` <div [cCardImg]="orientation"></div> `
8+
template: ` <div [cCardImg]="orientation()"></div> `
99
})
1010
export class TestComponent {
11-
orientation: 'top' | 'bottom' | 'start' | 'end' | undefined = undefined;
11+
readonly orientation = input<'top' | 'bottom' | 'start' | 'end' | undefined>(undefined);
1212
}
1313

1414
describe('CardImgDirective', () => {
@@ -34,16 +34,16 @@ describe('CardImgDirective', () => {
3434
});
3535

3636
it('should have css classes', () => {
37-
component.orientation = 'start';
37+
fixture.componentRef.setInput('orientation', 'start');
3838
fixture.detectChanges();
3939
expect(debugElement.nativeElement).toHaveClass('rounded-start');
40-
component.orientation = 'end';
40+
fixture.componentRef.setInput('orientation', 'end');
4141
fixture.detectChanges();
4242
expect(debugElement.nativeElement).toHaveClass('rounded-end');
43-
component.orientation = 'top';
43+
fixture.componentRef.setInput('orientation', 'top');
4444
fixture.detectChanges();
4545
expect(debugElement.nativeElement).toHaveClass('card-img-top');
46-
component.orientation = 'bottom';
46+
fixture.componentRef.setInput('orientation', 'bottom');
4747
fixture.detectChanges();
4848
expect(debugElement.nativeElement).toHaveClass('card-img-bottom');
4949
});

projects/coreui-angular/src/lib/collapse/collapse.directive.spec.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
import { CollapseDirective } from './collapse.directive';
2-
import { Component, DebugElement, ElementRef, Renderer2 } from '@angular/core';
2+
import { Component, DebugElement, ElementRef, Renderer2, signal } from '@angular/core';
33
import { ComponentFixture, fakeAsync, TestBed } from '@angular/core/testing';
44
import { NoopAnimationsModule } from '@angular/platform-browser/animations';
55
import { By } from '@angular/platform-browser';
66

77
class MockElementRef extends ElementRef {}
88

99
@Component({
10-
template: '<div cCollapse [horizontal]="horizontal" [(visible)]="visible" [animate]="false">Test</div>',
10+
template: '<div cCollapse [horizontal]="horizontal()" [(visible)]="visible" [animate]="false">Test</div>',
1111
imports: [CollapseDirective]
1212
})
1313
class TestComponent {
14-
horizontal = false;
15-
visible = false;
14+
readonly horizontal = signal(false);
15+
readonly visible = signal(false);
1616
}
1717

1818
describe('CollapseDirective', () => {
@@ -29,7 +29,7 @@ describe('CollapseDirective', () => {
2929
fixture = TestBed.createComponent(TestComponent);
3030
component = fixture.componentInstance;
3131
elementRef = fixture.debugElement.query(By.directive(CollapseDirective));
32-
component.visible = false;
32+
component.visible.set(false);
3333
fixture.detectChanges(); // initial binding
3434
});
3535

@@ -43,13 +43,13 @@ describe('CollapseDirective', () => {
4343
it('should have css classes', fakeAsync(() => {
4444
expect(elementRef.nativeElement.style.display).toContain('none');
4545
expect(elementRef.nativeElement).not.toHaveClass('collapse-horizontal');
46-
component.horizontal = true;
47-
component.visible = true;
46+
component.horizontal.set(true);
47+
component.visible.set(true);
4848
fixture.detectChanges();
4949
expect(elementRef.nativeElement).toHaveClass('collapse-horizontal');
5050
expect(elementRef.nativeElement.style.display).toBe('');
51-
component.horizontal = false;
52-
component.visible = false;
51+
component.horizontal.set(false);
52+
component.visible.set(false);
5353
fixture.detectChanges();
5454
expect(elementRef.nativeElement).not.toHaveClass('collapse-horizontal');
5555
}));

projects/coreui-angular/src/lib/dropdown/dropdown-close/dropdown-close.directive.spec.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Component, DebugElement, ElementRef, Renderer2, viewChild } from '@angular/core';
1+
import { Component, DebugElement, ElementRef, Renderer2, signal, viewChild } from '@angular/core';
22
import { ComponentFixture, fakeAsync, TestBed } from '@angular/core/testing';
33
import { By } from '@angular/platform-browser';
44
import { DropdownService } from '../dropdown.service';
@@ -13,14 +13,20 @@ class MockElementRef extends ElementRef {}
1313
template: `
1414
<c-dropdown #dropdown="cDropdown" visible>
1515
<div cDropdownMenu>
16-
<button cButtonClose cDropdownClose [disabled]="disabled" [dropdownComponent]="dropdown" tabIndex="0"></button>
16+
<button
17+
cButtonClose
18+
cDropdownClose
19+
[disabled]="disabled()"
20+
[dropdownComponent]="dropdown"
21+
tabIndex="0"
22+
></button>
1723
</div>
1824
</c-dropdown>
1925
`,
2026
imports: [ButtonCloseDirective, DropdownComponent, DropdownMenuDirective, DropdownCloseDirective]
2127
})
2228
class TestComponent {
23-
disabled = false;
29+
readonly disabled = signal(false);
2430
readonly dropdown = viewChild(DropdownComponent);
2531
}
2632

@@ -38,7 +44,7 @@ describe('DropdownCloseDirective', () => {
3844
fixture = TestBed.createComponent(TestComponent);
3945
component = fixture.componentInstance;
4046
elementRef = fixture.debugElement.query(By.directive(DropdownCloseDirective));
41-
component.disabled = false;
47+
component.disabled.set(false);
4248
fixture.detectChanges(); // initial binding
4349
});
4450

@@ -53,7 +59,7 @@ describe('DropdownCloseDirective', () => {
5359
expect(elementRef.nativeElement).not.toHaveClass('disabled');
5460
expect(elementRef.nativeElement.getAttribute('aria-disabled')).toBeNull();
5561
expect(elementRef.nativeElement.getAttribute('tabindex')).toBe('0');
56-
component.disabled = true;
62+
component.disabled.set(true);
5763
fixture.detectChanges();
5864
expect(elementRef.nativeElement).toHaveClass('disabled');
5965
expect(elementRef.nativeElement.getAttribute('aria-disabled')).toBe('true');

projects/coreui-angular/src/lib/dropdown/dropdown-item/dropdown-item.directive.spec.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Component, DebugElement, DOCUMENT, ElementRef, Renderer2, viewChild } from '@angular/core';
1+
import { Component, DebugElement, DOCUMENT, ElementRef, Renderer2, signal, viewChild } from '@angular/core';
22
import { ComponentFixture, fakeAsync, TestBed } from '@angular/core/testing';
33
import { By } from '@angular/platform-browser';
44

@@ -14,7 +14,7 @@ class MockElementRef extends ElementRef {}
1414
<c-dropdown #dropdown="cDropdown" visible>
1515
<ul cDropdownMenu>
1616
<li>
17-
<button cDropdownItem [active]="active" [disabled]="disabled" tabIndex="0" #item="cDropdownItem">
17+
<button cDropdownItem [active]="active()" [disabled]="disabled()" tabIndex="0" #item="cDropdownItem">
1818
Action
1919
</button>
2020
</li>
@@ -24,8 +24,8 @@ class MockElementRef extends ElementRef {}
2424
imports: [DropdownComponent, DropdownMenuDirective, DropdownItemDirective]
2525
})
2626
class TestComponent {
27-
disabled = false;
28-
active = false;
27+
readonly disabled = signal(false);
28+
readonly active = signal(false);
2929
readonly dropdown = viewChild(DropdownComponent);
3030
readonly item = viewChild(DropdownItemDirective);
3131
}
@@ -46,7 +46,7 @@ describe('DropdownItemDirective', () => {
4646
fixture = TestBed.createComponent(TestComponent);
4747
component = fixture.componentInstance;
4848
elementRef = fixture.debugElement.query(By.directive(DropdownItemDirective));
49-
component.disabled = false;
49+
component.disabled.set(false);
5050
fixture.detectChanges(); // initial binding
5151
});
5252

@@ -62,8 +62,8 @@ describe('DropdownItemDirective', () => {
6262
expect(elementRef.nativeElement.getAttribute('aria-disabled')).toBeNull();
6363
expect(elementRef.nativeElement.getAttribute('aria-current')).toBeNull();
6464
expect(elementRef.nativeElement.getAttribute('tabindex')).toBe('0');
65-
component.disabled = true;
66-
component.active = true;
65+
component.disabled.set(true);
66+
component.active.set(true);
6767
fixture.detectChanges();
6868
expect(elementRef.nativeElement).toHaveClass('disabled');
6969
expect(elementRef.nativeElement.getAttribute('aria-disabled')).toBe('true');

projects/coreui-angular/src/lib/dropdown/dropdown-menu/dropdown-menu.directive.spec.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Component, DebugElement, DOCUMENT, ElementRef, Renderer2, viewChild } from '@angular/core';
1+
import { Component, DebugElement, DOCUMENT, ElementRef, Renderer2, signal, viewChild } from '@angular/core';
22
import { ComponentFixture, fakeAsync, TestBed } from '@angular/core/testing';
33
import { By } from '@angular/platform-browser';
44
import { DropdownService } from '../dropdown.service';
@@ -13,7 +13,7 @@ class MockElementRef extends ElementRef {}
1313
template: `
1414
<c-dropdown #dropdown="cDropdown" [(visible)]="visible">
1515
<button cButton cDropdownToggle color="secondary">Dropdown button</button>
16-
<ul cDropdownMenu [alignment]="alignment">
16+
<ul cDropdownMenu [alignment]="alignment()">
1717
<li>
1818
<button cDropdownItem [active]="true" tabIndex="0" #item="cDropdownItem">Action</button>
1919
</li>
@@ -23,8 +23,8 @@ class MockElementRef extends ElementRef {}
2323
imports: [DropdownComponent, DropdownMenuDirective, DropdownItemDirective, ButtonDirective, DropdownToggleDirective]
2424
})
2525
class TestComponent {
26-
visible = true;
27-
alignment?: string;
26+
readonly visible = signal(true);
27+
readonly alignment = signal<string>('');
2828
readonly dropdown = viewChild(DropdownComponent);
2929
readonly menu = viewChild(DropdownMenuDirective);
3030
readonly item = viewChild(DropdownItemDirective);
@@ -49,7 +49,7 @@ describe('DropdownMenuDirective', () => {
4949
dropdownRef = fixture.debugElement.query(By.directive(DropdownComponent));
5050
elementRef = fixture.debugElement.query(By.directive(DropdownMenuDirective));
5151
itemRef = fixture.debugElement.query(By.directive(DropdownItemDirective));
52-
component.visible = true;
52+
component.visible.set(true);
5353
fixture.detectChanges(); // initial binding
5454
});
5555

@@ -61,25 +61,25 @@ describe('DropdownMenuDirective', () => {
6161
});
6262

6363
it('should have css classes', fakeAsync(() => {
64-
component.visible = false;
64+
component.visible.set(false);
6565
fixture.detectChanges();
6666
expect(dropdownRef.nativeElement).not.toHaveClass('show');
6767
expect(elementRef.nativeElement).toHaveClass('dropdown-menu');
6868
expect(elementRef.nativeElement).not.toHaveClass('dropdown-menu-end');
6969
expect(elementRef.nativeElement).not.toHaveClass('dropdown-menu-start');
7070
expect(elementRef.nativeElement).not.toHaveClass('show');
71-
component.visible = true;
72-
component.alignment = 'end';
71+
component.visible.set(true);
72+
component.alignment.set('end');
7373
fixture.detectChanges();
7474
expect(dropdownRef.nativeElement).toHaveClass('show');
7575
expect(elementRef.nativeElement).toHaveClass('dropdown-menu-end');
7676
expect(elementRef.nativeElement).not.toHaveClass('dropdown-menu-start');
7777
expect(elementRef.nativeElement).toHaveClass('show');
78-
component.alignment = 'start';
78+
component.alignment.set('start');
7979
fixture.detectChanges();
8080
expect(elementRef.nativeElement).not.toHaveClass('dropdown-menu-end');
8181
expect(elementRef.nativeElement).toHaveClass('dropdown-menu-start');
82-
component.alignment = undefined;
82+
component.alignment.set('');
8383
fixture.detectChanges();
8484
expect(elementRef.nativeElement).not.toHaveClass('dropdown-menu-end');
8585
expect(elementRef.nativeElement).not.toHaveClass('dropdown-menu-start');
@@ -89,7 +89,7 @@ describe('DropdownMenuDirective', () => {
8989
expect(document.activeElement).not.toEqual(elementRef.nativeElement);
9090
elementRef.nativeElement.dispatchEvent(new KeyboardEvent('keydown', { code: 'Space' }));
9191
elementRef.nativeElement.dispatchEvent(new KeyboardEvent('keyup', { key: 'Tab' }));
92-
component.visible = true;
92+
component.visible.set(true);
9393
fixture.detectChanges();
9494
elementRef.nativeElement.dispatchEvent(new KeyboardEvent('keydown', { code: 'Space' }));
9595
elementRef.nativeElement.dispatchEvent(new KeyboardEvent('keyup', { key: 'Tab' }));

projects/coreui-angular/src/lib/dropdown/dropdown/dropdown.component.spec.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { ComponentFixture, fakeAsync, TestBed } from '@angular/core/testing';
22

33
import { DropdownComponent, DropdownToggleDirective } from './dropdown.component';
4-
import { Component, DebugElement, DOCUMENT, ElementRef } from '@angular/core';
4+
import { Component, DebugElement, DOCUMENT, ElementRef, signal } from '@angular/core';
55
import { DropdownService } from '../dropdown.service';
66
import { By } from '@angular/platform-browser';
77
import { DropdownMenuDirective } from '../dropdown-menu/dropdown-menu.directive';
@@ -36,7 +36,7 @@ class MockElementRef extends ElementRef {}
3636

3737
@Component({
3838
template: `
39-
<c-dropdown #dropdown="cDropdown" [(visible)]="visible" direction="dropup" [variant]="variant">
39+
<c-dropdown #dropdown="cDropdown" [(visible)]="visible" direction="dropup" [variant]="variant()">
4040
<div cDropdownToggle [caret]="caret" [split]="split" [disabled]="disabled" [dropdownComponent]="dropdown"></div>
4141
<ul cDropdownMenu>
4242
<li><a cDropdownItem>Action</a></li>
@@ -49,8 +49,8 @@ class MockElementRef extends ElementRef {}
4949
imports: [DropdownToggleDirective, DropdownComponent, DropdownMenuDirective, DropdownItemDirective]
5050
})
5151
class TestComponent {
52-
variant: 'btn-group' | 'dropdown' | 'input-group' | 'nav-item' | undefined = 'nav-item';
53-
visible = false;
52+
readonly variant = signal<'btn-group' | 'dropdown' | 'input-group' | 'nav-item' | undefined>('nav-item');
53+
readonly visible = signal(false);
5454
disabled = false;
5555
caret = true;
5656
split = false;
@@ -96,7 +96,7 @@ describe('DropdownToggleDirective', () => {
9696
expect(elementRef.nativeElement).not.toHaveClass('disabled');
9797
expect(elementRef.nativeElement).toHaveClass('dropdown-toggle');
9898
expect(elementRef.nativeElement).not.toHaveClass('dropdown-toggle-split');
99-
component.variant = 'input-group';
99+
component.variant.set('input-group');
100100
component.disabled = true;
101101
component.split = true;
102102
component.caret = false;
@@ -105,30 +105,30 @@ describe('DropdownToggleDirective', () => {
105105
expect(elementRef.nativeElement).not.toHaveClass('dropdown-toggle');
106106
expect(elementRef.nativeElement).toHaveClass('dropdown-toggle-split');
107107
expect(elementRef.nativeElement.getAttribute('aria-expanded')).toBe('false');
108-
component.variant = 'nav-item';
109-
component.visible = true;
108+
component.variant.set('nav-item');
109+
component.visible.set(true);
110110
fixture.detectChanges();
111111
expect(elementRef.nativeElement.getAttribute('aria-expanded')).toBe('true');
112112
}));
113113

114114
it('should call event handling functions', fakeAsync(() => {
115-
expect(component.visible).toBeFalse();
115+
expect(component.visible()).toBeFalse();
116116
elementRef.nativeElement.dispatchEvent(new MouseEvent('click'));
117117
fixture.detectChanges();
118-
expect(component.visible).toBeTrue();
118+
expect(component.visible()).toBeTrue();
119119
elementRef.nativeElement.dispatchEvent(new MouseEvent('click'));
120120
fixture.detectChanges();
121-
expect(component.visible).toBeFalse();
121+
expect(component.visible()).toBeFalse();
122122
elementRef.nativeElement.dispatchEvent(new MouseEvent('click'));
123123
fixture.detectChanges();
124-
expect(component.visible).toBeTrue();
124+
expect(component.visible()).toBeTrue();
125125
dropdownRef.nativeElement.dispatchEvent(new KeyboardEvent('keyup', { key: 'Escape' }));
126126
fixture.detectChanges();
127-
expect(component.visible).toBeFalse();
128-
component.visible = true;
127+
expect(component.visible()).toBeFalse();
128+
component.visible.set(true);
129129
fixture.detectChanges();
130130
document.dispatchEvent(new KeyboardEvent('keyup', { key: 'Tab' }));
131131
fixture.detectChanges();
132-
expect(component.visible).toBeFalse();
132+
expect(component.visible()).toBeFalse();
133133
}));
134134
});

projects/coreui-angular/src/lib/form/form-check/form-check-input.directive.spec.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
import { Component, DebugElement, ElementRef, Renderer2 } from '@angular/core';
1+
import { Component, DebugElement, ElementRef, Renderer2, signal } from '@angular/core';
22
import { ComponentFixture, TestBed } from '@angular/core/testing';
33
import { By } from '@angular/platform-browser';
44
import { FormCheckInputDirective } from './form-check-input.directive';
55

66
class MockElementRef extends ElementRef {}
77

88
@Component({
9-
template: '<input cFormCheckInput [indeterminate]="indeterminate" [checked]="true">',
9+
template: '<input cFormCheckInput [indeterminate]="indeterminate()" [checked]="true">',
1010
imports: [FormCheckInputDirective]
1111
})
1212
class TestComponent {
13-
indeterminate = false;
13+
readonly indeterminate = signal(false);
1414
}
1515

1616
describe('FormCheckInputDirective', () => {
@@ -42,7 +42,7 @@ describe('FormCheckInputDirective', () => {
4242
});
4343

4444
it('should have indeterminate state', () => {
45-
component.indeterminate = true;
45+
component.indeterminate.set(true);
4646
fixture.detectChanges();
4747
expect(inputEl.nativeElement.checked).toBeFalse();
4848
expect(inputEl.nativeElement.indeterminate).toBeTrue();

0 commit comments

Comments
 (0)