Skip to content

Commit

Permalink
feat(bulk-import): update preview form to use separate formik context
Browse files Browse the repository at this point in the history
  • Loading branch information
debsmita1 committed Nov 5, 2024
1 parent 9eeb889 commit 72d17c8
Show file tree
Hide file tree
Showing 11 changed files with 191 additions and 232 deletions.
5 changes: 5 additions & 0 deletions workspaces/bulk-import/.changeset/nervous-waves-lie.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@red-hat-developer-hub/backstage-plugin-bulk-import': minor
---

update preview form to use separate formik context
3 changes: 2 additions & 1 deletion workspaces/bulk-import/plugins/bulk-import/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@
"js-yaml": "^4.1.0",
"lodash": "^4.17.21",
"react-use": "^17.2.4",
"yaml": "^2.0.0"
"yaml": "^2.0.0",
"yup": "^1.4.0"
},
"peerDependencies": {
"react": "16.13.1 || ^17.0.0 || ^18.0.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ export const PreviewFileSidebar = ({
[id]: {
error: {
message: [
`The entity YAML in your pull request is invalid (empty file or missing apiVersion, kind, or metadata.name). A new YAML has been generated below.`,
'The entity YAML in your pull request is invalid (empty file or missing apiVersion, kind, or metadata.name). A new YAML has been generated below.',
],
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ export const PreviewFileSidebarDrawerContent = ({
isSubmitting ||
(!!formErrors &&
Object.values(formErrors).length > 0 &&
Object.values(formErrors).every(
Object.values(formErrors).some(
fe => !!fe && Object.values(fe).length > 0,
))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,6 @@ export const PreviewPullRequest = ({
}) => {
const { status } = useFormikContext<AddRepositoriesFormValues>();

const [entityOwner, setEntityOwner] = React.useState<string>('');

const error = status?.errors?.[repoId];
const info = status?.infos?.[repoId];
if (
Expand Down Expand Up @@ -114,8 +112,6 @@ export const PreviewPullRequest = ({
</Box>
)}
<PreviewPullRequestForm
entityOwner={entityOwner}
setEntityOwner={setEntityOwner}
repoId={repoId}
repoUrl={repoUrl}
pullRequest={pullRequest}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/ import React, { useState } from 'react';
*/ import React from 'react';

import { configApiRef } from '@backstage/core-plugin-api';
import { CatalogApi, catalogApiRef } from '@backstage/plugin-catalog-react';
import { MockConfigApi, TestApiProvider } from '@backstage/test-utils';

import { fireEvent, render } from '@testing-library/react';
import { fireEvent, render, screen, waitFor } from '@testing-library/react';
import { useFormikContext } from 'formik';

import { bulkImportApiRef } from '../../api/BulkImportBackendClient';
Expand All @@ -28,11 +28,6 @@ import { ApprovalTool, ImportJobStatus } from '../../types';
import { getPRTemplate } from '../../utils/repository-utils';
import { PreviewPullRequestForm } from './PreviewPullRequestForm';

jest.mock('react', () => ({
...jest.requireActual('react'),
useState: jest.fn(),
}));

jest.mock('@material-ui/core', () => ({
...jest.requireActual('@material-ui/core'),
makeStyles: () => () => {
Expand Down Expand Up @@ -64,12 +59,6 @@ class MockBulkImportApi {
}
}

const setState = jest.fn();

beforeEach(() => {
(useState as jest.Mock).mockImplementation(initial => [initial, setState]);
});

const mockBulkImportApi = new MockBulkImportApi();

const mockCatalogApi: Partial<CatalogApi> = {
Expand Down Expand Up @@ -110,8 +99,6 @@ describe('Preview Pull Request Form', () => {
<PreviewPullRequestForm
repoId="org/dessert/cupcake"
repoUrl="https://github.com/org/dessert/cupcake"
entityOwner="user:default/guest"
setEntityOwner={jest.fn()}
pullRequest={{
cupcake: getPRTemplate(
'org/dessert/cupcake',
Expand Down Expand Up @@ -165,8 +152,6 @@ describe('Preview Pull Request Form', () => {
<PreviewPullRequestForm
repoId="org/dessert/cupcake"
repoUrl="https://github.com/org/dessert/cupcake"
entityOwner="user:default/guest"
setEntityOwner={jest.fn()}
pullRequest={{
'org/dessert/cupcake': getPRTemplate(
'org/dessert/cupcake',
Expand Down Expand Up @@ -222,8 +207,6 @@ describe('Preview Pull Request Form', () => {
<PreviewPullRequestForm
repoId="org/dessert/cupcake"
repoUrl="https://github.com/org/dessert/cupcake"
entityOwner="user:default/guest"
setEntityOwner={jest.fn()}
pullRequest={{
'org/dessert/cupcake': getPRTemplate(
'org/dessert/cupcake',
Expand All @@ -244,18 +227,18 @@ describe('Preview Pull Request Form', () => {
/Add Backstage catalog entity descriptor files/,
);
fireEvent.change(prTitle, { target: { value: '' } });
expect(setFormErrors).toHaveBeenCalledWith({
'org/dessert/cupcake': {
prTitle: 'Pull request title is missing',
},
await waitFor(() => {
expect(
screen.queryByText('Pull request title is required'),
).toBeInTheDocument();
});

const componentName = getByPlaceholderText(/Component Name/);
fireEvent.change(componentName, { target: { value: '' } });
expect(setFormErrors).toHaveBeenCalledWith({
'org/dessert/cupcake': {
componentName: 'Component name is missing',
},
await waitFor(() => {
expect(
screen.queryByText('Component name is required'),
).toBeInTheDocument();
});
});
});
Loading

0 comments on commit 72d17c8

Please sign in to comment.