-
Notifications
You must be signed in to change notification settings - Fork 153
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat: Allow Configuring the testing module to not throw errors when a…
…dding icons The FontAwesomeTestingModule is now configurable via a `forRoot` method. The configuration object passed to it configures how the mock icon library should behave when adding icons to it: ignore them, warn about them or throw an error. Fix #440 #440
- Loading branch information
1 parent
0548d47
commit 8351ccb
Showing
5 changed files
with
293 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { InjectionToken } from "@angular/core" | ||
|
||
/** | ||
* The configuration options that can be passed to the FontAwesomeTestingModule. | ||
*/ | ||
// This interface is user-facing and will be converted to the | ||
// _FontAwesomeTestingModuleInternalConfig for internal use by the module. | ||
export interface FontAwesomeTestingModuleConfig extends Partial<_FontAwesomeTestingModuleInternalConfig> { } | ||
|
||
/** | ||
* The internal configuration for the FontAwesomeTestingModule. | ||
* This interface is private. Conforming objects should be constructed by the | ||
* _getFontAwesomeTestingModuleInternalConfig() function. | ||
*/ | ||
export interface _FontAwesomeTestingModuleInternalConfig { | ||
/** | ||
* What to do when `addIcons()` is invoked on an IconLibrary provided by the FontAwesomeTestingModule. | ||
* | ||
* Possible values are: | ||
* - `'logWarning'`: Writes a warning to the console. | ||
* - `'throwError'`: Throws an error | ||
* - `'noop'`: Does nothing | ||
* | ||
* Note that in any case the icon will not be added to the library. | ||
*/ | ||
whenAddingIcons: 'logWarning' | 'throwError' | 'noop' | ||
} | ||
|
||
export const FontAwesomeTestingModuleConfigInjectionToken = new InjectionToken<_FontAwesomeTestingModuleInternalConfig>('FontAwesomeTestingModuleInternalConfig') | ||
|
||
/** | ||
* The default values used for configuration if the user passes no configuration, | ||
* or an incomplete one. | ||
*/ | ||
const DEFAULT_CONFIG = Object.freeze({ | ||
/** The default value maintains compatibility with versions <= 0.14.1 */ | ||
whenAddingIcons: 'throwError' | ||
}) | ||
|
||
export function _getFontAwesomeTestingModuleInternalConfig(publicConfig: FontAwesomeTestingModuleConfig = {}): _FontAwesomeTestingModuleInternalConfig { | ||
return { | ||
whenAddingIcons: publicConfig.whenAddingIcons ?? DEFAULT_CONFIG.whenAddingIcons | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export { FontAwesomeTestingModule } from './testing.module'; | ||
export { MockFaIconLibrary } from './icon/mock-icon-library.service' | ||
export { MockFaIconLibrary } from './icon/mock-icon-library.service' | ||
export { FontAwesomeTestingModuleConfig } from './TestingModuleConfig' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,34 @@ | ||
import { NgModule } from '@angular/core'; | ||
import { ModuleWithProviders, NgModule } from '@angular/core'; | ||
import { FaIconLibrary, FontAwesomeModule } from '@fortawesome/angular-fontawesome'; | ||
import { MockFaIconLibrary } from './icon/mock-icon-library.service'; | ||
import { | ||
FontAwesomeTestingModuleConfig, | ||
FontAwesomeTestingModuleConfigInjectionToken, | ||
_getFontAwesomeTestingModuleInternalConfig, | ||
} from './TestingModuleConfig'; | ||
|
||
@NgModule({ | ||
exports: [FontAwesomeModule], | ||
providers: [{ provide: FaIconLibrary, useExisting: MockFaIconLibrary }], | ||
}) | ||
export class FontAwesomeTestingModule {} | ||
export class FontAwesomeTestingModule { | ||
/** | ||
* Use this method to configure the module’s behaviour when trying to add icons | ||
* and icon packs to the mock icon library. | ||
*/ | ||
public static forRoot(config: FontAwesomeTestingModuleConfig = {}): ModuleWithProviders<FontAwesomeTestingModule> { | ||
return { | ||
ngModule: FontAwesomeTestingModule, | ||
providers: [ | ||
{ | ||
provide: FaIconLibrary, | ||
useExisting: MockFaIconLibrary, | ||
}, | ||
{ | ||
provide: FontAwesomeTestingModuleConfigInjectionToken, | ||
useValue: _getFontAwesomeTestingModuleInternalConfig(config), | ||
} | ||
] | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters