Skip to content

Commit

Permalink
fix(ngMocks.faster): support of angular 14.2.0 #3466
Browse files Browse the repository at this point in the history
  • Loading branch information
satanTime committed Sep 5, 2022
1 parent b1922df commit 5f893bb
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 28 deletions.
1 change: 1 addition & 0 deletions libs/ng-mocks/src/lib/common/core.helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import funcGetName from './func.get-name';

/**
* It will be removed from public interface with the next release: A14
* Use ngMocks.get(token) instead.
*
* @deprecated
* @internal
Expand Down
11 changes: 7 additions & 4 deletions libs/ng-mocks/src/lib/common/ng-mocks-global-overrides.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,10 @@ const applyPlatformOverrides = (testBed: TestBed, touches: Set<any>) => {
};

const configureTestingModule =
(original: TestBedStatic['configureTestingModule']): TestBedStatic['configureTestingModule'] =>
(
original: TestBedStatic['configureTestingModule'],
instance: TestBedStatic,
): TestBedStatic['configureTestingModule'] =>
(moduleDef: TestModuleMetadata) => {
initTestBed();

Expand All @@ -202,18 +205,18 @@ const configureTestingModule =
applyPlatformOverrides(testBed, touches);
}

return original.call(TestBed, moduleDef);
return original.call(instance, moduleDef);
};

const resetTestingModule =
(original: TestBedStatic['resetTestingModule']): TestBedStatic['resetTestingModule'] =>
(original: TestBedStatic['resetTestingModule'], instance: TestBedStatic): TestBedStatic['resetTestingModule'] =>
() => {
ngMocksUniverse.global.delete('builder:config');
ngMocksUniverse.global.delete('builder:module');
(TestBed as any).ngMocksSelectors = undefined;
applyNgMocksOverrides(TestBed);

return original.call(TestBed);
return original.call(instance);
};

const viewContainerInstall = () => {
Expand Down
20 changes: 13 additions & 7 deletions libs/ng-mocks/src/lib/mock-builder/mock-builder.promise.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import {
PipeTransform,
} from '@angular/core';

import { getTestBedInjection } from '../common/core.helpers';
import mockHelperConsoleThrow from '../mock-helper/mock-helper.console-throw';
import mockHelperGet from '../mock-helper/mock-helper.get';

import { MockBuilder } from './mock-builder';

Expand Down Expand Up @@ -50,24 +50,28 @@ describe('MockBuilderPromise', () => {

it('skips dependencies in kept providers', async () => {
await MockBuilder().keep(TargetService, { dependency: true });
expect(getTestBedInjection(TargetService)).toBeFalsy();
expect(() => mockHelperGet(TargetService)).toThrowError(
/Cannot find an instance/,
);
});

it('adds non dependencies in kept providers', async () => {
await MockBuilder().keep(TargetService);
expect(getTestBedInjection(TargetService)).toBeTruthy();
expect(mockHelperGet(TargetService)).toBeTruthy();
});

it('skips dependencies in mock providers', async () => {
await MockBuilder().mock(TargetService, TargetService, {
dependency: true,
});
expect(getTestBedInjection(TargetService)).toBeFalsy();
expect(() => mockHelperGet(TargetService)).toThrowError(
/Cannot find an instance/,
);
});

it('adds non dependencies in mock providers', async () => {
await MockBuilder().mock(TargetService);
expect(getTestBedInjection(TargetService)).toBeTruthy();
expect(mockHelperGet(TargetService)).toBeTruthy();
});

it('respects several kept overloads', async () => {
Expand All @@ -92,7 +96,7 @@ describe('MockBuilderPromise', () => {
},
],
});
expect(getTestBedInjection(TARGET_TOKEN)).toEqual([1, 2]);
expect(mockHelperGet(TARGET_TOKEN)).toEqual([1, 2]);
});

it('respects several mock overloads', async () => {
Expand All @@ -117,7 +121,9 @@ describe('MockBuilderPromise', () => {
},
],
});
expect(getTestBedInjection(TARGET_TOKEN)).toBeUndefined();
expect(() => mockHelperGet(TARGET_TOKEN)).toThrowError(
/Cannot find an instance/,
);
});

it('throws an error on a services replacement', () => {
Expand Down
2 changes: 1 addition & 1 deletion libs/ng-mocks/src/lib/mock-builder/mock-builder.promise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ export class MockBuilderPromise implements IMockBuilder {
reject?: ((reason: any) => PromiseLike<any>) | undefined | null,
): Promise<TResult1> {
const promise = new Promise((resolve: (value: IMockBuilderResult) => void): void => {
const testBed = TestBed.configureTestingModule(this.build());
const testBed: TestBedStatic = TestBed.configureTestingModule(this.build()) as never;
for (const callback of mapValues(this.beforeCC)) {
callback(testBed);
}
Expand Down
62 changes: 50 additions & 12 deletions libs/ng-mocks/src/lib/mock-helper/mock-helper.faster-install.ts
Original file line number Diff line number Diff line change
@@ -1,56 +1,94 @@
import { TestBed, TestBedStatic, TestModuleMetadata } from '@angular/core/testing';
import { getTestBed, TestBed, TestBedStatic, TestModuleMetadata } from '@angular/core/testing';

import coreDefineProperty from '../common/core.define-property';
import ngMocksUniverse from '../common/ng-mocks-universe';

const hooks: {
after: Array<(original: TestBedStatic['resetTestingModule']) => TestBedStatic['resetTestingModule']>;
before: Array<(original: TestBedStatic['configureTestingModule']) => TestBedStatic['configureTestingModule']>;
after: Array<
(original: TestBedStatic['resetTestingModule'], instance: TestBedStatic) => TestBedStatic['resetTestingModule']
>;
before: Array<
(
original: TestBedStatic['configureTestingModule'],
instance: TestBedStatic,
) => TestBedStatic['configureTestingModule']
>;
} = ngMocksUniverse.global.get('faster-hooks') || {
after: [],
before: [],
};
ngMocksUniverse.global.set('faster-hooks', hooks);

const configureTestingModule =
(original: TestBedStatic['configureTestingModule']): TestBedStatic['configureTestingModule'] =>
(
original: TestBedStatic['configureTestingModule'],
instance: TestBedStatic,
): TestBedStatic['configureTestingModule'] =>
(moduleDef: TestModuleMetadata) => {
if ((TestBed as any).ngMocksFasterLock) {
return original.call(instance, moduleDef);
}

ngMocksUniverse.global.set('bullet:customized', true);

let final = original;
for (const callback of hooks.before) {
final = callback(final);
final = callback(final, instance);
}

return final.call(TestBed, moduleDef);
try {
coreDefineProperty(TestBed, 'ngMocksFasterLock', true);

return final.call(instance, moduleDef);
} finally {
coreDefineProperty(TestBed, 'ngMocksFasterLock', undefined);
}
};

const resetTestingModule =
(original: TestBedStatic['resetTestingModule']): TestBedStatic['resetTestingModule'] =>
(original: TestBedStatic['resetTestingModule'], instance: TestBedStatic): TestBedStatic['resetTestingModule'] =>
() => {
if ((TestBed as any).ngMocksFasterLock) {
return original.call(instance);
}

if (ngMocksUniverse.global.has('bullet')) {
if (ngMocksUniverse.global.has('bullet:customized')) {
ngMocksUniverse.global.set('bullet:reset', true);
}

return TestBed;
return instance;
}
ngMocksUniverse.global.delete('bullet:customized');
ngMocksUniverse.global.delete('bullet:reset');

let final = original;
for (const callback of hooks.after) {
final = callback(final);
final = callback(final, instance);
}

return final.call(TestBed);
try {
coreDefineProperty(TestBed, 'ngMocksFasterLock', true);

return final.call(instance);
} finally {
coreDefineProperty(TestBed, 'ngMocksFasterLock', undefined);
}
};

export default () => {
if (!(TestBed as any).ngMocksFasterInstalled) {
TestBed.configureTestingModule = configureTestingModule(TestBed.configureTestingModule);
TestBed.resetTestingModule = resetTestingModule(TestBed.resetTestingModule);
TestBed.configureTestingModule = configureTestingModule(TestBed.configureTestingModule as never, TestBed as never);
TestBed.resetTestingModule = resetTestingModule(TestBed.resetTestingModule as never, TestBed as never);
coreDefineProperty(TestBed, 'ngMocksFasterInstalled', true);
}

const testBed = getTestBed();
if (!(testBed as any).ngMocksFasterInstalled) {
testBed.configureTestingModule = configureTestingModule(testBed.configureTestingModule as never, testBed as never);
testBed.resetTestingModule = resetTestingModule(testBed.resetTestingModule as never, testBed as never);
coreDefineProperty(testBed, 'ngMocksFasterInstalled', true);
}

return hooks;
};
4 changes: 2 additions & 2 deletions tests/abstract-methods-provider/test.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Injectable, NgModule } from '@angular/core';

import { getTestBedInjection, MockBuilder } from 'ng-mocks';
import { MockBuilder, ngMocks } from 'ng-mocks';

@Injectable()
abstract class LoggerInterface {
Expand Down Expand Up @@ -30,7 +30,7 @@ describe('abstract-methods-provider', () => {
beforeEach(() => MockBuilder().mock(TargetModule));

it('provides a mock copy with an implemented abstract method', () => {
const actual = getTestBedInjection(LoggerInterface);
const actual = ngMocks.get(LoggerInterface);
expect(actual && actual.log).toBeDefined();
});
});
6 changes: 4 additions & 2 deletions tests/ng-mocks-faster/test.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { InjectionToken } from '@angular/core';
import { TestBed } from '@angular/core/testing';

import { getTestBedInjection, ngMocks } from 'ng-mocks';
import { ngMocks } from 'ng-mocks';

const TOKEN = new InjectionToken('TOKEN');

Expand Down Expand Up @@ -33,7 +33,9 @@ describe('ngMocks.faster', () => {
ngMocks.faster();

it('works in clear reset', () => {
expect(getTestBedInjection(TOKEN)).toBeUndefined();
expect(() => ngMocks.get(TOKEN)).toThrowError(
/Cannot find an instance/,
);
});
});
});
Expand Down

0 comments on commit 5f893bb

Please sign in to comment.