Skip to content

Commit b507037

Browse files
Improving tests
1 parent 7ff7ba4 commit b507037

File tree

1 file changed

+47
-7
lines changed

1 file changed

+47
-7
lines changed

tests/unit/vuedraggable.spec.js

Lines changed: 47 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ import Sortable from "sortablejs";
33
jest.genMockFromModule('sortablejs');
44
jest.mock('sortablejs');
55
const SortableFake = {
6-
destroy: jest.fn()
6+
destroy: jest.fn(),
7+
option: jest.fn()
78
};
89
Sortable.mockImplementation(() => SortableFake);
910
import draggable from "@/vuedraggable";
@@ -24,10 +25,15 @@ function getEvent(name) {
2425
return Sortable.mock.calls[0][1][name];
2526
}
2627

28+
function resetMocks() {
29+
Sortable.mockClear();
30+
SortableFake.destroy.mockClear();
31+
SortableFake.option.mockClear();
32+
}
33+
2734
describe("draggable.vue when initialized with list", () => {
2835
beforeEach(() => {
29-
Sortable.mockClear();
30-
SortableFake.destroy.mockClear();
36+
resetMocks();
3137
items = ["a", "b", "c"];
3238
wrapper = shallowMount(draggable, {
3339
attachToDocument: true,
@@ -230,7 +236,7 @@ describe("draggable.vue when initialized with list", () => {
230236

231237
describe("when add is called", () => {
232238
let newItem;
233-
beforeEach(async() => {
239+
beforeEach(async () => {
234240
await Vue.nextTick();
235241
newItem = document.createElement("div");
236242
const newContent = document.createTextNode("d");
@@ -387,16 +393,50 @@ describe("draggable.vue when initialized with list", () => {
387393
})
388394
});
389395

390-
it("does calls Sortable destroy when mounted",()=>{
396+
it("does calls Sortable destroy when mounted", () => {
391397
expect(SortableFake.destroy.mock.calls.length).toBe(0);
392398
});
393399

394-
it("calls Sortable destroy when destroyed",()=>{
400+
it("calls Sortable destroy when destroyed", () => {
395401
wrapper.destroy();
396402
expect(SortableFake.destroy).toHaveBeenCalled();
397403
expect(SortableFake.destroy.mock.calls.length).toBe(1);
398404
});
399-
});
405+
406+
describe("when attribute changes:", () => {
407+
const { error } = console;
408+
beforeEach(() => {
409+
console.error = () => { };
410+
});
411+
afterEach(() => {
412+
console.error = error;
413+
})
414+
415+
test.each([
416+
["sortableOption", "newValue", "sortableOption"],
417+
["to-be-camelized", 1, "toBeCamelized"]
418+
])(
419+
"attribute %s change for value %o, calls sortable option with %s attribute",
420+
(attribute, value, sortableAttribute) => {
421+
vm.$attrs = { [attribute]: value };
422+
expect(SortableFake.option).toHaveBeenCalledWith(sortableAttribute, value);
423+
}
424+
);
425+
});
426+
427+
test.each([
428+
["sortableOption", "newValue", "sortableOption"],
429+
["to-be-camelized", 1, "toBeCamelized"]
430+
])(
431+
"when option %s change for value %o, calls sortable option with %s attribute",
432+
(attribute, value, sortableAttribute) => {
433+
wrapper.setProps({options: { [attribute]: value }});
434+
expect(SortableFake.option).toHaveBeenCalledWith(sortableAttribute, value);
435+
}
436+
);
437+
438+
})
439+
400440

401441
describe("draggable.vue when initialized with value", () => {
402442
beforeEach(() => {

0 commit comments

Comments
 (0)