From 2f8cdc2380bfd6d8bd1dc231aee49156a4f9c167 Mon Sep 17 00:00:00 2001 From: Matt Date: Mon, 4 Sep 2017 16:21:22 +1000 Subject: [PATCH] Add: Some test cases. --- types/react-select/react-select-tests.tsx | 111 ++++++++++++++++++++++ 1 file changed, 111 insertions(+) diff --git a/types/react-select/react-select-tests.tsx b/types/react-select/react-select-tests.tsx index b19656ec5d..7ac189f231 100644 --- a/types/react-select/react-select-tests.tsx +++ b/types/react-select/react-select-tests.tsx @@ -10,6 +10,29 @@ const EXAMPLE_OPTIONS: ReactSelect.Options = [ { value: "two", label: "Two" } ]; +interface CustomValueType { + prop1: string; + prop2: number; +} + +/* + * This JSX/TSX generic component workaround is needed because of this issue: + * https://github.com/Microsoft/TypeScript/issues/3960 + * If https://github.com/Microsoft/TypeScript/issues/6395 ever lands, this + * workaround may become redundant. + */ +type CustomValueReactSelect = new (props: ReactSelect.ReactSelectProps) => ReactSelect; +const CustomValueReactSelect = ReactSelect as CustomValueReactSelect; + +const EXAMPLE_CUSTOM_OPTIONS: ReactSelect.Options = [ + { value: { prop1: "OneProp1", prop2: 1 }, label: "One" }, + { value: { prop1: "TwoProp2", prop2: 2 }, label: "Two" } +]; + +const EXAMPLE_CUSTOM_VALUE: ReactSelect.Option = { + value: { prop1: "ThreeProp1", prop2: 3 }, label: "Three" +}; + describe("react-select", () => { it("options", () => { const optionsString: ReactSelect.Options = [ @@ -56,6 +79,34 @@ describe("react-select", () => { }; }); + it("Custom value async options", () => { + const getAsyncLegacyOptions: ReactSelect.LoadOptionsLegacyHandler = (input, callback) => { + setTimeout(() => { + callback(null, { + options: [ + { value: { prop1: "One", prop2: 4 }, label: "Two" } + ], + complete: false + }); + }); + }; + + const dummyRequest = async () => { + return new Promise>(resolve => { + resolve(EXAMPLE_CUSTOM_OPTIONS); + }); + }; + + const getAsyncOptions: ReactSelect.LoadOptionsAsyncHandler = async input => { + const result = await dummyRequest(); + + return { + options: result, + complete: false + }; + }; + }); + it("creatable", () => { function Component(props: {}): JSX.Element { return ; @@ -135,6 +186,22 @@ describe("Examples", () => { } }); + it("Custom value onChange", () => { + class Component extends React.Component { + private onSelectChange: ReactSelect.OnChangeSingleHandler = (option) => { + const optionValue: CustomValueType = option.value; + } + + render() { + return ; + } + } + }); + it("Menu renderer example", () => { class Component extends React.Component { private onSelectChange: ReactSelect.OnChangeSingleHandler = option => { @@ -160,6 +227,32 @@ describe("Examples", () => { } }); + it("Menu renderer with custom value type example", () => { + class Component extends React.Component { + private menuRenderer: ReactSelect.MenuRendererHandler = props => { + const options = props.options.map(option => { + return ( +
props.selectValue(option)}> + {option.label} +
); + }); + + return
{options}
; + } + + render() { + return ; + } + } + }); + it("Input render example", () => { class Component extends React.Component { private onSelectChange: ReactSelect.OnChangeSingleHandler = option => { @@ -201,6 +294,24 @@ describe("Examples", () => { />; }); + it("Option render with custom value option", () => { + const optionRenderer = (option: ReactSelect.Option): ReactSelect.HandlerRendererResult => + null; + + ; + }); + + it("Value render with custom value option", () => { + const valueRenderer = (option: ReactSelect.Option): ReactSelect.HandlerRendererResult => + null; + + ; + }); + it("No Results renderer with string", () => { ; });