-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: setup knowledge graph selector - WF-138
- Loading branch information
Showing
19 changed files
with
757 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { beforeEach, describe, expect, it, vi } from "vitest"; | ||
import BuilderGraphSelect from "./BuilderGraphSelect.vue"; | ||
import { flushPromises, shallowMount } from "@vue/test-utils"; | ||
import { buildMockCore } from "@/tests/mocks"; | ||
import injectionKeys from "@/injectionKeys"; | ||
import BuilderSelect from "./BuilderSelect.vue"; | ||
import WdsTextInput from "@/wds/WdsTextInput.vue"; | ||
|
||
describe("BuilderGraphSelect", () => { | ||
const graphs = [ | ||
{ id: "1", name: "one" }, | ||
{ id: "2", name: "two" }, | ||
]; | ||
let core: ReturnType<typeof buildMockCore>["core"]; | ||
|
||
beforeEach(() => { | ||
core = buildMockCore().core; | ||
}); | ||
|
||
it("should fetch and display graphs", async () => { | ||
const sendListResourcesRequest = vi | ||
.spyOn(core, "sendListResourcesRequest") | ||
.mockResolvedValue(graphs); | ||
|
||
const wrapper = shallowMount(BuilderGraphSelect, { | ||
props: { modelValue: "1" }, | ||
global: { | ||
stubs: { | ||
BuilderSelect: true, | ||
}, | ||
provide: { | ||
[injectionKeys.core as symbol]: core, | ||
}, | ||
}, | ||
}); | ||
await flushPromises(); | ||
expect(sendListResourcesRequest).toHaveBeenCalledOnce(); | ||
expect(wrapper.findComponent(BuilderSelect).exists()).toBe(true); | ||
}); | ||
|
||
it("should fallback to input", async () => { | ||
const sendListResourcesRequest = vi | ||
.spyOn(core, "sendListResourcesRequest") | ||
.mockRejectedValue(new Error()); | ||
|
||
const wrapper = shallowMount(BuilderGraphSelect, { | ||
props: { modelValue: "1" }, | ||
global: { | ||
stubs: { | ||
BuilderSelect: true, | ||
}, | ||
provide: { | ||
[injectionKeys.core as symbol]: core, | ||
}, | ||
}, | ||
}); | ||
await flushPromises(); | ||
expect(sendListResourcesRequest).toHaveBeenCalledOnce(); | ||
expect(wrapper.findComponent(BuilderSelect).exists()).toBe(false); | ||
expect(wrapper.findComponent(WdsTextInput).exists()).toBe(true); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
<script setup lang="ts"> | ||
import injectionKeys from "@/injectionKeys"; | ||
import { | ||
computed, | ||
defineAsyncComponent, | ||
inject, | ||
onMounted, | ||
PropType, | ||
} from "vue"; | ||
import { useListResources } from "@/composables/useListResources"; | ||
import type { Option } from "./BuilderSelect.vue"; | ||
import BuilderAsyncLoader from "./BuilderAsyncLoader.vue"; | ||
import type { WriterGraph } from "@/writerTypes"; | ||
import WdsTextInput from "@/wds/WdsTextInput.vue"; | ||
import LoadingSymbol from "@/renderer/LoadingSymbol.vue"; | ||
const BuilderSelect = defineAsyncComponent({ | ||
loader: () => import("./BuilderSelect.vue"), | ||
loadingComponent: BuilderAsyncLoader, | ||
}); | ||
const wf = inject(injectionKeys.core); | ||
defineProps({ | ||
enableMultiSelection: { type: Boolean, required: false }, | ||
}); | ||
const currentValue = defineModel({ | ||
type: [String, Array] as PropType<string | string[]>, | ||
required: true, | ||
}); | ||
const { | ||
load: loadGraphs, | ||
data: graphs, | ||
isLoading, | ||
} = useListResources<WriterGraph>(wf, "graphs"); | ||
onMounted(loadGraphs); | ||
const options = computed(() => | ||
graphs.value | ||
.map<Option>((graph) => ({ | ||
label: graph.name, | ||
detail: graph.description, | ||
value: graph.id, | ||
})) | ||
.sort((a, b) => a.label.localeCompare(b.label)), | ||
); | ||
const currentValueStr = computed<string>({ | ||
get() { | ||
if (currentValue.value === undefined) return ""; | ||
return typeof currentValue.value === "string" | ||
? currentValue.value | ||
: currentValue.value.join(","); | ||
}, | ||
set(value: string) { | ||
currentValue.value = value.split(","); | ||
}, | ||
}); | ||
</script> | ||
|
||
<template> | ||
<div class="BuilderGraphSelect--text"> | ||
<BuilderSelect | ||
v-if="graphs.length > 0" | ||
v-model="currentValue" | ||
:options="options" | ||
hide-icons | ||
enable-search | ||
:enable-multi-selection="enableMultiSelection" | ||
/> | ||
<WdsTextInput v-else v-model="currentValueStr" /> | ||
<LoadingSymbol v-if="isLoading" /> | ||
</div> | ||
</template> | ||
|
||
<style scoped> | ||
.BuilderGraphSelect--text { | ||
width: 100%; | ||
display: flex; | ||
gap: 12px; | ||
align-items: center; | ||
} | ||
</style> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { flushPromises, mount, shallowMount } from "@vue/test-utils"; | ||
import { describe, expect, it } from "vitest"; | ||
|
||
import BuilderSelect from "./BuilderSelect.vue"; | ||
import { Option } from "./BuilderSelect.vue"; | ||
import WdsDropdownMenu from "@/wds/WdsDropdownMenu.vue"; | ||
|
||
describe("BuilderSelect", () => { | ||
const options: Option[] = [ | ||
{ label: "Label A", value: "a" }, | ||
{ label: "Label B", value: "b" }, | ||
{ label: "Label C", value: "c" }, | ||
]; | ||
|
||
it("should display unknow value selected", () => { | ||
const wrapper = shallowMount(BuilderSelect, { | ||
props: { | ||
modelValue: "x", | ||
enableMultiSelection: false, | ||
hideIcons: true, | ||
options, | ||
}, | ||
}); | ||
|
||
expect(wrapper.get(".material-symbols-outlined").text()).toBe( | ||
"help_center", | ||
); | ||
}); | ||
|
||
it("should support single mode", async () => { | ||
const wrapper = mount(BuilderSelect, { | ||
props: { | ||
modelValue: "a", | ||
enableMultiSelection: false, | ||
options, | ||
}, | ||
global: { | ||
stubs: { | ||
WdsDropdownMenu: true, | ||
}, | ||
}, | ||
}); | ||
await flushPromises(); | ||
|
||
await wrapper.get(".BuilderSelect__trigger").trigger("click"); | ||
await flushPromises(); | ||
|
||
const dropdownMenu = wrapper.getComponent(WdsDropdownMenu); | ||
dropdownMenu.vm.$emit("select", "b"); | ||
await flushPromises(); | ||
|
||
expect(wrapper.emitted("update:modelValue").at(0)).toStrictEqual(["b"]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.