grafana/public/app/features/org/OrgDetailsPage.test.tsx

116 lines
2.9 KiB
TypeScript
Raw Normal View History

import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import React from 'react';
import { mockToolkitActionCreator } from 'test/core/redux/mocks';
import { TestProvider } from 'test/helpers/TestProvider';
import { NavModel } from '@grafana/data';
import { ModalManager } from 'app/core/services/ModalManager';
import { backendSrv } from '../../core/services/backend_srv';
import { Organization } from '../../types';
import { OrgDetailsPage, Props } from './OrgDetailsPage';
import { setOrganizationName } from './state/reducers';
2018-10-29 07:46:12 -05:00
AccessControl: FGAC permissions for orgs endpoint on frontend (#41050) * AccessControl: FGAC permissions for orgs endpoint on frontend Protect org update endpoints add or refactor missing right messages cover org page * removing scopes from orgs * Perform permission control with global org * Perform the error handling in case of 403 * Simplify frontend code by requiring read access for sure * Remove roles I added to decrease the number of changes * Remove the check for server admin to reduce the number of changes * change error message * Cleaning todos * Remove unecessary changes * Fix tests * Update test snapshot * Update pkg/api/roles.go Co-authored-by: Ursula Kallio <73951760+osg-grafana@users.noreply.github.com> * Update public/app/features/admin/AdminEditOrgPage.tsx Co-authored-by: Ursula Kallio <73951760+osg-grafana@users.noreply.github.com> * Format AdminEditOrgPage for linting * Update public/app/features/admin/AdminEditOrgPage.tsx Co-authored-by: Vardan Torosyan <vardants@gmail.com> * Update public/app/features/admin/AdminEditOrgPage.tsx Co-authored-by: Alexander Zobnin <alexanderzobnin@gmail.com> * Update public/app/features/admin/AdminListOrgsPage.tsx Co-authored-by: Alexander Zobnin <alexanderzobnin@gmail.com> * Commit suggestions * Commit suggestion canRead canWrite * fix typo Co-authored-by: Ursula Kallio <73951760+osg-grafana@users.noreply.github.com> Co-authored-by: Vardan Torosyan <vardants@gmail.com> Co-authored-by: Alexander Zobnin <alexanderzobnin@gmail.com>
2021-11-18 07:10:38 -06:00
jest.mock('app/core/core', () => {
return {
...jest.requireActual('app/core/core'),
AccessControl: FGAC permissions for orgs endpoint on frontend (#41050) * AccessControl: FGAC permissions for orgs endpoint on frontend Protect org update endpoints add or refactor missing right messages cover org page * removing scopes from orgs * Perform permission control with global org * Perform the error handling in case of 403 * Simplify frontend code by requiring read access for sure * Remove roles I added to decrease the number of changes * Remove the check for server admin to reduce the number of changes * change error message * Cleaning todos * Remove unecessary changes * Fix tests * Update test snapshot * Update pkg/api/roles.go Co-authored-by: Ursula Kallio <73951760+osg-grafana@users.noreply.github.com> * Update public/app/features/admin/AdminEditOrgPage.tsx Co-authored-by: Ursula Kallio <73951760+osg-grafana@users.noreply.github.com> * Format AdminEditOrgPage for linting * Update public/app/features/admin/AdminEditOrgPage.tsx Co-authored-by: Vardan Torosyan <vardants@gmail.com> * Update public/app/features/admin/AdminEditOrgPage.tsx Co-authored-by: Alexander Zobnin <alexanderzobnin@gmail.com> * Update public/app/features/admin/AdminListOrgsPage.tsx Co-authored-by: Alexander Zobnin <alexanderzobnin@gmail.com> * Commit suggestions * Commit suggestion canRead canWrite * fix typo Co-authored-by: Ursula Kallio <73951760+osg-grafana@users.noreply.github.com> Co-authored-by: Vardan Torosyan <vardants@gmail.com> Co-authored-by: Alexander Zobnin <alexanderzobnin@gmail.com>
2021-11-18 07:10:38 -06:00
contextSrv: {
hasPermission: () => true,
},
};
});
jest.mock('@grafana/runtime', () => {
const originalModule = jest.requireActual('@grafana/runtime');
return {
...originalModule,
config: {
...originalModule.config,
featureToggles: {
internationalization: true,
},
},
};
});
2018-10-29 07:46:12 -05:00
const setup = (propOverrides?: object) => {
jest.clearAllMocks();
// needed because SharedPreferences is rendered in the test
jest.spyOn(backendSrv, 'put');
jest
.spyOn(backendSrv, 'get')
.mockResolvedValue({ timezone: 'UTC', homeDashboardUID: 'home-dashboard', theme: 'dark' });
jest.spyOn(backendSrv, 'search').mockResolvedValue([]);
2018-10-29 07:46:12 -05:00
const props: Props = {
organization: {} as Organization,
2019-01-16 09:29:07 -06:00
navModel: {
main: {
text: 'Configuration',
2019-01-16 09:29:07 -06:00
},
node: {
text: 'Org details',
},
2019-01-16 09:29:07 -06:00
} as NavModel,
2018-10-29 07:46:12 -05:00
loadOrganization: jest.fn(),
setOrganizationName: mockToolkitActionCreator(setOrganizationName),
2018-10-29 07:46:12 -05:00
updateOrganization: jest.fn(),
};
Object.assign(props, propOverrides);
render(
<TestProvider>
<OrgDetailsPage {...props} />
</TestProvider>
);
2018-10-29 07:46:12 -05:00
};
describe('Render', () => {
beforeEach(() => {
jest.spyOn(console, 'error').mockImplementation(() => {});
});
2018-10-29 07:46:12 -05:00
it('should render component', () => {
expect(() => setup()).not.toThrow();
2018-10-29 07:46:12 -05:00
});
it('should render organization and preferences', () => {
expect(() =>
setup({
organization: {
name: 'Cool org',
id: 1,
},
preferences: {
homeDashboardUID: 'home-dashboard',
theme: 'Default',
timezone: 'Default',
locale: '',
},
})
).not.toThrow();
2018-10-29 07:46:12 -05:00
});
it('should show a modal when submitting', async () => {
new ModalManager().init();
setup({
organization: {
name: 'Cool org',
id: 1,
},
preferences: {
homeDashboardUID: 'home-dashboard',
theme: 'Default',
timezone: 'Default',
locale: '',
},
});
await userEvent.click(screen.getByRole('button', { name: 'Save' }));
expect(screen.getByText('Confirm preferences update')).toBeInTheDocument();
});
2018-10-29 07:46:12 -05:00
});