1- import React from 'react' ;
2- import { config , setLocationSrv } from '@grafana/runtime' ;
31import { shallow } from 'enzyme' ;
2+ import React from 'react' ;
3+ import { setLocationSrv } from '@grafana/runtime' ;
4+ import { ApplicationRoot } from '../../constants' ;
45import { Config } from './config' ;
56
67/*
@@ -18,8 +19,6 @@ const getPlugin = (overridePlugin: any = { meta: {} }) => ({
1819 Config
1920 */
2021describe ( 'Config' , ( ) => {
21- let initialDataSources = config . datasources ;
22-
2322 beforeAll ( ( ) => {
2423 jest . spyOn ( Config , 'getLocation' ) . mockImplementation ( ( ) : any => ( {
2524 assign : jest . fn ( ) ,
@@ -31,67 +30,41 @@ describe('Config', () => {
3130 Initialization
3231 */
3332 describe ( 'Initialization' , ( ) => {
34- it ( 'If plugin is not enabled, state should have isEnabled = false and isConfigured = false' , ( ) => {
35- const plugin = getPlugin ( { meta : { enabled : false } } ) ;
36- config . datasources = {
37- redis : {
38- type : 'redis-datasource' ,
39- } as any ,
40- } ;
33+ it ( 'If plugin is not enabled and meta is not set, state should have isEnabled = false' , ( ) => {
34+ const plugin = getPlugin ( { } ) ;
4135 const wrapper = shallow < Config > ( < Config plugin = { plugin } query = { null as any } /> ) ;
42- expect ( wrapper . state ( ) . isEnabled ) . toBeFalsy ( ) ;
43- expect ( wrapper . state ( ) . isConfigured ) . toBeFalsy ( ) ;
36+ expect ( wrapper . state ( ) . isEnabled ) . toBeTruthy ( ) ;
4437 } ) ;
4538
46- it ( 'If plugin is enabled but config does not have needed datasources, state should have isEnabled = true and isConfigured = false' , ( ) => {
47- const plugin = getPlugin ( { meta : { enabled : true } } ) ;
48- config . datasources = { } ;
39+ it ( 'If plugin is not enabled, state should have isEnabled = false' , ( ) => {
40+ const plugin = getPlugin ( { meta : { enabled : false } } ) ;
4941 const wrapper = shallow < Config > ( < Config plugin = { plugin } query = { null as any } /> ) ;
50- expect ( wrapper . state ( ) . isEnabled ) . toBeTruthy ( ) ;
51- expect ( wrapper . state ( ) . isConfigured ) . toBeFalsy ( ) ;
42+ expect ( wrapper . state ( ) . isEnabled ) . toBeFalsy ( ) ;
5243 } ) ;
5344
54- it ( 'If plugin is enabled and config has needed datasources , state should have isEnabled = true and isConfigured = true' , ( ) => {
45+ it ( 'If plugin is enabled, state should have isEnabled = true' , ( ) => {
5546 const plugin = getPlugin ( { meta : { enabled : true } } ) ;
56- config . datasources = {
57- redis : {
58- type : 'redis-datasource' ,
59- } as any ,
60- } ;
6147 const wrapper = shallow < Config > ( < Config plugin = { plugin } query = { null as any } /> ) ;
6248 expect ( wrapper . state ( ) . isEnabled ) . toBeTruthy ( ) ;
63- expect ( wrapper . state ( ) . isConfigured ) . toBeTruthy ( ) ;
6449 } ) ;
6550 } ) ;
6651
6752 /*
6853 Rendering
6954 */
7055 describe ( 'rendering' , ( ) => {
71- beforeAll ( ( ) => {
72- config . datasources = {
73- redis : {
74- type : 'redis-datasource' ,
75- } as any ,
76- } ;
77- } ) ;
78-
79- it ( 'If plugin is not configured, should show only enable button' , ( ) => {
56+ it ( 'If plugin is not configured, should show enable button' , ( ) => {
8057 const plugin = getPlugin ( { meta : { enabled : false } } ) ;
8158 const wrapper = shallow < Config > ( < Config plugin = { plugin } query = { null as any } /> ) ;
8259 const enableButton = wrapper . findWhere ( ( node ) => node . name ( ) === 'Button' && node . text ( ) === 'Enable' ) ;
83- const updateButton = wrapper . findWhere ( ( node ) => node . name ( ) === 'Button' && node . text ( ) === 'Update' ) ;
8460 expect ( enableButton . exists ( ) ) . toBeTruthy ( ) ;
85- expect ( updateButton . exists ( ) ) . not . toBeTruthy ( ) ;
8661 } ) ;
8762
88- it ( 'If plugin is configured, should show update and disable buttons' , ( ) => {
63+ it ( 'If plugin is configured, should show disable buttons' , ( ) => {
8964 const plugin = getPlugin ( { meta : { enabled : true } } ) ;
9065 const wrapper = shallow < Config > ( < Config plugin = { plugin } query = { null as any } /> ) ;
9166 const disableButton = wrapper . findWhere ( ( node ) => node . name ( ) === 'Button' && node . text ( ) === 'Disable' ) ;
92- const updateButton = wrapper . findWhere ( ( node ) => node . name ( ) === 'Button' && node . text ( ) === 'Update' ) ;
9367 expect ( disableButton . exists ( ) ) . toBeTruthy ( ) ;
94- expect ( updateButton . exists ( ) ) . toBeTruthy ( ) ;
9568 } ) ;
9669
9770 it ( 'Enable button should call onEnable method' , ( ) => {
@@ -113,38 +86,12 @@ describe('Config', () => {
11386 disableButton . simulate ( 'click' ) ;
11487 expect ( testedMethod ) . toHaveBeenCalled ( ) ;
11588 } ) ;
116-
117- it ( 'Update button should call onUpdate method' , ( ) => {
118- const plugin = getPlugin ( { meta : { enabled : true } } ) ;
119- const wrapper = shallow < Config > ( < Config plugin = { plugin } query = { null as any } /> ) ;
120- const testedMethod = jest . spyOn ( wrapper . instance ( ) , 'onUpdate' ) . mockImplementation ( ( ) => null ) ;
121- wrapper . instance ( ) . forceUpdate ( ) ;
122- const updateButton = wrapper . findWhere ( ( node ) => node . name ( ) === 'Button' && node . text ( ) === 'Update' ) ;
123- updateButton . simulate ( 'click' ) ;
124- expect ( testedMethod ) . toHaveBeenCalled ( ) ;
125- } ) ;
12689 } ) ;
12790
12891 /*
12992 Methods
13093 */
13194 describe ( 'Methods' , ( ) => {
132- it ( 'onUpdate should call goHome method' , ( ) => {
133- const plugin = getPlugin ( { meta : { enabled : true } } ) ;
134- const wrapper = shallow < Config > ( < Config plugin = { plugin } query = { null as any } /> ) ;
135- const testedMethod = jest . spyOn ( wrapper . instance ( ) , 'goHome' ) . mockImplementation ( ) ;
136- wrapper . instance ( ) . onUpdate ( ) ;
137- expect ( testedMethod ) . toHaveBeenCalled ( ) ;
138- } ) ;
139-
140- it ( 'onUpdate should not call goHome method if enabled=false' , ( ) => {
141- const plugin = getPlugin ( { meta : { enabled : false } } ) ;
142- const wrapper = shallow < Config > ( < Config plugin = { plugin } query = { null as any } /> ) ;
143- const testedMethod = jest . spyOn ( wrapper . instance ( ) , 'goHome' ) . mockImplementation ( ) ;
144- wrapper . instance ( ) . onUpdate ( ) ;
145- expect ( testedMethod ) . not . toHaveBeenCalled ( ) ;
146- } ) ;
147-
14895 it ( 'onDisable should call updatePluginSettings method' , ( ) => {
14996 const plugin = getPlugin ( { meta : { enabled : true } } ) ;
15097 const wrapper = shallow < Config > ( < Config plugin = { plugin } query = { null as any } /> ) ;
@@ -186,14 +133,13 @@ describe('Config', () => {
186133 const wrapper = shallow < Config > ( < Config plugin = { plugin } query = { null as any } /> ) ;
187134 wrapper . instance ( ) . goHome ( ) ;
188135 expect ( updateLocationMock ) . toHaveBeenCalledWith ( {
189- path : 'a/redis-app/' ,
136+ path : ApplicationRoot ,
190137 partial : false ,
191138 } ) ;
192139 } ) ;
193140 } ) ;
194141
195142 afterAll ( ( ) => {
196- config . datasources = initialDataSources ;
197143 jest . resetAllMocks ( ) ;
198144 } ) ;
199145} ) ;
0 commit comments