Skip to content

Commit

Permalink
KOGITO-5150: Create the mechanism for fetching fields from nest types (
Browse files Browse the repository at this point in the history
…apache#122)

* KOGITO-5150: Fetching mechanism

* KOGITO-5150: Fetching mechanism

* KOGITO-5150: Fetching mechanism

* KOGITO-5150: Fetching mechanism

* KOGITO-5150: Fetching mechanism

* KOGITO-5150: Fetching mechanism

* KOGITO-5150: Fetching mechanism

* KOGITO-5150: Fetching mechanism

* KOGITO-5150: Fetching mechanism

* KOGITO-5150: Changes requests

* KOGITO-5150: Snapshots tests fixed

* KOGITO-5150: Wizard cleanup at closure

* KOGITO-5150: Wizard cleanup at closure

* KOGITO-5150: Fetching mechanism

* KOGITO-5150: Fetching mechanism

* KOGITO-5150: Fetching mechanism

* KOGITO-5150: Fetching mechanism

* KOGITO-5150: Fetching mechanism

* KOGITO-5150: Fetching mechanism

* KOGITO-5150: Fetching mechanism

* KOGITO-5150: Fetching mechanism

* KOGITO-5150: Fetching mechanism

* KOGITO-5150: Changes requests

* KOGITO-5150: Snapshots tests fixed

* KOGITO-5150: Wizard cleanup at closure

* KOGITO-5150: Wizard cleanup at closure

* KOGITO-5150: Applying Change Requests

* KOGITO-5150: Applying Change Requests

* KOGITO-5150: Applying Change Requests

* KOGITO-5150: Prettier is now happy

* KOGITO-5150: Prettier is now happy

* KOGITO-5150: Snapshots updated

* KOGITO-5150: Tests

* KOGITO-5150: Tests

* KOGITO-5150: Tests

* KOGITO-5150: Tests

* KOGITO-5150: Prettier
  • Loading branch information
yesamer authored Oct 12, 2021
1 parent 94e827a commit a843578
Show file tree
Hide file tree
Showing 20 changed files with 1,044 additions and 170 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@ code {
margin: 20px;
}

.import-java-classes {
.main {
margin: 100px;
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,27 +44,38 @@ const Showcase: React.FunctionComponent = () => {
return [];
}
};
const lspGetClassFieldsServiceMocked = (className: string) => {
const lspGetClassFieldsServiceMocked = async (className: string) => {
/* Mocked data retrieved from LSP Service */
const bookClassFieldsList = new Map<string, string>();
bookClassFieldsList.set("author", "string");
bookClassFieldsList.set("title", "string");
bookClassFieldsList.set("year", "integer");
bookClassFieldsList.set("author", "org.kie.test.kogito.Author");
bookClassFieldsList.set("title", "java.lang.String");
bookClassFieldsList.set("year", "java.lang.Integer");
bookClassFieldsList.set("boom", "org.kie.test.kogito.Boom");
const boomClassFieldsList = new Map<string, string>();
boomClassFieldsList.set("time", "date");
boomClassFieldsList.set("big", "boolean");
boomClassFieldsList.set("color", "string");
boomClassFieldsList.set("time", "java.util.Date");
boomClassFieldsList.set("big", "java.lang.Boolean");
boomClassFieldsList.set("color", "java.lang.String");
boomClassFieldsList.set("countdown", "java.time.Duration");
const authorClassFieldsList = new Map<string, string>();
authorClassFieldsList.set("age", "int");
authorClassFieldsList.set("name", "java.lang.String");

await delay();

/* Temporary mocks managing */
if (className === "org.kie.test.kogito.Book") {
return bookClassFieldsList;
} else if (className === "org.kie.test.kogito.Boom") {
return boomClassFieldsList;
} else if (className === "org.kie.test.kogito.Author") {
return authorClassFieldsList;
} else {
return new Map<string, string>();
}
};

const delay = () => new Promise((res) => setTimeout(res, Math.random() * (4000 - 750) + 1000));

window.envelopeMock = {
lspGetClassServiceMocked: (value: string) => lspGetClassServiceMocked(value),
lspGetClassFieldsServiceMocked: (className: string) => lspGetClassFieldsServiceMocked(className),
Expand Down Expand Up @@ -92,7 +103,7 @@ const Showcase: React.FunctionComponent = () => {
<strong>Tooltip Message (Optional)</strong>
<input value={buttonTooltipMessage} onChange={onInputChange} />
</div>
<div className="import-java-classes">
<div className="main">
<ImportJavaClasses buttonDisabledStatus={buttonDisableStatus} buttonTooltipMessage={buttonTooltipMessage} />
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/

import * as React from "react";
import { fireEvent, render } from "@testing-library/react";
import { fireEvent, render, waitFor } from "@testing-library/react";
import { ImportJavaClasses } from "../../components";
import * as _ from "lodash";

Expand Down Expand Up @@ -69,23 +69,59 @@ describe("ImportJavaClasses component tests", () => {
expect(baseElement).toMatchSnapshot();
});

test("Should move to second step", () => {
test("Should move to second step", async () => {
lspGetClassServiceMock(jest.fn((value) => ["com.Book", "com.Author", "com.Test"]));
lspGetClassFieldServiceMock(jest.fn(lspGetClassFieldsServiceMocked));
lspGetClassFieldServiceMock();
const { baseElement, getByText } = render(<ImportJavaClasses buttonDisabledStatus={false} />);
testSearchInput(baseElement, getByText);
testJavaClassSelection(baseElement, true);
const nextButton = getByText("Next") as HTMLButtonElement;
fireEvent.click(nextButton);
const expandToggle = baseElement.querySelector('[id="expand-toggle0"]')! as HTMLButtonElement;
expect(expandToggle).toHaveAttribute("aria-expanded", "true");
fireEvent.click(expandToggle);
expect(expandToggle).toHaveAttribute("aria-expanded", "false");
fireEvent.click(expandToggle);
await testSecondStepFields(baseElement, getByText);

expect(baseElement).toMatchSnapshot();
});

test("Should move to second step and fetch a Java Class", async () => {
lspGetClassServiceMock(jest.fn((value) => ["com.Book", "com.Author", "com.Test"]));
lspGetClassFieldServiceMock();
const { baseElement, getByText } = render(<ImportJavaClasses buttonDisabledStatus={false} />);
testSearchInput(baseElement, getByText);
testJavaClassSelection(baseElement, true);
await testSecondStepFields(baseElement, getByText);

const fetchButton = getByText('Fetch "Test" class')! as HTMLButtonElement;
fetchButton.click();

await waitFor(() => {
expect(getByText("(Test)")!).toBeInTheDocument();
});

expect(baseElement).toMatchSnapshot();
});

test("Should move to second step and fetch, remove a Java Class", async () => {
lspGetClassServiceMock(jest.fn((value) => ["com.Book", "com.Author", "com.Test"]));
lspGetClassFieldServiceMock();
const { baseElement, getByText } = render(<ImportJavaClasses buttonDisabledStatus={false} />);
testSearchInput(baseElement, getByText);
testJavaClassSelection(baseElement, true);
await testSecondStepFields(baseElement, getByText);
await testFetchClicked(getByText);

const backButton = getByText("Back") as HTMLButtonElement;
fireEvent.click(backButton);
let checkThirdElement = baseElement.querySelector('[aria-labelledby="com.Test"]')! as HTMLInputElement;
expect(checkThirdElement).toBeInTheDocument();
expect(checkThirdElement).toBeChecked();
fireEvent.click(checkThirdElement);
checkThirdElement = baseElement.querySelector('[aria-labelledby="com.Test"]')! as HTMLInputElement;
expect(checkThirdElement).not.toBeInTheDocument();

const nextButton = getByText("Next") as HTMLButtonElement;
fireEvent.click(nextButton);
const fetchButton = getByText('Fetch "Test" class')! as HTMLButtonElement;
expect(fetchButton).toBeInTheDocument();
});

function testSearchInput(baseElement: Element, getByText: (text: string) => HTMLElement) {
const modalWizardButton = getByText("Import Java classes")! as HTMLButtonElement;
modalWizardButton.click();
Expand Down Expand Up @@ -129,35 +165,49 @@ describe("ImportJavaClasses component tests", () => {
expect(checkSecondElement).toBeChecked();
if (hasThirdElement) {
expect(checkThirdElement).not.toBeChecked();
fireEvent.click(checkThirdElement);
checkThirdElement = baseElement.querySelector('[aria-labelledby="com.Test"]')! as HTMLInputElement;
}
expect(checkFirstElement).toBeChecked();
expect(checkSecondElement).toBeChecked();
if (hasThirdElement) {
expect(checkThirdElement).toBeChecked();
}
}

async function testSecondStepFields(baseElement: Element, getByText: (text: string) => HTMLElement) {
const nextButton = getByText("Next") as HTMLButtonElement;
fireEvent.click(nextButton);
await waitFor(() => {
expect(baseElement.querySelector('[aria-label="field-table"]')!).toBeInTheDocument();
});
const expandToggle = baseElement.querySelector('[id="expand-toggle0"]')! as HTMLButtonElement;
expect(expandToggle).toHaveAttribute("aria-expanded", "true");
fireEvent.click(expandToggle);
expect(expandToggle).toHaveAttribute("aria-expanded", "false");
fireEvent.click(expandToggle);
}

async function testFetchClicked(getByText: (text: string) => HTMLElement) {
const fetchButton = getByText('Fetch "Test" class')! as HTMLButtonElement;
fetchButton.click();

await waitFor(() => {
expect(getByText("(Test)")!).toBeInTheDocument();
});
}

function lspGetClassServiceMock(mockedBroadcastDefinition: jest.Mock) {
window.envelopeMock = _.extend(window.envelopeMock || {}, {
lspGetClassServiceMocked: (value: string) => mockedBroadcastDefinition(value),
});
}

function lspGetClassFieldServiceMock(mockedBroadcastDefinition: jest.Mock) {
window.envelopeMock = _.extend(window.envelopeMock || {}, {
lspGetClassFieldsServiceMocked: (className: string) => Promise.resolve(mockedBroadcastDefinition(className)),
});
function lspGetClassFieldServiceMock() {
window.envelopeMock.lspGetClassFieldsServiceMocked = jest.fn(lspGetClassFieldsServiceMocked);
}

const lspGetClassFieldsServiceMocked = async (className: string) => {
const bookClassFieldsMap = new Map<string, string>();
bookClassFieldsMap.set("title", "string");
bookClassFieldsMap.set("year", "integer");
bookClassFieldsMap.set("title", "java.lang.String");
bookClassFieldsMap.set("year", "java.lang.Integer");
bookClassFieldsMap.set("test", "com.Test");
const authorClassFieldsMap = new Map<string, string>();
authorClassFieldsMap.set("name", "string");
authorClassFieldsMap.set("isAlive", "boolean");
authorClassFieldsMap.set("name", "java.lang.String");
authorClassFieldsMap.set("isAlive", "java.lang.Boolean");
if (className === "com.Book") {
return bookClassFieldsMap;
} else if (className === "com.Author") {
Expand Down
Loading

0 comments on commit a843578

Please sign in to comment.