-
-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(ngMocks.faster): support of angular 14.2.0 #3466
- Loading branch information
Showing
8 changed files
with
134 additions
and
28 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
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
36 changes: 24 additions & 12 deletions
36
libs/ng-mocks/src/lib/mock-helper/mock-helper.faster-install.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 |
---|---|---|
@@ -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; | ||
}; |
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 |
---|---|---|
@@ -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/, | ||
); | ||
}); | ||
}); |
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