mirror of https://github.com/grafana/grafana
Admin: Fix updating organization name not updating configuration subtitle (#26315)
* Fix updating organization name not updating configuration subtitle * PR feedback: Remove unnecessary square brackets * Refactor code to update redux state in a safer way, add org action test * Refactor updateConfigurationSubtitle test, remove jest.mock usage * Consolidate dependency typepull/26852/head
parent
eed313f312
commit
bf3dcc809d
@ -1,5 +1,5 @@ |
||||
import { clearAppNotification, notifyApp } from '../reducers/appNotification'; |
||||
import { updateLocation } from '../reducers/location'; |
||||
import { updateNavIndex } from '../reducers/navModel'; |
||||
import { updateNavIndex, updateConfigurationSubtitle } from '../reducers/navModel'; |
||||
|
||||
export { updateLocation, updateNavIndex, notifyApp, clearAppNotification }; |
||||
export { updateLocation, updateNavIndex, updateConfigurationSubtitle, notifyApp, clearAppNotification }; |
||||
|
@ -0,0 +1,40 @@ |
||||
import { updateOrganization } from './actions'; |
||||
import { updateConfigurationSubtitle } from 'app/core/actions'; |
||||
import { thunkTester } from 'test/core/thunk/thunkTester'; |
||||
|
||||
const setup = () => { |
||||
const initialState = { |
||||
organization: { |
||||
organization: { |
||||
id: 1, |
||||
name: 'New Org Name', |
||||
}, |
||||
}, |
||||
}; |
||||
|
||||
return { |
||||
initialState, |
||||
}; |
||||
}; |
||||
|
||||
describe('updateOrganization', () => { |
||||
describe('when updateOrganization thunk is dispatched', () => { |
||||
const getMock = jest.fn().mockResolvedValue({ id: 1, name: 'New Org Name' }); |
||||
const putMock = jest.fn().mockResolvedValue({ id: 1, name: 'New Org Name' }); |
||||
const backendSrvMock: any = { |
||||
get: getMock, |
||||
put: putMock, |
||||
}; |
||||
|
||||
it('then it should dispatch updateConfigurationSubtitle', async () => { |
||||
const { initialState } = setup(); |
||||
|
||||
const dispatchedActions = await thunkTester(initialState) |
||||
.givenThunk(updateOrganization) |
||||
.whenThunkIsDispatched({ getBackendSrv: () => backendSrvMock }); |
||||
|
||||
expect(dispatchedActions[0].type).toEqual(updateConfigurationSubtitle.type); |
||||
expect(dispatchedActions[0].payload).toEqual(initialState.organization.organization.name); |
||||
}); |
||||
}); |
||||
}); |
@ -1,22 +1,30 @@ |
||||
import { ThunkResult } from 'app/types'; |
||||
import { getBackendSrv } from '@grafana/runtime'; |
||||
import { organizationLoaded } from './reducers'; |
||||
import { updateConfigurationSubtitle } from 'app/core/actions'; |
||||
|
||||
export function loadOrganization(): ThunkResult<any> { |
||||
type OrganizationDependencies = { getBackendSrv: typeof getBackendSrv }; |
||||
|
||||
export function loadOrganization( |
||||
dependencies: OrganizationDependencies = { getBackendSrv: getBackendSrv } |
||||
): ThunkResult<any> { |
||||
return async dispatch => { |
||||
const organizationResponse = await getBackendSrv().get('/api/org'); |
||||
const organizationResponse = await dependencies.getBackendSrv().get('/api/org'); |
||||
dispatch(organizationLoaded(organizationResponse)); |
||||
|
||||
return organizationResponse; |
||||
}; |
||||
} |
||||
|
||||
export function updateOrganization(): ThunkResult<any> { |
||||
export function updateOrganization( |
||||
dependencies: OrganizationDependencies = { getBackendSrv: getBackendSrv } |
||||
): ThunkResult<any> { |
||||
return async (dispatch, getStore) => { |
||||
const organization = getStore().organization.organization; |
||||
|
||||
await getBackendSrv().put('/api/org', { name: organization.name }); |
||||
await dependencies.getBackendSrv().put('/api/org', { name: organization.name }); |
||||
|
||||
dispatch(loadOrganization()); |
||||
dispatch(updateConfigurationSubtitle(organization.name)); |
||||
dispatch(loadOrganization(dependencies)); |
||||
}; |
||||
} |
||||
|
Loading…
Reference in new issue