tightening coverage + some cleanup

This commit is contained in:
MapleLeaf
2021-12-27 03:04:15 -06:00
parent c56abcaa34
commit 05d2367881
5 changed files with 89 additions and 180 deletions

View File

@@ -5,7 +5,7 @@ import type { OptionProps } from "./option"
export class OptionNode extends Node<OptionProps> { export class OptionNode extends Node<OptionProps> {
get options(): MessageSelectOptionOptions { get options(): MessageSelectOptionOptions {
return { return {
label: this.props.children || this.props.label || this.props.value || "", label: this.props.children || this.props.label || this.props.value,
value: this.props.value, value: this.props.value,
description: this.props.description, description: this.props.description,
emoji: this.props.emoji, emoji: this.props.emoji,

View File

@@ -3,6 +3,6 @@ import { Node } from "./node.js"
export class TextNode extends Node<string> { export class TextNode extends Node<string> {
override modifyMessageOptions(options: MessageOptions) { override modifyMessageOptions(options: MessageOptions) {
options.content = (options.content ?? "") + this.props options.content = options.content + this.props
} }
} }

View File

@@ -43,6 +43,7 @@
}, },
"devDependencies": { "devDependencies": {
"@itsmapleleaf/configs": "^1.1.2", "@itsmapleleaf/configs": "^1.1.2",
"@jest/globals": "^27.4.4",
"@types/jest": "^27.0.3", "@types/jest": "^27.0.3",
"@types/lodash-es": "^4.17.5", "@types/lodash-es": "^4.17.5",
"@typescript-eslint/eslint-plugin": "^5.8.0", "@typescript-eslint/eslint-plugin": "^5.8.0",

2
pnpm-lock.yaml generated
View File

@@ -8,6 +8,7 @@ importers:
.: .:
specifiers: specifiers:
'@itsmapleleaf/configs': ^1.1.2 '@itsmapleleaf/configs': ^1.1.2
'@jest/globals': ^27.4.4
'@types/jest': ^27.0.3 '@types/jest': ^27.0.3
'@types/lodash-es': ^4.17.5 '@types/lodash-es': ^4.17.5
'@types/node': '*' '@types/node': '*'
@@ -50,6 +51,7 @@ importers:
rxjs: 7.4.0 rxjs: 7.4.0
devDependencies: devDependencies:
'@itsmapleleaf/configs': 1.1.2 '@itsmapleleaf/configs': 1.1.2
'@jest/globals': 27.4.4
'@types/jest': 27.0.3 '@types/jest': 27.0.3
'@types/lodash-es': 4.17.5 '@types/lodash-es': 4.17.5
'@typescript-eslint/eslint-plugin': 5.8.0_836011a006f4f5d67178564baf2b6d34 '@typescript-eslint/eslint-plugin': 5.8.0_836011a006f4f5d67178564baf2b6d34

View File

@@ -1,10 +1,13 @@
import { jest } from "@jest/globals"
import React, { useState } from "react" import React, { useState } from "react"
import { Button, Option, Select } from "../library/main" import { Button, Option, Select } from "../library/main"
import { setupReacordTesting } from "./setup-testing" import { setupReacordTesting } from "./setup-testing"
const { adapter, reply, assertMessages } = setupReacordTesting() const { adapter, reply, assertRender, assertMessages } = setupReacordTesting()
test("single select", async () => { test("single select", async () => {
const onSelect = jest.fn()
function TestSelect() { function TestSelect() {
const [value, setValue] = useState<string>() const [value, setValue] = useState<string>()
const [disabled, setDisabled] = useState(false) const [disabled, setDisabled] = useState(false)
@@ -13,11 +16,12 @@ test("single select", async () => {
<Select <Select
placeholder="choose one" placeholder="choose one"
value={value} value={value}
onSelect={onSelect}
onSelectValue={setValue} onSelectValue={setValue}
disabled={disabled} disabled={disabled}
> >
<Option value="1">one</Option> <Option value="1" />
<Option value="2">two</Option> <Option value="2" label="two" />
<Option value="3">three</Option> <Option value="3">three</Option>
</Select> </Select>
<Button label="disable" onClick={() => setDisabled(true)} /> <Button label="disable" onClick={() => setDisabled(true)} />
@@ -25,8 +29,7 @@ test("single select", async () => {
) )
} }
reply.render(<TestSelect />) async function assertSelect(values: string[], disabled = false) {
await assertMessages([ await assertMessages([
{ {
content: "", content: "",
@@ -36,10 +39,10 @@ test("single select", async () => {
{ {
type: "select", type: "select",
placeholder: "choose one", placeholder: "choose one",
values: [], values,
disabled: false, disabled,
options: [ options: [
{ label: "one", value: "1" }, { label: "1", value: "1" },
{ label: "two", value: "2" }, { label: "two", value: "2" },
{ label: "three", value: "3" }, { label: "three", value: "3" },
], ],
@@ -49,84 +52,27 @@ test("single select", async () => {
], ],
}, },
]) ])
}
reply.render(<TestSelect />)
await assertSelect([])
expect(onSelect).toHaveBeenCalledTimes(0)
adapter.findSelectByPlaceholder("choose one").select("2") adapter.findSelectByPlaceholder("choose one").select("2")
await assertSelect(["2"])
await assertMessages([ expect(onSelect).toHaveBeenCalledWith({ values: ["2"] })
{
content: "",
embeds: [],
actionRows: [
[
{
type: "select",
placeholder: "choose one",
values: ["2"],
disabled: false,
options: [
{ label: "one", value: "1" },
{ label: "two", value: "2" },
{ label: "three", value: "3" },
],
},
],
[{ type: "button", style: "secondary", label: "disable" }],
],
},
])
adapter.findButtonByLabel("disable").click() adapter.findButtonByLabel("disable").click()
await assertSelect(["2"], true)
await assertMessages([
{
content: "",
embeds: [],
actionRows: [
[
{
type: "select",
placeholder: "choose one",
values: ["2"],
disabled: true,
options: [
{ label: "one", value: "1" },
{ label: "two", value: "2" },
{ label: "three", value: "3" },
],
},
],
[{ type: "button", style: "secondary", label: "disable" }],
],
},
])
adapter.findSelectByPlaceholder("choose one").select("1") adapter.findSelectByPlaceholder("choose one").select("1")
await assertSelect(["2"], true)
await assertMessages([ expect(onSelect).toHaveBeenCalledTimes(1)
{
content: "",
embeds: [],
actionRows: [
[
{
type: "select",
placeholder: "choose one",
values: ["2"],
disabled: true,
options: [
{ label: "one", value: "1" },
{ label: "two", value: "2" },
{ label: "three", value: "3" },
],
},
],
[{ type: "button", style: "secondary", label: "disable" }],
],
},
])
}) })
test("multiple select", async () => { test("multiple select", async () => {
const onSelect = jest.fn()
function TestSelect() { function TestSelect() {
const [values, setValues] = useState<string[]>([]) const [values, setValues] = useState<string[]>([])
return ( return (
@@ -134,6 +80,7 @@ test("multiple select", async () => {
placeholder="select" placeholder="select"
multiple multiple
values={values} values={values}
onSelect={onSelect}
onSelectMultiple={setValues} onSelectMultiple={setValues}
> >
<Option value="1">one</Option> <Option value="1">one</Option>
@@ -143,8 +90,7 @@ test("multiple select", async () => {
) )
} }
reply.render(<TestSelect />) async function assertSelect(values: string[]) {
await assertMessages([ await assertMessages([
{ {
content: "", content: "",
@@ -154,7 +100,7 @@ test("multiple select", async () => {
{ {
type: "select", type: "select",
placeholder: "select", placeholder: "select",
values: [], values,
minValues: 0, minValues: 0,
maxValues: 25, maxValues: 25,
options: [ options: [
@@ -167,78 +113,38 @@ test("multiple select", async () => {
], ],
}, },
]) ])
}
reply.render(<TestSelect />)
await assertSelect([])
expect(onSelect).toHaveBeenCalledTimes(0)
adapter.findSelectByPlaceholder("select").select("1", "3") adapter.findSelectByPlaceholder("select").select("1", "3")
await assertSelect(expect.arrayContaining(["1", "3"]))
await assertMessages([ expect(onSelect).toHaveBeenCalledWith({
{
content: "",
embeds: [],
actionRows: [
[
{
type: "select",
placeholder: "select",
values: expect.arrayContaining(["1", "3"]), values: expect.arrayContaining(["1", "3"]),
minValues: 0, })
maxValues: 25,
options: [
{ label: "one", value: "1" },
{ label: "two", value: "2" },
{ label: "three", value: "3" },
],
},
],
],
},
])
adapter.findSelectByPlaceholder("select").select("2") adapter.findSelectByPlaceholder("select").select("2")
await assertSelect(expect.arrayContaining(["2"]))
await assertMessages([ expect(onSelect).toHaveBeenCalledWith({
{ values: expect.arrayContaining(["2"]),
content: "", })
embeds: [],
actionRows: [
[
{
type: "select",
placeholder: "select",
values: ["2"],
minValues: 0,
maxValues: 25,
options: [
{ label: "one", value: "1" },
{ label: "two", value: "2" },
{ label: "three", value: "3" },
],
},
],
],
},
])
adapter.findSelectByPlaceholder("select").select() adapter.findSelectByPlaceholder("select").select()
await assertSelect([])
expect(onSelect).toHaveBeenCalledWith({ values: [] })
})
test("optional onSelect + unknown value", async () => {
reply.render(<Select placeholder="select" />)
adapter.findSelectByPlaceholder("select").select("something")
await assertMessages([ await assertMessages([
{ {
content: "", content: "",
embeds: [], embeds: [],
actionRows: [ actionRows: [
[ [{ type: "select", placeholder: "select", options: [], values: [] }],
{
type: "select",
placeholder: "select",
values: [],
minValues: 0,
maxValues: 25,
options: [
{ label: "one", value: "1" },
{ label: "two", value: "2" },
{ label: "three", value: "3" },
],
},
],
], ],
}, },
]) ])