Page MenuHomeWickedGov Phorge

utils.js
No OneTemporary

Size
1 KB
Referenced Files
None
Subscribers
None

utils.js

'use strict';
/**
* Mocks mw.Api().saveOption() and returns a jest.fn()
* that is used as the saveOption() method. This can
* be used to expect that the saveOption() method is
* called with the correct arguments.
*
* @param {boolean} shouldBeSuccessful If the call to mw.Api().saveOption() should be a success
* @param {Object} [mockResponse] Optional. The response from the API.
* @param {string} [mockErrorCode] Optional. If the request is mocked to fail, this defines
* the error code returned to the reject handler.
* @return {jest.fn}
*/
function mockApiSaveOption( shouldBeSuccessful, mockResponse, mockErrorCode ) {
const apiSaveOption = jest.fn();
if ( shouldBeSuccessful ) {
apiSaveOption.mockResolvedValue( mockResponse || { options: 'success' } );
} else {
// Create a mock that acts like $.Deferred(), which means that the reject
// handler can be passed two arguments.
apiSaveOption.mockReturnValue( {
then: ( _, rejectHandler ) => rejectHandler(
mockErrorCode || 'errorcode', mockResponse || { error: { info: 'Error' } }
)
} );
}
jest.spyOn( mw, 'Api' ).mockImplementation( () => ( {
saveOption: apiSaveOption
} ) );
return apiSaveOption;
}
/**
* Waits for the return value of a given function to be true.
* Will wait for a maximum of 1 second for the condition to be true.
*
* @param {Function} conditionCheck
*/
async function waitFor( conditionCheck ) {
let tries = 0;
while ( !conditionCheck() && tries < 20 ) {
tries++;
await new Promise( ( resolve ) => {
setTimeout( () => resolve(), 50 );
} );
}
}
module.exports = {
mockApiSaveOption,
waitFor
};

File Metadata

Mime Type
text/plain
Expires
Aug 18 2026, 07:23 (7 w, 1 d ago)
Storage Engine
local-disk
Storage Format
Raw Data
Storage Handle
61/a2/94766414a29aa4ebfa44df8db64c
Default Alt Text
utils.js (1 KB)

Event Timeline