@@ -138,6 +138,8 @@ describe('FilePatchComponent', () => {
138
138
const repository = await buildRepository ( workdirPath )
139
139
const filePath = path . join ( workdirPath , 'sample.js' )
140
140
const originalLines = fs . readFileSync ( filePath , 'utf8' ) . split ( '\n' )
141
+
142
+ // write some unstaged changes
141
143
const unstagedLines = originalLines . slice ( )
142
144
unstagedLines . splice ( 1 , 1 ,
143
145
'this is a modified line' ,
@@ -150,28 +152,43 @@ describe('FilePatchComponent', () => {
150
152
const hunkComponentsByHunk = new Map ( )
151
153
function registerHunkComponent ( hunk , component ) { hunkComponentsByHunk . set ( hunk , component ) }
152
154
155
+ // stage a subset of lines from first hunk
153
156
const component = new FilePatchComponent ( { filePatch : unstagedFilePatch , repository, stagingStatus : 'unstaged' , registerHunkComponent} )
154
157
let hunk = unstagedFilePatch . getHunks ( ) [ 0 ]
155
158
hunkComponentsByHunk . get ( hunk ) . didSelectLines ( new Set ( hunk . getLines ( ) . slice ( 1 , 4 ) ) )
156
159
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 ,
159
162
'this is a modified line' ,
160
163
'this is a new line'
161
164
)
162
- assert . equal ( await repository . readFileFromIndex ( 'sample.js' ) , expectedStagedLines . join ( '\n' ) )
165
+ assert . equal ( await repository . readFileFromIndex ( 'sample.js' ) , expectedLines . join ( '\n' ) )
163
166
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' ) )
165
176
177
+ // unstage a subset of lines from the first hunk
166
178
const [ stagedFilePatch ] = await repository . getStagedChanges ( )
167
179
await component . update ( { filePatch : stagedFilePatch , repository, stagingStatus : 'staged' , registerHunkComponent} )
168
180
hunk = stagedFilePatch . getHunks ( ) [ 0 ]
169
181
hunkComponentsByHunk . get ( hunk ) . didSelectLines ( new Set ( hunk . getLines ( ) . slice ( 1 , 3 ) ) )
170
182
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' ) )
174
189
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' ) )
176
193
} )
177
194
} )
0 commit comments