mattermost-mobile/test/client/client.test.js
Thomas Hopkins 8611b3e28a Use fetch-mock to mock requests in tests (#18)
* Use fetch-mock to mock requests in tests

* Use flag to enable/disable fetch-mock
2016-10-17 14:37:18 -04:00

59 lines
1.5 KiB
JavaScript

// Copyright (c) 2016 Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.
import assert from 'assert';
import fetchMock from 'fetch_mock';
import TestHelper from 'test_helper.js';
const fakeNotFoundResp = {
status: 404,
body: {
id: 'api.context.404.app_error',
message: 'Sorry, we could not find the page.',
detailed_error: [
"There doesn't appear to be an api call for the url='/api/v3/fake/url/general/ping'.",
'Typo? are you missing a team_id or user_id as part of the url?'
].join(' '),
status_code: 404
}
};
fetchMock.get(/\/ping/, (url) => {
if (url.match(/\/fake\/url/)) {
return fakeNotFoundResp;
}
return {
server_time: `${Date.now()}`,
version: '3.4.0'
};
});
fetchMock.post(/\/log_client/, {
status: 200,
headers: {'Content-Type': 'application/json'},
body: {status: 'OK'}
});
describe('Client', () => {
it('doFetch', (done) => {
const client = TestHelper.createClient();
let onRequestCalled = false;
client.doFetch(
`${client.getGeneralRoute()}/ping`,
{},
() => {
onRequestCalled = true;
},
() => {
assert.ok(onRequestCalled, 'onSuccess called before onRequest');
done();
},
(err) => {
done(new Error(err));
}
);
});
});