-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
407 additions
and
6 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
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,11 +1,7 @@ | ||
export { | ||
makeMetadataManagerProviderFromSetterFactory, | ||
MetadataSetterFactory, | ||
MakeMetadataManagerProviderFromSetterFactoryOptions, | ||
} from './make-metadata-manager-provider-from-setter-factory' | ||
export { | ||
_injectMetadataManagers, | ||
NgxMetaMetadataManager, | ||
MetadataSetter, | ||
MetadataResolverOptions, | ||
} from './ngx-meta-metadata-manager' | ||
export * from './provider' |
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,23 @@ | ||
export { | ||
provideNgxMetaManager, | ||
withManagerOptions, | ||
withManagerDeps, | ||
withManagerGlobal, | ||
withManagerId, | ||
withManagerObjectMerging, | ||
withManagerJsonPath, | ||
_ProvideNgxMetaManagerOptions, | ||
} from './provide-ngx-meta-manager' | ||
export { | ||
_provideNgxMetaModuleManager, | ||
_ProvideNgxMetaModuleManagerOptions, | ||
_withModuleManagerNameAttribute, | ||
_withModuleManagerOptions, | ||
_withModuleManagerScope, | ||
_withModuleManagerSetterFactory, | ||
} from './provide-ngx-meta-module-manager' | ||
export { | ||
makeMetadataManagerProviderFromSetterFactory, | ||
MetadataSetterFactory, | ||
MakeMetadataManagerProviderFromSetterFactoryOptions, | ||
} from './make-metadata-manager-provider-from-setter-factory' |
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
126 changes: 126 additions & 0 deletions
126
projects/ngx-meta/src/core/src/managers/provider/provide-ngx-meta-manager.spec.ts
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,126 @@ | ||
import { injectOneMetadataManager } from '@/ngx-meta/test/inject-one-metadata-manager' | ||
import { FactoryProvider, InjectionToken, Provider } from '@angular/core' | ||
import { TestBed } from '@angular/core/testing' | ||
import { DOCUMENT } from '@angular/common' | ||
import { Router } from '@angular/router' | ||
import { MetadataSetterFactory } from './make-metadata-manager-provider-from-setter-factory' | ||
import { _isDefined } from '../../utils' | ||
import { | ||
provideNgxMetaManager, | ||
withManagerDeps, | ||
withManagerGlobal, | ||
withManagerId, | ||
withManagerObjectMerging, | ||
withManagerOptions, | ||
} from './provide-ngx-meta-manager' | ||
|
||
describe('provide manager', () => { | ||
const jsonPathAsArray = ['dummy-scope', 'dummy-key'] | ||
const jsonPath = 'dummy-scope.dummy-key' | ||
|
||
it('should provide a manager with the given JSON path', () => { | ||
const provider = makeSut({ jsonPath }) | ||
|
||
const manager = provideAndInject(provider) | ||
|
||
expect(manager.resolverOptions.jsonPath).toEqual(jsonPathAsArray) | ||
}) | ||
|
||
it('should provide a manager with JSON path as id', () => { | ||
const provider = makeSut({ jsonPath }) | ||
|
||
const manager = provideAndInject(provider) | ||
|
||
expect(manager.id).toEqual(jsonPath) | ||
}) | ||
|
||
it('should provide a manager with the given factory', () => { | ||
const setter = () => {} | ||
const provider = makeSut({ factory: () => setter }) | ||
|
||
const manager = provideAndInject(provider) | ||
|
||
expect(manager.set).toEqual(setter) | ||
}) | ||
|
||
describe('when dependencies are given', () => { | ||
const deps = [DOCUMENT, Router] satisfies FactoryProvider['deps'] | ||
|
||
it('should pass them to the factory function', () => { | ||
const factory = jasmine | ||
.createSpy<MetadataSetterFactory<unknown>>() | ||
.and.returnValue(() => {}) | ||
const provider = makeSut({ deps, factory }) | ||
|
||
provideAndInject(provider) | ||
|
||
expect(factory).toHaveBeenCalledWith( | ||
...deps.map((dep) => TestBed.inject(dep as InjectionToken<unknown>)), | ||
) | ||
}) | ||
}) | ||
|
||
describe('when global is given', () => { | ||
const global = 'global' | ||
|
||
it('should set it in the manager', () => { | ||
const provider = makeSut({ global }) | ||
|
||
const manager = provideAndInject(provider) | ||
|
||
expect(manager.resolverOptions.global).toEqual(global) | ||
}) | ||
}) | ||
|
||
describe('when an id is given', () => { | ||
const id = 'id' | ||
|
||
it('should set it in the manager', () => { | ||
const provider = makeSut({ id }) | ||
|
||
const manager = provideAndInject(provider) | ||
|
||
expect(manager.id).toEqual(id) | ||
}) | ||
}) | ||
|
||
describe('when object merging is enabled', () => { | ||
const objectMerge = true | ||
|
||
it('should set it in the manager', () => { | ||
const provider = makeSut({ objectMerge }) | ||
|
||
const manager = provideAndInject(provider) | ||
|
||
expect(manager.resolverOptions.objectMerge).toBeTrue() | ||
}) | ||
}) | ||
}) | ||
|
||
const makeSut = ( | ||
opts: { | ||
jsonPath?: string | ||
factory?: MetadataSetterFactory<unknown> | ||
deps?: FactoryProvider['deps'] | ||
global?: string | ||
id?: string | ||
objectMerge?: true | ||
} = {}, | ||
) => | ||
provideNgxMetaManager( | ||
opts.jsonPath ?? 'dummy-scope.dummy-key', | ||
opts.factory ?? (() => () => {}), | ||
withManagerOptions( | ||
...[ | ||
opts.deps ? withManagerDeps(...opts.deps) : undefined, | ||
opts.global ? withManagerGlobal(opts.global) : undefined, | ||
opts.id ? withManagerId(opts.id) : undefined, | ||
opts.objectMerge ? withManagerObjectMerging() : undefined, | ||
].filter(_isDefined), | ||
), | ||
) | ||
|
||
const provideAndInject = (provider: Provider) => { | ||
TestBed.configureTestingModule({ providers: [provider] }) | ||
return injectOneMetadataManager() | ||
} |
105 changes: 105 additions & 0 deletions
105
projects/ngx-meta/src/core/src/managers/provider/provide-ngx-meta-manager.ts
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,105 @@ | ||
import { MetadataSetterFactory } from './make-metadata-manager-provider-from-setter-factory' | ||
import { FactoryProvider } from '@angular/core' | ||
import { | ||
MetadataResolverOptions, | ||
NgxMetaMetadataManager, | ||
} from '../ngx-meta-metadata-manager' | ||
|
||
/** | ||
* @param jsonPath - | ||
* @param setterFactory - | ||
* @param options - | ||
* | ||
* @alpha | ||
*/ | ||
export const provideNgxMetaManager = <T>( | ||
jsonPath: string, | ||
setterFactory: MetadataSetterFactory<T>, | ||
options: _ProvideNgxMetaManagerOptions = {}, | ||
): FactoryProvider => ({ | ||
provide: NgxMetaMetadataManager, | ||
multi: true, | ||
useFactory: (...deps: ReadonlyArray<unknown>) => | ||
({ | ||
id: options.id ?? jsonPath, | ||
set: setterFactory(...deps), | ||
resolverOptions: { | ||
jsonPath: jsonPath.split('.'), | ||
global: options.global, | ||
objectMerge: options.objectMerge, | ||
}, | ||
}) satisfies NgxMetaMetadataManager<T>, | ||
deps: options.deps, | ||
}) | ||
|
||
/** | ||
* @internal | ||
*/ | ||
export type _ProvideNgxMetaManagerOptions = Partial<{ | ||
deps: FactoryProvider['deps'] | ||
global: MetadataResolverOptions['global'] | ||
id: NgxMetaMetadataManager['id'] | ||
objectMerge: MetadataResolverOptions['objectMerge'] | ||
}> | ||
|
||
/** | ||
* | ||
* @param options - | ||
* | ||
* @alpha | ||
*/ | ||
export const withManagerOptions = ( | ||
...options: ReadonlyArray<_ProvideNgxMetaManagerOptions> | ||
): _ProvideNgxMetaManagerOptions => | ||
options.reduce<Partial<_ProvideNgxMetaManagerOptions>>( | ||
(acc, curr) => ({ ...acc, ...curr }), | ||
{}, | ||
) | ||
|
||
/** | ||
* | ||
* @param deps - | ||
* | ||
* @alpha | ||
*/ | ||
export const withManagerDeps = ( | ||
...deps: Exclude<FactoryProvider['deps'], undefined> | ||
): Partial<_ProvideNgxMetaManagerOptions> => ({ | ||
deps, | ||
}) | ||
|
||
/** | ||
* | ||
* @param global - | ||
* | ||
* @alpha | ||
*/ | ||
export const withManagerGlobal = ( | ||
global: string, | ||
): Partial<_ProvideNgxMetaManagerOptions> => ({ global }) | ||
|
||
/** | ||
* | ||
* @param id - | ||
* | ||
* @alpha | ||
*/ | ||
export const withManagerId = ( | ||
id: string, | ||
): Partial<_ProvideNgxMetaManagerOptions> => ({ id }) | ||
|
||
/** | ||
* @alpha | ||
*/ | ||
export const withManagerObjectMerging = | ||
(): Partial<_ProvideNgxMetaManagerOptions> => ({ objectMerge: true }) | ||
|
||
/** | ||
* | ||
* @param jsonPath - | ||
* | ||
* @alpha | ||
*/ | ||
export const withManagerJsonPath = ( | ||
...jsonPath: MetadataResolverOptions['jsonPath'] | ||
): string => jsonPath.join('.') | ||
Oops, something went wrong.