Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
N
nexpie-grafana-theme
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Registry
Registry
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Kornkitt Poolsup
nexpie-grafana-theme
Commits
c4e31100
Unverified
Commit
c4e31100
authored
Feb 03, 2020
by
Shavonn Brown
Committed by
GitHub
Feb 03, 2020
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
deps so can mock in tests (#21827)
parent
7720bfab
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
44 additions
and
17 deletions
+44
-17
public/app/features/datasources/state/actions.test.ts
+29
-14
public/app/features/datasources/state/actions.ts
+15
-3
No files found.
public/app/features/datasources/state/actions.test.ts
View file @
c4e31100
import
{
findNewName
,
nameExits
,
InitDataSourceSettingDependencies
,
testDataSource
}
from
'./actions'
;
import
{
findNewName
,
nameExits
,
InitDataSourceSettingDependencies
,
testDataSource
,
TestDataSourceDependencies
,
}
from
'./actions'
;
import
{
getMockPlugin
,
getMockPlugins
}
from
'../../plugins/__mocks__/pluginMocks'
;
import
{
getMockPlugin
,
getMockPlugins
}
from
'../../plugins/__mocks__/pluginMocks'
;
import
{
thunkTester
}
from
'test/core/thunk/thunkTester'
;
import
{
thunkTester
}
from
'test/core/thunk/thunkTester'
;
import
{
import
{
...
@@ -11,10 +17,17 @@ import {
...
@@ -11,10 +17,17 @@ import {
import
{
initDataSourceSettings
}
from
'../state/actions'
;
import
{
initDataSourceSettings
}
from
'../state/actions'
;
import
{
ThunkResult
,
ThunkDispatch
}
from
'app/types'
;
import
{
ThunkResult
,
ThunkDispatch
}
from
'app/types'
;
import
{
GenericDataSourcePlugin
}
from
'../settings/PluginSettings'
;
import
{
GenericDataSourcePlugin
}
from
'../settings/PluginSettings'
;
import
*
as
DatasourceSrv
from
'app/features/plugins/datasource_srv'
;
jest
.
mock
(
'app/features/plugins/datasource_srv'
);
const
getBackendSrvMock
=
()
=>
const
getDatasourceSrvMock
=
(
DatasourceSrv
.
getDatasourceSrv
as
any
)
as
jest
.
Mock
<
DatasourceSrv
.
DatasourceSrv
>
;
({
get
:
jest
.
fn
().
mockReturnValue
({
testDatasource
:
jest
.
fn
().
mockReturnValue
({
status
:
''
,
message
:
''
,
}),
}),
withNoBackendCache
:
jest
.
fn
().
mockImplementationOnce
(
cb
=>
cb
()),
}
as
any
);
describe
(
'Name exists'
,
()
=>
{
describe
(
'Name exists'
,
()
=>
{
const
plugins
=
getMockPlugins
(
5
);
const
plugins
=
getMockPlugins
(
5
);
...
@@ -131,8 +144,8 @@ describe('initDataSourceSettings', () => {
...
@@ -131,8 +144,8 @@ describe('initDataSourceSettings', () => {
describe
(
'testDataSource'
,
()
=>
{
describe
(
'testDataSource'
,
()
=>
{
describe
(
'when a datasource is tested'
,
()
=>
{
describe
(
'when a datasource is tested'
,
()
=>
{
it
(
'then testDataSourceStarting and testDataSourceSucceeded should be dispatched'
,
async
()
=>
{
it
(
'then testDataSourceStarting and testDataSourceSucceeded should be dispatched'
,
async
()
=>
{
getDatasourceSrvMock
.
mockImplementation
(
const
dependencies
:
TestDataSourceDependencies
=
{
()
=>
getDatasourceSrv
:
()
=>
({
({
get
:
jest
.
fn
().
mockReturnValue
({
get
:
jest
.
fn
().
mockReturnValue
({
testDatasource
:
jest
.
fn
().
mockReturnValue
({
testDatasource
:
jest
.
fn
().
mockReturnValue
({
...
@@ -140,8 +153,9 @@ describe('testDataSource', () => {
...
@@ -140,8 +153,9 @@ describe('testDataSource', () => {
message
:
''
,
message
:
''
,
}),
}),
}),
}),
}
as
any
)
}
as
any
),
);
getBackendSrv
:
getBackendSrvMock
,
};
const
state
=
{
const
state
=
{
testingStatus
:
{
testingStatus
:
{
status
:
''
,
status
:
''
,
...
@@ -150,22 +164,23 @@ describe('testDataSource', () => {
...
@@ -150,22 +164,23 @@ describe('testDataSource', () => {
};
};
const
dispatchedActions
=
await
thunkTester
(
state
)
const
dispatchedActions
=
await
thunkTester
(
state
)
.
givenThunk
(
testDataSource
)
.
givenThunk
(
testDataSource
)
.
whenThunkIsDispatched
(
'Azure Monitor'
);
.
whenThunkIsDispatched
(
'Azure Monitor'
,
dependencies
);
expect
(
dispatchedActions
).
toEqual
([
testDataSourceStarting
(),
testDataSourceSucceeded
(
state
.
testingStatus
)]);
expect
(
dispatchedActions
).
toEqual
([
testDataSourceStarting
(),
testDataSourceSucceeded
(
state
.
testingStatus
)]);
});
});
it
(
'then testDataSourceFailed should be dispatched'
,
async
()
=>
{
it
(
'then testDataSourceFailed should be dispatched'
,
async
()
=>
{
getDatasourceSrvMock
.
mockImplementation
(
const
dependencies
:
TestDataSourceDependencies
=
{
()
=>
getDatasourceSrv
:
()
=>
({
({
get
:
jest
.
fn
().
mockReturnValue
({
get
:
jest
.
fn
().
mockReturnValue
({
testDatasource
:
jest
.
fn
().
mockImplementation
(()
=>
{
testDatasource
:
jest
.
fn
().
mockImplementation
(()
=>
{
throw
new
Error
(
'Error testing datasource'
);
throw
new
Error
(
'Error testing datasource'
);
}),
}),
}),
}),
}
as
any
)
}
as
any
),
);
getBackendSrv
:
getBackendSrvMock
,
};
const
result
=
{
const
result
=
{
message
:
'Error testing datasource'
,
message
:
'Error testing datasource'
,
};
};
...
@@ -177,7 +192,7 @@ describe('testDataSource', () => {
...
@@ -177,7 +192,7 @@ describe('testDataSource', () => {
};
};
const
dispatchedActions
=
await
thunkTester
(
state
)
const
dispatchedActions
=
await
thunkTester
(
state
)
.
givenThunk
(
testDataSource
)
.
givenThunk
(
testDataSource
)
.
whenThunkIsDispatched
(
'Azure Monitor'
);
.
whenThunkIsDispatched
(
'Azure Monitor'
,
dependencies
);
expect
(
dispatchedActions
).
toEqual
([
testDataSourceStarting
(),
testDataSourceFailed
(
result
)]);
expect
(
dispatchedActions
).
toEqual
([
testDataSourceStarting
(),
testDataSourceFailed
(
result
)]);
});
});
...
...
public/app/features/datasources/state/actions.ts
View file @
c4e31100
...
@@ -21,6 +21,7 @@ import {
...
@@ -21,6 +21,7 @@ import {
}
from
'./reducers'
;
}
from
'./reducers'
;
import
{
buildCategories
}
from
'./buildCategories'
;
import
{
buildCategories
}
from
'./buildCategories'
;
import
{
getDataSource
,
getDataSourceMeta
}
from
'./selectors'
;
import
{
getDataSource
,
getDataSourceMeta
}
from
'./selectors'
;
import
{
getDataSourceSrv
}
from
'@grafana/runtime'
;
export
interface
DataSourceTypesLoadedPayload
{
export
interface
DataSourceTypesLoadedPayload
{
plugins
:
DataSourcePluginMeta
[];
plugins
:
DataSourcePluginMeta
[];
...
@@ -34,6 +35,11 @@ export interface InitDataSourceSettingDependencies {
...
@@ -34,6 +35,11 @@ export interface InitDataSourceSettingDependencies {
importDataSourcePlugin
:
typeof
importDataSourcePlugin
;
importDataSourcePlugin
:
typeof
importDataSourcePlugin
;
}
}
export
interface
TestDataSourceDependencies
{
getDatasourceSrv
:
typeof
getDataSourceSrv
;
getBackendSrv
:
typeof
getBackendSrv
;
}
export
const
initDataSourceSettings
=
(
export
const
initDataSourceSettings
=
(
pageId
:
number
,
pageId
:
number
,
dependencies
:
InitDataSourceSettingDependencies
=
{
dependencies
:
InitDataSourceSettingDependencies
=
{
...
@@ -67,9 +73,15 @@ export const initDataSourceSettings = (
...
@@ -67,9 +73,15 @@ export const initDataSourceSettings = (
};
};
};
};
export
const
testDataSource
=
(
dataSourceName
:
string
):
ThunkResult
<
void
>
=>
{
export
const
testDataSource
=
(
dataSourceName
:
string
,
dependencies
:
TestDataSourceDependencies
=
{
getDatasourceSrv
,
getBackendSrv
,
}
):
ThunkResult
<
void
>
=>
{
return
async
(
dispatch
:
ThunkDispatch
,
getState
)
=>
{
return
async
(
dispatch
:
ThunkDispatch
,
getState
)
=>
{
const
dsApi
=
await
getDatasourceSrv
().
get
(
dataSourceName
);
const
dsApi
=
await
dependencies
.
getDatasourceSrv
().
get
(
dataSourceName
);
if
(
!
dsApi
.
testDatasource
)
{
if
(
!
dsApi
.
testDatasource
)
{
return
;
return
;
...
@@ -77,7 +89,7 @@ export const testDataSource = (dataSourceName: string): ThunkResult<void> => {
...
@@ -77,7 +89,7 @@ export const testDataSource = (dataSourceName: string): ThunkResult<void> => {
dispatch
(
testDataSourceStarting
());
dispatch
(
testDataSourceStarting
());
getBackendSrv
().
withNoBackendCache
(
async
()
=>
{
dependencies
.
getBackendSrv
().
withNoBackendCache
(
async
()
=>
{
try
{
try
{
const
result
=
await
dsApi
.
testDatasource
();
const
result
=
await
dsApi
.
testDatasource
();
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment