@@ -3,20 +3,26 @@ let fetchSpy;
33
44/**
55 * Checks whether a string (in the first parameter) matches a regular expression,
6- * substring or test function (in the second parameter).
6+ * contains the substring or satisfies the test function (in the second parameter).
77 * If the second parameter is not passed (no condition), returns true.
88 * @param {string } str - string to check
99 * @param {RegExp|string|Function|undefined } condition - regular expression,
1010 * substring, or test function.
11+ * @param {Object } [options] - apply condition options:
12+ * @param {Object } [options.fullStringMatch] - whether to perform a full string
13+ * comparison on the strings in the "str" and "condition" parameters, defaults
14+ * to false.
1115 * @returns {boolean }
1216 */
13- function checkString ( str , condition ) {
17+ function checkString ( str , condition , options = { } ) {
1418 if ( condition === undefined ) {
1519 return true ;
1620 } else if ( condition instanceof RegExp ) {
1721 return condition . test ( str ) ;
1822 } else if ( typeof condition === 'string' ) {
19- return str && ( str . indexOf ( condition ) !== - 1 )
23+ return options ?. fullStringMatch ?
24+ str === condition
25+ : ( str !== null && str !== undefined && str . indexOf ( condition ) !== - 1 ) ;
2026 } else if ( condition instanceof Function ) {
2127 return condition ( str ) ;
2228 } else {
@@ -36,19 +42,26 @@ function checkString(str, condition) {
3642 * response as the third item.
3743 * For mocking POST requests the first item of a response description with a URL
3844 * condition could be replaced with an object:
39- * {url: string|Regexp, body: string|RegExp},
45+ * {url: string|Regexp, body: string|RegExp, method: string },
4046 * where "url" is a RegExp for a URL or a URL substring,
41- * "body" is a RegExp for the body content or a substring of the body content.
47+ * "body" is a RegExp for the body content or a substring of the body content,
48+ * and "method" is a RegExp for the HTTP method (e.g. "POST") or a substring of
49+ * the HTTP method.
50+ *
4251 * @param {Object } options - options for the mock.
4352 * @param {number } [options.timeout=0] - timeout for the mock response.
4453 */
4554function mockFetchResults ( results , { timeout = 0 } = { } ) {
4655 fetchSpy = jest . spyOn ( global , 'fetch' ) . mockImplementation (
47- ( url , options ) => new Promise ( ( resolve , reject ) => {
56+ ( url , options ) => new Promise ( ( resolve ) => {
4857 const mockedItem = results ?. find (
4958 ( r ) => {
50- if ( typeof r [ 0 ] === 'object' && ( r [ 0 ] . url || r [ 0 ] . body ) ) {
51- return checkString ( url , r [ 0 ] ?. url ) && checkString ( options . body , r [ 0 ] ?. body ) ;
59+ if ( typeof r [ 0 ] === 'object' && r [ 0 ] !== null && ( r [ 0 ] . url ||
60+ r [ 0 ] . body || r [ 0 ] . method ) ) {
61+ return checkString ( url , r [ 0 ] ?. url ) &&
62+ checkString ( options . body , r [ 0 ] ?. body ) &&
63+ checkString ( options . method ?? 'GET' , r [ 0 ] ?. method ,
64+ { fullStringMatch : true } ) ;
5265 } else {
5366 return checkString ( url , r [ 0 ] ) ;
5467 }
@@ -60,7 +73,7 @@ function mockFetchResults(results, {timeout = 0} = {}) {
6073 options ?. method === 'POST' &&
6174 optionHeaders . get ( 'Content-Type' ) !== 'application/fhir+json; charset=utf-8'
6275 ) {
63- reportError ( 'Unexpected request header.' , reject ) ;
76+ reportError ( 'Unexpected request header.' ) ;
6477 }
6578
6679 const okResult = mockedItem ?. [ 1 ] ;
@@ -84,7 +97,7 @@ function mockFetchResults(results, {timeout = 0} = {}) {
8497 ok : false
8598 } ) ;
8699 } else {
87- reportError ( `"${ url } " is not mocked.` , reject ) ;
100+ reportError ( `"${ url } " is not mocked.` ) ;
88101 }
89102 } , timeout ) ;
90103 } )
@@ -94,13 +107,12 @@ function mockFetchResults(results, {timeout = 0} = {}) {
94107/**
95108 * Report an error message.
96109 * @param {string } msg - error message to report.
97- * @param {Function } rejectFn - function to call to reject the promise.
98110 */
99- function reportError ( msg , rejectFn ) {
111+ function reportError ( msg ) {
100112 // Show error message in the console:
101113 console . error ( msg ) ;
102- // Reject the promise so that the request to the server fails:
103- rejectFn ( msg ) ;
114+ // Throw an error so that the test fails:
115+ throw new Error ( msg ) ;
104116}
105117
106118
0 commit comments