Skip to content

Commit

Permalink
Refractor for spirint1 (#375)
Browse files Browse the repository at this point in the history
* feat: add tooltip

Signed-off-by: suzhou <[email protected]>

* feat: fix json order

Signed-off-by: suzhou <[email protected]>

* feat: update containers

Signed-off-by: suzhou <[email protected]>

* feat: update unit test

Signed-off-by: suzhou <[email protected]>

* feat: update unit test

Signed-off-by: suzhou <[email protected]>

* feat: update function test

Signed-off-by: suzhou <[email protected]>

* fix: table selection when reload table

Signed-off-by: suzhou <[email protected]>

* feat: update function test case

Signed-off-by: suzhou <[email protected]>

Signed-off-by: suzhou <[email protected]>
  • Loading branch information
SuZhou-Joe authored Nov 14, 2022
1 parent 7e717cf commit e93d5f9
Show file tree
Hide file tree
Showing 25 changed files with 1,488 additions and 1,159 deletions.
46 changes: 7 additions & 39 deletions cypress/integration/create_index.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,7 @@ describe("Create Index", () => {
});

it("Update alias successfully", () => {
cy.get(`[data-test-subj="view-index-detail-button-${SAMPLE_INDEX}"]`)
.click()
.get("#index-detail-modal-alias")
.click()
.get('[data-test-subj="detail-modal-edit"]')
.click();
cy.get(`[data-test-subj="view-index-detail-button-${SAMPLE_INDEX}"]`).click().get("#index-detail-modal-alias").click();

// add a alias and remove the exist alias
cy.get('[data-test-subj="comboBoxSearchInput"]')
Expand All @@ -109,17 +104,10 @@ describe("Create Index", () => {
.end()
.get('[data-test-subj="createIndexCreateButton"]')
.click({ force: true })
.end()
.get('[data-test-subj="change_diff_confirm-confirm"]')
.click();
.end();

// check the index
cy.get(`[data-test-subj="view-index-detail-button-${SAMPLE_INDEX}"]`)
.click()
.get("#index-detail-modal-alias")
.click()
.get('[data-test-subj="detail-modal-edit"]')
.click();
cy.get(`[data-test-subj="view-index-detail-button-${SAMPLE_INDEX}"]`).click().get("#index-detail-modal-alias").click();

cy.get(`[value="${SAMPLE_INDEX}"]`)
.should("exist")
Expand All @@ -133,12 +121,7 @@ describe("Create Index", () => {
});

it("Update settings successfully", () => {
cy.get(`[data-test-subj="view-index-detail-button-${SAMPLE_INDEX}"]`)
.click()
.get("#index-detail-modal-settings")
.click()
.get('[data-test-subj="detail-modal-edit"]')
.click();
cy.get(`[data-test-subj="view-index-detail-button-${SAMPLE_INDEX}"]`).click().get("#index-detail-modal-settings").click();

cy.get('[aria-controls="accordion_for_create_index_settings"]')
.click()
Expand Down Expand Up @@ -172,23 +155,13 @@ describe("Create Index", () => {
.get('[data-test-subj="change_diff_confirm-confirm"]')
.click();

cy.get(`[data-test-subj="view-index-detail-button-${SAMPLE_INDEX}"]`)
.click()
.get("#index-detail-modal-settings")
.click()
.get('[data-test-subj="detail-modal-edit"]')
.click();
cy.get(`[data-test-subj="view-index-detail-button-${SAMPLE_INDEX}"]`).click().get("#index-detail-modal-settings").click();

cy.get('[placeholder="The number of replica shards each primary shard should have."]').should("have.value", 12);
});

it("Update mappings successfully", () => {
cy.get(`[data-test-subj="view-index-detail-button-${SAMPLE_INDEX}"]`)
.click()
.get("#index-detail-modal-mappings")
.click()
.get('[data-test-subj="detail-modal-edit"]')
.click();
cy.get(`[data-test-subj="view-index-detail-button-${SAMPLE_INDEX}"]`).click().get("#index-detail-modal-mappings").click();

cy.get('[data-test-subj="create index add field button"]')
.click()
Expand All @@ -201,12 +174,7 @@ describe("Create Index", () => {
.get('[data-test-subj="change_diff_confirm-confirm"]')
.click();

cy.get(`[data-test-subj="view-index-detail-button-${SAMPLE_INDEX}"]`)
.click()
.get("#index-detail-modal-mappings")
.click()
.get('[data-test-subj="detail-modal-edit"]')
.click();
cy.get(`[data-test-subj="view-index-detail-button-${SAMPLE_INDEX}"]`).click().get("#index-detail-modal-mappings").click();

cy.get('[data-test-subj="mapping-visual-editor-2-field-type"]').should("have.value", "text").end();
});
Expand Down
18 changes: 3 additions & 15 deletions cypress/integration/split_index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,14 @@ describe("Split Index", () => {
// The index should exist
cy.get(`#_selection_column_${SAMPLE_INDEX}-checkbox`).should("have.exist").end();

cy.get(`[data-test-subj="view-index-detail-button-${SAMPLE_INDEX}"]`)
.click()
.end()
.get("#index-detail-modal-settings")
.click()
.end()
.get('[data-test-subj="detail-modal-edit"]')
.click()
.end();
cy.get(`[data-test-subj="view-index-detail-button-${SAMPLE_INDEX}"]`).click().end().get("#index-detail-modal-settings").click().end();

cy.get('[placeholder="The number of primary shards in the index. Default is 1."]').then(($shardNumber) => {
split_number = $shardNumber.val() * 2;
});

// Update Index status to blocks write otherwise we can't apply split operation on it
cy.updateIndexSettings(SAMPLE_INDEX, {"index.blocks.write":"true"})
.end()
cy.updateIndexSettings(SAMPLE_INDEX, { "index.blocks.write": "true" }).end();
});

it("Split successfully", () => {
Expand Down Expand Up @@ -89,9 +80,6 @@ describe("Split Index", () => {
.end()
.get("#index-detail-modal-settings")
.click()
.end()
.get('[data-test-subj="detail-modal-edit"]')
.click()
.end();

cy.get('[placeholder="The number of primary shards in the index. Default is 1."]').should("have.value", `${split_number}`).end();
Expand All @@ -110,6 +98,6 @@ describe("Split Index", () => {
.get('[data-test-subj="flyout-footer-action-button"]')
.click()
.end();
})
});
});
});
32 changes: 32 additions & 0 deletions public/components/EuiToolTipWrapper/EuiToolTipWrapper.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

import React from "react";
import { render, waitFor, act } from "@testing-library/react";
import { fireEvent } from "@testing-library/dom";
import EuiToolTipWrapper from "./index";

const WrappedInput = EuiToolTipWrapper((props: any) => <input {...props} />);

describe("<FormGenerator /> spec", () => {
it("render the component", async () => {
render(<WrappedInput />);
await waitFor(() => {});
expect(document.body.children).toMatchSnapshot();
});

it("render the error", async () => {
const { queryByText, container } = render(<WrappedInput disabledReason="test error" disabled data-test-subj="test" />);
const anchorDOM = container.querySelector(".euiToolTipAnchor") as Element;
await act(async () => {
await fireEvent.mouseOver(anchorDOM, {
bubbles: true,
});
});
await waitFor(() => {
expect(queryByText("test error")).not.toBeNull();
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`<FormGenerator /> spec render the component 1`] = `
HTMLCollection [
<div>
<span
class="euiToolTipAnchor euiToolTipAnchor--displayBlock"
>
<input />
</span>
</div>,
]
`;
79 changes: 79 additions & 0 deletions public/components/EuiToolTipWrapper/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import React, { forwardRef } from "react";
import { EuiToolTip } from "@elastic/eui";

interface IEuiToolTipWrapperOptions {
disabledKey?: string;
}

export interface IEuiToolTipWrapperProps {
disabledReason?:
| string
| {
visible: boolean;
message: string;
}[];
}

export default function EuiToolTipWrapper<T>(
Component: React.ComponentType<T>,
options?: IEuiToolTipWrapperOptions
): React.ComponentType<T & IEuiToolTipWrapperProps> {
return forwardRef(({ disabledReason, children, ...others }, ref) => {
const finalOptions: Required<IEuiToolTipWrapperOptions> = {
...{
disabledKey: "disabled",
},
...options,
};
let formattedReason: IEuiToolTipWrapperProps["disabledReason"];
if (typeof disabledReason === "string") {
formattedReason = [
{
visible: true,
message: disabledReason,
},
];
} else {
formattedReason = disabledReason;
}
formattedReason = formattedReason?.filter((item) => item.visible);
const propsDisabled = (others as Record<string, any>)[finalOptions.disabledKey];
const disabled = propsDisabled === undefined ? !!formattedReason?.length : propsDisabled;
const finalProps: IEuiToolTipWrapperProps = {
...others,
[finalOptions.disabledKey]: disabled,
};
return (
<EuiToolTip
content={
disabled && formattedReason?.length ? (
<>
This field is disabled because:
<ol>
{formattedReason?.map((item, index) => (
<li style={{ display: "flex" }} key={item.message}>
<span
style={{
width: "1.5em",
textAlign: "right",
}}
>
{index + 1}.
</span>
<span>{item.message}</span>
</li>
))}
</ol>
</>
) : undefined
}
display="block"
position="right"
>
<>
<Component {...(finalProps as T)} ref={ref} />
</>
</EuiToolTip>
);
}) as React.ComponentType<T & IEuiToolTipWrapperProps>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,25 @@ HTMLCollection [
<div
class="euiFormRow__fieldWrapper"
>
<div
class="euiFormControlLayout"
<span
class="euiToolTipAnchor euiToolTipAnchor--displayBlock"
>
<div
class="euiFormControlLayout__childrenWrapper"
class="euiFormControlLayout"
>
<input
class="euiFieldText"
data-meta="Field"
id="test"
type="text"
value=""
/>
<div
class="euiFormControlLayout__childrenWrapper"
>
<input
class="euiFieldText"
data-meta="Field"
id="test"
type="text"
value=""
/>
</div>
</div>
</div>
</span>
</div>
</div>
<div
Expand Down Expand Up @@ -85,21 +89,25 @@ HTMLCollection [
<div
class="euiFormRow__fieldWrapper"
>
<div
class="euiFormControlLayout"
<span
class="euiToolTipAnchor euiToolTipAnchor--displayBlock"
>
<div
class="euiFormControlLayout__childrenWrapper"
class="euiFormControlLayout"
>
<input
class="euiFieldText"
data-meta="Field"
id="test_component_2"
type="text"
value=""
/>
<div
class="euiFormControlLayout__childrenWrapper"
>
<input
class="euiFieldText"
data-meta="Field"
id="test_component_2"
type="text"
value=""
/>
</div>
</div>
</div>
</span>
</div>
</div>
</div>
Expand Down
37 changes: 25 additions & 12 deletions public/components/FormGenerator/built_in_components/index.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,33 @@
import React, { forwardRef } from "react";
import { EuiFieldNumber, EuiFieldText, EuiSwitch } from "@elastic/eui";
import EuiToolTipWrapper, { IEuiToolTipWrapperProps } from "../../EuiToolTipWrapper";

type ComponentMapEnum = "Input" | "Number" | "Switch";

const componentMap: Record<ComponentMapEnum, React.FC<{ onChange: (val: any) => void; value?: any }>> = {
Input: forwardRef(({ onChange, value, ...others }, ref: React.Ref<any>) => (
<EuiFieldText inputRef={ref} value={value || ""} onChange={(e) => onChange(e.target.value)} {...others} />
)),
Number: forwardRef(({ onChange, value, ...others }, ref: React.Ref<any>) => (
<EuiFieldNumber inputRef={ref} onChange={(e) => onChange(e.target.value)} value={value || ""} {...others} />
)),
Switch: forwardRef(({ value, onChange, ...others }, ref: React.Ref<any>) => (
<div ref={ref}>
<EuiSwitch showLabel={false} label="" checked={value || false} onChange={(e) => onChange(e.target.checked)} {...others} />
</div>
)),
export interface IFieldComponentProps extends IEuiToolTipWrapperProps {
onChange: (val: IFieldComponentProps["value"]) => void;
value?: any;
[key: string]: any;
}

const componentMap: Record<ComponentMapEnum, React.ComponentType<IFieldComponentProps>> = {
Input: EuiToolTipWrapper(
forwardRef(({ onChange, value, disabledReason, disabled, ...others }, ref: React.Ref<HTMLInputElement>) => (
<EuiFieldText inputRef={ref} value={value || ""} onChange={(e) => onChange(e.target.value)} disabled={disabled} {...others} />
)) as React.ComponentType<IFieldComponentProps>
),
Number: EuiToolTipWrapper(
forwardRef(({ onChange, value, ...others }, ref: React.Ref<HTMLInputElement>) => (
<EuiFieldNumber inputRef={ref} onChange={(e) => onChange(e.target.value)} value={value || ""} {...others} />
)) as React.ComponentType<IFieldComponentProps>
),
Switch: EuiToolTipWrapper(
forwardRef(({ value, onChange, ...others }, ref: React.Ref<any>) => (
<div ref={ref}>
<EuiSwitch showLabel={false} label="" checked={value || false} onChange={(e) => onChange(e.target.checked)} {...others} />
</div>
)) as React.ComponentType<IFieldComponentProps>
),
};

export default componentMap;
2 changes: 1 addition & 1 deletion public/containers/IndexDetail/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export default function IndexDetail(props: IIndexDetailProps) {
props.onGetIndicesDetail && props.onGetIndicesDetail(finalResponse);
setLoading(false);
})();
}, [props.indices, setLoading, setItems, coreServices]);
}, [props.indices.join(","), setLoading, setItems, coreServices]);
return (
<ContentPanel title="Source index details" titleSize="s">
<EuiBasicTable
Expand Down
Loading

0 comments on commit e93d5f9

Please sign in to comment.