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 becd0f1
Show file tree
Hide file tree
Showing 8 changed files with 134 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
8 changes: 4 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,7 @@ const applyPlatformOverrides = (testBed: TestBed, touches: Set<any>) => {
};

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

Expand All @@ -202,18 +202,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: TestBed): 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
36 changes: 24 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,68 @@
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: TestBed) => TestBedStatic['resetTestingModule']
>;
before: Array<
(original: TestBedStatic['configureTestingModule'], instance: TestBed) => 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: TestBed): TestBedStatic['configureTestingModule'] =>
(moduleDef: TestModuleMetadata) => {
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);
return final.call(instance, moduleDef);
};

const resetTestingModule =
(original: TestBedStatic['resetTestingModule']): TestBedStatic['resetTestingModule'] =>
(original: TestBedStatic['resetTestingModule'], instance: TestBed): TestBedStatic['resetTestingModule'] =>
() => {
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);
return final.call(instance);
};

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

const testBed = getTestBed();
if (!(testBed as any).ngMocksFasterInstalled) {
testBed.configureTestingModule = configureTestingModule(testBed.configureTestingModule, testBed);
testBed.resetTestingModule = resetTestingModule(testBed.resetTestingModule, testBed);
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();
});
});
85 changes: 85 additions & 0 deletions tests/ng-mocks-faster/idea.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import { InjectionToken } from '@angular/core';
import {
getTestBed,
TestBed,
TestBedStatic,
} from '@angular/core/testing';

import { ngMocks } from 'ng-mocks';

const TOKEN = new InjectionToken('TOKEN');

describe('ngMocks.faster:idea', () => {
it('does have access to token before optimization', () => {
expect(() => ngMocks.get(TOKEN)).toThrowError(
/Cannot find an instance/,
);
});

describe('optimization', () => {
const backup: Partial<TestBedStatic> = {};
const backupInstance: Partial<TestBedStatic> = {};

beforeAll(() => {
backup.configureTestingModule = TestBed.configureTestingModule;
backup.resetTestingModule = TestBed.resetTestingModule;

const testBed = getTestBed();
backupInstance.configureTestingModule =
testBed.configureTestingModule;
backupInstance.resetTestingModule = testBed.resetTestingModule;
});

afterAll(() => {
if (backup.configureTestingModule) {
TestBed.configureTestingModule =
backup.configureTestingModule;
}
if (backup.resetTestingModule) {
TestBed.resetTestingModule = backup.resetTestingModule;
}

const testBed = getTestBed();
if (backupInstance.configureTestingModule) {
testBed.configureTestingModule =
backupInstance.configureTestingModule;
}
if (backupInstance.resetTestingModule) {
testBed.resetTestingModule =
backupInstance.resetTestingModule;
}
TestBed.resetTestingModule();
});

describe('suite with faster logic', () => {
beforeAll(() => {
TestBed.resetTestingModule = () => getTestBed() as never;
const testBed = getTestBed();
testBed.resetTestingModule = () => getTestBed() as never;

return TestBed.configureTestingModule({
providers: [
{
provide: TOKEN,
useValue: true,
},
],
}).compileComponents();
});

it('has access to token after configuration', () => {
expect(ngMocks.get(TOKEN)).toEqual(true);
});

it('has access to token after supressed reset', () => {
expect(ngMocks.get(TOKEN)).toEqual(true);
});
});
});

it('does have access to token after optimization', () => {
expect(() => ngMocks.get(TOKEN)).toThrowError(
/Cannot find an instance/,
);
});
});
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 becd0f1

Please sign in to comment.