File tree Expand file tree Collapse file tree 3 files changed +57
-1
lines changed
Expand file tree Collapse file tree 3 files changed +57
-1
lines changed Original file line number Diff line number Diff line change 1+ const anticipatedCall = require ( '../no-proxy' ) ;
2+
3+ describe ( 'no-proxy' , ( ) => {
4+ describe ( 'nextCall' , ( ) => {
5+ test ( 'should delay resolution until first call' , ( ) => {
6+ const spy = jest . fn ( ) ;
7+ const myFn = anticipatedCall ( spy ) ;
8+
9+ const promise = myFn . anticipated . nextCall
10+ . then ( ( ) => {
11+ expect ( spy ) . toHaveBeenCalledTimes ( 1 ) ;
12+ } ) ;
13+ expect ( spy ) . not . toHaveBeenCalled ( ) ;
14+ myFn ( ) ;
15+ expect ( spy ) . toHaveBeenCalledTimes ( 1 ) ;
16+ } ) ;
17+ test ( 'should return a new Promise' , ( ) => {
18+ const myFn = anticipatedCall ( ) ;
19+ const promise1 = myFn . anticipated . nextCall ;
20+ const promise2 = myFn . anticipated . nextCall ;
21+ expect ( promise1 ) . not . toBe ( promise2 ) ;
22+ } ) ;
23+ test ( 'should handle subsequent calls as expected' , ( ) => {
24+ const spy = jest . fn ( ) ;
25+ const myFn = anticipatedCall ( spy ) ;
26+
27+ const promise1 = myFn . anticipated . nextCall ;
28+
29+ myFn ( ) ;
30+
31+ return promise1 . then ( ( ) => {
32+ expect ( spy ) . toHaveBeenCalledTimes ( 1 ) ;
33+
34+ const promise2 = myFn . anticipated . nextCall ;
35+ myFn ( ) ;
36+ return promise2 ;
37+ } ) . then ( ( ) => {
38+ expect ( spy ) . toHaveBeenCalledTimes ( 2 ) ;
39+ } )
40+ } )
41+ } )
42+ } )
Original file line number Diff line number Diff line change @@ -38,4 +38,6 @@ function anticipateCall(fn = noop) {
3838 return new Proxy ( fn , new Anticipated ( ) ) ;
3939}
4040
41- module . exports = anticipateCall ;
41+ module . exports = anticipateCall ;
42+ module . exports . Anticipated = Anticipated ;
43+ module . exports . noop = noop ;
Original file line number Diff line number Diff line change 1+ const { Anticipated, noop} = require ( './index' ) ;
2+
3+ function anticipateCallWithoutProxy ( fn ) {
4+ const anticipated = new Anticipated ( ) ;
5+ function proxiedCall ( ...args ) {
6+ return anticipated . apply ( fn , this , args ) ;
7+ }
8+ proxiedCall . anticipated = anticipated ;
9+ return proxiedCall ;
10+ }
11+
12+ module . exports = anticipateCallWithoutProxy ;
You can’t perform that action at this time.
0 commit comments