Skip to content

Commit

Permalink
Merge 990888f into b5e239c
Browse files Browse the repository at this point in the history
  • Loading branch information
sagalbot committed Apr 2, 2019
2 parents b5e239c + 990888f commit b76cd04
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 73 deletions.
40 changes: 11 additions & 29 deletions src/mixins/ajax.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,13 @@ export default {
*/
loading: {
type: Boolean,
default: false
default: false,
},

/**
* Accept a callback function that will be
* run when the search text changes.
*
* loading() accepts a boolean value, and can
* be used to toggle a loading class from
* the onSearch callback.
*
* @param {search} String Current search text
* @param {loading} Function(bool) Toggle loading class
*/
onSearch: {
type: Function,
default: function(search, loading) {} // eslint-disable-line no-unused-vars
}
},

data() {
data () {
return {
mutableLoading: false
mutableLoading: false,
};
},

Expand All @@ -38,20 +22,18 @@ export default {
* If a callback & search text has been provided,
* invoke the onSearch callback.
*/
search() {
if (this.search.length > 0) {
this.onSearch(this.search, this.toggleLoading);
this.$emit("search", this.search, this.toggleLoading);
}
search () {
this.$emit('search', this.search, this.toggleLoading);
},

/**
* Sync the loading prop with the internal
* mutable loading value.
* @param val
*/
loading(val) {
loading (val) {
this.mutableLoading = val;
}
},
},

methods: {
Expand All @@ -62,11 +44,11 @@ export default {
* @param toggle Boolean
* @returns {*}
*/
toggleLoading(toggle = null) {
toggleLoading (toggle = null) {
if (toggle == null) {
return (this.mutableLoading = !this.mutableLoading);
}
return (this.mutableLoading = toggle);
}
}
},
},
};
58 changes: 14 additions & 44 deletions tests/unit/Ajax.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { selectWithProps } from "../helpers";
import { shallowMount } from '@vue/test-utils';
import vSelect from '../../src/components/Select';

describe("Asynchronous Loading", () => {
it("can toggle the loading class", () => {
Expand All @@ -11,33 +13,6 @@ describe("Asynchronous Loading", () => {
expect(Select.vm.mutableLoading).toEqual(true);
});

it("should trigger the onSearch callback when the search text changes", () => {
const propsData = { onSearch: () => {} };
const spy = jest.spyOn(propsData, "onSearch");
const Select = selectWithProps(propsData);

Select.vm.search = "foo";

expect(spy).toHaveBeenCalled();
});

it("should not trigger the onSearch callback if the search text is empty", () => {
let calledWith = [];
const propsData = {
onSearch: search => {
calledWith.push(search);
}
};
const spy = jest.spyOn(propsData, "onSearch");
const Select = selectWithProps(propsData);

Select.vm.search = "foo";
Select.vm.search = "";

expect(spy).toHaveBeenCalledTimes(1);
expect(calledWith).toEqual(["foo"]);
});

it("should trigger the search event when the search text changes", () => {
const Select = selectWithProps();

Expand All @@ -49,38 +24,33 @@ describe("Asynchronous Loading", () => {
expect(events.length).toEqual(1);
});

it("should not trigger the search event if the search text is empty", () => {
it("should trigger the search event if the search text is empty", () => {
const Select = selectWithProps();

Select.vm.search = "foo";
Select.vm.search = "";

const events = Select.emitted("search");

expect(events).toContainEqual(["foo", Select.vm.toggleLoading]);
expect(events.length).toEqual(1);
expect(events).toContainEqual(["", Select.vm.toggleLoading]);
expect(events.length).toEqual(2);
});

it("can set loading to false from the onSearch callback", () => {
const Select = selectWithProps({
onSearch: (search, loading) => loading(false)
it("can set loading to false from the @search event callback", () => {
const Select = shallowMount(vSelect, {
listeners: {
search: (search, loading) => {
loading(false)
},
},
});

Select.vm.search = "foo";
Select.vm.mutableLoading = true;
Select.vm.search = 'foo';

expect(Select.vm.mutableLoading).toEqual(false);
});

it("can set loading to true from the onSearch callback", () => {
const Select = selectWithProps({
onSearch: (search, loading) => loading(true)
});

Select.vm.search = "foo";

expect(Select.vm.mutableLoading).toEqual(true);
});

it("will sync mutable loading with the loading prop", () => {
const Select = selectWithProps({ loading: false });
Select.setProps({ loading: true });
Expand Down

0 comments on commit b76cd04

Please sign in to comment.