Skip to content

Commit

Permalink
Simplify testing configuration implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
devoto13 committed May 7, 2024
1 parent aaf9bb9 commit 782f3e5
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 133 deletions.
7 changes: 2 additions & 5 deletions angular.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,7 @@
"test": {
"builder": "@angular-devkit/build-angular:karma",
"options": {
"include": [
"src/**/*.spec.ts",
"testing/**/*.spec.ts"
],
"include": ["src/**/*.spec.ts", "testing/src/**/*.spec.ts"],
"polyfills": ["zone.js", "zone.js/testing"],
"tsConfig": "tsconfig.spec.json",
"karmaConfig": "karma.conf.js"
Expand All @@ -38,7 +35,7 @@
"lint": {
"builder": "@angular-eslint/builder:lint",
"options": {
"lintFilePatterns": ["src/**/*.ts", "src/**/*.html"]
"lintFilePatterns": ["src/**/*.ts", "src/**/*.html", "testing/src/**/*.ts", "testing/src/**/*.html"]
}
}
}
Expand Down
44 changes: 0 additions & 44 deletions testing/src/TestingModuleConfig.ts

This file was deleted.

21 changes: 21 additions & 0 deletions testing/src/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Injectable } from '@angular/core';

@Injectable({
providedIn: 'root',
})
export class FaTestingConfig {
/**
* What to do when `addIcons()` or `addIconPacks()` is invoked on
* the FaIconLibrary provided by the FontAwesomeTestingModule.
*
* Possible values are:
* - `'throwError'` - Throw an error.
* - `'logWarning'` - Write a warning to the console.
* - `'noop'` - Do nothing.
*
* Note that in any case the icon will not be added to the library.
*
* @default 'throwError'
*/
whenAddingIcons: 'throwError' | 'logWarning' | 'noop' = 'throwError';
}
27 changes: 5 additions & 22 deletions testing/src/icon/mock-icon-library.service.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import { Inject, Injectable, Optional } from '@angular/core';
import { Injectable } from '@angular/core';
import { FaIconLibraryInterface, IconDefinition, IconName, IconPrefix } from '@fortawesome/angular-fontawesome';
import {
FontAwesomeTestingModuleConfigInjectionToken,
_FontAwesomeTestingModuleInternalConfig,
_getFontAwesomeTestingModuleInternalConfig
} from '../TestingModuleConfig';
import { FaTestingConfig } from '../config';

export const dummyIcon: IconDefinition = {
prefix: 'fad',
Expand All @@ -18,27 +14,14 @@ export const ADD_ICON_MESSAGE = 'Attempt to add an icon to the MockFaIconLibrary
providedIn: 'root',
})
export class MockFaIconLibrary implements FaIconLibraryInterface {
constructor(private config: FaTestingConfig) {}

private config: _FontAwesomeTestingModuleInternalConfig

// The configuration object is optional in order to maintain backwards compatibility with versions <= 0.14.1.
// If the module is unconfigured (that is, FontAwesomeTestingModule.forRoot() has never been called),
// then the dependency injection will provide `null`.
//
// We could alternatively provide a default configuration in the `providers` array of the `NgModule` decorator,
// but that would break the use case of injecting a service directly without providing a configuration,
// as is done in testing/src/icon/mock-icon-library.service.spec.ts
constructor(
@Inject(FontAwesomeTestingModuleConfigInjectionToken) @Optional() config: _FontAwesomeTestingModuleInternalConfig
) {
this.config = config ?? _getFontAwesomeTestingModuleInternalConfig()
}
addIcons() {
if (this.config.whenAddingIcons === 'throwError') {
throw new Error(ADD_ICON_MESSAGE);
}
if (this.config.whenAddingIcons === 'logWarning') {
console.warn(ADD_ICON_MESSAGE)
console.warn(ADD_ICON_MESSAGE);
}
}

Expand All @@ -47,7 +30,7 @@ export class MockFaIconLibrary implements FaIconLibraryInterface {
throw new Error(ADD_ICON_MESSAGE);
}
if (this.config.whenAddingIcons === 'logWarning') {
console.warn(ADD_ICON_MESSAGE)
console.warn(ADD_ICON_MESSAGE);
}
}

Expand Down
4 changes: 2 additions & 2 deletions testing/src/public_api.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export { FontAwesomeTestingModule } from './testing.module';
export { MockFaIconLibrary } from './icon/mock-icon-library.service'
export { FontAwesomeTestingModuleConfig } from './TestingModuleConfig'
export { MockFaIconLibrary } from './icon/mock-icon-library.service';
export { FaTestingConfig } from './config';
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { Component } from '@angular/core';
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { FaIconLibrary } from '@fortawesome/angular-fontawesome';
import { faUser } from '@fortawesome/free-solid-svg-icons';
import { ADD_ICON_MESSAGE } from 'testing/src/icon/mock-icon-library.service';
import { FontAwesomeTestingModule } from 'testing/src/public_api';
import { ADD_ICON_MESSAGE } from './icon/mock-icon-library.service';
import { FontAwesomeTestingModule } from './testing.module';

@Component({
selector: 'fa-host',
Expand All @@ -12,75 +12,73 @@ import { FontAwesomeTestingModule } from 'testing/src/public_api';
class HostComponent {}

describe('Using the `FontAwesomeTestingModule', () => {

describe('Providing no configuration', () => {
// This describe block asserts that the behaviour of versions <= 0.14.1 is maintained

let component: HostComponent;
let fixture: ComponentFixture<HostComponent>;

beforeEach(() => {
TestBed.configureTestingModule({
imports: [FontAwesomeTestingModule],
declarations: [HostComponent],
});

fixture = TestBed.createComponent(HostComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});

it('should allow you to import the module without errors', () => {
expect(component).toBeTruthy();
});

it('should throw on attempt to add an icon to the mocked icon library', () => {
const service = TestBed.inject(FaIconLibrary);
expect(() => service.addIcons(faUser)).toThrow(new Error(ADD_ICON_MESSAGE));
});

it('should throw on attempt to add an icon pack to the mocked icon library', () => {
const service = TestBed.inject(FaIconLibrary);
expect(() => service.addIcons(faUser)).toThrow(new Error(ADD_ICON_MESSAGE));
});
})
});

describe('Providing an empty configuration object', () => {
// This describe block asserts that a partial configuration object
// is correctly filled up to the ‘full’ internal object.
// The used configuration should mimick the default values for ‘no configuration’.
// The used configuration should mimic the default values for ‘no configuration’.

let component: HostComponent;
let fixture: ComponentFixture<HostComponent>;

beforeEach(() => {
TestBed.configureTestingModule({
imports: [FontAwesomeTestingModule.forRoot({})],
declarations: [HostComponent],
});
});

beforeEach(() => {
fixture = TestBed.createComponent(HostComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});

it('should allow you to import the module without errors', () => {
expect(component).toBeTruthy();
});

it('should throw on attempt to add an icon to the mocked icon library', () => {
const service = TestBed.inject(FaIconLibrary);
expect(() => service.addIcons(faUser)).toThrow(new Error(ADD_ICON_MESSAGE));
});

it('should throw on attempt to add an icon pack to the mocked icon library', () => {
const service = TestBed.inject(FaIconLibrary);
expect(() => service.addIcons(faUser)).toThrow(new Error(ADD_ICON_MESSAGE));
});
})

});

describe('Providing {addIcons: "throwError"}', () => {
// This describe block asserts that feature request
Expand All @@ -89,35 +87,34 @@ describe('Using the `FontAwesomeTestingModule', () => {

let component: HostComponent;
let fixture: ComponentFixture<HostComponent>;

beforeEach(() => {
TestBed.configureTestingModule({
imports: [FontAwesomeTestingModule.forRoot({whenAddingIcons: 'throwError'})],
imports: [FontAwesomeTestingModule.forRoot({ whenAddingIcons: 'throwError' })],
declarations: [HostComponent],
});
});

beforeEach(() => {
fixture = TestBed.createComponent(HostComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});

it('should allow you to import the module without errors', () => {
expect(component).toBeTruthy();
});

it('should throw on attempt to add an icon to the mocked icon library', () => {
const service = TestBed.inject(FaIconLibrary);
expect(() => service.addIcons(faUser)).toThrow(new Error('Attempt to add an icon to the MockFaIconLibrary.'));
});

it('should throw on attempt to add an icon pack to the mocked icon library', () => {
const service = TestBed.inject(FaIconLibrary);
expect(() => service.addIcons(faUser)).toThrow(new Error('Attempt to add an icon to the MockFaIconLibrary.'));
});
})

});

describe('Providing {addIcons: "logWarning"}', () => {
// This describe block asserts that feature request
Expand All @@ -126,40 +123,38 @@ describe('Using the `FontAwesomeTestingModule', () => {

let component: HostComponent;
let fixture: ComponentFixture<HostComponent>;

beforeEach(() => {
TestBed.configureTestingModule({
imports: [FontAwesomeTestingModule.forRoot({whenAddingIcons: 'logWarning'})],
imports: [FontAwesomeTestingModule.forRoot({ whenAddingIcons: 'logWarning' })],
declarations: [HostComponent],
});
});

beforeEach(() => {
fixture = TestBed.createComponent(HostComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});

it('should allow you to import the module without errors', () => {
expect(component).toBeTruthy();
});

it('should call console.warn on attempt to add an icon to the mocked icon library', () => {
const service = TestBed.inject(FaIconLibrary);
spyOn(console, 'warn')
spyOn(console, 'warn');
expect(() => service.addIcons(faUser)).not.toThrow();
expect(console.warn).toHaveBeenCalledOnceWith(ADD_ICON_MESSAGE)
expect(console.warn).toHaveBeenCalledOnceWith(ADD_ICON_MESSAGE);
});

it('should call console.warn on attempt to add an icon pack to the mocked icon library', () => {
const service = TestBed.inject(FaIconLibrary);
spyOn(console, 'warn')
spyOn(console, 'warn');
expect(() => service.addIcons(faUser)).not.toThrow();
expect(console.warn).toHaveBeenCalledOnceWith(ADD_ICON_MESSAGE)
expect(console.warn).toHaveBeenCalledOnceWith(ADD_ICON_MESSAGE);
});
})


});

describe('Providing {addIcons: "noop"}', () => {
// This describe block asserts that feature request
Expand All @@ -168,36 +163,36 @@ describe('Using the `FontAwesomeTestingModule', () => {

let component: HostComponent;
let fixture: ComponentFixture<HostComponent>;

beforeEach(() => {
TestBed.configureTestingModule({
imports: [FontAwesomeTestingModule.forRoot({whenAddingIcons: 'noop'})],
imports: [FontAwesomeTestingModule.forRoot({ whenAddingIcons: 'noop' })],
declarations: [HostComponent],
});
});

beforeEach(() => {
fixture = TestBed.createComponent(HostComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});

it('should allow you to import the module without errors', () => {
expect(component).toBeTruthy();
});

it('should ignore attempts to add an icon to the mocked icon library', () => {
const service = TestBed.inject(FaIconLibrary);
spyOn(console, 'warn')
spyOn(console, 'warn');
expect(() => service.addIcons(faUser)).not.toThrow();
expect(console.warn).not.toHaveBeenCalledOnceWith(ADD_ICON_MESSAGE)
expect(console.warn).not.toHaveBeenCalledOnceWith(ADD_ICON_MESSAGE);
});

it('should ignore attempts to add an icon pack to the mocked icon library', () => {
const service = TestBed.inject(FaIconLibrary);
spyOn(console, 'warn')
spyOn(console, 'warn');
expect(() => service.addIcons(faUser)).not.toThrow();
expect(console.warn).not.toHaveBeenCalledOnceWith(ADD_ICON_MESSAGE)
expect(console.warn).not.toHaveBeenCalledOnceWith(ADD_ICON_MESSAGE);
});
})
});
});
Loading

0 comments on commit 782f3e5

Please sign in to comment.