1
0
mirror of https://github.com/matrix-org/matrix-react-sdk.git synced 2025-07-28 15:22:05 +03:00

Allow fetch() to be stubbed for the RtsClient

- so that we can write some tests for it.
This commit is contained in:
Richard van der Hoff
2017-06-15 01:50:56 +01:00
parent a05bafed6a
commit 9ff52b182f
3 changed files with 16 additions and 5 deletions

View File

@ -146,7 +146,6 @@ src/Roles.js
src/RoomListSorter.js src/RoomListSorter.js
src/RoomNotifs.js src/RoomNotifs.js
src/Rooms.js src/Rooms.js
src/RtsClient.js
src/ScalarAuthClient.js src/ScalarAuthClient.js
src/ScalarMessaging.js src/ScalarMessaging.js
src/SdkConfig.js src/SdkConfig.js

View File

@ -1,5 +1,7 @@
import 'whatwg-fetch'; import 'whatwg-fetch';
let fetchFunction = fetch;
function checkStatus(response) { function checkStatus(response) {
if (!response.ok) { if (!response.ok) {
return response.text().then((text) => { return response.text().then((text) => {
@ -31,7 +33,7 @@ const request = (url, opts) => {
opts.body = JSON.stringify(opts.body); opts.body = JSON.stringify(opts.body);
opts.headers['Content-Type'] = 'application/json'; opts.headers['Content-Type'] = 'application/json';
} }
return fetch(url, opts) return fetchFunction(url, opts)
.then(checkStatus) .then(checkStatus)
.then(parseJson); .then(parseJson);
}; };
@ -64,7 +66,7 @@ export default class RtsClient {
client_secret: clientSecret, client_secret: clientSecret,
}, },
method: 'POST', method: 'POST',
} },
); );
} }
@ -74,7 +76,7 @@ export default class RtsClient {
qs: { qs: {
team_token: teamToken, team_token: teamToken,
}, },
} },
); );
} }
@ -91,7 +93,12 @@ export default class RtsClient {
qs: { qs: {
user_id: userId, user_id: userId,
}, },
} },
); );
} }
// allow fetch to be replaced, for testing.
static setFetch(fn) {
fetchFunction = fn;
}
} }

View File

@ -15,6 +15,7 @@ limitations under the License.
*/ */
import Skinner from './Skinner'; import Skinner from './Skinner';
import RtsClient from './RtsClient';
module.exports.loadSkin = function(skinObject) { module.exports.loadSkin = function(skinObject) {
Skinner.load(skinObject); Skinner.load(skinObject);
@ -27,3 +28,7 @@ module.exports.resetSkin = function() {
module.exports.getComponent = function(componentName) { module.exports.getComponent = function(componentName) {
return Skinner.getComponent(componentName); return Skinner.getComponent(componentName);
}; };
module.exports.setFetch = function(fetchFunction) {
RtsClient.setFetch(fetchFunction);
};