Skip to content

Commit

Permalink
feat(MockBuilder): supports EnvironmentProviders help-me-mom#7011
Browse files Browse the repository at this point in the history
  • Loading branch information
satanTime committed Nov 15, 2023
1 parent ffde5dd commit b3ca1d4
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 7 deletions.
1 change: 1 addition & 0 deletions libs/ng-mocks/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export {
IMockBuilderConfigDirective,
IMockBuilderConfigModule,
IMockBuilderResult,
IMockBuilderProvider,
} from './lib/mock-builder/types';

export { MockModule } from './lib/mock-module/mock-module';
Expand Down
12 changes: 9 additions & 3 deletions libs/ng-mocks/src/lib/mock-builder/mock-builder.promise.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { NgModule, Provider } from '@angular/core';
import { NgModule } from '@angular/core';
import { TestBed, TestBedStatic, TestModuleMetadata } from '@angular/core/testing';

import CoreDefStack from '../common/core.def-stack';
Expand All @@ -23,7 +23,13 @@ import initUniverse from './promise/init-universe';
import parseMockArguments from './promise/parse-mock-arguments';
import parseProvider from './promise/parse-provider';
import { BuilderData } from './promise/types';
import { IMockBuilder, IMockBuilderConfig, IMockBuilderConfigAll, IMockBuilderResult } from './types';
import {
IMockBuilder,
IMockBuilderConfig,
IMockBuilderConfigAll,
IMockBuilderProvider,
IMockBuilderResult,
} from './types';

const normaliseModule = (
module: any,
Expand Down Expand Up @@ -162,7 +168,7 @@ export class MockBuilderPromise implements IMockBuilder {
return this;
}

public provide(def: Provider): this {
public provide(def: IMockBuilderProvider): this {
for (const provider of flatten(def)) {
const { provide, multi } = parseProvider(provider);
const existing = this.providerDef.has(provide) ? this.providerDef.get(provide) : [];
Expand Down
4 changes: 2 additions & 2 deletions libs/ng-mocks/src/lib/mock-builder/promise/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { InjectionToken, NgModule, Provider } from '@angular/core';

import { Type } from '../../common/core.types';
import { NgModuleWithProviders } from '../../common/func.is-ng-module-def-with-providers';
import { IMockBuilderConfigAll } from '../types';
import { IMockBuilderConfigAll, IMockBuilderProvider } from '../types';

export type BuilderData = {
configDef: Map<Type<any> | InjectionToken<any> | string, any>;
Expand All @@ -21,5 +21,5 @@ export type BuilderData = {
export type NgMeta = {
declarations: Array<Type<any>>;
imports: Array<Type<any> | NgModuleWithProviders>;
providers: Provider[];
providers: IMockBuilderProvider[];
};
13 changes: 12 additions & 1 deletion libs/ng-mocks/src/lib/mock-builder/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ export interface IMockBuilder extends Promise<IMockBuilderResult> {
*
* @see https://ng-mocks.sudo.eu/api/MockBuilder#provide
*/
provide(def: Provider): this;
provide(def: IMockBuilderProvider): this;

/**
* .replace() lets substitute declarations.
Expand All @@ -219,3 +219,14 @@ export interface IMockBuilder extends Promise<IMockBuilderResult> {
* @see https://ng-mocks.sudo.eu/api/MockBuilder#extending-mockbuilder
*/
export interface IMockBuilderExtended extends IMockBuilder {}

/**
* IMockBuilderProvider
*
* A special type to cover providers including EnvironmentProviders.
*/
export type IMockBuilderProvider =
| Provider
| {
ɵbrand: 'EnvironmentProviders';
};
7 changes: 6 additions & 1 deletion tests-failures/mock-builder-provide.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { InjectionToken } from '@angular/core';

import { MockBuilder } from 'ng-mocks';
import { provideHttpClient } from '@angular/common/http';

class MyService {
public readonly name = 'MyService';
Expand All @@ -15,6 +16,9 @@ const TOKEN_BOOLEAN = new InjectionToken<boolean>('TOKEN_BOOLEAN');
const TOKEN_STRING = new InjectionToken<string>('TOKEN_STRING');
const TOKEN_UNKNOWN = new InjectionToken('TOKEN_UNKNOWN');

// @see https://github.com/help-me-mom/ng-mocks/issues/7011
const ENVIRONMENT_PROVIDERS = provideHttpClient();

// Accepts classes only.
MockBuilder()
.provide(MyService)
Expand Down Expand Up @@ -44,7 +48,8 @@ MockBuilder()
provide: TOKEN_UNKNOWN,
useValue: undefined,
},
]);
])
.provide(ENVIRONMENT_PROVIDERS);

// @ts-expect-error: does not support wrong types.
MockBuilder().provide('123');
Expand Down

0 comments on commit b3ca1d4

Please sign in to comment.