@@ -10,16 +10,28 @@ interface TestInterface {
1010 func : ( num : number , str : string ) => boolean ;
1111 func2 : ( entity : TestClass ) => void ;
1212 func3 : ( ) => Promise < { prop : number } > ;
13+ nested : {
14+ someOtherNum : number ;
15+ func4 : ( ) => boolean ;
16+ } ;
1317}
1418
1519class TestClass {
1620 someProperty ! : number ;
1721
22+ nested = new NestedTestClass ( ) ;
23+
1824 someMethod ( ) {
1925 return 42 ;
2026 }
2127}
2228
29+ class NestedTestClass {
30+ someOtherMethod ( ) {
31+ return 24 ;
32+ }
33+ }
34+
2335describe ( 'Mocks' , ( ) => {
2436 const request = {
2537 headers : {
@@ -200,6 +212,22 @@ describe('Mocks', () => {
200212 expect ( serviceMock . foo ( ) ) . toEqual ( false ) ;
201213 expect ( serviceMock . foo ( ) ) . toEqual ( true ) ;
202214 } ) ;
215+
216+ it ( 'should work with nested properties and functions' , ( ) => {
217+ const mock = createMock < TestInterface > ( ) ;
218+ mock . nested . someOtherNum = 99 ;
219+ mock . nested . func4 . mockReturnValueOnce ( true ) ;
220+ const result = mock . nested . func4 ( ) ;
221+ expect ( mock . nested . someOtherNum ) . toBe ( 99 ) ;
222+ expect ( result ) . toBe ( true ) ;
223+ } ) ;
224+
225+ it ( 'should work with classes having nested properties' , ( ) => {
226+ const mock = createMock < TestClass > ( ) ;
227+ mock . nested . someOtherMethod . mockReturnValueOnce ( 99 ) ;
228+ const result = mock . nested . someOtherMethod ( ) ;
229+ expect ( result ) . toBe ( 99 ) ;
230+ } ) ;
203231 } ) ;
204232
205233 describe ( 'auto mocked' , ( ) => {
@@ -360,7 +388,9 @@ describe('Mocks', () => {
360388 it ( 'should throw error when calling unstubbed method in strict mode' , ( ) => {
361389 const mock = createMock < TestInterface > ( { } , { strict : true } ) ;
362390
363- expect ( ( ) => mock . func ( 1 , 'test' ) ) . toThrow ( 'Method mock.func was called without being explicitly stubbed' ) ;
391+ expect ( ( ) => mock . func ( 1 , 'test' ) ) . toThrow (
392+ 'Method mock.func was called without being explicitly stubbed' ,
393+ ) ;
364394
365395 mock . func . mockReturnValue ( true ) ;
366396 expect ( mock . func ( 1 , 'test' ) ) . toBe ( true ) ;
0 commit comments