From 67c613a45a3ab3b15b587e6999e83a63d52a1582 Mon Sep 17 00:00:00 2001 From: Tobias Skarhed Date: Fri, 27 Jul 2018 13:29:57 +0200 Subject: [PATCH 1/4] Begin conversion --- public/app/core/specs/backend_srv.jest.ts | 39 +++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 public/app/core/specs/backend_srv.jest.ts diff --git a/public/app/core/specs/backend_srv.jest.ts b/public/app/core/specs/backend_srv.jest.ts new file mode 100644 index 00000000000..6281f3814ce --- /dev/null +++ b/public/app/core/specs/backend_srv.jest.ts @@ -0,0 +1,39 @@ +import { BackendSrv } from 'app/core/services/backend_srv'; +jest.mock('app/core/store'); + +describe('backend_srv', function() { + let _httpBackend = options => { + if (options.method === 'GET' && options.url === 'gateway-error') { + return Promise.reject({ status: 502 }); + } else if (options.method === 'POST') { + // return Promise.resolve({}); + } + return Promise.resolve({}); + }; + + let _backendSrv = new BackendSrv(_httpBackend, {}, {}, {}, {}); + + // beforeEach(angularMocks.module('grafana.core')); + // beforeEach(angularMocks.module('grafana.services')); + // beforeEach( + // angularMocks.inject(function($httpBackend, $http, backendSrv) { + // _httpBackend = $httpBackend; + // _backendSrv = backendSrv; + // }) + // ); + + describe('when handling errors', function() { + it('should return the http status code', function(done) { + // _httpBackend.whenGET('gateway-error').respond(502); + _backendSrv + .datasourceRequest({ + url: 'gateway-error', + }) + .catch(function(err) { + expect(err.status).toBe(502); + done(); + }); + // _httpBackend.flush(); + }); + }); +}); From 4e6168f3a331e5701e279305774413eca87499d4 Mon Sep 17 00:00:00 2001 From: Tobias Skarhed Date: Fri, 27 Jul 2018 14:22:48 +0200 Subject: [PATCH 2/4] Add async/await --- public/app/core/specs/backend_srv.jest.ts | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/public/app/core/specs/backend_srv.jest.ts b/public/app/core/specs/backend_srv.jest.ts index 6281f3814ce..2d62716622a 100644 --- a/public/app/core/specs/backend_srv.jest.ts +++ b/public/app/core/specs/backend_srv.jest.ts @@ -3,10 +3,9 @@ jest.mock('app/core/store'); describe('backend_srv', function() { let _httpBackend = options => { - if (options.method === 'GET' && options.url === 'gateway-error') { + console.log(options); + if (options.url === 'gateway-error') { return Promise.reject({ status: 502 }); - } else if (options.method === 'POST') { - // return Promise.resolve({}); } return Promise.resolve({}); }; @@ -22,17 +21,14 @@ describe('backend_srv', function() { // }) // ); - describe('when handling errors', function() { - it('should return the http status code', function(done) { + describe('when handling errors', () => { + it('should return the http status code', async () => { // _httpBackend.whenGET('gateway-error').respond(502); - _backendSrv - .datasourceRequest({ - url: 'gateway-error', - }) - .catch(function(err) { - expect(err.status).toBe(502); - done(); - }); + let res = await _backendSrv.datasourceRequest({ + url: 'gateway-error', + }); + console.log(res); + expect(res.status).toBe(502); // _httpBackend.flush(); }); }); From 2f6b302375bbe7c562e6df09760f1f4b495b2715 Mon Sep 17 00:00:00 2001 From: Tobias Skarhed Date: Fri, 27 Jul 2018 15:51:56 +0200 Subject: [PATCH 3/4] Test passing. Remove Karma --- public/app/core/specs/backend_srv.jest.ts | 23 +++++----------- public/app/core/specs/backend_srv_specs.ts | 31 ---------------------- 2 files changed, 7 insertions(+), 47 deletions(-) delete mode 100644 public/app/core/specs/backend_srv_specs.ts diff --git a/public/app/core/specs/backend_srv.jest.ts b/public/app/core/specs/backend_srv.jest.ts index 2d62716622a..c65464aa875 100644 --- a/public/app/core/specs/backend_srv.jest.ts +++ b/public/app/core/specs/backend_srv.jest.ts @@ -12,24 +12,15 @@ describe('backend_srv', function() { let _backendSrv = new BackendSrv(_httpBackend, {}, {}, {}, {}); - // beforeEach(angularMocks.module('grafana.core')); - // beforeEach(angularMocks.module('grafana.services')); - // beforeEach( - // angularMocks.inject(function($httpBackend, $http, backendSrv) { - // _httpBackend = $httpBackend; - // _backendSrv = backendSrv; - // }) - // ); - describe('when handling errors', () => { it('should return the http status code', async () => { - // _httpBackend.whenGET('gateway-error').respond(502); - let res = await _backendSrv.datasourceRequest({ - url: 'gateway-error', - }); - console.log(res); - expect(res.status).toBe(502); - // _httpBackend.flush(); + try { + await _backendSrv.datasourceRequest({ + url: 'gateway-error', + }); + } catch (err) { + expect(err.status).toBe(502); + } }); }); }); diff --git a/public/app/core/specs/backend_srv_specs.ts b/public/app/core/specs/backend_srv_specs.ts deleted file mode 100644 index 74b058b98c8..00000000000 --- a/public/app/core/specs/backend_srv_specs.ts +++ /dev/null @@ -1,31 +0,0 @@ -import { describe, beforeEach, it, expect, angularMocks } from 'test/lib/common'; -import 'app/core/services/backend_srv'; - -describe('backend_srv', function() { - var _backendSrv; - var _httpBackend; - - beforeEach(angularMocks.module('grafana.core')); - beforeEach(angularMocks.module('grafana.services')); - beforeEach( - angularMocks.inject(function($httpBackend, $http, backendSrv) { - _httpBackend = $httpBackend; - _backendSrv = backendSrv; - }) - ); - - describe('when handling errors', function() { - it('should return the http status code', function(done) { - _httpBackend.whenGET('gateway-error').respond(502); - _backendSrv - .datasourceRequest({ - url: 'gateway-error', - }) - .catch(function(err) { - expect(err.status).to.be(502); - done(); - }); - _httpBackend.flush(); - }); - }); -}); From c11d0f5cc6289b708d1e0d7c072de7eb6b1b8422 Mon Sep 17 00:00:00 2001 From: Tobias Skarhed Date: Fri, 27 Jul 2018 15:52:22 +0200 Subject: [PATCH 4/4] Remove lo --- public/app/core/specs/backend_srv.jest.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/public/app/core/specs/backend_srv.jest.ts b/public/app/core/specs/backend_srv.jest.ts index c65464aa875..b19bd117766 100644 --- a/public/app/core/specs/backend_srv.jest.ts +++ b/public/app/core/specs/backend_srv.jest.ts @@ -3,7 +3,6 @@ jest.mock('app/core/store'); describe('backend_srv', function() { let _httpBackend = options => { - console.log(options); if (options.url === 'gateway-error') { return Promise.reject({ status: 502 }); }