@@ -26,30 +26,6 @@ ngMocksStack.subscribePop((state, stack) => {
2626 currentStack = stack [ stack . length - 1 ] ;
2727} ) ;
2828
29- ngMocksStack . subscribePush ( ( ) => {
30- // On start we have to flush any caches,
31- // they are not from this spec.
32- const set = ngMocksUniverse . getLocalMocks ( ) ;
33- set . splice ( 0 , set . length ) ;
34- } ) ;
35- ngMocksStack . subscribePop ( ( ) => {
36- const set = ngMocksUniverse . getLocalMocks ( ) ;
37- while ( set . length ) {
38- const [ declaration , config ] = set . pop ( ) || /* istanbul ignore next */ [ ] ;
39- const universeConfig = ngMocksUniverse . configInstance . has ( declaration )
40- ? ngMocksUniverse . configInstance . get ( declaration )
41- : /* istanbul ignore next */ { } ;
42- ngMocksUniverse . configInstance . set ( declaration , {
43- ...universeConfig ,
44- ...config ,
45- } ) ;
46- }
47- } ) ;
48-
49- const restore = ( declaration : any , config : any ) : void => {
50- ngMocksUniverse . getLocalMocks ( ) . push ( [ declaration , config ] ) ;
51- } ;
52-
5329interface MockInstanceArgs {
5430 accessor ?: 'get' | 'set' ;
5531 data ?: any ;
@@ -65,7 +41,10 @@ const parseMockInstanceArgs = (args: any[]): MockInstanceArgs => {
6541 set . value = args [ 1 ] ;
6642 set . accessor = args [ 2 ] ;
6743 } else {
68- set . data = args [ 0 ] ;
44+ set . value = args [ 0 ] ;
45+ if ( typeof set . value !== 'function' ) {
46+ set . value = set . value ?. init ;
47+ }
6948 }
7049
7150 return set ;
@@ -82,41 +61,9 @@ if (typeof beforeEach !== 'undefined') {
8261 afterEach ( ( ) => ( checkCollect = false ) ) ;
8362}
8463
85- const mockInstanceConfig = < T > ( declaration : Type < T > | AbstractType < T > | InjectionToken < T > , data ?: any ) : void => {
86- const config = typeof data === 'function' ? { init : data } : data ;
87- const universeConfig = ngMocksUniverse . configInstance . has ( declaration )
88- ? ngMocksUniverse . configInstance . get ( declaration )
89- : { } ;
90- restore ( declaration , universeConfig ) ;
91-
92- if ( config ) {
93- ngMocksUniverse . configInstance . set ( declaration , {
94- ...universeConfig ,
95- ...config ,
96- } ) ;
97- } else {
98- ngMocksUniverse . configInstance . set ( declaration , {
99- ...universeConfig ,
100- init : undefined ,
101- overloads : [ ] ,
102- } ) ;
103- }
104-
105- if ( ! config ) {
106- // When we are calling MockInstance without a config we need to reset it from the checks too.
107- for ( let i = checkReset . length - 1 ; i >= 0 ; i -= 1 ) {
108- if ( checkReset [ i ] [ 0 ] === declaration && checkReset [ i ] [ 2 ] === currentStack ) {
109- checkReset . splice ( i , 1 ) ;
110- }
111- }
112- } else if ( checkCollect ) {
113- checkReset . push ( [ declaration , ngMocksUniverse . configInstance . get ( declaration ) , currentStack ] ) ;
114- }
115- } ;
116-
117- const mockInstanceMember = < T > (
64+ const mockInstanceConfig = < T > (
11865 declaration : Type < T > | AbstractType < T > | InjectionToken < T > ,
119- name : string ,
66+ name : string | undefined ,
12067 stub : any ,
12168 encapsulation ?: 'get' | 'set' ,
12269) => {
@@ -230,12 +177,25 @@ export function MockInstance<T>(
230177export function MockInstance < T > ( declaration : Type < T > | AbstractType < T > | InjectionToken < T > , ...args : any [ ] ) {
231178 funcImportExists ( declaration , 'MockInstance' ) ;
232179
233- const { key, value, accessor, data } = parseMockInstanceArgs ( args ) ;
234- if ( key ) {
235- return mockInstanceMember ( declaration , key , value , accessor ) ;
180+ const { key, value, accessor } = parseMockInstanceArgs ( args ) ;
181+
182+ if ( value ) {
183+ return mockInstanceConfig ( declaration , key , value , accessor ) ;
236184 }
237185
238- mockInstanceConfig ( declaration , data ) ;
186+ const config = ngMocksUniverse . configInstance . get ( declaration ) || /* istanbul ignore next */ { } ;
187+
188+ ngMocksUniverse . configInstance . set ( declaration , {
189+ ...config ,
190+ overloads : [ ] ,
191+ } ) ;
192+
193+ // When we are calling MockInstance without a config we need to reset it from the checks too.
194+ for ( let i = checkReset . length - 1 ; i >= 0 ; i -= 1 ) {
195+ if ( checkReset [ i ] [ 0 ] === declaration && checkReset [ i ] [ 2 ] === currentStack ) {
196+ checkReset . splice ( i , 1 ) ;
197+ }
198+ }
239199}
240200
241201/**
0 commit comments