@@ -1312,6 +1312,36 @@ describe('moduleMocker', () => {
1312
1312
) ;
1313
1313
} ) ;
1314
1314
1315
+ it ( 'supports restoring a spy' , ( ) => {
1316
+ let methodOneCalls = 0 ;
1317
+ const obj = {
1318
+ methodOne ( ) {
1319
+ methodOneCalls ++ ;
1320
+ } ,
1321
+ } ;
1322
+
1323
+ const spy1 = moduleMocker . spyOn ( obj , 'methodOne' ) ;
1324
+
1325
+ obj . methodOne ( ) ;
1326
+
1327
+ // The spy and the original function got called.
1328
+ expect ( methodOneCalls ) . toBe ( 1 ) ;
1329
+ expect ( spy1 . mock . calls ) . toHaveLength ( 1 ) ;
1330
+
1331
+ expect ( moduleMocker . isMockFunction ( obj . methodOne ) ) . toBe ( true ) ;
1332
+
1333
+ spy1 . mockRestore ( ) ;
1334
+
1335
+ // After restoring the spy, the method is not mock function.
1336
+ expect ( moduleMocker . isMockFunction ( obj . methodOne ) ) . toBe ( false ) ;
1337
+
1338
+ obj . methodOne ( ) ;
1339
+
1340
+ // After restoring the spy only the real method bumps its call count, not the spy.
1341
+ expect ( methodOneCalls ) . toBe ( 2 ) ;
1342
+ expect ( spy1 . mock . calls ) . toHaveLength ( 0 ) ;
1343
+ } ) ;
1344
+
1315
1345
it ( 'supports restoring all spies' , ( ) => {
1316
1346
let methodOneCalls = 0 ;
1317
1347
let methodTwoCalls = 0 ;
@@ -1327,25 +1357,32 @@ describe('moduleMocker', () => {
1327
1357
const spy1 = moduleMocker . spyOn ( obj , 'methodOne' ) ;
1328
1358
const spy2 = moduleMocker . spyOn ( obj , 'methodTwo' ) ;
1329
1359
1330
- // First, we call with the spies: both spies and both original functions
1331
- // should be called.
1332
1360
obj . methodOne ( ) ;
1333
1361
obj . methodTwo ( ) ;
1362
+
1363
+ // Both spies and both original functions got called.
1334
1364
expect ( methodOneCalls ) . toBe ( 1 ) ;
1335
1365
expect ( methodTwoCalls ) . toBe ( 1 ) ;
1336
1366
expect ( spy1 . mock . calls ) . toHaveLength ( 1 ) ;
1337
1367
expect ( spy2 . mock . calls ) . toHaveLength ( 1 ) ;
1338
1368
1369
+ expect ( moduleMocker . isMockFunction ( obj . methodOne ) ) . toBe ( true ) ;
1370
+ expect ( moduleMocker . isMockFunction ( obj . methodTwo ) ) . toBe ( true ) ;
1371
+
1339
1372
moduleMocker . restoreAllMocks ( ) ;
1340
1373
1341
- // Then, after resetting all mocks, we call methods again. Only the real
1342
- // methods should bump their count, not the spies.
1374
+ // After restoring all mocks, the methods are not mock functions.
1375
+ expect ( moduleMocker . isMockFunction ( obj . methodOne ) ) . toBe ( false ) ;
1376
+ expect ( moduleMocker . isMockFunction ( obj . methodTwo ) ) . toBe ( false ) ;
1377
+
1343
1378
obj . methodOne ( ) ;
1344
1379
obj . methodTwo ( ) ;
1380
+
1381
+ // After restoring all mocks only the real methods bump their count, not the spies.
1345
1382
expect ( methodOneCalls ) . toBe ( 2 ) ;
1346
1383
expect ( methodTwoCalls ) . toBe ( 2 ) ;
1347
- expect ( spy1 . mock . calls ) . toHaveLength ( 1 ) ;
1348
- expect ( spy2 . mock . calls ) . toHaveLength ( 1 ) ;
1384
+ expect ( spy1 . mock . calls ) . toHaveLength ( 0 ) ;
1385
+ expect ( spy2 . mock . calls ) . toHaveLength ( 0 ) ;
1349
1386
} ) ;
1350
1387
1351
1388
it ( 'should work with getters' , ( ) => {
@@ -1482,6 +1519,33 @@ describe('moduleMocker', () => {
1482
1519
) ;
1483
1520
} ) ;
1484
1521
1522
+ it ( 'supports restoring a spy' , ( ) => {
1523
+ let methodOneCalls = 0 ;
1524
+ const obj = {
1525
+ get methodOne ( ) {
1526
+ return function ( ) {
1527
+ methodOneCalls ++ ;
1528
+ } ;
1529
+ } ,
1530
+ } ;
1531
+
1532
+ const spy1 = moduleMocker . spyOn ( obj , 'methodOne' , 'get' ) ;
1533
+
1534
+ obj . methodOne ( ) ;
1535
+
1536
+ // The spy and the original function are called.
1537
+ expect ( methodOneCalls ) . toBe ( 1 ) ;
1538
+ expect ( spy1 . mock . calls ) . toHaveLength ( 1 ) ;
1539
+
1540
+ spy1 . mockRestore ( ) ;
1541
+
1542
+ obj . methodOne ( ) ;
1543
+
1544
+ // After restoring the spy only the real method bumps its call count, not the spy.
1545
+ expect ( methodOneCalls ) . toBe ( 2 ) ;
1546
+ expect ( spy1 . mock . calls ) . toHaveLength ( 0 ) ;
1547
+ } ) ;
1548
+
1485
1549
it ( 'supports restoring all spies' , ( ) => {
1486
1550
let methodOneCalls = 0 ;
1487
1551
let methodTwoCalls = 0 ;
@@ -1501,25 +1565,25 @@ describe('moduleMocker', () => {
1501
1565
const spy1 = moduleMocker . spyOn ( obj , 'methodOne' , 'get' ) ;
1502
1566
const spy2 = moduleMocker . spyOn ( obj , 'methodTwo' , 'get' ) ;
1503
1567
1504
- // First, we call with the spies: both spies and both original functions
1505
- // should be called.
1506
1568
obj . methodOne ( ) ;
1507
1569
obj . methodTwo ( ) ;
1570
+
1571
+ // Both spies and both original functions got called.
1508
1572
expect ( methodOneCalls ) . toBe ( 1 ) ;
1509
1573
expect ( methodTwoCalls ) . toBe ( 1 ) ;
1510
1574
expect ( spy1 . mock . calls ) . toHaveLength ( 1 ) ;
1511
1575
expect ( spy2 . mock . calls ) . toHaveLength ( 1 ) ;
1512
1576
1513
1577
moduleMocker . restoreAllMocks ( ) ;
1514
1578
1515
- // Then, after resetting all mocks, we call methods again. Only the real
1516
- // methods should bump their count, not the spies.
1517
1579
obj . methodOne ( ) ;
1518
1580
obj . methodTwo ( ) ;
1581
+
1582
+ // After restoring all mocks only the real methods bump their count, not the spies.
1519
1583
expect ( methodOneCalls ) . toBe ( 2 ) ;
1520
1584
expect ( methodTwoCalls ) . toBe ( 2 ) ;
1521
- expect ( spy1 . mock . calls ) . toHaveLength ( 1 ) ;
1522
- expect ( spy2 . mock . calls ) . toHaveLength ( 1 ) ;
1585
+ expect ( spy1 . mock . calls ) . toHaveLength ( 0 ) ;
1586
+ expect ( spy2 . mock . calls ) . toHaveLength ( 0 ) ;
1523
1587
} ) ;
1524
1588
1525
1589
it ( 'should work with getters on the prototype chain' , ( ) => {
@@ -1587,6 +1651,34 @@ describe('moduleMocker', () => {
1587
1651
expect ( obj . property ) . toBe ( true ) ;
1588
1652
} ) ;
1589
1653
1654
+ it ( 'supports restoring a spy on the prototype chain' , ( ) => {
1655
+ let methodOneCalls = 0 ;
1656
+ const prototype = {
1657
+ get methodOne ( ) {
1658
+ return function ( ) {
1659
+ methodOneCalls ++ ;
1660
+ } ;
1661
+ } ,
1662
+ } ;
1663
+ const obj = Object . create ( prototype , { } ) ;
1664
+
1665
+ const spy1 = moduleMocker . spyOn ( obj , 'methodOne' , 'get' ) ;
1666
+
1667
+ obj . methodOne ( ) ;
1668
+
1669
+ // The spy and the original function are called.
1670
+ expect ( methodOneCalls ) . toBe ( 1 ) ;
1671
+ expect ( spy1 . mock . calls ) . toHaveLength ( 1 ) ;
1672
+
1673
+ spy1 . mockRestore ( ) ;
1674
+
1675
+ obj . methodOne ( ) ;
1676
+
1677
+ // After restoring the spy only the real method bumps its call count, not the spy.
1678
+ expect ( methodOneCalls ) . toBe ( 2 ) ;
1679
+ expect ( spy1 . mock . calls ) . toHaveLength ( 0 ) ;
1680
+ } ) ;
1681
+
1590
1682
it ( 'supports restoring all spies on the prototype chain' , ( ) => {
1591
1683
let methodOneCalls = 0 ;
1592
1684
let methodTwoCalls = 0 ;
@@ -1607,25 +1699,25 @@ describe('moduleMocker', () => {
1607
1699
const spy1 = moduleMocker . spyOn ( obj , 'methodOne' , 'get' ) ;
1608
1700
const spy2 = moduleMocker . spyOn ( obj , 'methodTwo' , 'get' ) ;
1609
1701
1610
- // First, we call with the spies: both spies and both original functions
1611
- // should be called.
1612
1702
obj . methodOne ( ) ;
1613
1703
obj . methodTwo ( ) ;
1704
+
1705
+ // Both spies and both original functions got called.
1614
1706
expect ( methodOneCalls ) . toBe ( 1 ) ;
1615
1707
expect ( methodTwoCalls ) . toBe ( 1 ) ;
1616
1708
expect ( spy1 . mock . calls ) . toHaveLength ( 1 ) ;
1617
1709
expect ( spy2 . mock . calls ) . toHaveLength ( 1 ) ;
1618
1710
1619
1711
moduleMocker . restoreAllMocks ( ) ;
1620
1712
1621
- // Then, after resetting all mocks, we call methods again. Only the real
1622
- // methods should bump their count, not the spies.
1623
1713
obj . methodOne ( ) ;
1624
1714
obj . methodTwo ( ) ;
1715
+
1716
+ // After restoring all mocks only the real methods bump their count, not the spies.
1625
1717
expect ( methodOneCalls ) . toBe ( 2 ) ;
1626
1718
expect ( methodTwoCalls ) . toBe ( 2 ) ;
1627
- expect ( spy1 . mock . calls ) . toHaveLength ( 1 ) ;
1628
- expect ( spy2 . mock . calls ) . toHaveLength ( 1 ) ;
1719
+ expect ( spy1 . mock . calls ) . toHaveLength ( 0 ) ;
1720
+ expect ( spy2 . mock . calls ) . toHaveLength ( 0 ) ;
1629
1721
} ) ;
1630
1722
} ) ;
1631
1723
@@ -1664,7 +1756,7 @@ describe('moduleMocker', () => {
1664
1756
expect ( obj . property ) . toBe ( 1 ) ;
1665
1757
} ) ;
1666
1758
1667
- it ( 'should allow mocking with value of different value ' , ( ) => {
1759
+ it ( 'should allow mocking with value of different type ' , ( ) => {
1668
1760
const obj = {
1669
1761
property : 1 ,
1670
1762
} ;
0 commit comments