wiki/list-editor/spec/store.spec.js

229 lines
8.2 KiB
JavaScript
Raw Normal View History

2020-10-14 21:43:52 +00:00
import createStore from '../store'
describe("A store", function () {
beforeEach(function () {
this.store = createStore([
{text: "Hello", id: "_a", indented: 0}
])
})
it("contains a length method", function () {
expect(this.store.length()).toBe(1)
})
it("contains an append method", function () {
this.store.append({text: "1"})
expect(this.store.length()).toBe(2)
})
it("contains an nextCursorPosition method", function () {
this.store.append({text: "1"})
expect(this.store.length()).toBe(2)
})
it("contains an indent method", function () {
this.store.append({text: "1", indented: 0})
this.store.append({text: "2", indented: 1})
this.store.append({text: "3", indented: 2})
this.store.indent(1, 3, 1)
expect(this.store.value(this.store.currentID(0)).indented).toBe(0)
expect(this.store.value(this.store.currentID(1)).indented).toBe(1)
expect(this.store.value(this.store.currentID(2)).indented).toBe(2)
expect(this.store.value(this.store.currentID(3)).indented).toBe(3)
})
it("contains a lastHigherIndented method", function () {
this.store.append({text: "1", indented: 0})
this.store.append({text: "2", indented: 1})
this.store.append({text: "3", indented: 2})
this.store.append({text: "3", indented: 0})
expect(this.store.lastHigherIndented(1)).toBe(4)
})
it("contains a tree method that returns same indent items as a list", function () {
let store = createStore([
{text: "a", id: "_a", indented: 0},
{text: "b", id: "_b", indented: 0},
{text: "c", id: "_c", indented: 0},
{text: "d", id: "_d", indented: 0}
])
let tree = store.tree()
expect(tree).toEqual([
{text: "a", id: "_a", indented: 0},
{text: "b", id: "_b", indented: 0},
{text: "c", id: "_c", indented: 0},
{text: "d", id: "_d", indented: 0}
])
})
it("contains a tree method that returns children as a list in children", function () {
let store = createStore([
{text: "a", id: "_a", indented: 0},
{text: "b", id: "_b", indented: 1},
{text: "c", id: "_c", indented: 1},
{text: "d", id: "_d", indented: 1}
])
let tree = store.tree()
expect(tree).toEqual([
{
text: "a", id: "_a", indented: 0, children: [
{text: "b", id: "_b", indented: 1},
{text: "c", id: "_c", indented: 1},
{text: "d", id: "_d", indented: 1}
]
},
])
})
it("contains a tree method that returns multiple children as a list in children", function () {
let store = createStore([
{text: "a", id: "_a", indented: 0},
{text: "b", id: "_b", indented: 1},
{text: "c", id: "_c", indented: 0},
{text: "d", id: "_d", indented: 1}
])
let tree = store.tree()
expect(tree).toEqual([
{
text: "a", id: "_a", indented: 0, children: [
{text: "b", id: "_b", indented: 1},
]
},
{
text: "c", id: "_c", indented: 0, children: [
{text: "d", id: "_d", indented: 1}
]
},
])
})
it("contains a tree method that transform 0,1,2", function () {
let store = createStore([
{text: "a", id: "_a", indented: 0},
{text: "b", id: "_b", indented: 1},
{text: "c", id: "_c", indented: 2},
])
let tree = store.tree()
expect(tree).toEqual([
{
text: "a", id: "_a", indented: 0, children: [
{
text: "b", id: "_b", indented: 1, children: [
{text: "c", id: "_c", indented: 2}]
}
]
},
])
})
it("contains a tree method that returns deeper children as a list in children", function () {
let store = createStore([
{text: "a", id: "_a", indented: 0},
{text: "b", id: "_b", indented: 1},
{text: "c", id: "_c", indented: 2},
{text: "d", id: "_d", indented: 0},
])
let tree = store.tree()
expect(tree).toEqual([
{
text: "a", id: "_a", indented: 0, children: [
{
text: "b", id: "_b", indented: 1, children: [
{text: "c", id: "_c", indented: 2},
]
},
]
},
{text: "d", id: "_d", indented: 0}
])
})
it("contains a tree method that accepts a from argument", function () {
let store = createStore([
{text: "a", id: "_a", indented: 0},
{text: "b", id: "_b", indented: 1},
{text: "c", id: "_c", indented: 2},
{text: "d", id: "_d", indented: 0},
])
let tree = store.tree("_a")
expect(tree).toEqual([
{
text: "a", id: "_a", indented: 0, children: [
{
text: "b", id: "_b", indented: 1, children: [
{text: "c", id: "_c", indented: 2},
]
},
]
},
])
})
2020-10-30 22:43:00 +00:00
describe("contains a moveBefore method 2", function () {
let store
beforeEach(function () {
store = createStore([
{text: "a", id: "_a", indented: 0},
{text: "b", id: "_b", indented: 0},
{text: "c", id: "_c", indented: 0},
{text: "d", id: "_d", indented: 0},
])
})
it("moveBefore _d, _a", function () {
store.moveBefore("_d", "_a")
expect(store.debug().idList).toEqual(["_d", "_a", "_b", "_c"])
})
it("moveBefore _d, _b", function () {
store.moveBefore("_d", "_b")
expect(store.debug().idList).toEqual(["_a", "_d", "_b", "_c"])
})
it("moveBefore _a, _d", function() {
store.moveBefore("_a", "_d")
expect(store.debug().idList).toEqual(["_b", "_c", "_a", "_d"])
})
it("moveBefore _b, _d", function() {
store.moveBefore("_b", "_d")
expect(store.debug().idList).toEqual(["_a", "_c", "_b", "_d"])
})
})
describe("contains a moveBefore method which handles indentation", function () {
let store
beforeEach(function () {
store = createStore([
{text: "a", id: "_a", indented: 0},
{text: "b", id: "_b", indented: 1},
{text: "c", id: "_c", indented: 2},
{text: "d", id: "_d", indented: 3},
{text: "e", id: "_e", indented: 0},
])
})
it("moveBefore _e, _c", function () {
store.moveBefore("_e", "_c")
let debug = store.debug();
expect(debug.idList).toEqual(["_a", "_b", "_e", "_c", "_d"])
expect(debug.result[2]).toEqual({text: "e", id: "_e", indented: 2})
})
it("moveBefore _d, at-end", function () {
store.moveBefore("_d", "at-end")
let debug = store.debug();
expect(debug.idList).toEqual(["_a", "_b", "_c", "_e", "_d"])
expect(debug.result[4]).toEqual({text: "d", id: "_d", indented: 0})
})
it("moveBefore _b, at-end", function () {
store.moveBefore("_b", "at-end")
let debug = store.debug();
expect(debug.idList).toEqual(["_a", "_e", "_b", "_c", "_d"])
expect(debug.result[0]).toEqual({text: "a", id: "_a", indented: 0})
expect(debug.result[1]).toEqual({text: "e", id: "_e", indented: 0})
expect(debug.result[2]).toEqual({text: "b", id: "_b", indented: 0})
expect(debug.result[3]).toEqual({text: "c", id: "_c", indented: 1})
expect(debug.result[4]).toEqual({text: "d", id: "_d", indented: 2})
})
})
2020-10-14 21:43:52 +00:00
})