Problem: moving up from closed item, moved too far
All checks were successful
continuous-integration/drone/push Build is passing

Solution: add tests for this problem, and remove indent code when moving
up
This commit is contained in:
Peter Stuifzand 2022-04-29 21:27:57 +02:00
parent f617dd973f
commit 79aba57dcb
2 changed files with 55 additions and 7 deletions

View File

@ -47,4 +47,58 @@ describe("A cursor", function() {
expect(this.cursor.get()).toBe(3) expect(this.cursor.get()).toBe(3)
}) })
}) })
describe("with a store with current closed", function () {
beforeEach(function () {
this.store = createStore([
{indented:0, fold: 'open'},
{indented:1, fold: 'open'},
{indented:2, fold: 'open'},
{indented:3, fold: 'open'},
{indented:1, fold: 'closed', hidden: true},
])
})
it("moveUp moves up by one", function() {
this.cursor.set(4)
this.cursor.moveUp(this.store)
expect(this.cursor.get()).toBe(3)
})
})
describe("with a store with above closed", function () {
beforeEach(function () {
this.store = createStore([
{indented:0, fold: 'open'},
{indented:1, fold: 'open'},
{indented:2, fold: 'open'},
{indented:3, fold: 'closed', hidden: true},
{indented:1, fold: 'open'},
])
})
it("moveUp moves up by one", function() {
this.cursor.set(4)
this.cursor.moveUp(this.store)
expect(this.cursor.get()).toBe(2)
})
})
describe("with a store with multiple above closed", function () {
beforeEach(function () {
this.store = createStore([
{indented:0, fold: 'open'},
{indented:1, fold: 'open'},
{indented:2, fold: 'closed', hidden: true},
{indented:3, fold: 'closed', hidden: true},
{indented:1, fold: 'open'},
])
})
it("moveUp moves up by one", function() {
this.cursor.set(4)
this.cursor.moveUp(this.store)
expect(this.cursor.get()).toBe(1)
})
})
}) })

View File

@ -55,21 +55,15 @@ function Store(inputData) {
} }
function prevCursorPosition(cursor) { function prevCursorPosition(cursor) {
let curIndent = values[idList[cursor]].indented
let curClosed = values[idList[cursor]].fold !== 'open';
if (!curClosed) {
curIndent = 10000000;
}
let moving = true let moving = true
while (moving) { while (moving) {
cursor-- cursor--
if (cursor < 0) { if (cursor < 0) {
cursor = idList.length - 1 cursor = idList.length - 1
curIndent = values[idList[cursor]].indented
} }
let next = values[idList[cursor]]; let next = values[idList[cursor]];
if (curIndent >= next.indented && !next.hidden) { if (!next.hidden) {
moving = false moving = false
} }
} }