Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit 9ff52b1

Browse files
committed
Allow fetch() to be stubbed for the RtsClient
- so that we can write some tests for it.
1 parent a05bafe commit 9ff52b1

File tree

3 files changed

+16
-5
lines changed

3 files changed

+16
-5
lines changed

.eslintignore.errorfiles

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,6 @@ src/Roles.js
146146
src/RoomListSorter.js
147147
src/RoomNotifs.js
148148
src/Rooms.js
149-
src/RtsClient.js
150149
src/ScalarAuthClient.js
151150
src/ScalarMessaging.js
152151
src/SdkConfig.js

src/RtsClient.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import 'whatwg-fetch';
22

3+
let fetchFunction = fetch;
4+
35
function checkStatus(response) {
46
if (!response.ok) {
57
return response.text().then((text) => {
@@ -31,7 +33,7 @@ const request = (url, opts) => {
3133
opts.body = JSON.stringify(opts.body);
3234
opts.headers['Content-Type'] = 'application/json';
3335
}
34-
return fetch(url, opts)
36+
return fetchFunction(url, opts)
3537
.then(checkStatus)
3638
.then(parseJson);
3739
};
@@ -64,7 +66,7 @@ export default class RtsClient {
6466
client_secret: clientSecret,
6567
},
6668
method: 'POST',
67-
}
69+
},
6870
);
6971
}
7072

@@ -74,7 +76,7 @@ export default class RtsClient {
7476
qs: {
7577
team_token: teamToken,
7678
},
77-
}
79+
},
7880
);
7981
}
8082

@@ -91,7 +93,12 @@ export default class RtsClient {
9193
qs: {
9294
user_id: userId,
9395
},
94-
}
96+
},
9597
);
9698
}
99+
100+
// allow fetch to be replaced, for testing.
101+
static setFetch(fn) {
102+
fetchFunction = fn;
103+
}
97104
}

src/index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ limitations under the License.
1515
*/
1616

1717
import Skinner from './Skinner';
18+
import RtsClient from './RtsClient';
1819

1920
module.exports.loadSkin = function(skinObject) {
2021
Skinner.load(skinObject);
@@ -27,3 +28,7 @@ module.exports.resetSkin = function() {
2728
module.exports.getComponent = function(componentName) {
2829
return Skinner.getComponent(componentName);
2930
};
31+
32+
module.exports.setFetch = function(fetchFunction) {
33+
RtsClient.setFetch(fetchFunction);
34+
};

0 commit comments

Comments
 (0)