Skip to content

Commit

Permalink
fix: root header doesn't display when there are no elements (#13439)
Browse files Browse the repository at this point in the history
  • Loading branch information
lassopicasso authored Sep 6, 2024
1 parent 6ac454a commit 61e0ab2
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import type { QueryClient } from '@tanstack/react-query';
import userEvent from '@testing-library/user-event';
import { createApiErrorMock } from 'app-shared/mocks/apiErrorMock';
import { app, org } from '@studio/testing/testids';

import { user as userMock } from 'app-shared/mocks/mocks';
// workaround for https://jestjs.io/docs/26.x/manual-mocks#mocking-methods-which-are-not-implemented-in-jsdom
Object.defineProperty(window, 'matchMedia', {
writable: true,
Expand Down Expand Up @@ -99,7 +99,9 @@ describe('DataModelling', () => {
);
queryClient.setQueryData([QueryKey.DataModelsJson, org, app], [jsonMetadata1Mock]);
queryClient.setQueryData([QueryKey.DataModelsXsd, org, app], []);
queryClient.setQueryData([QueryKey.CurrentUser], userMock);
render({ generateModels }, queryClient);

const errorsPanel = screen.queryByText(textMock('api_errors.DM_01'));
expect(errorsPanel).not.toBeInTheDocument();

Expand All @@ -125,6 +127,7 @@ describe('DataModelling', () => {
);
queryClient.setQueryData([QueryKey.DataModelsJson, org, app], [jsonMetadata1Mock]);
queryClient.setQueryData([QueryKey.DataModelsXsd, org, app], []);
queryClient.setQueryData([QueryKey.CurrentUser], userMock);
render({ generateModels }, queryClient);

const generateModelButton = screen.getByRole('button', {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,21 @@ describe('SchemaEditor', () => {
expect(updatedModel.asArray().length).toBe(uiSchemaNodesMock.length + 1);
});

test('should show context menu when there are no nodes on root', async () => {
const jsonSchema: JsonSchema = {
[Keyword.Properties]: {},
[Keyword.Definitions]: {},
};
const schemaModel = SchemaModel.fromArray(buildUiSchema(jsonSchema));
renderEditor({ appContextProps: { schemaModel } });

const noItemsSelectedMessage = screen.getByText(textMock('schema_editor.no_item_selected'));
expect(noItemsSelectedMessage).toBeInTheDocument();

await clickOpenAddNodeButton();
expect(screen.getByRole('dialog')).toBeInTheDocument();
});

test('should show context menu and trigger correct dispatch when adding text field on a specific node', async () => {
const jsonSchema: JsonSchema = {
[Keyword.Properties]: { mockItem: { [Keyword.Type]: FieldType.Object } },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ export const SchemaEditor = () => {
const { data: user } = useUserQuery();
const moveProperty = useMoveProperty();
const addReference = useAddReference();

if (schemaModel.isEmpty()) return null;
const definitions: UiSchemaNodes = schemaModel.getDefinitions();
const selectedType = selectedTypePointer && schemaModel.getNode(selectedTypePointer);

Expand Down
14 changes: 0 additions & 14 deletions frontend/packages/schema-model/src/lib/SchemaModel.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,20 +84,6 @@ describe('SchemaModel', () => {
});
});

describe('isEmpty', () => {
it('Returns true if only the root node is present', () => {
const rootNode = { ...rootNodeMock, children: [] };
const schema = [rootNode];
validateTestUiSchema(schema);
const emptyModel = SchemaModel.fromArray(schema);
expect(emptyModel.isEmpty()).toBe(true);
});

it('Returns false if additional nodes are present', () => {
expect(schemaModel.isEmpty()).toBe(false);
});
});

describe('getRootNode', () => {
it('Returns the root node', () => {
expect(schemaModel.getRootNode()).toEqual(rootNodeMock);
Expand Down
4 changes: 0 additions & 4 deletions frontend/packages/schema-model/src/lib/SchemaModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,6 @@ export class SchemaModel {
return Array.from(this.nodeMap.values());
}

public isEmpty(): boolean {
return this.nodeMap.size <= 1;
}

public getRootNode(): FieldNode | CombinationNode {
const rootNode = this.getNode(ROOT_POINTER);
if (!isFieldOrCombination(rootNode))
Expand Down

0 comments on commit 61e0ab2

Please sign in to comment.