Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix for resetting url in config page #281

Merged
merged 2 commits into from
Dec 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
@@ -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
),
};
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
33 changes: 33 additions & 0 deletions src/views/ConfigEditor.spec.tsx
Original file line number Diff line number Diff line change
@@ -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(<ConfigEditor options={options} onOptionsChange={onOptionsChange} />);
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(<ConfigEditor options={options} onOptionsChange={onOptionsChange} />);
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();
});
});
5 changes: 3 additions & 2 deletions src/views/ConfigEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import { Divider } from 'components/Divider';
export type ConfigEditorProps = DataSourcePluginOptionsEditorProps<GithubDataSourceOptions, GithubSecureJsonData>;

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);

Expand Down Expand Up @@ -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);
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down