Skip to content

Commit 3ee722e

Browse files
authored
Rename method to in Monitor class and add tests for loading monitor data and handling errors (#44)
1 parent 6b1571a commit 3ee722e

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

lib/monitor.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ class Monitor {
6666
this._api = this.constructor._api;
6767
}
6868

69-
async data() {
69+
async loadData() {
7070
return this._api.axios
7171
.get(this._api.monitorUrl(this.key))
7272
.then((res) => {

test/test.js

+25
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,31 @@ describe('Monitor', () => {
201201
expect(stub).to.be.calledWith('https://eu.cronitor.link/ping/apiKey123/a-key');
202202
done();
203203
});
204+
205+
it('should load monitor data', async function() {
206+
const monitor = new cronitor.Monitor('a-key');
207+
const mockResponse = { data: { name: 'Test Monitor', type: 'job' } };
208+
const stub = sinon.stub(monitor._api.axios, 'get').resolves(mockResponse);
209+
210+
const result = await monitor.loadData();
211+
212+
expect(stub).to.be.calledWith(monitor._api.monitorUrl(monitor.key));
213+
expect(result).to.deep.equal(mockResponse.data);
214+
expect(monitor.data).to.deep.equal(mockResponse.data);
215+
});
216+
217+
it('should return error response when loadData fails', async function() {
218+
const monitor = new cronitor.Monitor('a-key');
219+
const errorResponse = { status: 404, data: { error: 'Not Found' } };
220+
const stub = sinon.stub(monitor._api.axios, 'get')
221+
.rejects({ response: errorResponse });
222+
223+
const result = await monitor.loadData();
224+
225+
expect(stub).to.be.calledWith(monitor._api.monitorUrl(monitor.key));
226+
expect(result).to.deep.equal(errorResponse);
227+
expect(monitor.data).to.be.null;
228+
});
204229
});
205230

206231
describe('Event', () => {

0 commit comments

Comments
 (0)