Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit abfe389

Browse files
author
Nathan Sobo
committed
Test clearing of selected lines after they are staged
1 parent 7610d56 commit abfe389

File tree

2 files changed

+29
-12
lines changed

2 files changed

+29
-12
lines changed

lib/views/file-patch-component.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,11 @@ export default class FilePatchComponent {
8686
}
8787

8888
didClickStageButtonForHunk (hunk) {
89-
// TODO: Remove these 4 lines and actually test drive the corner cases
90-
// they solve: Clicking a hunk other than the one containing the selected
91-
// lines and staging the rest of a hunk after staging some of its lines.
89+
// TODO: Test the behavior of this line, which ensure we only attempt to
90+
// stage the selected lines if we clicked the stage button on the hunk
91+
// containing them.
92+
const clickedSelectedHunk = hunk === this.selectedHunk
9293
const selectedLines = this.selectedLines
93-
const clickedSelectedHunk = (hunk === this.selectedHunk)
9494
this.selectedLines = null
9595
this.selectedHunk = null
9696

test/views/file-patch-component.test.js

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,8 @@ describe('FilePatchComponent', () => {
138138
const repository = await buildRepository(workdirPath)
139139
const filePath = path.join(workdirPath, 'sample.js')
140140
const originalLines = fs.readFileSync(filePath, 'utf8').split('\n')
141+
142+
// write some unstaged changes
141143
const unstagedLines = originalLines.slice()
142144
unstagedLines.splice(1, 1,
143145
'this is a modified line',
@@ -150,28 +152,43 @@ describe('FilePatchComponent', () => {
150152
const hunkComponentsByHunk = new Map()
151153
function registerHunkComponent (hunk, component) { hunkComponentsByHunk.set(hunk, component) }
152154

155+
// stage a subset of lines from first hunk
153156
const component = new FilePatchComponent({filePatch: unstagedFilePatch, repository, stagingStatus: 'unstaged', registerHunkComponent})
154157
let hunk = unstagedFilePatch.getHunks()[0]
155158
hunkComponentsByHunk.get(hunk).didSelectLines(new Set(hunk.getLines().slice(1, 4)))
156159
await hunkComponentsByHunk.get(hunk).didClickStageButton()
157-
const expectedStagedLines = originalLines.slice()
158-
expectedStagedLines.splice(1, 1,
160+
let expectedLines = originalLines.slice()
161+
expectedLines.splice(1, 1,
159162
'this is a modified line',
160163
'this is a new line'
161164
)
162-
assert.equal(await repository.readFileFromIndex('sample.js'), expectedStagedLines.join('\n'))
165+
assert.equal(await repository.readFileFromIndex('sample.js'), expectedLines.join('\n'))
163166

164-
// TODO: Ensure we only stage lines when clicking the stage button on the hunk containing the selected lines
167+
// stage remaining lines in hunk
168+
await hunkComponentsByHunk.get(hunk).didClickStageButton()
169+
expectedLines = originalLines.slice()
170+
expectedLines.splice(1, 1,
171+
'this is a modified line',
172+
'this is a new line',
173+
'this is another new line'
174+
)
175+
assert.equal(await repository.readFileFromIndex('sample.js'), expectedLines.join('\n'))
165176

177+
// unstage a subset of lines from the first hunk
166178
const [stagedFilePatch] = await repository.getStagedChanges()
167179
await component.update({filePatch: stagedFilePatch, repository, stagingStatus: 'staged', registerHunkComponent})
168180
hunk = stagedFilePatch.getHunks()[0]
169181
hunkComponentsByHunk.get(hunk).didSelectLines(new Set(hunk.getLines().slice(1, 3)))
170182
await hunkComponentsByHunk.get(hunk).didClickStageButton()
171-
const expectedStagedLinesAfterUnstaging = originalLines.slice()
172-
expectedStagedLinesAfterUnstaging.splice(2, 0, 'this is a new line')
173-
assert.equal(await repository.readFileFromIndex('sample.js'), expectedStagedLinesAfterUnstaging.join('\n'))
183+
expectedLines = originalLines.slice()
184+
expectedLines.splice(2, 0,
185+
'this is a new line',
186+
'this is another new line'
187+
)
188+
assert.equal(await repository.readFileFromIndex('sample.js'), expectedLines.join('\n'))
174189

175-
// TODO: Ensure we only unstage lines when clicking the unstage button on the hunk containing the selected lines
190+
// unstage the rest of the hunk
191+
await hunkComponentsByHunk.get(hunk).didClickStageButton()
192+
assert.equal(await repository.readFileFromIndex('sample.js'), originalLines.join('\n'))
176193
})
177194
})

0 commit comments

Comments
 (0)