mirror of
https://github.com/tenrok/vue-select.git
synced 2026-06-19 09:50:33 +03:00
feat: add autoscroll boolean prop (#1160)
* feat: add autoscroll boolean prop Fixes #449 * refactor: update autoscroll implementation Closes #1028 Closes #910 * refactor: only call maybeAdjustScroll in the watcher * docs: upgrade vuepress
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
import { mountDefault } from "../helpers";
|
||||
|
||||
describe("Automatic Scrolling", () => {
|
||||
it("should check if the scroll position needs to be adjusted on up arrow keyUp", async () => {
|
||||
// Given
|
||||
const Select = mountDefault();
|
||||
const spy = jest.spyOn(Select.vm, "maybeAdjustScroll");
|
||||
Select.vm.typeAheadPointer = 1;
|
||||
|
||||
// When
|
||||
Select.find({ ref: "search" }).trigger("keydown.up");
|
||||
await Select.vm.$nextTick();
|
||||
|
||||
// Then
|
||||
expect(spy).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("should check if the scroll position needs to be adjusted on down arrow keyUp", async () => {
|
||||
// Given
|
||||
const Select = mountDefault();
|
||||
const spy = jest.spyOn(Select.vm, "maybeAdjustScroll");
|
||||
Select.vm.typeAheadPointer = 1;
|
||||
|
||||
// When
|
||||
Select.find({ ref: "search" }).trigger("keydown.down");
|
||||
await Select.vm.$nextTick();
|
||||
|
||||
// Then
|
||||
expect(spy).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("should check if the scroll position needs to be adjusted when filtered options changes", async () => {
|
||||
// Given
|
||||
const Select = mountDefault();
|
||||
const spy = jest.spyOn(Select.vm, "maybeAdjustScroll");
|
||||
Select.vm.typeAheadPointer = 1;
|
||||
|
||||
// When
|
||||
Select.vm.search = "two";
|
||||
await Select.vm.$nextTick();
|
||||
|
||||
// Then
|
||||
expect(spy).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("should not adjust scroll position when autoscroll is false", async () => {
|
||||
// Given
|
||||
const Select = mountDefault({
|
||||
autoscroll: false
|
||||
});
|
||||
const spy = jest.spyOn(Select.vm, "maybeAdjustScroll");
|
||||
Select.vm.typeAheadPointer = 1;
|
||||
|
||||
// When
|
||||
Select.vm.search = "two";
|
||||
await Select.vm.$nextTick();
|
||||
|
||||
// Then
|
||||
expect(spy).toHaveBeenCalledTimes(0);
|
||||
});
|
||||
});
|
||||
@@ -1,18 +1,17 @@
|
||||
import { shallowMount } from '@vue/test-utils';
|
||||
import { shallowMount } from "@vue/test-utils";
|
||||
import VueSelect from "../../src/components/Select";
|
||||
import { mountDefault, mountWithoutTestUtils } from '../helpers';
|
||||
import typeAheadMixin from '../../src/mixins/typeAheadPointer';
|
||||
import Vue from 'vue';
|
||||
import { mountDefault, mountWithoutTestUtils } from "../helpers";
|
||||
import typeAheadMixin from "../../src/mixins/typeAheadPointer";
|
||||
import Vue from "vue";
|
||||
|
||||
describe("Moving the Typeahead Pointer", () => {
|
||||
|
||||
it('should set the pointer to zero when the filteredOptions watcher is called', async () => {
|
||||
it("should set the pointer to zero when the filteredOptions watcher is called", async () => {
|
||||
const Select = shallowMount(VueSelect, {
|
||||
propsData: { options: ['one', 'two', 'three'] },
|
||||
propsData: { options: ["one", "two", "three"] },
|
||||
sync: false
|
||||
});
|
||||
|
||||
Select.vm.search = 'one';
|
||||
Select.vm.search = "one";
|
||||
|
||||
await Select.vm.$nextTick();
|
||||
expect(Select.vm.typeAheadPointer).toEqual(0);
|
||||
@@ -45,114 +44,4 @@ describe("Moving the Typeahead Pointer", () => {
|
||||
Select.vm.typeAheadDown();
|
||||
expect(Select.vm.typeAheadPointer).toEqual(2);
|
||||
});
|
||||
|
||||
describe("Automatic Scrolling", () => {
|
||||
it("should check if the scroll position needs to be adjusted on up arrow keyUp", () => {
|
||||
const Select = mountDefault();
|
||||
const spy = jest.spyOn(Select.vm, "maybeAdjustScroll");
|
||||
|
||||
Select.vm.typeAheadPointer = 1;
|
||||
|
||||
Select.find({ ref: "search" }).trigger("keydown.up");
|
||||
expect(spy).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("should check if the scroll position needs to be adjusted on down arrow keyUp", () => {
|
||||
const Select = mountDefault();
|
||||
const spy = jest.spyOn(Select.vm, "maybeAdjustScroll");
|
||||
|
||||
Select.vm.typeAheadPointer = 1;
|
||||
|
||||
Select.find({ ref: "search" }).trigger("keydown.down");
|
||||
expect(spy).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
/**
|
||||
* This test fails despite working in the browser.
|
||||
* After many attempts to get it to pass, it's been
|
||||
* rewritten below.
|
||||
*/
|
||||
it.skip("should check if the scroll position needs to be adjusted when filtered options changes", () => {
|
||||
const Select = mountDefault();
|
||||
const spy = jest.spyOn(Select.vm, "maybeAdjustScroll");
|
||||
|
||||
Select.vm.search = "two";
|
||||
|
||||
expect(spy).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("should scroll up if the pointer is above the current viewport bounds", () => {
|
||||
const Select = mountDefault();
|
||||
const spy = jest.spyOn(Select.vm, "scrollTo");
|
||||
|
||||
Select.setMethods({
|
||||
pixelsToPointerTop() {
|
||||
return 1;
|
||||
},
|
||||
viewport() {
|
||||
return { top: 2, bottom: 0 };
|
||||
}
|
||||
});
|
||||
|
||||
Select.vm.maybeAdjustScroll();
|
||||
|
||||
expect(spy).toHaveBeenCalledWith(1);
|
||||
});
|
||||
|
||||
it("should scroll down if the pointer is below the current viewport bounds", () => {
|
||||
const Select = mountDefault();
|
||||
const spy = jest.spyOn(Select.vm, "scrollTo");
|
||||
|
||||
Select.setMethods({
|
||||
pixelsToPointerBottom() {
|
||||
return 2;
|
||||
},
|
||||
viewport() {
|
||||
return { top: 0, bottom: 1 };
|
||||
}
|
||||
});
|
||||
|
||||
Select.vm.maybeAdjustScroll();
|
||||
expect(spy).toHaveBeenCalledWith(
|
||||
Select.vm.viewport().top + Select.vm.pointerHeight()
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("Measuring pixel distances", () => {
|
||||
it("should calculate pointerHeight as the offsetHeight of the pointer element if it exists", async () => {
|
||||
const Select = mountDefault();
|
||||
|
||||
// Drop down must be open for $refs to exist
|
||||
Select.vm.open = true;
|
||||
await Select.vm.$nextTick();
|
||||
|
||||
/**
|
||||
* Since JSDom doesn't render layouts, set the offsetHeight explicitly
|
||||
* to 25px for each list item.
|
||||
*
|
||||
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty
|
||||
*/
|
||||
let i = 0;
|
||||
for (let option of Select.vm.$refs.dropdownMenu.children) {
|
||||
Object.defineProperty(option, "offsetHeight", {
|
||||
value: 1 + i
|
||||
});
|
||||
i++;
|
||||
}
|
||||
|
||||
// Fresh instances start with the pointer at -1
|
||||
Select.vm.typeAheadPointer = -1;
|
||||
expect(Select.vm.pointerHeight()).toEqual(0);
|
||||
|
||||
Select.vm.typeAheadPointer = 0;
|
||||
expect(Select.vm.pointerHeight()).toEqual(1);
|
||||
|
||||
Select.vm.typeAheadPointer = 1;
|
||||
expect(Select.vm.pointerHeight()).toEqual(2);
|
||||
|
||||
Select.vm.typeAheadPointer = 2;
|
||||
expect(Select.vm.pointerHeight()).toEqual(3);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user