|
| 1 | +import _ from "lodash"; |
| 2 | + |
| 3 | +const buildFetchErrorResponse = (data, status) => |
| 4 | + Promise.resolve({ |
| 5 | + ok: false, |
| 6 | + status: status || 400, |
| 7 | + headers: { |
| 8 | + get: () => null, |
| 9 | + map: { |
| 10 | + "content-type": "" |
| 11 | + } |
| 12 | + }, |
| 13 | + json: () => Promise.resolve(data) |
| 14 | + }); |
| 15 | + |
| 16 | +const buildFetchResponse = data => |
| 17 | + Promise.resolve({ |
| 18 | + ok: {}, |
| 19 | + headers: { |
| 20 | + get: () => null, |
| 21 | + map: { |
| 22 | + "content-type": "" |
| 23 | + } |
| 24 | + }, |
| 25 | + json: () => Promise.resolve(data) |
| 26 | + }); |
| 27 | + |
| 28 | +const mockFetch = (stubConfig, { calls } = {}) => { |
| 29 | + stubConfig.sort((a, b) => { |
| 30 | + const aIsArray = !!a.reduce; |
| 31 | + const bIsArray = !!b.reduce; |
| 32 | + |
| 33 | + if (!aIsArray === bIsArray) { |
| 34 | + return 0; |
| 35 | + } |
| 36 | + |
| 37 | + if (aIsArray) { |
| 38 | + return -1; |
| 39 | + } |
| 40 | + |
| 41 | + return 1; |
| 42 | + }); |
| 43 | + |
| 44 | + if (!stubConfig) { |
| 45 | + throw new Error("stubConfig is required!"); |
| 46 | + } |
| 47 | + |
| 48 | + const NOT_FOUND = -1; |
| 49 | + if ( |
| 50 | + !stubConfig.length || |
| 51 | + stubConfig.findIndex( |
| 52 | + c => |
| 53 | + !c.url || |
| 54 | + (!c.response && typeof c.response === "function") || |
| 55 | + (!c.errorResponse && typeof c.errorResponse === "function") |
| 56 | + ) > NOT_FOUND |
| 57 | + ) { |
| 58 | + throw new Error( |
| 59 | + 'You must specify at least one config as part of the stubConfig argument and each config must have a valid format. Example: [{url:"/foo", response: fooResponseGetter}]' |
| 60 | + ); |
| 61 | + } |
| 62 | + |
| 63 | + if (calls) { |
| 64 | + calls.get = function(url, method) { |
| 65 | + return this.find( |
| 66 | + c => |
| 67 | + ((!c.config.method && (!method || method.toLowerCase() === "get")) || |
| 68 | + (c.config.method && |
| 69 | + c.config.method.toLowerCase() === method.toLowerCase())) && |
| 70 | + c.url.includes(url) |
| 71 | + ); |
| 72 | + }; |
| 73 | + |
| 74 | + calls.expect = function(url, method) { |
| 75 | + expect(!!this.get(url, method)).toBeTruthy(); |
| 76 | + }; |
| 77 | + |
| 78 | + calls.getBody = function(url, method) { |
| 79 | + const call = this.get(url, method); |
| 80 | + |
| 81 | + const body = _.get(call, "config.body"); |
| 82 | + |
| 83 | + return body && JSON.parse(body); |
| 84 | + }; |
| 85 | + } |
| 86 | + |
| 87 | + return (url, config) => { |
| 88 | + let foundUrl; |
| 89 | + |
| 90 | + const isGetOrDefault = (stubUrlConfig, curConfig) => |
| 91 | + (!curConfig.method || curConfig.method.toLocaleString() === "get") && |
| 92 | + (!stubUrlConfig.method || stubUrlConfig.method.toLowerCase() === "get"); |
| 93 | + |
| 94 | + const stubMethodMatchesConfig = stubUrlConfig => |
| 95 | + stubUrlConfig.method && |
| 96 | + config.method && |
| 97 | + config.method.toLowerCase() === stubUrlConfig.method.toLowerCase(); |
| 98 | + |
| 99 | + const methodIsGetOrMethodsMatch = (stubUrlConfig, curConfig) => |
| 100 | + isGetOrDefault(stubUrlConfig, curConfig) || |
| 101 | + stubMethodMatchesConfig(stubUrlConfig); |
| 102 | + |
| 103 | + const urlIncludesFragments = (url, fragments) => { |
| 104 | + if (fragments.reduce) { |
| 105 | + return fragments.reduce((acc, cur) => { |
| 106 | + if (!acc) { |
| 107 | + return acc; |
| 108 | + } |
| 109 | + return url.includes(cur); |
| 110 | + }, true); |
| 111 | + } |
| 112 | + |
| 113 | + return url.includes(fragments); |
| 114 | + }; |
| 115 | + |
| 116 | + foundUrl = stubConfig.find( |
| 117 | + stubUrlConfig => |
| 118 | + methodIsGetOrMethodsMatch(stubUrlConfig, config) && |
| 119 | + urlIncludesFragments(url, stubUrlConfig.url) |
| 120 | + ); |
| 121 | + |
| 122 | + if (foundUrl) { |
| 123 | + if (calls && calls.length !== undefined) { |
| 124 | + calls.push({ |
| 125 | + url, |
| 126 | + config |
| 127 | + }); |
| 128 | + } |
| 129 | + |
| 130 | + if (foundUrl.errorResponse) { |
| 131 | + return buildFetchErrorResponse( |
| 132 | + foundUrl.response(), |
| 133 | + foundUrl.statusCode |
| 134 | + ); |
| 135 | + } else { |
| 136 | + return buildFetchResponse(foundUrl.response()); |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + throw new Error( |
| 141 | + `There was an unexpected ajax call. Details follow... |
| 142 | + url: ${url} |
| 143 | + method: ${config.method || "GET"} |
| 144 | + config: ${JSON.stringify(config)}. |
| 145 | + stubConfig: ${JSON.stringify(stubConfig)}` |
| 146 | + ); |
| 147 | + }; |
| 148 | +}; |
| 149 | + |
| 150 | +export default mockFetch; |
0 commit comments