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

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