Skip to content

Commit 0a4cdeb

Browse files
Adding transition tests
1 parent 6a47e15 commit 0a4cdeb

File tree

1 file changed

+162
-0
lines changed

1 file changed

+162
-0
lines changed

tests/unit/vuedraggable.spec.js

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ let element;
2020
let input;
2121
const initialRender = "<div><header></header><div>a</div><div>b</div><div>c</div><footer></footer></div>";
2222
const initialRenderRaw = "<div><div>a</div><div>b</div><div>c</div></div>";
23+
const initialRenderTransition = "<div><span><div>a</div><div>b</div><div>c</div></span></div>";
2324

2425
function getEvent(name) {
2526
return Sortable.mock.calls[0][1][name];
@@ -583,6 +584,10 @@ describe("draggable.vue when initialized with value", () => {
583584
expect(vm.visibleIndexes).toEqual([0, 1, 2]);
584585
});
585586

587+
it("renders correctly", () => {
588+
expect(wrapper.html()).toEqual(initialRenderRaw);
589+
})
590+
586591
it("update indexes", async () => {
587592
await Vue.nextTick();
588593
const computeIndexes = jest.fn();
@@ -700,6 +705,163 @@ describe("draggable.vue when initialized with value", () => {
700705
})
701706
});
702707

708+
describe("when sending DragEnd", () => {
709+
let endEvt;
710+
beforeEach(() => {
711+
endEvt = {
712+
data: "data"
713+
};
714+
const onEnd = getEvent("onEnd");
715+
onEnd(endEvt);
716+
})
717+
718+
it("sends a update event", async () => {
719+
await Vue.nextTick();
720+
expect(wrapper.emitted().end).toEqual([[endEvt]]);
721+
})
722+
})
723+
});
724+
});
725+
726+
describe("draggable.vue when initialized with a transition group", () => {
727+
beforeEach(() => {
728+
Sortable.mockClear();
729+
items = ["a", "b", "c"];
730+
const inside = items.map(item => `<div>${item}</div>`).join("");
731+
const template =`<transition-group>${inside}</transition-group>`
732+
wrapper = shallowMount(draggable, {
733+
attachToDocument: true,
734+
propsData: {
735+
value: items
736+
},
737+
slots: {
738+
default: template,
739+
}
740+
});
741+
vm = wrapper.vm;
742+
props = vm.$options.props;
743+
element = wrapper.element;
744+
});
745+
746+
it("computes indexes", async () => {
747+
await Vue.nextTick();
748+
expect(vm.visibleIndexes).toEqual([0, 1, 2]);
749+
});
750+
751+
it("set realList", () => {
752+
expect(vm.realList).toEqual(["a", "b", "c"]);
753+
});
754+
755+
it("transition mode should be false", () => {
756+
expect(vm.transitionMode).toBe(true);
757+
});
758+
759+
it("enders correctly", () => {
760+
expect(wrapper.html()).toEqual(initialRenderTransition);
761+
})
762+
763+
describe("when initiating a drag operation", () => {
764+
let evt;
765+
beforeEach(() => {
766+
item = element.children[0].children[1];
767+
evt = { item };
768+
const start = getEvent("onStart");
769+
start(evt);
770+
});
771+
772+
it("sends a start event", async () => {
773+
await Vue.nextTick();
774+
expect(wrapper.emitted()).toEqual({
775+
start: [[evt]]
776+
});
777+
})
778+
779+
it("sets context", async () => {
780+
await Vue.nextTick();
781+
expect(vm.context).toEqual({
782+
element: "b",
783+
index: 1
784+
});
785+
})
786+
787+
describe("when remove is called", () => {
788+
beforeEach(() => {
789+
element.children[0].removeChild(item);
790+
const remove = getEvent("onRemove");
791+
remove({
792+
item,
793+
oldIndex: 1
794+
});
795+
})
796+
797+
it("DOM changes should be reverted", async () => {
798+
await Vue.nextTick();
799+
expect(wrapper.html()).toEqual(initialRenderTransition);
800+
})
801+
802+
it("input should with updated value", async () => {
803+
await Vue.nextTick();
804+
const expected = ["a", "c"];
805+
expect(wrapper.emitted().input).toEqual([[expected]]);
806+
})
807+
808+
it("sends a remove event", async () => {
809+
await Vue.nextTick();
810+
const expectedEvt = { item, oldIndex: 1 };
811+
expect(wrapper.emitted().remove).toEqual([[expectedEvt]]);
812+
})
813+
814+
it("sends a change event", async () => {
815+
await Vue.nextTick();
816+
const expectedEvt = { removed: { element: "b", oldIndex: 1 } };
817+
expect(wrapper.emitted().change).toEqual([[expectedEvt]]);
818+
})
819+
})
820+
821+
describe("when update is called", () => {
822+
beforeEach(() => {
823+
const transitionRoot = element.children[0];
824+
const firstDraggable = transitionRoot.children[0];
825+
transitionRoot.removeChild(item);
826+
transitionRoot.insertBefore(item, firstDraggable);
827+
const update = getEvent("onUpdate");
828+
update({
829+
item,
830+
oldIndex: 1,
831+
newIndex: 0,
832+
from: transitionRoot
833+
});
834+
})
835+
836+
it("DOM changes should be reverted", async () => {
837+
await Vue.nextTick();
838+
expect(wrapper.html()).toEqual(initialRenderTransition);
839+
})
840+
841+
it("send an input event", async () => {
842+
await Vue.nextTick();
843+
const expected = ["b", "a", "c"];
844+
expect(wrapper.emitted().input).toEqual([[expected]]);
845+
})
846+
847+
it("sends a update event", async () => {
848+
await Vue.nextTick();
849+
const expectedEvt = {
850+
item,
851+
oldIndex: 1,
852+
newIndex: 0,
853+
from: element.children[0]
854+
};
855+
expect(wrapper.emitted().update).toEqual([[expectedEvt]]);
856+
})
857+
858+
it("sends a change event", async () => {
859+
await Vue.nextTick();
860+
const expectedEvt = { moved: { element: "b", oldIndex: 1, newIndex: 0 } };
861+
expect(wrapper.emitted().change).toEqual([[expectedEvt]]);
862+
})
863+
});
864+
703865
describe("when sending DragEnd", () => {
704866
let endEvt;
705867
beforeEach(() => {

0 commit comments

Comments
 (0)