Skip to content

Commit 204c473

Browse files
Reduce boilerplate in tests using direct component creation with bindings (#478)
1 parent 993769e commit 204c473

File tree

12 files changed

+2695
-735
lines changed

12 files changed

+2695
-735
lines changed

package.json

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,21 +30,21 @@
3030
"homepage": "https://github.com/FortAwesome/angular-fontawesome",
3131
"devDependencies": {
3232
"@angular-devkit/build-angular": "patch:@angular-devkit/build-angular@npm%3A20.0.0#~/.yarn/patches/@angular-devkit-build-angular-npm-19.0.0-131974ef98.patch",
33-
"@angular-devkit/core": "^20.0.0",
34-
"@angular-devkit/schematics": "^20.0.0",
35-
"@angular/animations": "^20.0.0",
36-
"@angular/build": "^20.0.0",
37-
"@angular/cli": "^20.0.0",
38-
"@angular/common": "^20.0.0",
39-
"@angular/compiler": "^20.0.0",
40-
"@angular/compiler-cli": "^20.0.0",
41-
"@angular/core": "^20.0.0",
42-
"@angular/language-service": "^20.0.0",
43-
"@angular/platform-browser": "^20.0.0",
44-
"@angular/platform-browser-dynamic": "^20.0.0",
45-
"@angular/platform-server": "^20.0.0",
46-
"@angular/router": "^20.0.0",
47-
"@angular/ssr": "^20.0.0",
33+
"@angular-devkit/core": "^20.3.1",
34+
"@angular-devkit/schematics": "^20.3.1",
35+
"@angular/animations": "^20.3.0",
36+
"@angular/build": "^20.3.1",
37+
"@angular/cli": "^20.3.1",
38+
"@angular/common": "^20.3.0",
39+
"@angular/compiler": "^20.3.0",
40+
"@angular/compiler-cli": "^20.3.0",
41+
"@angular/core": "^20.3.0",
42+
"@angular/language-service": "^20.3.0",
43+
"@angular/platform-browser": "^20.3.0",
44+
"@angular/platform-browser-dynamic": "^20.3.0",
45+
"@angular/platform-server": "^20.3.0",
46+
"@angular/router": "^20.3.0",
47+
"@angular/ssr": "^20.3.1",
4848
"@fortawesome/free-regular-svg-icons": "^7.0.0",
4949
"@fortawesome/free-solid-svg-icons": "^7.0.0",
5050
"@types/express": "^4.17.21",

projects/demo/src/main.server.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { bootstrapApplication } from '@angular/platform-browser';
1+
import { bootstrapApplication, BootstrapContext } from '@angular/platform-browser';
22
import { AppComponent } from './app/app.component';
33
import { config } from './app/app.config.server';
44

5-
const bootstrap = () => bootstrapApplication(AppComponent, config);
5+
const bootstrap = (context: BootstrapContext) => bootstrapApplication(AppComponent, config, context);
66

77
export default bootstrap;

projects/schematics/src/ng-add/index.spec.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { angularFontawesomeVersion, iconPackVersionMap } from './versions';
77
const collectionPath = path.join(__dirname, '../collection.json');
88

99
describe('ng-add', () => {
10-
it('adds v6 dependencies to package.json', async () => {
10+
it('adds v7 dependencies to package.json', async () => {
1111
const { runner, appTree } = await setup();
1212

1313
const tree = await runner.runSchematic<Schema>('ng-add', { project: 'test-app' }, appTree);
@@ -17,6 +17,20 @@ describe('ng-add', () => {
1717

1818
const dependencies = packageJson.dependencies;
1919

20+
expect(dependencies['@fortawesome/free-solid-svg-icons']).toBe(iconPackVersionMap['7'].iconPackVersion);
21+
expect(dependencies['@fortawesome/angular-fontawesome']).toBe(angularFontawesomeVersion);
22+
});
23+
24+
it('adds v6 dependencies to package.json', async () => {
25+
const { runner, appTree } = await setup();
26+
27+
const tree = await runner.runSchematic<Schema>('ng-add', { project: 'test-app', version: '6' }, appTree);
28+
29+
const packageJson = JSON.parse(tree.readContent('package.json'));
30+
expect(packageJson.dependencies).toBeDefined();
31+
32+
const dependencies = packageJson.dependencies;
33+
2034
expect(dependencies['@fortawesome/free-solid-svg-icons']).toBe(iconPackVersionMap['6'].iconPackVersion);
2135
expect(dependencies['@fortawesome/angular-fontawesome']).toBe(angularFontawesomeVersion);
2236
});

src/lib/icon/duotone-icon.component.spec.ts

Lines changed: 23 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,110 +1,60 @@
1-
import { Component, signal, viewChild, ViewContainerRef } from '@angular/core';
1+
import { Component, inputBinding, viewChild, ViewContainerRef } from '@angular/core';
2+
import { TestBed } from '@angular/core/testing';
23
import { faUser } from '@fortawesome/free-solid-svg-icons';
3-
import { faDummy, initTest, queryByCss } from '../../testing/helpers';
4+
import { faDummy, queryByCss } from '../../testing/helpers';
45
import { FaDuotoneIconComponent } from './duotone-icon.component';
56

67
describe('FaDuotoneIconComponent', () => {
78
it('should render the duotone icon', () => {
8-
@Component({
9-
selector: 'fa-host',
10-
standalone: false,
11-
template: '<fa-duotone-icon [icon]="faDummy()" />',
12-
})
13-
class HostComponent {
14-
faDummy = signal(faDummy);
15-
}
16-
17-
const fixture = initTest(HostComponent);
9+
const fixture = TestBed.createComponent(FaDuotoneIconComponent, {
10+
bindings: [inputBinding('icon', () => faDummy)],
11+
});
1812
fixture.detectChanges();
1913
expect(queryByCss(fixture, 'svg')).toBeTruthy();
2014
});
2115

2216
it('should allow to swap opacity of the layers', () => {
23-
@Component({
24-
selector: 'fa-host',
25-
standalone: false,
26-
template: '<fa-duotone-icon [icon]="faDummy()" [swapOpacity]="true" />',
27-
})
28-
class HostComponent {
29-
faDummy = signal(faDummy);
30-
}
31-
32-
const fixture = initTest(HostComponent);
17+
const fixture = TestBed.createComponent(FaDuotoneIconComponent, {
18+
bindings: [inputBinding('icon', () => faDummy), inputBinding('swapOpacity', () => true)],
19+
});
3320
fixture.detectChanges();
3421
expect(queryByCss(fixture, 'svg').classList.contains('fa-swap-opacity')).toBeTruthy();
3522
});
3623

3724
it('should allow to customize opacity of the primary layer', () => {
38-
@Component({
39-
selector: 'fa-host',
40-
standalone: false,
41-
template: '<fa-duotone-icon [icon]="faDummy()" [primaryOpacity]="0.1" />',
42-
})
43-
class HostComponent {
44-
faDummy = signal(faDummy);
45-
}
46-
47-
const fixture = initTest(HostComponent);
25+
const fixture = TestBed.createComponent(FaDuotoneIconComponent, {
26+
bindings: [inputBinding('icon', () => faDummy), inputBinding('primaryOpacity', () => 0.1)],
27+
});
4828
fixture.detectChanges();
4929
expect(queryByCss(fixture, 'svg').style.getPropertyValue('--fa-primary-opacity')).toBe('0.1');
5030
});
5131

5232
it('should allow to customize opacity of the secondary layer', () => {
53-
@Component({
54-
selector: 'fa-host',
55-
standalone: false,
56-
template: '<fa-duotone-icon [icon]="faDummy()" [secondaryOpacity]="0.9" />',
57-
})
58-
class HostComponent {
59-
faDummy = signal(faDummy);
60-
}
61-
62-
const fixture = initTest(HostComponent);
33+
const fixture = TestBed.createComponent(FaDuotoneIconComponent, {
34+
bindings: [inputBinding('icon', () => faDummy), inputBinding('secondaryOpacity', () => 0.9)],
35+
});
6336
fixture.detectChanges();
6437
expect(queryByCss(fixture, 'svg').style.getPropertyValue('--fa-secondary-opacity')).toBe('0.9');
6538
});
6639

6740
it('should allow to customize color of the primary layer', () => {
68-
@Component({
69-
selector: 'fa-host',
70-
standalone: false,
71-
template: '<fa-duotone-icon [icon]="faDummy()" primaryColor="red" />',
72-
})
73-
class HostComponent {
74-
faDummy = signal(faDummy);
75-
}
76-
77-
const fixture = initTest(HostComponent);
41+
const fixture = TestBed.createComponent(FaDuotoneIconComponent, {
42+
bindings: [inputBinding('icon', () => faDummy), inputBinding('primaryColor', () => 'red')],
43+
});
7844
fixture.detectChanges();
7945
expect(queryByCss(fixture, 'svg').style.getPropertyValue('--fa-primary-color')).toBe('red');
8046
});
8147

8248
it('should allow to customize color of the secondary layer', () => {
83-
@Component({
84-
selector: 'fa-host',
85-
standalone: false,
86-
template: '<fa-duotone-icon [icon]="faDummy()" secondaryColor="red" />',
87-
})
88-
class HostComponent {
89-
faDummy = signal(faDummy);
90-
}
91-
92-
const fixture = initTest(HostComponent);
49+
const fixture = TestBed.createComponent(FaDuotoneIconComponent, {
50+
bindings: [inputBinding('icon', () => faDummy), inputBinding('secondaryColor', () => 'red')],
51+
});
9352
fixture.detectChanges();
9453
expect(queryByCss(fixture, 'svg').style.getPropertyValue('--fa-secondary-color')).toBe('red');
9554
});
9655

9756
it('should throw if specified icon is not a Duotone icon', () => {
98-
@Component({
99-
selector: 'fa-host',
100-
standalone: false,
101-
template: '<fa-duotone-icon [icon]="faUser()" />',
102-
})
103-
class HostComponent {
104-
faUser = signal(faUser);
105-
}
106-
107-
const fixture = initTest(HostComponent);
57+
const fixture = TestBed.createComponent(FaDuotoneIconComponent, { bindings: [inputBinding('icon', () => faUser)] });
10858
expect(() => fixture.detectChanges()).toThrow(
10959
new Error(
11060
'The specified icon does not appear to be a Duotone icon. ' +
@@ -117,7 +67,6 @@ describe('FaDuotoneIconComponent', () => {
11767
it('should be able to create component dynamically', () => {
11868
@Component({
11969
selector: 'fa-host',
120-
standalone: false,
12170
template: '<ng-container #host></ng-container>',
12271
})
12372
class HostComponent {
@@ -129,7 +78,7 @@ describe('FaDuotoneIconComponent', () => {
12978
}
13079
}
13180

132-
const fixture = initTest(HostComponent);
81+
const fixture = TestBed.createComponent(HostComponent);
13382
fixture.detectChanges();
13483
expect(queryByCss(fixture, 'svg')).toBeFalsy();
13584

0 commit comments

Comments
 (0)