@@ -45,19 +45,36 @@ class MockFunctionContext {
4545this . #times = times ;
4646}
4747
48+ /**
49+ * Gets an array of recorded calls made to the mock function.
50+ * @returns {Array } An array of recorded calls.
51+ */
4852get calls ( ) {
4953return ArrayPrototypeSlice ( this . #calls, 0 ) ;
5054}
5155
56+ /**
57+ * Retrieves the number of times the mock function has been called.
58+ * @returns {number } The call count.
59+ */
5260callCount ( ) {
5361return this . #calls. length ;
5462}
5563
64+ /**
65+ * Sets a new implementation for the mock function.
66+ * @param {Function } implementation - The new implementation for the mock function.
67+ */
5668mockImplementation ( implementation ) {
5769validateFunction ( implementation , 'implementation' ) ;
5870this . #implementation = implementation ;
5971}
6072
73+ /**
74+ * Replaces the implementation of the function only once.
75+ * @param {Function } implementation - The substitute function.
76+ * @param {number } [onCall] - The call index to be replaced.
77+ */
6178mockImplementationOnce ( implementation , onCall ) {
6279validateFunction ( implementation , 'implementation' ) ;
6380const nextCall = this . #calls. length ;
@@ -66,6 +83,9 @@ class MockFunctionContext {
6683this . #mocks. set ( call , implementation ) ;
6784}
6885
86+ /**
87+ * Restores the original function that was mocked.
88+ */
6989restore ( ) {
7090const { descriptor, object, original, methodName } = this . #restore;
7191
@@ -79,14 +99,25 @@ class MockFunctionContext {
7999}
80100}
81101
102+ /**
103+ * Resets the recorded calls to the mock function
104+ */
82105resetCalls ( ) {
83106this . #calls = [ ] ;
84107}
85108
109+ /**
110+ * Tracks a call made to the mock function.
111+ * @param {object } call - The call details.
112+ */
86113trackCall ( call ) {
87114ArrayPrototypePush ( this . #calls, call ) ;
88115}
89116
117+ /**
118+ * Gets the next implementation to use for the mock function.
119+ * @returns {Function } The next implementation.
120+ */
90121nextImpl ( ) {
91122const nextCall = this . #calls. length ;
92123const mock = this . #mocks. get ( nextCall ) ;
@@ -109,11 +140,23 @@ class MockTracker {
109140 #mocks = [ ] ;
110141 #timers;
111142
143+ /**
144+ * Returns the mock timers of this MockTracker instance.
145+ * @returns {MockTimers } The mock timers instance.
146+ */
112147get timers ( ) {
113148this . #timers ??= new MockTimers ( ) ;
114149return this . #timers;
115150}
116151
152+ /**
153+ * Creates a mock function tracker.
154+ * @param {Function } [original] - The original function to be tracked.
155+ * @param {Function } [implementation] - An optional replacement function for the original one.
156+ * @param {object } [options] - Additional tracking options.
157+ * @param {number } [options.times=Infinity] - The maximum number of times the mock function can be called.
158+ * @returns {ProxyConstructor } The mock function tracker.
159+ */
117160fn (
118161original = function ( ) { } ,
119162implementation = original ,
@@ -137,6 +180,17 @@ class MockTracker {
137180return this . #setupMock( ctx , original ) ;
138181}
139182
183+ /**
184+ * Creates a method tracker for a specified object or function.
185+ * @param {(object | Function) } objectOrFunction - The object or function containing the method to be tracked.
186+ * @param {string } methodName - The name of the method to be tracked.
187+ * @param {Function } [implementation] - An optional replacement function for the original method.
188+ * @param {object } [options] - Additional tracking options.
189+ * @param {boolean } [options.getter=false] - Indicates whether this is a getter method.
190+ * @param {boolean } [options.setter=false] - Indicates whether this is a setter method.
191+ * @param {number } [options.times=Infinity] - The maximum number of times the mock method can be called.
192+ * @returns {ProxyConstructor } The mock method tracker.
193+ */
140194method (
141195objectOrFunction ,
142196methodName ,
@@ -216,6 +270,18 @@ class MockTracker {
216270return mock ;
217271}
218272
273+ /**
274+ * Mocks a getter method of an object.
275+ * This is a syntax sugar for the MockTracker.method with options.getter set to true
276+ * @param {object } object - The target object.
277+ * @param {string } methodName - The name of the getter method to be mocked.
278+ * @param {Function } [implementation] - An optional replacement function for the targeted method.
279+ * @param {object } [options] - Additional tracking options.
280+ * @param {boolean } [options.getter=true] - Indicates whether this is a getter method.
281+ * @param {boolean } [options.setter=false] - Indicates whether this is a setter method.
282+ * @param {number } [options.times=Infinity] - The maximum number of times the mock method can be called.
283+ * @returns {ProxyConstructor } The mock method tracker.
284+ */
219285getter (
220286object ,
221287methodName ,
@@ -244,6 +310,18 @@ class MockTracker {
244310} ) ;
245311}
246312
313+ /**
314+ * Mocks a setter method of an object.
315+ * This function is a syntax sugar for MockTracker.method with options.setter set to true.
316+ * @param {object } object - The target object.
317+ * @param {string } methodName - The setter method to be mocked.
318+ * @param {Function } [implementation] - An optional replacement function for the targeted method.
319+ * @param {object } [options] - Additional tracking options.
320+ * @param {boolean } [options.getter=false] - Indicates whether this is a getter method.
321+ * @param {boolean } [options.setter=true] - Indicates whether this is a setter method.
322+ * @param {number } [options.times=Infinity] - The maximum number of times the mock method can be called.
323+ * @returns {ProxyConstructor } The mock method tracker.
324+ */
247325setter (
248326object ,
249327methodName ,
@@ -272,12 +350,18 @@ class MockTracker {
272350} ) ;
273351}
274352
353+ /**
354+ * Resets the mock tracker, restoring all mocks and clearing timers.
355+ */
275356reset ( ) {
276357this . restoreAll ( ) ;
277358this . #timers?. reset ( ) ;
278359this . #mocks = [ ] ;
279360}
280361
362+ /**
363+ * Restore all mocks created by this MockTracker instance.
364+ */
281365restoreAll ( ) {
282366for ( let i = 0 ; i < this . #mocks. length ; i ++ ) {
283367FunctionPrototypeCall ( restore , this . #mocks[ i ] ) ;
0 commit comments