Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
78a3fb1879 |
@@ -1,5 +1,5 @@
|
|||||||
// @vitest-environment jsdom
|
// @vitest-environment jsdom
|
||||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||||
import { mountWithApp } from "./helpers/mountWithApp.js";
|
import { mountWithApp } from "./helpers/mountWithApp.js";
|
||||||
import ReferenceAutocompletePOS from "@/components/forms/department/pos/input/ReferenceAutocompletePOS.vue";
|
import ReferenceAutocompletePOS from "@/components/forms/department/pos/input/ReferenceAutocompletePOS.vue";
|
||||||
|
|
||||||
@@ -16,8 +16,72 @@ const flushPromises = async () => {
|
|||||||
await Promise.resolve();
|
await Promise.resolve();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Buefy schedules dropdown viewport checks after activation; stubbing it keeps this focused unit test from
|
||||||
|
// leaking those browser-only timers past jsdom teardown.
|
||||||
|
const AutocompleteStub = {
|
||||||
|
name: "BAutocomplete",
|
||||||
|
inheritAttrs: false,
|
||||||
|
props: {
|
||||||
|
modelValue: {
|
||||||
|
type: String,
|
||||||
|
default: "",
|
||||||
|
},
|
||||||
|
data: {
|
||||||
|
type: Array,
|
||||||
|
default: () => [],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
emits: ["blur", "focus", "keydown", "select", "typing", "update:modelValue"],
|
||||||
|
methods: {
|
||||||
|
emitInput(event) {
|
||||||
|
const value = event.target.value;
|
||||||
|
this.$emit("update:modelValue", value);
|
||||||
|
this.$emit("typing", value);
|
||||||
|
},
|
||||||
|
optionsFor(group) {
|
||||||
|
return Array.isArray(group?.items) ? group.items : [];
|
||||||
|
},
|
||||||
|
optionKey(option) {
|
||||||
|
return `${option.source}-${option.origin_id}-${option.reference}`;
|
||||||
|
},
|
||||||
|
},
|
||||||
|
template: `
|
||||||
|
<div class="b-autocomplete-stub">
|
||||||
|
<input
|
||||||
|
v-bind="$attrs"
|
||||||
|
:value="modelValue"
|
||||||
|
@input="emitInput"
|
||||||
|
@focus="$emit('focus', $event)"
|
||||||
|
@blur="$emit('blur', $event)"
|
||||||
|
@keydown="$emit('keydown', $event)"
|
||||||
|
/>
|
||||||
|
<div class="dropdown-content">
|
||||||
|
<template v-for="(group, index) in data" :key="group.group || index">
|
||||||
|
<slot name="group" :group="group.group" :index="index">
|
||||||
|
<span>{{ group.group }}</span>
|
||||||
|
</slot>
|
||||||
|
<button
|
||||||
|
v-for="option in optionsFor(group)"
|
||||||
|
:key="optionKey(option)"
|
||||||
|
type="button"
|
||||||
|
class="dropdown-item"
|
||||||
|
@click="$emit('select', option, $event)"
|
||||||
|
>
|
||||||
|
<slot :option="option">
|
||||||
|
{{ option.reference }}
|
||||||
|
</slot>
|
||||||
|
</button>
|
||||||
|
</template>
|
||||||
|
<slot v-if="data.length === 0" name="empty" />
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
`,
|
||||||
|
};
|
||||||
|
|
||||||
|
const mountedWrappers = [];
|
||||||
|
|
||||||
function mountAutocomplete(props = {}) {
|
function mountAutocomplete(props = {}) {
|
||||||
return mountWithApp(ReferenceAutocompletePOS, {
|
const wrapper = mountWithApp(ReferenceAutocompletePOS, {
|
||||||
props: {
|
props: {
|
||||||
modelValue: "",
|
modelValue: "",
|
||||||
departmentId: 12,
|
departmentId: 12,
|
||||||
@@ -26,7 +90,15 @@ function mountAutocomplete(props = {}) {
|
|||||||
debounceMs: 0,
|
debounceMs: 0,
|
||||||
...props,
|
...props,
|
||||||
},
|
},
|
||||||
|
global: {
|
||||||
|
stubs: {
|
||||||
|
BAutocomplete: AutocompleteStub,
|
||||||
|
},
|
||||||
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
mountedWrappers.push(wrapper);
|
||||||
|
return wrapper;
|
||||||
}
|
}
|
||||||
|
|
||||||
describe("ReferenceAutocompletePOS", () => {
|
describe("ReferenceAutocompletePOS", () => {
|
||||||
@@ -35,6 +107,12 @@ describe("ReferenceAutocompletePOS", () => {
|
|||||||
requestState.authenticatedRequest.mockResolvedValue({ data: { data: [] } });
|
requestState.authenticatedRequest.mockResolvedValue({ data: { data: [] } });
|
||||||
});
|
});
|
||||||
|
|
||||||
|
afterEach(() => {
|
||||||
|
for (const wrapper of mountedWrappers.splice(0)) {
|
||||||
|
wrapper.unmount();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
it("fetches async suggestions with the current POS context", async () => {
|
it("fetches async suggestions with the current POS context", async () => {
|
||||||
requestState.authenticatedRequest.mockResolvedValue({
|
requestState.authenticatedRequest.mockResolvedValue({
|
||||||
data: {
|
data: {
|
||||||
|
|||||||
Reference in New Issue
Block a user