diff --git a/jest.config.js b/jest.config.js index 79fd52a1..a95f7896 100644 --- a/jest.config.js +++ b/jest.config.js @@ -1,8 +1,14 @@ +const { grafanaESModules, nodeModulesToTransform } = require('./.config/jest/utils'); + // force timezone to UTC to allow tests to work regardless of local timezone // generally used by snapshots, but can affect specific tests process.env.TZ = 'UTC'; +const originalConfig = require('./.config/jest.config'); module.exports = { // Jest configuration provided by Grafana scaffolding - ...require('./.config/jest.config'), + ...originalConfig, + transformIgnorePatterns: originalConfig.transformIgnorePatterns.map((pattern) => + pattern.startsWith('node_modules') ? `../../${pattern}` : pattern + ), }; diff --git a/package.json b/package.json index 31676cca..88d7dee6 100644 --- a/package.json +++ b/package.json @@ -41,6 +41,7 @@ "@swc/jest": "^0.2.23", "@testing-library/jest-dom": "^5.16.2", "@testing-library/react": "^12.1.4", + "@testing-library/user-event": "^14.5.1", "@types/glob": "^8.0.0", "@types/jest": "^29.2.2", "@types/lodash": "^4.14.188", diff --git a/src/views/ConfigEditor.spec.tsx b/src/views/ConfigEditor.spec.tsx new file mode 100644 index 00000000..385d0666 --- /dev/null +++ b/src/views/ConfigEditor.spec.tsx @@ -0,0 +1,33 @@ +import React from 'react'; +import { screen, render, waitFor } from '@testing-library/react'; +import userEvent from '@testing-library/user-event'; +import ConfigEditor from './ConfigEditor'; + +describe('Config Editor', () => { + it('should select basic license type as checked by default', async () => { + const onOptionsChange = jest.fn(); + const options = { jsonData: {}, secureJsonFields: {} } as any; + render(); + await waitFor(() => expect(screen.getByText('Additional Settings')).toBeInTheDocument()); + expect(screen.getByLabelText('Basic')).toBeChecked(); + expect(screen.getByLabelText('Enterprise')).not.toBeChecked(); + expect(screen.queryByText('GitHub Enterprise URL')).not.toBeInTheDocument(); + expect(onOptionsChange).toHaveBeenCalledTimes(0); + await userEvent.click(screen.getByLabelText('Enterprise')); + expect(onOptionsChange).toHaveBeenCalledTimes(0); + expect(screen.queryByText('GitHub Enterprise URL')).toBeInTheDocument(); + }); + it('should select enterprise license type as checked when the url is not empty', async () => { + const onOptionsChange = jest.fn(); + const options = { jsonData: { githubUrl: 'https://foo.bar' }, secureJsonFields: {} } as any; + render(); + await waitFor(() => expect(screen.getByText('Additional Settings')).toBeInTheDocument()); + expect(screen.getByLabelText('Basic')).not.toBeChecked(); + expect(screen.getByLabelText('Enterprise')).toBeChecked(); + expect(screen.queryByText('GitHub Enterprise URL')).toBeInTheDocument(); + expect(onOptionsChange).toHaveBeenCalledTimes(0); + await userEvent.click(screen.getByLabelText('Basic')); + expect(onOptionsChange).toHaveBeenNthCalledWith(1, { jsonData: { githubUrl: '' }, secureJsonFields: {} }); + expect(screen.queryByText('GitHub Enterprise URL')).not.toBeInTheDocument(); + }); +}); diff --git a/src/views/ConfigEditor.tsx b/src/views/ConfigEditor.tsx index d7568a57..058600c0 100644 --- a/src/views/ConfigEditor.tsx +++ b/src/views/ConfigEditor.tsx @@ -10,7 +10,8 @@ import { Divider } from 'components/Divider'; export type ConfigEditorProps = DataSourcePluginOptionsEditorProps; const ConfigEditor = (props: ConfigEditorProps) => { - const { jsonData, secureJsonData, secureJsonFields } = props.options; + const { options, onOptionsChange } = props; + const { jsonData, secureJsonData, secureJsonFields } = options; const secureSettings = (secureJsonData || {}) as GithubSecureJsonData; const styles = useStyles2(getStyles); @@ -40,7 +41,7 @@ const ConfigEditor = (props: ConfigEditorProps) => { const onLicenseChange = (value: string) => { if (value === 'github-basic') { - jsonData.githubUrl = ''; + onOptionsChange({ ...options, jsonData: { ...jsonData, githubUrl: '' } }); } setSelectedLicense(value); diff --git a/yarn.lock b/yarn.lock index c299fabb..2cafcbd7 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3684,6 +3684,11 @@ "@testing-library/dom" "^8.0.0" "@types/react-dom" "<18.0.0" +"@testing-library/user-event@^14.5.1": + version "14.5.1" + resolved "https://registry.yarnpkg.com/@testing-library/user-event/-/user-event-14.5.1.tgz#27337d72046d5236b32fd977edee3f74c71d332f" + integrity sha512-UCcUKrUYGj7ClomOo2SpNVvx4/fkd/2BbIHDCle8A0ax+P3bU7yJwDBDrS6ZwdTMARWTGODX1hEsCcO+7beJjg== + "@tootallnate/once@2": version "2.0.0" resolved "https://registry.yarnpkg.com/@tootallnate/once/-/once-2.0.0.tgz#f544a148d3ab35801c1f633a7441fd87c2e484bf"