2018-11-08 19:23:39 +05:30
import MockAdapter from 'axios-mock-adapter' ;
2022-07-16 23:28:13 +05:30
import Cookies from '~/lib/utils/cookies' ;
2023-06-20 00:43:36 +05:30
import waitForPromises from 'helpers/wait_for_promises' ;
2020-10-24 23:57:45 +05:30
import { useLocalStorageSpy } from 'helpers/local_storage_helper' ;
2021-03-08 18:12:59 +05:30
import { TEST _HOST } from 'helpers/test_constants' ;
import testAction from 'helpers/vuex_action_helper' ;
2022-08-13 15:12:31 +05:30
import { getDiffFileMock } from 'jest/diffs/mock_data/diff_file' ;
2018-11-08 19:23:39 +05:30
import {
DIFF _VIEW _COOKIE _NAME ,
INLINE _DIFF _VIEW _TYPE ,
PARALLEL _DIFF _VIEW _TYPE ,
2023-07-09 08:55:56 +05:30
EVT _MR _PREPARED ,
2018-11-08 19:23:39 +05:30
} from '~/diffs/constants' ;
2023-06-20 00:43:36 +05:30
import { LOAD _SINGLE _DIFF _FAILED } from '~/diffs/i18n' ;
2022-06-21 17:19:12 +05:30
import * as diffActions from '~/diffs/store/actions' ;
2018-11-08 19:23:39 +05:30
import * as types from '~/diffs/store/mutation_types' ;
2020-04-22 19:07:51 +05:30
import * as utils from '~/diffs/store/utils' ;
2021-11-18 22:05:49 +05:30
import * as treeWorkerUtils from '~/diffs/utils/tree_worker_utils' ;
2023-05-27 22:25:52 +05:30
import { createAlert } from '~/alert' ;
2021-03-11 19:13:27 +05:30
import axios from '~/lib/utils/axios_utils' ;
2020-04-22 19:07:51 +05:30
import * as commonUtils from '~/lib/utils/common_utils' ;
2023-04-23 21:23:45 +05:30
import {
HTTP _STATUS _BAD _REQUEST ,
HTTP _STATUS _INTERNAL _SERVER _ERROR ,
HTTP _STATUS _NOT _FOUND ,
HTTP _STATUS _OK ,
} from '~/lib/utils/http_status' ;
2020-06-23 00:09:42 +05:30
import { mergeUrlParams } from '~/lib/utils/url_utility' ;
2021-03-11 19:13:27 +05:30
import eventHub from '~/notes/event_hub' ;
2023-06-20 00:43:36 +05:30
import diffsEventHub from '~/diffs/event_hub' ;
2020-06-23 00:09:42 +05:30
import { diffMetadata } from '../mock_data/diff_metadata' ;
2020-04-22 19:07:51 +05:30
2023-05-27 22:25:52 +05:30
jest . mock ( '~/alert' ) ;
2018-11-08 19:23:39 +05:30
2023-06-20 00:43:36 +05:30
jest . mock ( '~/lib/utils/secret_detection' , ( ) => ( {
confirmSensitiveAction : jest . fn ( ( ) => Promise . resolve ( false ) ) ,
containsSensitiveToken : jest . requireActual ( '~/lib/utils/secret_detection' ) . containsSensitiveToken ,
} ) ) ;
2018-11-08 19:23:39 +05:30
describe ( 'DiffsStoreActions' , ( ) => {
2022-06-21 17:19:12 +05:30
let mock ;
2020-04-22 19:07:51 +05:30
useLocalStorageSpy ( ) ;
2018-11-20 20:47:30 +05:30
const originalMethods = {
requestAnimationFrame : global . requestAnimationFrame ,
requestIdleCallback : global . requestIdleCallback ,
} ;
beforeEach ( ( ) => {
2020-04-22 19:07:51 +05:30
jest . spyOn ( window . history , 'pushState' ) ;
jest . spyOn ( commonUtils , 'historyPushState' ) ;
jest . spyOn ( commonUtils , 'handleLocationHash' ) . mockImplementation ( ( ) => null ) ;
jest . spyOn ( commonUtils , 'scrollToElement' ) . mockImplementation ( ( ) => null ) ;
jest . spyOn ( utils , 'convertExpandLines' ) . mockImplementation ( ( ) => null ) ;
jest . spyOn ( utils , 'idleCallback' ) . mockImplementation ( ( ) => null ) ;
2021-03-08 18:12:59 +05:30
[ 'requestAnimationFrame' , 'requestIdleCallback' ] . forEach ( ( method ) => {
global [ method ] = ( cb ) => {
2021-04-29 21:17:54 +05:30
cb ( { timeRemaining : ( ) => 10 } ) ;
2018-11-20 20:47:30 +05:30
} ;
} ) ;
} ) ;
2022-06-21 17:19:12 +05:30
beforeEach ( ( ) => {
mock = new MockAdapter ( axios ) ;
} ) ;
2018-11-20 20:47:30 +05:30
afterEach ( ( ) => {
2021-03-08 18:12:59 +05:30
[ 'requestAnimationFrame' , 'requestIdleCallback' ] . forEach ( ( method ) => {
2018-11-20 20:47:30 +05:30
global [ method ] = originalMethods [ method ] ;
} ) ;
2022-11-25 23:54:43 +05:30
createAlert . mockClear ( ) ;
2022-06-21 17:19:12 +05:30
mock . restore ( ) ;
2018-11-20 20:47:30 +05:30
} ) ;
2018-11-08 19:23:39 +05:30
describe ( 'setBaseConfig' , ( ) => {
2022-06-21 17:19:12 +05:30
it ( 'should set given endpoint and project path' , ( ) => {
2018-11-08 19:23:39 +05:30
const endpoint = '/diffs/set/endpoint' ;
2020-01-01 13:55:28 +05:30
const endpointMetadata = '/diffs/set/endpoint/metadata' ;
const endpointBatch = '/diffs/set/endpoint/batch' ;
2023-05-27 22:25:52 +05:30
const endpointDiffForPath = '/diffs/set/endpoint/path' ;
2020-04-08 14:13:33 +05:30
const endpointCoverage = '/diffs/set/coverage_reports' ;
2018-11-08 19:23:39 +05:30
const projectPath = '/root/project' ;
2019-09-04 21:01:54 +05:30
const dismissEndpoint = '/-/user_callouts' ;
const showSuggestPopover = false ;
2021-12-11 22:18:48 +05:30
const mrReviews = {
a : [ 'z' , 'hash:a' ] ,
b : [ 'y' , 'hash:a' ] ,
} ;
2018-11-08 19:23:39 +05:30
2022-06-21 17:19:12 +05:30
return testAction (
diffActions . setBaseConfig ,
2020-01-01 13:55:28 +05:30
{
endpoint ,
endpointBatch ,
2023-05-27 22:25:52 +05:30
endpointDiffForPath ,
2020-01-01 13:55:28 +05:30
endpointMetadata ,
2020-04-08 14:13:33 +05:30
endpointCoverage ,
2020-01-01 13:55:28 +05:30
projectPath ,
dismissEndpoint ,
showSuggestPopover ,
2021-12-11 22:18:48 +05:30
mrReviews ,
2020-01-01 13:55:28 +05:30
} ,
{
endpoint : '' ,
endpointBatch : '' ,
2023-05-27 22:25:52 +05:30
endpointDiffForPath : '' ,
2020-01-01 13:55:28 +05:30
endpointMetadata : '' ,
2020-04-08 14:13:33 +05:30
endpointCoverage : '' ,
2020-01-01 13:55:28 +05:30
projectPath : '' ,
dismissEndpoint : '' ,
showSuggestPopover : true ,
} ,
2019-09-04 21:01:54 +05:30
[
{
type : types . SET _BASE _CONFIG ,
2020-01-01 13:55:28 +05:30
payload : {
endpoint ,
endpointMetadata ,
endpointBatch ,
2023-05-27 22:25:52 +05:30
endpointDiffForPath ,
2020-04-08 14:13:33 +05:30
endpointCoverage ,
2020-01-01 13:55:28 +05:30
projectPath ,
dismissEndpoint ,
showSuggestPopover ,
2021-12-11 22:18:48 +05:30
mrReviews ,
2020-01-01 13:55:28 +05:30
} ,
2019-09-04 21:01:54 +05:30
} ,
2021-12-11 22:18:48 +05:30
{
type : types . SET _DIFF _FILE _VIEWED ,
payload : { id : 'z' , seen : true } ,
} ,
{
type : types . SET _DIFF _FILE _VIEWED ,
payload : { id : 'a' , seen : true } ,
} ,
{
type : types . SET _DIFF _FILE _VIEWED ,
payload : { id : 'y' , seen : true } ,
} ,
2019-09-04 21:01:54 +05:30
] ,
2018-11-08 19:23:39 +05:30
[ ] ,
) ;
} ) ;
} ) ;
2023-06-20 00:43:36 +05:30
describe ( 'fetchFileByFile' , ( ) => {
beforeEach ( ( ) => {
window . location . hash = 'e334a2a10f036c00151a04cea7938a5d4213a818' ;
} ) ;
it ( 'should do nothing if there is no tree entry for the file ID' , ( ) => {
return testAction ( diffActions . fetchFileByFile , { } , { flatBlobsList : [ ] } , [ ] , [ ] ) ;
} ) ;
it ( 'should do nothing if the tree entry for the file ID has already been marked as loaded' , ( ) => {
return testAction (
diffActions . fetchFileByFile ,
{ } ,
{
flatBlobsList : [
{ fileHash : 'e334a2a10f036c00151a04cea7938a5d4213a818' , diffLoaded : true } ,
] ,
} ,
[ ] ,
[ ] ,
) ;
} ) ;
describe ( 'when a tree entry exists for the file, but it has not been marked as loaded' , ( ) => {
let state ;
2023-07-09 08:55:56 +05:30
let getters ;
2023-06-20 00:43:36 +05:30
let commit ;
let hubSpy ;
2023-07-09 08:55:56 +05:30
const defaultParams = {
old _path : 'old/123' ,
new _path : 'new/123' ,
w : '1' ,
view : 'inline' ,
} ;
2023-06-20 00:43:36 +05:30
const endpointDiffForPath = '/diffs/set/endpoint/path' ;
2023-07-09 08:55:56 +05:30
const diffForPath = mergeUrlParams ( defaultParams , endpointDiffForPath ) ;
2023-06-20 00:43:36 +05:30
const treeEntry = {
fileHash : 'e334a2a10f036c00151a04cea7938a5d4213a818' ,
filePaths : { old : 'old/123' , new : 'new/123' } ,
} ;
const fileResult = {
diff _files : [ { file _hash : 'e334a2a10f036c00151a04cea7938a5d4213a818' } ] ,
} ;
beforeEach ( ( ) => {
commit = jest . fn ( ) ;
state = {
endpointDiffForPath ,
diffFiles : [ ] ,
} ;
2023-07-09 08:55:56 +05:30
getters = {
flatBlobsList : [ treeEntry ] ,
getDiffFileByHash ( hash ) {
return state . diffFiles ? . find ( ( entry ) => entry . file _hash === hash ) ;
} ,
} ;
2023-06-20 00:43:36 +05:30
hubSpy = jest . spyOn ( diffsEventHub , '$emit' ) ;
} ) ;
it ( 'does nothing if the file already exists in the loaded diff files' , ( ) => {
state . diffFiles = fileResult . diff _files ;
return testAction ( diffActions . fetchFileByFile , state , getters , [ ] , [ ] ) ;
} ) ;
it ( 'does some standard work every time' , async ( ) => {
mock . onGet ( diffForPath ) . reply ( HTTP _STATUS _OK , fileResult ) ;
await diffActions . fetchFileByFile ( { state , getters , commit } ) ;
expect ( commit ) . toHaveBeenCalledWith ( types . SET _BATCH _LOADING _STATE , 'loading' ) ;
expect ( commit ) . toHaveBeenCalledWith ( types . SET _RETRIEVING _BATCHES , true ) ;
// wait for the mocked network request to return and start processing the .then
await waitForPromises ( ) ;
expect ( commit ) . toHaveBeenCalledWith ( types . SET _DIFF _DATA _BATCH , fileResult ) ;
expect ( commit ) . toHaveBeenCalledWith ( types . SET _BATCH _LOADING _STATE , 'loaded' ) ;
expect ( hubSpy ) . toHaveBeenCalledWith ( 'diffFilesModified' ) ;
} ) ;
it . each `
urlHash | diffFiles | expected
$ { treeEntry . fileHash } | $ { [ ] } | $ { '' }
$ { 'abcdef1234567890' } | $ { fileResult . diff _files } | $ { 'e334a2a10f036c00151a04cea7938a5d4213a818' }
` (
"sets the current file to the first diff file ('$id') if it's not a note hash and there isn't a current ID set" ,
async ( { urlHash , diffFiles , expected } ) => {
window . location . hash = urlHash ;
mock . onGet ( diffForPath ) . reply ( HTTP _STATUS _OK , fileResult ) ;
state . diffFiles = diffFiles ;
await diffActions . fetchFileByFile ( { state , getters , commit } ) ;
// wait for the mocked network request to return and start processing the .then
await waitForPromises ( ) ;
expect ( commit ) . toHaveBeenCalledWith ( types . SET _CURRENT _DIFF _FILE , expected ) ;
} ,
) ;
2023-07-09 08:55:56 +05:30
it ( 'should fetch data without commit ID' , async ( ) => {
getters . commitId = null ;
mock . onGet ( diffForPath ) . reply ( HTTP _STATUS _OK , fileResult ) ;
await diffActions . fetchFileByFile ( { state , getters , commit } ) ;
// wait for the mocked network request to return and start processing the .then
await waitForPromises ( ) ;
// This tests that commit_id is NOT added, if there isn't one in the store
expect ( mock . history . get [ 0 ] . url ) . toEqual ( diffForPath ) ;
} ) ;
it ( 'should fetch data with commit ID' , async ( ) => {
const finalPath = mergeUrlParams (
{ ... defaultParams , commit _id : '123' } ,
endpointDiffForPath ,
) ;
getters . commitId = '123' ;
mock . onGet ( finalPath ) . reply ( HTTP _STATUS _OK , fileResult ) ;
await diffActions . fetchFileByFile ( { state , getters , commit } ) ;
// wait for the mocked network request to return and start processing the .then
await waitForPromises ( ) ;
expect ( mock . history . get [ 0 ] . url ) . toEqual ( finalPath ) ;
} ) ;
describe ( 'version parameters' , ( ) => {
const diffId = '4' ;
const startSha = 'abc' ;
const pathRoot = 'a/a/-/merge_requests/1' ;
it ( 'fetches the data when there is no mergeRequestDiff' , async ( ) => {
diffActions . fetchFileByFile ( { state , getters , commit } ) ;
// wait for the mocked network request to return and start processing the .then
await waitForPromises ( ) ;
expect ( mock . history . get [ 0 ] . url ) . toEqual ( diffForPath ) ;
} ) ;
it . each `
desc | versionPath | start _sha | diff _id
$ { 'no additional version information' } | $ { ` ${ pathRoot } ?search=terms ` } | $ { undefined } | $ { undefined }
$ { 'the diff_id' } | $ { ` ${ pathRoot } ?diff_id= ${ diffId } ` } | $ { undefined } | $ { diffId }
$ { 'the start_sha' } | $ { ` ${ pathRoot } ?start_sha= ${ startSha } ` } | $ { startSha } | $ { undefined }
$ { 'all available version information' } | $ { ` ${ pathRoot } ?diff_id= ${ diffId } &start_sha= ${ startSha } ` } | $ { startSha } | $ { diffId }
` ('fetches the data and includes $ desc', async ({ versionPath, start_sha, diff_id }) => {
const finalPath = mergeUrlParams (
{ ... defaultParams , diff _id , start _sha } ,
endpointDiffForPath ,
) ;
state . mergeRequestDiff = { version _path : versionPath } ;
mock . onGet ( finalPath ) . reply ( HTTP _STATUS _OK , fileResult ) ;
diffActions . fetchFileByFile ( { state , getters , commit } ) ;
// wait for the mocked network request to return and start processing the .then
await waitForPromises ( ) ;
expect ( mock . history . get [ 0 ] . url ) . toEqual ( finalPath ) ;
} ) ;
} ) ;
2023-06-20 00:43:36 +05:30
} ) ;
} ) ;
2020-01-01 13:55:28 +05:30
describe ( 'fetchDiffFilesBatch' , ( ) => {
2022-06-21 17:19:12 +05:30
it ( 'should fetch batch diff files' , ( ) => {
2020-01-01 13:55:28 +05:30
const endpointBatch = '/fetch/diffs_batch' ;
2021-09-30 23:02:18 +05:30
const res1 = { diff _files : [ { file _hash : 'test' } ] , pagination : { total _pages : 7 } } ;
const res2 = { diff _files : [ { file _hash : 'test2' } ] , pagination : { total _pages : 7 } } ;
2020-01-01 13:55:28 +05:30
mock
2020-06-23 00:09:42 +05:30
. onGet (
mergeUrlParams (
{
w : '1' ,
view : 'inline' ,
2021-09-30 23:02:18 +05:30
page : 0 ,
per _page : 5 ,
2020-06-23 00:09:42 +05:30
} ,
endpointBatch ,
) ,
)
2023-04-23 21:23:45 +05:30
. reply ( HTTP _STATUS _OK , res1 )
2020-06-23 00:09:42 +05:30
. onGet (
mergeUrlParams (
{
w : '1' ,
view : 'inline' ,
2021-09-30 23:02:18 +05:30
page : 5 ,
per _page : 7 ,
2020-06-23 00:09:42 +05:30
} ,
endpointBatch ,
) ,
)
2023-04-23 21:23:45 +05:30
. reply ( HTTP _STATUS _OK , res2 ) ;
2020-01-01 13:55:28 +05:30
2022-06-21 17:19:12 +05:30
return testAction (
diffActions . fetchDiffFilesBatch ,
2020-01-01 13:55:28 +05:30
{ } ,
2022-04-04 11:22:00 +05:30
{ endpointBatch , diffViewType : 'inline' , diffFiles : [ ] } ,
2020-01-01 13:55:28 +05:30
[
2021-11-11 11:23:49 +05:30
{ type : types . SET _BATCH _LOADING _STATE , payload : 'loading' } ,
2020-03-13 15:44:24 +05:30
{ type : types . SET _RETRIEVING _BATCHES , payload : true } ,
2020-01-01 13:55:28 +05:30
{ type : types . SET _DIFF _DATA _BATCH , payload : { diff _files : res1 . diff _files } } ,
2021-11-11 11:23:49 +05:30
{ type : types . SET _BATCH _LOADING _STATE , payload : 'loaded' } ,
2021-12-11 22:18:48 +05:30
{ type : types . SET _CURRENT _DIFF _FILE , payload : 'test' } ,
2020-07-28 23:09:34 +05:30
{ type : types . SET _DIFF _DATA _BATCH , payload : { diff _files : res2 . diff _files } } ,
2021-11-11 11:23:49 +05:30
{ type : types . SET _BATCH _LOADING _STATE , payload : 'loaded' } ,
2021-12-11 22:18:48 +05:30
{ type : types . SET _CURRENT _DIFF _FILE , payload : 'test2' } ,
2020-03-13 15:44:24 +05:30
{ type : types . SET _RETRIEVING _BATCHES , payload : false } ,
2021-11-11 11:23:49 +05:30
{ type : types . SET _BATCH _LOADING _STATE , payload : 'error' } ,
2020-01-01 13:55:28 +05:30
] ,
2021-04-29 21:17:54 +05:30
[ { type : 'startRenderDiffsQueue' } , { type : 'startRenderDiffsQueue' } ] ,
2020-01-01 13:55:28 +05:30
) ;
} ) ;
} ) ;
describe ( 'fetchDiffFilesMeta' , ( ) => {
2022-11-25 23:54:43 +05:30
const endpointMetadata = '/fetch/diffs_metadata.json?view=inline&w=0' ;
2020-06-23 00:09:42 +05:30
const noFilesData = { ... diffMetadata } ;
2020-01-01 13:55:28 +05:30
2020-06-23 00:09:42 +05:30
beforeEach ( ( ) => {
delete noFilesData . diff _files ;
} ) ;
2022-06-21 17:19:12 +05:30
it ( 'should fetch diff meta information' , ( ) => {
2023-04-23 21:23:45 +05:30
mock . onGet ( endpointMetadata ) . reply ( HTTP _STATUS _OK , diffMetadata ) ;
2023-01-13 00:05:48 +05:30
2022-06-21 17:19:12 +05:30
return testAction (
diffActions . fetchDiffFilesMeta ,
2020-01-01 13:55:28 +05:30
{ } ,
2022-11-25 23:54:43 +05:30
{ endpointMetadata , diffViewType : 'inline' , showWhitespace : true } ,
2020-01-01 13:55:28 +05:30
[
{ type : types . SET _LOADING , payload : true } ,
{ type : types . SET _LOADING , payload : false } ,
2020-06-23 00:09:42 +05:30
{ type : types . SET _MERGE _REQUEST _DIFFS , payload : diffMetadata . merge _request _diffs } ,
2021-02-22 17:27:13 +05:30
{ type : types . SET _DIFF _METADATA , payload : noFilesData } ,
2021-04-29 21:17:54 +05:30
// Workers are synchronous in Jest environment (see https://gitlab.com/gitlab-org/gitlab/-/merge_requests/58805)
2021-09-04 01:27:46 +05:30
{
type : types . SET _TREE _DATA ,
2021-11-18 22:05:49 +05:30
payload : treeWorkerUtils . generateTreeList ( diffMetadata . diff _files ) ,
2021-09-04 01:27:46 +05:30
} ,
2020-01-01 13:55:28 +05:30
] ,
[ ] ,
) ;
} ) ;
2023-01-13 00:05:48 +05:30
2023-07-09 08:55:56 +05:30
describe ( 'on a 404 response' , ( ) => {
let dismissAlert ;
2023-01-13 00:05:48 +05:30
2023-07-09 08:55:56 +05:30
beforeAll ( ( ) => {
dismissAlert = jest . fn ( ) ;
mock . onGet ( endpointMetadata ) . reply ( HTTP _STATUS _NOT _FOUND ) ;
createAlert . mockImplementation ( ( ) => ( { dismiss : dismissAlert } ) ) ;
} ) ;
it ( 'should show a warning' , async ( ) => {
await testAction (
diffActions . fetchDiffFilesMeta ,
{ } ,
{ endpointMetadata , diffViewType : 'inline' , showWhitespace : true } ,
[ { type : types . SET _LOADING , payload : true } ] ,
[ ] ,
) ;
expect ( createAlert ) . toHaveBeenCalledTimes ( 1 ) ;
expect ( createAlert ) . toHaveBeenCalledWith ( {
message : expect . stringMatching (
'Building your merge request… This page will update when the build is complete.' ,
) ,
variant : 'warning' ,
} ) ;
} ) ;
it ( "should attempt to close the alert if the MR reports that it's been prepared" , async ( ) => {
await testAction (
diffActions . fetchDiffFilesMeta ,
{ } ,
{ endpointMetadata , diffViewType : 'inline' , showWhitespace : true } ,
[ { type : types . SET _LOADING , payload : true } ] ,
[ ] ,
) ;
diffsEventHub . $emit ( EVT _MR _PREPARED ) ;
2023-01-13 00:05:48 +05:30
2023-07-09 08:55:56 +05:30
expect ( dismissAlert ) . toHaveBeenCalled ( ) ;
2023-01-13 00:05:48 +05:30
} ) ;
} ) ;
it ( 'should show no warning on any other status code' , async ( ) => {
2023-04-23 21:23:45 +05:30
mock . onGet ( endpointMetadata ) . reply ( HTTP _STATUS _INTERNAL _SERVER _ERROR ) ;
2023-01-13 00:05:48 +05:30
2023-05-27 22:25:52 +05:30
try {
await testAction (
diffActions . fetchDiffFilesMeta ,
{ } ,
{ endpointMetadata , diffViewType : 'inline' , showWhitespace : true } ,
[ { type : types . SET _LOADING , payload : true } ] ,
[ ] ,
) ;
} catch ( error ) {
expect ( error . response . status ) . toBe ( HTTP _STATUS _INTERNAL _SERVER _ERROR ) ;
}
2023-01-13 00:05:48 +05:30
expect ( createAlert ) . not . toHaveBeenCalled ( ) ;
} ) ;
2020-01-01 13:55:28 +05:30
} ) ;
2020-04-08 14:13:33 +05:30
describe ( 'fetchCoverageFiles' , ( ) => {
const endpointCoverage = '/fetch' ;
2022-06-21 17:19:12 +05:30
it ( 'should commit SET_COVERAGE_DATA with received response' , ( ) => {
2021-03-08 18:12:59 +05:30
const data = { files : { 'app.js' : { 1 : 0 , 2 : 1 } } } ;
2020-04-08 14:13:33 +05:30
2023-04-23 21:23:45 +05:30
mock . onGet ( endpointCoverage ) . reply ( HTTP _STATUS _OK , { data } ) ;
2020-04-08 14:13:33 +05:30
2022-06-21 17:19:12 +05:30
return testAction (
diffActions . fetchCoverageFiles ,
2020-04-08 14:13:33 +05:30
{ } ,
{ endpointCoverage } ,
[ { type : types . SET _COVERAGE _DATA , payload : { data } } ] ,
[ ] ,
) ;
} ) ;
2023-05-27 22:25:52 +05:30
it ( 'should show alert on API error' , async ( ) => {
2023-04-23 21:23:45 +05:30
mock . onGet ( endpointCoverage ) . reply ( HTTP _STATUS _BAD _REQUEST ) ;
2020-04-08 14:13:33 +05:30
2022-06-21 17:19:12 +05:30
await testAction ( diffActions . fetchCoverageFiles , { } , { endpointCoverage } , [ ] , [ ] ) ;
2022-11-25 23:54:43 +05:30
expect ( createAlert ) . toHaveBeenCalledTimes ( 1 ) ;
expect ( createAlert ) . toHaveBeenCalledWith ( {
2022-06-21 17:19:12 +05:30
message : expect . stringMatching ( 'Something went wrong' ) ,
2020-04-08 14:13:33 +05:30
} ) ;
} ) ;
} ) ;
2019-02-15 15:39:39 +05:30
describe ( 'setHighlightedRow' , ( ) => {
2019-07-07 11:18:12 +05:30
it ( 'should mark currently selected diff and set lineHash and fileHash of highlightedRow' , ( ) => {
2022-06-21 17:19:12 +05:30
return testAction ( diffActions . setHighlightedRow , 'ABC_123' , { } , [
2019-02-15 15:39:39 +05:30
{ type : types . SET _HIGHLIGHTED _ROW , payload : 'ABC_123' } ,
2021-12-11 22:18:48 +05:30
{ type : types . SET _CURRENT _DIFF _FILE , payload : 'ABC' } ,
2019-02-15 15:39:39 +05:30
] ) ;
} ) ;
} ) ;
2018-11-20 20:47:30 +05:30
describe ( 'assignDiscussionsToDiff' , ( ) => {
2020-07-28 23:09:34 +05:30
afterEach ( ( ) => {
window . location . hash = '' ;
} ) ;
2022-06-21 17:19:12 +05:30
it ( 'should merge discussions into diffs' , ( ) => {
2020-03-13 15:44:24 +05:30
window . location . hash = 'ABC_123' ;
2018-11-20 20:47:30 +05:30
const state = {
diffFiles : [
{
2019-02-15 15:39:39 +05:30
file _hash : 'ABC' ,
parallel _diff _lines : [
2018-11-20 20:47:30 +05:30
{
left : {
2019-02-15 15:39:39 +05:30
line _code : 'ABC_1_1' ,
2018-11-20 20:47:30 +05:30
discussions : [ ] ,
} ,
right : {
2019-02-15 15:39:39 +05:30
line _code : 'ABC_1_1' ,
2018-11-20 20:47:30 +05:30
discussions : [ ] ,
} ,
} ,
] ,
2019-02-15 15:39:39 +05:30
highlighted _diff _lines : [
2018-11-20 20:47:30 +05:30
{
2019-02-15 15:39:39 +05:30
line _code : 'ABC_1_1' ,
2018-11-20 20:47:30 +05:30
discussions : [ ] ,
2019-02-15 15:39:39 +05:30
old _line : 5 ,
new _line : null ,
2018-11-20 20:47:30 +05:30
} ,
] ,
2019-02-15 15:39:39 +05:30
diff _refs : {
base _sha : 'abc' ,
head _sha : 'def' ,
start _sha : 'ghi' ,
2018-11-20 20:47:30 +05:30
} ,
2019-02-15 15:39:39 +05:30
new _path : 'file1' ,
old _path : 'file2' ,
2018-11-20 20:47:30 +05:30
} ,
] ,
} ;
const diffPosition = {
2019-02-15 15:39:39 +05:30
base _sha : 'abc' ,
head _sha : 'def' ,
start _sha : 'ghi' ,
new _line : null ,
new _path : 'file1' ,
old _line : 5 ,
old _path : 'file2' ,
2018-11-20 20:47:30 +05:30
} ;
const singleDiscussion = {
line _code : 'ABC_1_1' ,
diff _discussion : { } ,
diff _file : {
file _hash : 'ABC' ,
} ,
2019-02-15 15:39:39 +05:30
file _hash : 'ABC' ,
2018-11-20 20:47:30 +05:30
resolvable : true ,
position : diffPosition ,
original _position : diffPosition ,
} ;
2018-12-13 13:39:08 +05:30
const discussions = [ singleDiscussion ] ;
2018-11-20 20:47:30 +05:30
2022-06-21 17:19:12 +05:30
return testAction (
diffActions . assignDiscussionsToDiff ,
2018-11-20 20:47:30 +05:30
discussions ,
state ,
[
{
type : types . SET _LINE _DISCUSSIONS _FOR _FILE ,
payload : {
2018-12-13 13:39:08 +05:30
discussion : singleDiscussion ,
2018-11-20 20:47:30 +05:30
diffPositionByLineCode : {
ABC _1 _1 : {
2019-02-15 15:39:39 +05:30
base _sha : 'abc' ,
head _sha : 'def' ,
start _sha : 'ghi' ,
new _line : null ,
new _path : 'file1' ,
old _line : 5 ,
old _path : 'file2' ,
2020-06-23 00:09:42 +05:30
line _range : null ,
2019-02-15 15:39:39 +05:30
line _code : 'ABC_1_1' ,
position _type : 'text' ,
2018-11-20 20:47:30 +05:30
} ,
} ,
2019-12-04 20:38:33 +05:30
hash : 'ABC_123' ,
2018-11-20 20:47:30 +05:30
} ,
} ,
] ,
[ ] ,
) ;
} ) ;
2020-07-28 23:09:34 +05:30
2022-06-21 17:19:12 +05:30
it ( 'dispatches setCurrentDiffFileIdFromNote with note ID' , ( ) => {
2020-07-28 23:09:34 +05:30
window . location . hash = 'note_123' ;
2022-06-21 17:19:12 +05:30
return testAction (
diffActions . assignDiscussionsToDiff ,
2020-07-28 23:09:34 +05:30
[ ] ,
2023-05-27 22:25:52 +05:30
{ diffFiles : [ ] , flatBlobsList : [ ] } ,
2020-07-28 23:09:34 +05:30
[ ] ,
[ { type : 'setCurrentDiffFileIdFromNote' , payload : '123' } ] ,
) ;
} ) ;
2018-11-20 20:47:30 +05:30
} ) ;
describe ( 'removeDiscussionsFromDiff' , ( ) => {
2022-06-21 17:19:12 +05:30
it ( 'should remove discussions from diffs' , ( ) => {
2018-11-20 20:47:30 +05:30
const state = {
diffFiles : [
{
2019-02-15 15:39:39 +05:30
file _hash : 'ABC' ,
parallel _diff _lines : [
2018-11-20 20:47:30 +05:30
{
left : {
2019-02-15 15:39:39 +05:30
line _code : 'ABC_1_1' ,
2018-11-20 20:47:30 +05:30
discussions : [
{
id : 1 ,
} ,
] ,
} ,
right : {
2019-02-15 15:39:39 +05:30
line _code : 'ABC_1_1' ,
2018-11-20 20:47:30 +05:30
discussions : [ ] ,
} ,
} ,
] ,
2019-02-15 15:39:39 +05:30
highlighted _diff _lines : [
2018-11-20 20:47:30 +05:30
{
2019-02-15 15:39:39 +05:30
line _code : 'ABC_1_1' ,
2018-11-20 20:47:30 +05:30
discussions : [ ] ,
} ,
] ,
} ,
] ,
} ;
const singleDiscussion = {
2018-12-13 13:39:08 +05:30
id : '1' ,
2019-02-15 15:39:39 +05:30
file _hash : 'ABC' ,
2018-11-20 20:47:30 +05:30
line _code : 'ABC_1_1' ,
} ;
2022-06-21 17:19:12 +05:30
return testAction (
diffActions . removeDiscussionsFromDiff ,
2018-11-20 20:47:30 +05:30
singleDiscussion ,
state ,
[
{
type : types . REMOVE _LINE _DISCUSSIONS _FOR _FILE ,
payload : {
2018-12-13 13:39:08 +05:30
id : '1' ,
2018-11-20 20:47:30 +05:30
fileHash : 'ABC' ,
lineCode : 'ABC_1_1' ,
} ,
} ,
] ,
[ ] ,
) ;
} ) ;
} ) ;
describe ( 'startRenderDiffsQueue' , ( ) => {
2018-12-05 23:21:45 +05:30
it ( 'should set all files to RENDER_FILE' , ( ) => {
2018-11-20 20:47:30 +05:30
const state = {
diffFiles : [
{
id : 1 ,
renderIt : false ,
2019-07-07 11:18:12 +05:30
viewer : {
2021-01-03 14:25:43 +05:30
automaticallyCollapsed : false ,
2019-07-07 11:18:12 +05:30
} ,
2018-11-20 20:47:30 +05:30
} ,
{
id : 2 ,
renderIt : false ,
2019-07-07 11:18:12 +05:30
viewer : {
2021-01-03 14:25:43 +05:30
automaticallyCollapsed : false ,
2019-07-07 11:18:12 +05:30
} ,
2018-11-20 20:47:30 +05:30
} ,
] ,
} ;
const pseudoCommit = ( commitType , file ) => {
expect ( commitType ) . toBe ( types . RENDER _FILE ) ;
Object . assign ( file , {
renderIt : true ,
} ) ;
} ;
2022-06-21 17:19:12 +05:30
diffActions . startRenderDiffsQueue ( { state , commit : pseudoCommit } ) ;
2018-11-20 20:47:30 +05:30
2018-12-05 23:21:45 +05:30
expect ( state . diffFiles [ 0 ] . renderIt ) . toBe ( true ) ;
expect ( state . diffFiles [ 1 ] . renderIt ) . toBe ( true ) ;
2018-11-20 20:47:30 +05:30
} ) ;
} ) ;
2018-11-08 19:23:39 +05:30
describe ( 'setInlineDiffViewType' , ( ) => {
2022-06-21 17:19:12 +05:30
it ( 'should set diff view type to inline and also set the cookie properly' , async ( ) => {
await testAction (
diffActions . setInlineDiffViewType ,
2018-11-08 19:23:39 +05:30
null ,
{ } ,
[ { type : types . SET _DIFF _VIEW _TYPE , payload : INLINE _DIFF _VIEW _TYPE } ] ,
[ ] ,
) ;
2022-06-21 17:19:12 +05:30
expect ( Cookies . get ( 'diff_view' ) ) . toEqual ( INLINE _DIFF _VIEW _TYPE ) ;
2018-11-08 19:23:39 +05:30
} ) ;
} ) ;
describe ( 'setParallelDiffViewType' , ( ) => {
2022-06-21 17:19:12 +05:30
it ( 'should set diff view type to parallel and also set the cookie properly' , async ( ) => {
await testAction (
diffActions . setParallelDiffViewType ,
2018-11-08 19:23:39 +05:30
null ,
{ } ,
[ { type : types . SET _DIFF _VIEW _TYPE , payload : PARALLEL _DIFF _VIEW _TYPE } ] ,
[ ] ,
) ;
2022-06-21 17:19:12 +05:30
expect ( Cookies . get ( DIFF _VIEW _COOKIE _NAME ) ) . toEqual ( PARALLEL _DIFF _VIEW _TYPE ) ;
2018-11-08 19:23:39 +05:30
} ) ;
} ) ;
describe ( 'showCommentForm' , ( ) => {
2022-06-21 17:19:12 +05:30
it ( 'should call mutation to show comment form' , ( ) => {
2019-02-15 15:39:39 +05:30
const payload = { lineCode : 'lineCode' , fileHash : 'hash' } ;
2018-11-08 19:23:39 +05:30
2022-06-21 17:19:12 +05:30
return testAction (
diffActions . showCommentForm ,
2018-11-08 19:23:39 +05:30
payload ,
{ } ,
2019-02-15 15:39:39 +05:30
[ { type : types . TOGGLE _LINE _HAS _FORM , payload : { ... payload , hasForm : true } } ] ,
2018-11-08 19:23:39 +05:30
[ ] ,
) ;
} ) ;
} ) ;
describe ( 'cancelCommentForm' , ( ) => {
2022-06-21 17:19:12 +05:30
it ( 'should call mutation to cancel comment form' , ( ) => {
2019-02-15 15:39:39 +05:30
const payload = { lineCode : 'lineCode' , fileHash : 'hash' } ;
2018-11-08 19:23:39 +05:30
2022-06-21 17:19:12 +05:30
return testAction (
diffActions . cancelCommentForm ,
2018-11-08 19:23:39 +05:30
payload ,
{ } ,
2019-02-15 15:39:39 +05:30
[ { type : types . TOGGLE _LINE _HAS _FORM , payload : { ... payload , hasForm : false } } ] ,
2018-11-08 19:23:39 +05:30
[ ] ,
) ;
} ) ;
} ) ;
describe ( 'loadMoreLines' , ( ) => {
2022-06-21 17:19:12 +05:30
it ( 'should call mutation to show comment form' , ( ) => {
2018-11-08 19:23:39 +05:30
const endpoint = '/diffs/load/more/lines' ;
const params = { since : 6 , to : 26 } ;
const lineNumbers = { oldLineNumber : 3 , newLineNumber : 5 } ;
const fileHash = 'ff9200' ;
2019-10-12 21:52:04 +05:30
const isExpandDown = false ;
const nextLineNumbers = { } ;
const options = { endpoint , params , lineNumbers , fileHash , isExpandDown , nextLineNumbers } ;
2018-11-08 19:23:39 +05:30
const contextLines = { contextLines : [ { lineCode : 6 } ] } ;
2023-04-23 21:23:45 +05:30
mock . onGet ( endpoint ) . reply ( HTTP _STATUS _OK , contextLines ) ;
2018-11-08 19:23:39 +05:30
2022-06-21 17:19:12 +05:30
return testAction (
diffActions . loadMoreLines ,
2018-11-08 19:23:39 +05:30
options ,
{ } ,
[
{
type : types . ADD _CONTEXT _LINES ,
2019-10-12 21:52:04 +05:30
payload : { lineNumbers , contextLines , params , fileHash , isExpandDown , nextLineNumbers } ,
2018-11-08 19:23:39 +05:30
} ,
] ,
[ ] ,
) ;
} ) ;
} ) ;
describe ( 'loadCollapsedDiff' , ( ) => {
2019-09-04 21:01:54 +05:30
const state = { showWhitespace : true } ;
2022-06-21 17:19:12 +05:30
it ( 'should fetch data and call mutation with response and the give parameter' , ( ) => {
2019-02-15 15:39:39 +05:30
const file = { hash : 123 , load _collapsed _diff _url : '/load/collapsed/diff/url' } ;
2018-11-08 19:23:39 +05:30
const data = { hash : 123 , parallelDiffLines : [ { lineCode : 1 } ] } ;
2020-04-22 19:07:51 +05:30
const commit = jest . fn ( ) ;
2023-04-23 21:23:45 +05:30
mock . onGet ( file . loadCollapsedDiffUrl ) . reply ( HTTP _STATUS _OK , data ) ;
2018-11-08 19:23:39 +05:30
2022-06-21 17:19:12 +05:30
return diffActions
. loadCollapsedDiff ( { commit , getters : { commitId : null } , state } , file )
2019-02-15 15:39:39 +05:30
. then ( ( ) => {
expect ( commit ) . toHaveBeenCalledWith ( types . ADD _COLLAPSED _DIFFS , { file , data } ) ;
2022-06-21 17:19:12 +05:30
} ) ;
2019-02-15 15:39:39 +05:30
} ) ;
it ( 'should fetch data without commit ID' , ( ) => {
const file = { load _collapsed _diff _url : '/load/collapsed/diff/url' } ;
const getters = {
commitId : null ,
} ;
2020-04-22 19:07:51 +05:30
jest . spyOn ( axios , 'get' ) . mockReturnValue ( Promise . resolve ( { data : { } } ) ) ;
2019-02-15 15:39:39 +05:30
2022-06-21 17:19:12 +05:30
diffActions . loadCollapsedDiff ( { commit ( ) { } , getters , state } , file ) ;
2019-02-15 15:39:39 +05:30
expect ( axios . get ) . toHaveBeenCalledWith ( file . load _collapsed _diff _url , {
2019-09-04 21:01:54 +05:30
params : { commit _id : null , w : '0' } ,
2019-02-15 15:39:39 +05:30
} ) ;
} ) ;
it ( 'should fetch data with commit ID' , ( ) => {
const file = { load _collapsed _diff _url : '/load/collapsed/diff/url' } ;
const getters = {
commitId : '123' ,
} ;
2020-04-22 19:07:51 +05:30
jest . spyOn ( axios , 'get' ) . mockReturnValue ( Promise . resolve ( { data : { } } ) ) ;
2019-02-15 15:39:39 +05:30
2022-06-21 17:19:12 +05:30
diffActions . loadCollapsedDiff ( { commit ( ) { } , getters , state } , file ) ;
2019-02-15 15:39:39 +05:30
expect ( axios . get ) . toHaveBeenCalledWith ( file . load _collapsed _diff _url , {
2019-09-04 21:01:54 +05:30
params : { commit _id : '123' , w : '0' } ,
2019-02-15 15:39:39 +05:30
} ) ;
2018-11-08 19:23:39 +05:30
} ) ;
2023-03-04 22:38:38 +05:30
describe ( 'version parameters' , ( ) => {
const diffId = '4' ;
const startSha = 'abc' ;
const pathRoot = 'a/a/-/merge_requests/1' ;
let file ;
let getters ;
beforeAll ( ( ) => {
file = { load _collapsed _diff _url : '/load/collapsed/diff/url' } ;
getters = { } ;
} ) ;
beforeEach ( ( ) => {
jest . spyOn ( axios , 'get' ) . mockReturnValue ( Promise . resolve ( { data : { } } ) ) ;
} ) ;
it ( 'fetches the data when there is no mergeRequestDiff' , ( ) => {
diffActions . loadCollapsedDiff ( { commit ( ) { } , getters , state } , file ) ;
expect ( axios . get ) . toHaveBeenCalledWith ( file . load _collapsed _diff _url , {
params : expect . any ( Object ) ,
} ) ;
} ) ;
it . each `
desc | versionPath | start _sha | diff _id
$ { 'no additional version information' } | $ { ` ${ pathRoot } ?search=terms ` } | $ { undefined } | $ { undefined }
$ { 'the diff_id' } | $ { ` ${ pathRoot } ?diff_id= ${ diffId } ` } | $ { undefined } | $ { diffId }
$ { 'the start_sha' } | $ { ` ${ pathRoot } ?start_sha= ${ startSha } ` } | $ { startSha } | $ { undefined }
$ { 'all available version information' } | $ { ` ${ pathRoot } ?diff_id= ${ diffId } &start_sha= ${ startSha } ` } | $ { startSha } | $ { diffId }
` ('fetches the data and includes $ desc', ({ versionPath, start_sha, diff_id }) => {
jest . spyOn ( axios , 'get' ) . mockReturnValue ( Promise . resolve ( { data : { } } ) ) ;
diffActions . loadCollapsedDiff (
{ commit ( ) { } , getters , state : { mergeRequestDiff : { version _path : versionPath } } } ,
file ,
) ;
expect ( axios . get ) . toHaveBeenCalledWith ( file . load _collapsed _diff _url , {
params : expect . objectContaining ( { start _sha , diff _id } ) ,
} ) ;
} ) ;
} ) ;
2018-11-08 19:23:39 +05:30
} ) ;
describe ( 'toggleFileDiscussions' , ( ) => {
it ( 'should dispatch collapseDiscussion when all discussions are expanded' , ( ) => {
const getters = {
2020-04-22 19:07:51 +05:30
getDiffFileDiscussions : jest . fn ( ( ) => [ { id : 1 } ] ) ,
diffHasAllExpandedDiscussions : jest . fn ( ( ) => true ) ,
diffHasAllCollapsedDiscussions : jest . fn ( ( ) => false ) ,
2018-11-08 19:23:39 +05:30
} ;
2020-04-22 19:07:51 +05:30
const dispatch = jest . fn ( ) ;
2018-11-08 19:23:39 +05:30
2022-06-21 17:19:12 +05:30
diffActions . toggleFileDiscussions ( { getters , dispatch } ) ;
2018-11-08 19:23:39 +05:30
2018-11-20 20:47:30 +05:30
expect ( dispatch ) . toHaveBeenCalledWith (
'collapseDiscussion' ,
{ discussionId : 1 } ,
{ root : true } ,
) ;
2018-11-08 19:23:39 +05:30
} ) ;
it ( 'should dispatch expandDiscussion when all discussions are collapsed' , ( ) => {
const getters = {
2020-04-22 19:07:51 +05:30
getDiffFileDiscussions : jest . fn ( ( ) => [ { id : 1 } ] ) ,
diffHasAllExpandedDiscussions : jest . fn ( ( ) => false ) ,
diffHasAllCollapsedDiscussions : jest . fn ( ( ) => true ) ,
2018-11-08 19:23:39 +05:30
} ;
2020-04-22 19:07:51 +05:30
const dispatch = jest . fn ( ) ;
2018-11-08 19:23:39 +05:30
2022-06-21 17:19:12 +05:30
diffActions . toggleFileDiscussions ( { getters , dispatch } ) ;
2018-11-08 19:23:39 +05:30
2018-11-20 20:47:30 +05:30
expect ( dispatch ) . toHaveBeenCalledWith (
'expandDiscussion' ,
{ discussionId : 1 } ,
{ root : true } ,
) ;
2018-11-08 19:23:39 +05:30
} ) ;
it ( 'should dispatch expandDiscussion when some discussions are collapsed and others are expanded for the collapsed discussion' , ( ) => {
const getters = {
2020-04-22 19:07:51 +05:30
getDiffFileDiscussions : jest . fn ( ( ) => [ { expanded : false , id : 1 } ] ) ,
diffHasAllExpandedDiscussions : jest . fn ( ( ) => false ) ,
diffHasAllCollapsedDiscussions : jest . fn ( ( ) => false ) ,
2018-11-08 19:23:39 +05:30
} ;
2020-04-22 19:07:51 +05:30
const dispatch = jest . fn ( ) ;
2018-11-08 19:23:39 +05:30
2022-06-21 17:19:12 +05:30
diffActions . toggleFileDiscussions ( { getters , dispatch } ) ;
2018-11-20 20:47:30 +05:30
expect ( dispatch ) . toHaveBeenCalledWith (
'expandDiscussion' ,
{ discussionId : 1 } ,
{ root : true } ,
) ;
} ) ;
} ) ;
describe ( 'scrollToLineIfNeededInline' , ( ) => {
const lineMock = {
2019-02-15 15:39:39 +05:30
line _code : 'ABC_123' ,
2018-11-20 20:47:30 +05:30
} ;
it ( 'should not call handleLocationHash when there is not hash' , ( ) => {
window . location . hash = '' ;
2022-06-21 17:19:12 +05:30
diffActions . scrollToLineIfNeededInline ( { } , lineMock ) ;
2018-11-20 20:47:30 +05:30
2020-04-22 19:07:51 +05:30
expect ( commonUtils . handleLocationHash ) . not . toHaveBeenCalled ( ) ;
2018-11-20 20:47:30 +05:30
} ) ;
it ( 'should not call handleLocationHash when the hash does not match any line' , ( ) => {
window . location . hash = 'XYZ_456' ;
2022-06-21 17:19:12 +05:30
diffActions . scrollToLineIfNeededInline ( { } , lineMock ) ;
2018-11-20 20:47:30 +05:30
2020-04-22 19:07:51 +05:30
expect ( commonUtils . handleLocationHash ) . not . toHaveBeenCalled ( ) ;
2018-11-20 20:47:30 +05:30
} ) ;
it ( 'should call handleLocationHash only when the hash matches a line' , ( ) => {
window . location . hash = 'ABC_123' ;
2022-06-21 17:19:12 +05:30
diffActions . scrollToLineIfNeededInline (
2018-11-20 20:47:30 +05:30
{ } ,
{
lineCode : 'ABC_456' ,
} ,
) ;
2022-06-21 17:19:12 +05:30
diffActions . scrollToLineIfNeededInline ( { } , lineMock ) ;
diffActions . scrollToLineIfNeededInline (
2018-11-20 20:47:30 +05:30
{ } ,
{
lineCode : 'XYZ_456' ,
} ,
) ;
2020-04-22 19:07:51 +05:30
expect ( commonUtils . handleLocationHash ) . toHaveBeenCalled ( ) ;
expect ( commonUtils . handleLocationHash ) . toHaveBeenCalledTimes ( 1 ) ;
2018-11-20 20:47:30 +05:30
} ) ;
} ) ;
describe ( 'scrollToLineIfNeededParallel' , ( ) => {
const lineMock = {
left : null ,
right : {
2019-02-15 15:39:39 +05:30
line _code : 'ABC_123' ,
2018-11-20 20:47:30 +05:30
} ,
} ;
it ( 'should not call handleLocationHash when there is not hash' , ( ) => {
window . location . hash = '' ;
2022-06-21 17:19:12 +05:30
diffActions . scrollToLineIfNeededParallel ( { } , lineMock ) ;
2018-11-20 20:47:30 +05:30
2020-04-22 19:07:51 +05:30
expect ( commonUtils . handleLocationHash ) . not . toHaveBeenCalled ( ) ;
2018-11-20 20:47:30 +05:30
} ) ;
it ( 'should not call handleLocationHash when the hash does not match any line' , ( ) => {
window . location . hash = 'XYZ_456' ;
2022-06-21 17:19:12 +05:30
diffActions . scrollToLineIfNeededParallel ( { } , lineMock ) ;
2018-11-20 20:47:30 +05:30
2020-04-22 19:07:51 +05:30
expect ( commonUtils . handleLocationHash ) . not . toHaveBeenCalled ( ) ;
2018-11-20 20:47:30 +05:30
} ) ;
it ( 'should call handleLocationHash only when the hash matches a line' , ( ) => {
window . location . hash = 'ABC_123' ;
2022-06-21 17:19:12 +05:30
diffActions . scrollToLineIfNeededParallel (
2018-11-20 20:47:30 +05:30
{ } ,
{
left : null ,
right : {
lineCode : 'ABC_456' ,
} ,
} ,
) ;
2022-06-21 17:19:12 +05:30
diffActions . scrollToLineIfNeededParallel ( { } , lineMock ) ;
diffActions . scrollToLineIfNeededParallel (
2018-11-20 20:47:30 +05:30
{ } ,
{
left : null ,
right : {
lineCode : 'XYZ_456' ,
} ,
} ,
) ;
2018-11-08 19:23:39 +05:30
2020-04-22 19:07:51 +05:30
expect ( commonUtils . handleLocationHash ) . toHaveBeenCalled ( ) ;
expect ( commonUtils . handleLocationHash ) . toHaveBeenCalledTimes ( 1 ) ;
2018-11-08 19:23:39 +05:30
} ) ;
} ) ;
2018-12-05 23:21:45 +05:30
describe ( 'saveDiffDiscussion' , ( ) => {
2023-06-20 00:43:36 +05:30
const dispatch = jest . fn ( ( name ) => {
switch ( name ) {
case 'saveNote' :
return Promise . resolve ( {
discussion : 'test' ,
} ) ;
case 'updateDiscussion' :
return Promise . resolve ( 'discussion' ) ;
default :
return Promise . resolve ( { } ) ;
}
} ) ;
2018-12-05 23:21:45 +05:30
2023-06-20 00:43:36 +05:30
const commitId = 'something' ;
const formData = {
diffFile : getDiffFileMock ( ) ,
noteableData : { } ,
} ;
const note = { } ;
const state = {
commit : {
id : commitId ,
} ,
} ;
it ( 'dispatches actions' , ( ) => {
2022-06-21 17:19:12 +05:30
return diffActions . saveDiffDiscussion ( { state , dispatch } , { note , formData } ) . then ( ( ) => {
expect ( dispatch ) . toHaveBeenCalledTimes ( 5 ) ;
expect ( dispatch ) . toHaveBeenNthCalledWith ( 1 , 'saveNote' , expect . any ( Object ) , {
root : true ,
} ) ;
2019-02-15 15:39:39 +05:30
2022-06-21 17:19:12 +05:30
const postData = dispatch . mock . calls [ 0 ] [ 1 ] ;
expect ( postData . data . note . commit _id ) . toBe ( commitId ) ;
2019-02-15 15:39:39 +05:30
2022-06-21 17:19:12 +05:30
expect ( dispatch ) . toHaveBeenNthCalledWith ( 2 , 'updateDiscussion' , 'test' , { root : true } ) ;
expect ( dispatch ) . toHaveBeenNthCalledWith ( 3 , 'assignDiscussionsToDiff' , [ 'discussion' ] ) ;
} ) ;
2018-12-05 23:21:45 +05:30
} ) ;
2023-06-20 00:43:36 +05:30
it ( 'should not add note with sensitive token' , async ( ) => {
const sensitiveMessage = 'token: glpat-1234567890abcdefghij' ;
await diffActions . saveDiffDiscussion (
{ state , dispatch } ,
{ note : sensitiveMessage , formData } ,
) ;
expect ( dispatch ) . not . toHaveBeenCalled ( ) ;
} ) ;
2018-12-05 23:21:45 +05:30
} ) ;
describe ( 'toggleTreeOpen' , ( ) => {
2022-06-21 17:19:12 +05:30
it ( 'commits TOGGLE_FOLDER_OPEN' , ( ) => {
return testAction (
diffActions . toggleTreeOpen ,
2018-12-05 23:21:45 +05:30
'path' ,
{ } ,
[ { type : types . TOGGLE _FOLDER _OPEN , payload : 'path' } ] ,
[ ] ,
) ;
} ) ;
} ) ;
2023-06-20 00:43:36 +05:30
describe ( 'goToFile' , ( ) => {
const getters = { } ;
const file = { path : 'path' } ;
const fileHash = 'test' ;
let state ;
let dispatch ;
let commit ;
beforeEach ( ( ) => {
getters . isTreePathLoaded = ( ) => false ;
state = {
viewDiffsFileByFile : true ,
treeEntries : {
path : {
fileHash ,
} ,
} ,
} ;
commit = jest . fn ( ) ;
dispatch = jest . fn ( ) . mockResolvedValue ( ) ;
} ) ;
it ( 'immediately defers to scrollToFile if the app is not in file-by-file mode' , ( ) => {
state . viewDiffsFileByFile = false ;
diffActions . goToFile ( { state , dispatch } , file ) ;
expect ( dispatch ) . toHaveBeenCalledWith ( 'scrollToFile' , file ) ;
} ) ;
describe ( 'when the app is in fileByFile mode' , ( ) => {
describe ( 'when the singleFileFileByFile feature flag is enabled' , ( ) => {
it ( 'commits SET_CURRENT_DIFF_FILE' , ( ) => {
diffActions . goToFile (
{ state , commit , dispatch , getters } ,
{ path : file . path , singleFile : true } ,
) ;
expect ( commit ) . toHaveBeenCalledWith ( types . SET _CURRENT _DIFF _FILE , fileHash ) ;
} ) ;
it ( 'does nothing more if the path has already been loaded' , ( ) => {
getters . isTreePathLoaded = ( ) => true ;
diffActions . goToFile (
{ state , dispatch , getters , commit } ,
{ path : file . path , singleFile : true } ,
) ;
expect ( commit ) . toHaveBeenCalledWith ( types . SET _CURRENT _DIFF _FILE , fileHash ) ;
expect ( dispatch ) . toHaveBeenCalledTimes ( 0 ) ;
} ) ;
describe ( 'when the tree entry has not been loaded' , ( ) => {
it ( 'updates location hash' , ( ) => {
diffActions . goToFile (
{ state , commit , getters , dispatch } ,
{ path : file . path , singleFile : true } ,
) ;
expect ( document . location . hash ) . toBe ( '#test' ) ;
} ) ;
it ( 'loads the file and then scrolls to it' , async ( ) => {
diffActions . goToFile (
{ state , commit , getters , dispatch } ,
{ path : file . path , singleFile : true } ,
) ;
// Wait for the fetchFileByFile dispatch to return, to trigger scrollToFile
await waitForPromises ( ) ;
expect ( dispatch ) . toHaveBeenCalledWith ( 'fetchFileByFile' ) ;
expect ( dispatch ) . toHaveBeenCalledWith ( 'scrollToFile' , file ) ;
expect ( dispatch ) . toHaveBeenCalledTimes ( 2 ) ;
} ) ;
it ( 'shows an alert when there was an error fetching the file' , async ( ) => {
dispatch = jest . fn ( ) . mockRejectedValue ( ) ;
diffActions . goToFile (
{ state , commit , getters , dispatch } ,
{ path : file . path , singleFile : true } ,
) ;
// Wait for the fetchFileByFile dispatch to return, to trigger the catch
await waitForPromises ( ) ;
expect ( createAlert ) . toHaveBeenCalledTimes ( 1 ) ;
expect ( createAlert ) . toHaveBeenCalledWith ( {
message : expect . stringMatching ( LOAD _SINGLE _DIFF _FAILED ) ,
} ) ;
} ) ;
} ) ;
} ) ;
} ) ;
} ) ;
2018-12-05 23:21:45 +05:30
describe ( 'scrollToFile' , ( ) => {
let commit ;
2021-10-27 15:23:28 +05:30
const getters = { isVirtualScrollingEnabled : false } ;
2018-12-05 23:21:45 +05:30
beforeEach ( ( ) => {
2020-04-22 19:07:51 +05:30
commit = jest . fn ( ) ;
2018-12-05 23:21:45 +05:30
} ) ;
it ( 'updates location hash' , ( ) => {
const state = {
treeEntries : {
path : {
fileHash : 'test' ,
} ,
} ,
} ;
2022-06-21 17:19:12 +05:30
diffActions . scrollToFile ( { state , commit , getters } , { path : 'path' } ) ;
2018-12-05 23:21:45 +05:30
expect ( document . location . hash ) . toBe ( '#test' ) ;
} ) ;
2021-12-11 22:18:48 +05:30
it ( 'commits SET_CURRENT_DIFF_FILE' , ( ) => {
2018-12-05 23:21:45 +05:30
const state = {
treeEntries : {
path : {
fileHash : 'test' ,
} ,
} ,
} ;
2022-06-21 17:19:12 +05:30
diffActions . scrollToFile ( { state , commit , getters } , { path : 'path' } ) ;
2018-12-05 23:21:45 +05:30
2021-12-11 22:18:48 +05:30
expect ( commit ) . toHaveBeenCalledWith ( types . SET _CURRENT _DIFF _FILE , 'test' ) ;
2018-12-05 23:21:45 +05:30
} ) ;
} ) ;
2021-02-22 17:27:13 +05:30
describe ( 'setShowTreeList' , ( ) => {
2022-06-21 17:19:12 +05:30
it ( 'commits toggle' , ( ) => {
return testAction (
diffActions . setShowTreeList ,
2021-02-22 17:27:13 +05:30
{ showTreeList : true } ,
{ } ,
[ { type : types . SET _SHOW _TREE _LIST , payload : true } ] ,
[ ] ,
) ;
2018-12-05 23:21:45 +05:30
} ) ;
it ( 'updates localStorage' , ( ) => {
2020-04-22 19:07:51 +05:30
jest . spyOn ( localStorage , 'setItem' ) . mockImplementation ( ( ) => { } ) ;
2018-12-05 23:21:45 +05:30
2022-06-21 17:19:12 +05:30
diffActions . setShowTreeList ( { commit ( ) { } } , { showTreeList : true } ) ;
2018-12-05 23:21:45 +05:30
expect ( localStorage . setItem ) . toHaveBeenCalledWith ( 'mr_tree_show' , true ) ;
} ) ;
2019-07-07 11:18:12 +05:30
it ( 'does not update localStorage' , ( ) => {
2020-04-22 19:07:51 +05:30
jest . spyOn ( localStorage , 'setItem' ) . mockImplementation ( ( ) => { } ) ;
2019-07-07 11:18:12 +05:30
2022-06-21 17:19:12 +05:30
diffActions . setShowTreeList ( { commit ( ) { } } , { showTreeList : true , saving : false } ) ;
2019-07-07 11:18:12 +05:30
expect ( localStorage . setItem ) . not . toHaveBeenCalled ( ) ;
} ) ;
2018-12-05 23:21:45 +05:30
} ) ;
2019-02-15 15:39:39 +05:30
describe ( 'renderFileForDiscussionId' , ( ) => {
const rootState = {
notes : {
discussions : [
{
id : '123' ,
diff _file : {
file _hash : 'HASH' ,
} ,
} ,
{
id : '456' ,
diff _file : {
file _hash : 'HASH' ,
} ,
} ,
] ,
} ,
} ;
let commit ;
let $emit ;
const state = ( { collapsed , renderIt } ) => ( {
diffFiles : [
{
file _hash : 'HASH' ,
2019-07-07 11:18:12 +05:30
viewer : {
2021-01-03 14:25:43 +05:30
automaticallyCollapsed : collapsed ,
2019-07-07 11:18:12 +05:30
} ,
2019-02-15 15:39:39 +05:30
renderIt ,
} ,
] ,
} ) ;
beforeEach ( ( ) => {
2020-04-22 19:07:51 +05:30
commit = jest . fn ( ) ;
$emit = jest . spyOn ( eventHub , '$emit' ) ;
2019-02-15 15:39:39 +05:30
} ) ;
it ( 'renders and expands file for the given discussion id' , ( ) => {
const localState = state ( { collapsed : true , renderIt : false } ) ;
2022-06-21 17:19:12 +05:30
diffActions . renderFileForDiscussionId ( { rootState , state : localState , commit } , '123' ) ;
2019-02-15 15:39:39 +05:30
expect ( commit ) . toHaveBeenCalledWith ( 'RENDER_FILE' , localState . diffFiles [ 0 ] ) ;
expect ( $emit ) . toHaveBeenCalledTimes ( 1 ) ;
2020-04-22 19:07:51 +05:30
expect ( commonUtils . scrollToElement ) . toHaveBeenCalledTimes ( 1 ) ;
2019-02-15 15:39:39 +05:30
} ) ;
it ( 'jumps to discussion on already rendered and expanded file' , ( ) => {
const localState = state ( { collapsed : false , renderIt : true } ) ;
2022-06-21 17:19:12 +05:30
diffActions . renderFileForDiscussionId ( { rootState , state : localState , commit } , '123' ) ;
2019-02-15 15:39:39 +05:30
expect ( commit ) . not . toHaveBeenCalled ( ) ;
expect ( $emit ) . toHaveBeenCalledTimes ( 1 ) ;
2020-04-22 19:07:51 +05:30
expect ( commonUtils . scrollToElement ) . not . toHaveBeenCalled ( ) ;
2019-02-15 15:39:39 +05:30
} ) ;
} ) ;
2019-03-02 22:35:43 +05:30
describe ( 'setRenderTreeList' , ( ) => {
2022-06-21 17:19:12 +05:30
it ( 'commits SET_RENDER_TREE_LIST' , ( ) => {
return testAction (
diffActions . setRenderTreeList ,
2021-10-27 15:23:28 +05:30
{ renderTreeList : true } ,
2019-03-02 22:35:43 +05:30
{ } ,
[ { type : types . SET _RENDER _TREE _LIST , payload : true } ] ,
[ ] ,
) ;
} ) ;
it ( 'sets localStorage' , ( ) => {
2022-06-21 17:19:12 +05:30
diffActions . setRenderTreeList ( { commit ( ) { } } , { renderTreeList : true } ) ;
2019-03-02 22:35:43 +05:30
expect ( localStorage . setItem ) . toHaveBeenCalledWith ( 'mr_diff_tree_list' , true ) ;
} ) ;
} ) ;
describe ( 'setShowWhitespace' , ( ) => {
2021-09-04 01:27:46 +05:30
const endpointUpdateUser = 'user/prefs' ;
let putSpy ;
2019-09-04 21:01:54 +05:30
beforeEach ( ( ) => {
2021-09-04 01:27:46 +05:30
putSpy = jest . spyOn ( axios , 'put' ) ;
2023-04-23 21:23:45 +05:30
mock . onPut ( endpointUpdateUser ) . reply ( HTTP _STATUS _OK , { } ) ;
2020-04-22 19:07:51 +05:30
jest . spyOn ( eventHub , '$emit' ) . mockImplementation ( ) ;
2019-09-04 21:01:54 +05:30
} ) ;
2022-06-21 17:19:12 +05:30
it ( 'commits SET_SHOW_WHITESPACE' , ( ) => {
return testAction (
diffActions . setShowWhitespace ,
2021-09-04 01:27:46 +05:30
{ showWhitespace : true , updateDatabase : false } ,
2019-03-02 22:35:43 +05:30
{ } ,
[ { type : types . SET _SHOW _WHITESPACE , payload : true } ] ,
[ ] ,
) ;
} ) ;
2021-09-30 23:02:18 +05:30
it ( 'saves to the database when the user is logged in' , async ( ) => {
window . gon = { current _user _id : 12345 } ;
2022-06-21 17:19:12 +05:30
await diffActions . setShowWhitespace (
2021-09-04 01:27:46 +05:30
{ state : { endpointUpdateUser } , commit ( ) { } } ,
{ showWhitespace : true , updateDatabase : true } ,
) ;
2019-09-04 21:01:54 +05:30
2021-09-04 01:27:46 +05:30
expect ( putSpy ) . toHaveBeenCalledWith ( endpointUpdateUser , { show _whitespace _in _diffs : true } ) ;
2019-09-04 21:01:54 +05:30
} ) ;
2021-09-30 23:02:18 +05:30
it ( 'does not try to save to the API if the user is not logged in' , async ( ) => {
window . gon = { } ;
2022-06-21 17:19:12 +05:30
await diffActions . setShowWhitespace (
2021-09-30 23:02:18 +05:30
{ state : { endpointUpdateUser } , commit ( ) { } } ,
{ showWhitespace : true , updateDatabase : true } ,
) ;
expect ( putSpy ) . not . toHaveBeenCalled ( ) ;
} ) ;
2021-09-04 01:27:46 +05:30
it ( 'emits eventHub event' , async ( ) => {
2022-06-21 17:19:12 +05:30
await diffActions . setShowWhitespace (
2021-09-04 01:27:46 +05:30
{ state : { } , commit ( ) { } } ,
{ showWhitespace : true , updateDatabase : false } ,
) ;
2019-09-04 21:01:54 +05:30
expect ( eventHub . $emit ) . toHaveBeenCalledWith ( 'refetchDiffData' ) ;
} ) ;
2019-03-02 22:35:43 +05:30
} ) ;
2019-07-07 11:18:12 +05:30
describe ( 'setRenderIt' , ( ) => {
2022-06-21 17:19:12 +05:30
it ( 'commits RENDER_FILE' , ( ) => {
return testAction (
diffActions . setRenderIt ,
'file' ,
{ } ,
[ { type : types . RENDER _FILE , payload : 'file' } ] ,
[ ] ,
) ;
2019-07-07 11:18:12 +05:30
} ) ;
} ) ;
describe ( 'receiveFullDiffError' , ( ) => {
2022-06-21 17:19:12 +05:30
it ( 'updates state with the file that did not load' , ( ) => {
return testAction (
diffActions . receiveFullDiffError ,
2019-07-07 11:18:12 +05:30
'file' ,
{ } ,
[ { type : types . RECEIVE _FULL _DIFF _ERROR , payload : 'file' } ] ,
[ ] ,
) ;
} ) ;
} ) ;
describe ( 'fetchFullDiff' , ( ) => {
describe ( 'success' , ( ) => {
beforeEach ( ( ) => {
2023-04-23 21:23:45 +05:30
mock . onGet ( ` ${ TEST _HOST } /context ` ) . replyOnce ( HTTP _STATUS _OK , [ 'test' ] ) ;
2019-07-07 11:18:12 +05:30
} ) ;
2022-06-21 17:19:12 +05:30
it ( 'commits the success and dispatches an action to expand the new lines' , ( ) => {
2019-07-31 22:56:46 +05:30
const file = {
2020-07-28 23:09:34 +05:30
context _lines _path : ` ${ TEST _HOST } /context ` ,
2019-07-31 22:56:46 +05:30
file _path : 'test' ,
file _hash : 'test' ,
} ;
2022-06-21 17:19:12 +05:30
return testAction (
diffActions . fetchFullDiff ,
2019-07-31 22:56:46 +05:30
file ,
2019-07-07 11:18:12 +05:30
null ,
2020-06-23 00:09:42 +05:30
[ { type : types . RECEIVE _FULL _DIFF _SUCCESS , payload : { filePath : 'test' } } ] ,
[ { type : 'setExpandedDiffLines' , payload : { file , data : [ 'test' ] } } ] ,
2019-07-07 11:18:12 +05:30
) ;
} ) ;
} ) ;
describe ( 'error' , ( ) => {
beforeEach ( ( ) => {
2023-04-23 21:23:45 +05:30
mock . onGet ( ` ${ TEST _HOST } /context ` ) . replyOnce ( HTTP _STATUS _INTERNAL _SERVER _ERROR ) ;
2019-07-07 11:18:12 +05:30
} ) ;
2022-06-21 17:19:12 +05:30
it ( 'dispatches receiveFullDiffError' , ( ) => {
return testAction (
diffActions . fetchFullDiff ,
2020-07-28 23:09:34 +05:30
{ context _lines _path : ` ${ TEST _HOST } /context ` , file _path : 'test' , file _hash : 'test' } ,
2019-07-07 11:18:12 +05:30
null ,
[ ] ,
[ { type : 'receiveFullDiffError' , payload : 'test' } ] ,
) ;
} ) ;
} ) ;
} ) ;
describe ( 'toggleFullDiff' , ( ) => {
let state ;
beforeEach ( ( ) => {
state = {
diffFiles : [ { file _path : 'test' , isShowingFullFile : false } ] ,
} ;
} ) ;
2022-06-21 17:19:12 +05:30
it ( 'dispatches fetchFullDiff when file is not expanded' , ( ) => {
return testAction (
diffActions . toggleFullDiff ,
2019-07-07 11:18:12 +05:30
'test' ,
state ,
2020-06-23 00:09:42 +05:30
[ { type : types . REQUEST _FULL _DIFF , payload : 'test' } ] ,
[ { type : 'fetchFullDiff' , payload : state . diffFiles [ 0 ] } ] ,
2019-07-07 11:18:12 +05:30
) ;
} ) ;
} ) ;
2020-05-24 23:13:21 +05:30
describe ( 'switchToFullDiffFromRenamedFile' , ( ) => {
const SUCCESS _URL = 'fakehost/context.success' ;
const testFilePath = 'testpath' ;
const updatedViewerName = 'testviewer' ;
const preparedLine = { prepared : 'in-a-test' } ;
const testFile = {
file _path : testFilePath ,
file _hash : 'testhash' ,
alternate _viewer : { name : updatedViewerName } ,
} ;
2021-01-29 00:20:46 +05:30
const updatedViewer = {
name : updatedViewerName ,
automaticallyCollapsed : false ,
manuallyCollapsed : false ,
} ;
2020-05-24 23:13:21 +05:30
const testData = [ { rich _text : 'test' } , { rich _text : 'file2' } ] ;
let renamedFile ;
beforeEach ( ( ) => {
jest . spyOn ( utils , 'prepareLineForRenamedFile' ) . mockImplementation ( ( ) => preparedLine ) ;
} ) ;
afterEach ( ( ) => {
renamedFile = null ;
} ) ;
describe ( 'success' , ( ) => {
beforeEach ( ( ) => {
renamedFile = { ... testFile , context _lines _path : SUCCESS _URL } ;
2023-04-23 21:23:45 +05:30
mock . onGet ( SUCCESS _URL ) . replyOnce ( HTTP _STATUS _OK , testData ) ;
2020-05-24 23:13:21 +05:30
} ) ;
it . each `
diffViewType
$ { INLINE _DIFF _VIEW _TYPE }
$ { PARALLEL _DIFF _VIEW _TYPE }
` (
'performs the correct mutations and starts a render queue for view type $diffViewType' ,
( { diffViewType } ) => {
return testAction (
2022-06-21 17:19:12 +05:30
diffActions . switchToFullDiffFromRenamedFile ,
2020-05-24 23:13:21 +05:30
{ diffFile : renamedFile } ,
{ diffViewType } ,
[
{
type : types . SET _DIFF _FILE _VIEWER ,
payload : { filePath : testFilePath , viewer : updatedViewer } ,
} ,
{
type : types . SET _CURRENT _VIEW _DIFF _FILE _LINES ,
payload : { filePath : testFilePath , lines : [ preparedLine , preparedLine ] } ,
} ,
] ,
[ { type : 'startRenderDiffsQueue' } ] ,
) ;
} ,
) ;
} ) ;
} ) ;
2021-01-29 00:20:46 +05:30
describe ( 'setFileUserCollapsed' , ( ) => {
2022-06-21 17:19:12 +05:30
it ( 'commits SET_FILE_COLLAPSED' , ( ) => {
return testAction (
diffActions . setFileCollapsedByUser ,
2019-07-07 11:18:12 +05:30
{ filePath : 'test' , collapsed : true } ,
null ,
2021-01-29 00:20:46 +05:30
[
{
type : types . SET _FILE _COLLAPSED ,
payload : { filePath : 'test' , collapsed : true , trigger : 'manual' } ,
} ,
] ,
2019-07-07 11:18:12 +05:30
[ ] ,
) ;
} ) ;
} ) ;
2019-07-31 22:56:46 +05:30
describe ( 'setExpandedDiffLines' , ( ) => {
beforeEach ( ( ) => {
2021-03-08 18:12:59 +05:30
utils . idleCallback . mockImplementation ( ( cb ) => {
2019-07-31 22:56:46 +05:30
cb ( { timeRemaining : ( ) => 50 } ) ;
} ) ;
} ) ;
2022-06-21 17:19:12 +05:30
it ( 'commits SET_CURRENT_VIEW_DIFF_FILE_LINES when lines less than MAX_RENDERING_DIFF_LINES' , ( ) => {
2020-04-22 19:07:51 +05:30
utils . convertExpandLines . mockImplementation ( ( ) => [ 'test' ] ) ;
2019-07-31 22:56:46 +05:30
2022-06-21 17:19:12 +05:30
return testAction (
diffActions . setExpandedDiffLines ,
2019-07-31 22:56:46 +05:30
{ file : { file _path : 'path' } , data : [ ] } ,
{ diffViewType : 'inline' } ,
[
{
type : 'SET_CURRENT_VIEW_DIFF_FILE_LINES' ,
payload : { filePath : 'path' , lines : [ 'test' ] } ,
} ,
] ,
[ ] ,
) ;
} ) ;
2022-06-21 17:19:12 +05:30
it ( 'commits ADD_CURRENT_VIEW_DIFF_FILE_LINES when lines more than MAX_RENDERING_DIFF_LINES' , ( ) => {
2019-07-31 22:56:46 +05:30
const lines = new Array ( 501 ) . fill ( ) . map ( ( _ , i ) => ` line- ${ i } ` ) ;
2020-04-22 19:07:51 +05:30
utils . convertExpandLines . mockReturnValue ( lines ) ;
2019-07-31 22:56:46 +05:30
2022-06-21 17:19:12 +05:30
return testAction (
diffActions . setExpandedDiffLines ,
2019-07-31 22:56:46 +05:30
{ file : { file _path : 'path' } , data : [ ] } ,
{ diffViewType : 'inline' } ,
[
{
type : 'SET_CURRENT_VIEW_DIFF_FILE_LINES' ,
payload : { filePath : 'path' , lines : lines . slice ( 0 , 200 ) } ,
} ,
{ type : 'TOGGLE_DIFF_FILE_RENDERING_MORE' , payload : 'path' } ,
... new Array ( 301 ) . fill ( ) . map ( ( _ , i ) => ( {
type : 'ADD_CURRENT_VIEW_DIFF_FILE_LINES' ,
payload : { filePath : 'path' , line : ` line- ${ i + 200 } ` } ,
} ) ) ,
{ type : 'TOGGLE_DIFF_FILE_RENDERING_MORE' , payload : 'path' } ,
] ,
[ ] ,
) ;
} ) ;
} ) ;
2019-09-04 21:01:54 +05:30
describe ( 'setSuggestPopoverDismissed' , ( ) => {
2022-06-21 17:19:12 +05:30
it ( 'commits SET_SHOW_SUGGEST_POPOVER' , async ( ) => {
2020-07-28 23:09:34 +05:30
const state = { dismissEndpoint : ` ${ TEST _HOST } /-/user_callouts ` } ;
2023-04-23 21:23:45 +05:30
mock . onPost ( state . dismissEndpoint ) . reply ( HTTP _STATUS _OK , { } ) ;
2019-09-04 21:01:54 +05:30
2020-04-22 19:07:51 +05:30
jest . spyOn ( axios , 'post' ) ;
2019-09-04 21:01:54 +05:30
2022-06-21 17:19:12 +05:30
await testAction (
diffActions . setSuggestPopoverDismissed ,
2019-09-04 21:01:54 +05:30
null ,
state ,
[ { type : types . SET _SHOW _SUGGEST _POPOVER } ] ,
[ ] ,
) ;
2022-06-21 17:19:12 +05:30
expect ( axios . post ) . toHaveBeenCalledWith ( state . dismissEndpoint , {
feature _name : 'suggest_popover_dismissed' ,
} ) ;
2019-09-04 21:01:54 +05:30
} ) ;
} ) ;
2020-05-24 23:13:21 +05:30
describe ( 'changeCurrentCommit' , ( ) => {
it ( 'commits the new commit information and re-requests the diff metadata for the commit' , ( ) => {
return testAction (
2022-06-21 17:19:12 +05:30
diffActions . changeCurrentCommit ,
2020-05-24 23:13:21 +05:30
{ commitId : 'NEW' } ,
{
commit : {
id : 'OLD' ,
} ,
endpoint : 'URL/OLD' ,
endpointBatch : 'URL/OLD' ,
endpointMetadata : 'URL/OLD' ,
} ,
[
{ type : types . SET _DIFF _FILES , payload : [ ] } ,
{
type : types . SET _BASE _CONFIG ,
payload : {
commit : {
id : 'OLD' , // Not a typo: the action fired next will overwrite all of the `commit` in state
} ,
endpoint : 'URL/NEW' ,
endpointBatch : 'URL/NEW' ,
endpointMetadata : 'URL/NEW' ,
} ,
} ,
] ,
[ { type : 'fetchDiffFilesMeta' } ] ,
) ;
} ) ;
it . each `
commitId | commit | msg
$ { undefined } | $ { { id : 'OLD' } } | $ { '`commitId` is a required argument' }
$ { 'NEW' } | $ { null } | $ { '`state` must already contain a valid `commit`' }
$ { undefined } | $ { null } | $ { '`commitId` is a required argument' }
` (
'returns a rejected promise with the error message $msg given `{ "commitId": $commitId, "state.commit": $commit }`' ,
( { commitId , commit , msg } ) => {
const err = new Error ( msg ) ;
const actionReturn = testAction (
2022-06-21 17:19:12 +05:30
diffActions . changeCurrentCommit ,
2020-05-24 23:13:21 +05:30
{ commitId } ,
{
endpoint : 'URL/OLD' ,
endpointBatch : 'URL/OLD' ,
endpointMetadata : 'URL/OLD' ,
commit ,
} ,
[ ] ,
[ ] ,
) ;
return expect ( actionReturn ) . rejects . toStrictEqual ( err ) ;
} ,
) ;
} ) ;
describe ( 'moveToNeighboringCommit' , ( ) => {
it . each `
direction | expected | currentCommit
$ { 'next' } | $ { 'NEXTSHA' } | $ { { next _commit _id : 'NEXTSHA' } }
$ { 'previous' } | $ { 'PREVIOUSSHA' } | $ { { prev _commit _id : 'PREVIOUSSHA' } }
` (
'for the direction "$direction", dispatches the action to move to the SHA "$expected"' ,
( { direction , expected , currentCommit } ) => {
return testAction (
2022-06-21 17:19:12 +05:30
diffActions . moveToNeighboringCommit ,
2020-05-24 23:13:21 +05:30
{ direction } ,
{ commit : currentCommit } ,
[ ] ,
[ { type : 'changeCurrentCommit' , payload : { commitId : expected } } ] ,
) ;
} ,
) ;
it . each `
direction | diffsAreLoading | currentCommit
$ { 'next' } | $ { false } | $ { { prev _commit _id : 'PREVIOUSSHA' } }
$ { 'next' } | $ { true } | $ { { prev _commit _id : 'PREVIOUSSHA' } }
$ { 'next' } | $ { false } | $ { undefined }
$ { 'previous' } | $ { false } | $ { { next _commit _id : 'NEXTSHA' } }
$ { 'previous' } | $ { true } | $ { { next _commit _id : 'NEXTSHA' } }
$ { 'previous' } | $ { false } | $ { undefined }
` (
'given `{ "isloading": $diffsAreLoading, "commit": $currentCommit }` in state, no actions are dispatched' ,
( { direction , diffsAreLoading , currentCommit } ) => {
return testAction (
2022-06-21 17:19:12 +05:30
diffActions . moveToNeighboringCommit ,
2020-05-24 23:13:21 +05:30
{ direction } ,
{ commit : currentCommit , isLoading : diffsAreLoading } ,
[ ] ,
[ ] ,
) ;
} ,
) ;
} ) ;
2020-07-28 23:09:34 +05:30
2023-06-20 00:43:36 +05:30
describe ( 'rereadNoteHash' , ( ) => {
beforeEach ( ( ) => {
window . location . hash = 'note_123' ;
} ) ;
it ( 'dispatches setCurrentDiffFileIdFromNote if the hash is a note URL' , ( ) => {
window . location . hash = 'note_123' ;
return testAction (
diffActions . rereadNoteHash ,
{ } ,
{ } ,
[ ] ,
[ { type : 'setCurrentDiffFileIdFromNote' , payload : '123' } ] ,
) ;
} ) ;
it ( 'dispatches fetchFileByFile if the app is in fileByFile mode' , ( ) => {
window . location . hash = 'note_123' ;
return testAction (
diffActions . rereadNoteHash ,
{ } ,
{ viewDiffsFileByFile : true } ,
[ ] ,
[ { type : 'setCurrentDiffFileIdFromNote' , payload : '123' } , { type : 'fetchFileByFile' } ] ,
) ;
} ) ;
it ( 'does not try to fetch the diff file if the app is not in fileByFile mode' , ( ) => {
window . location . hash = 'note_123' ;
return testAction (
diffActions . rereadNoteHash ,
{ } ,
{ viewDiffsFileByFile : false } ,
[ ] ,
[ { type : 'setCurrentDiffFileIdFromNote' , payload : '123' } ] ,
) ;
} ) ;
it ( 'does nothing if the hash is not a note URL' , ( ) => {
window . location . hash = 'abcdef1234567890' ;
return testAction ( diffActions . rereadNoteHash , { } , { } , [ ] , [ ] ) ;
} ) ;
} ) ;
2020-07-28 23:09:34 +05:30
describe ( 'setCurrentDiffFileIdFromNote' , ( ) => {
2021-12-11 22:18:48 +05:30
it ( 'commits SET_CURRENT_DIFF_FILE' , ( ) => {
2020-07-28 23:09:34 +05:30
const commit = jest . fn ( ) ;
2023-05-27 22:25:52 +05:30
const getters = { flatBlobsList : [ { fileHash : '123' } ] } ;
2020-07-28 23:09:34 +05:30
const rootGetters = {
getDiscussion : ( ) => ( { diff _file : { file _hash : '123' } } ) ,
2021-03-08 18:12:59 +05:30
notesById : { 1 : { discussion _id : '2' } } ,
2020-07-28 23:09:34 +05:30
} ;
2023-05-27 22:25:52 +05:30
diffActions . setCurrentDiffFileIdFromNote ( { commit , getters , rootGetters } , '1' ) ;
2020-07-28 23:09:34 +05:30
2021-12-11 22:18:48 +05:30
expect ( commit ) . toHaveBeenCalledWith ( types . SET _CURRENT _DIFF _FILE , '123' ) ;
2020-07-28 23:09:34 +05:30
} ) ;
2021-12-11 22:18:48 +05:30
it ( 'does not commit SET_CURRENT_DIFF_FILE when discussion has no diff_file' , ( ) => {
2020-07-28 23:09:34 +05:30
const commit = jest . fn ( ) ;
const rootGetters = {
getDiscussion : ( ) => ( { id : '1' } ) ,
2021-03-08 18:12:59 +05:30
notesById : { 1 : { discussion _id : '2' } } ,
2020-07-28 23:09:34 +05:30
} ;
2023-05-27 22:25:52 +05:30
diffActions . setCurrentDiffFileIdFromNote ( { commit , rootGetters } , '1' ) ;
2020-10-24 23:57:45 +05:30
expect ( commit ) . not . toHaveBeenCalled ( ) ;
} ) ;
2021-12-11 22:18:48 +05:30
it ( 'does not commit SET_CURRENT_DIFF_FILE when diff file does not exist' , ( ) => {
2020-10-24 23:57:45 +05:30
const commit = jest . fn ( ) ;
2023-05-27 22:25:52 +05:30
const getters = { flatBlobsList : [ { fileHash : '123' } ] } ;
2020-10-24 23:57:45 +05:30
const rootGetters = {
getDiscussion : ( ) => ( { diff _file : { file _hash : '124' } } ) ,
2021-03-08 18:12:59 +05:30
notesById : { 1 : { discussion _id : '2' } } ,
2020-10-24 23:57:45 +05:30
} ;
2023-05-27 22:25:52 +05:30
diffActions . setCurrentDiffFileIdFromNote ( { commit , getters , rootGetters } , '1' ) ;
2020-07-28 23:09:34 +05:30
expect ( commit ) . not . toHaveBeenCalled ( ) ;
} ) ;
} ) ;
describe ( 'navigateToDiffFileIndex' , ( ) => {
2022-06-21 17:19:12 +05:30
it ( 'commits SET_CURRENT_DIFF_FILE' , ( ) => {
return testAction (
diffActions . navigateToDiffFileIndex ,
2023-06-20 00:43:36 +05:30
{ index : 0 , singleFile : false } ,
2023-05-27 22:25:52 +05:30
{ flatBlobsList : [ { fileHash : '123' } ] } ,
2021-12-11 22:18:48 +05:30
[ { type : types . SET _CURRENT _DIFF _FILE , payload : '123' } ] ,
2020-07-28 23:09:34 +05:30
[ ] ,
) ;
} ) ;
2023-06-20 00:43:36 +05:30
it ( 'dispatches the fetchFileByFile action when the state value viewDiffsFileByFile is true and the single-file file-by-file feature flag is enabled' , ( ) => {
return testAction (
diffActions . navigateToDiffFileIndex ,
{ index : 0 , singleFile : true } ,
{ viewDiffsFileByFile : true , flatBlobsList : [ { fileHash : '123' } ] } ,
[ { type : types . SET _CURRENT _DIFF _FILE , payload : '123' } ] ,
[ { type : 'fetchFileByFile' } ] ,
) ;
} ) ;
2020-07-28 23:09:34 +05:30
} ) ;
2021-02-22 17:27:13 +05:30
describe ( 'setFileByFile' , ( ) => {
2021-04-29 21:17:54 +05:30
const updateUserEndpoint = 'user/prefs' ;
let putSpy ;
beforeEach ( ( ) => {
putSpy = jest . spyOn ( axios , 'put' ) ;
2023-04-23 21:23:45 +05:30
mock . onPut ( updateUserEndpoint ) . reply ( HTTP _STATUS _OK , { } ) ;
2021-04-29 21:17:54 +05:30
} ) ;
2021-02-22 17:27:13 +05:30
it . each `
value
$ { true }
$ { false }
2021-04-29 21:17:54 +05:30
` (
'commits SET_FILE_BY_FILE and persists the File-by-File user preference with the new value $value' ,
async ( { value } ) => {
await testAction (
2022-06-21 17:19:12 +05:30
diffActions . setFileByFile ,
2021-04-29 21:17:54 +05:30
{ fileByFile : value } ,
{
viewDiffsFileByFile : null ,
endpointUpdateUser : updateUserEndpoint ,
} ,
[ { type : types . SET _FILE _BY _FILE , payload : value } ] ,
[ ] ,
) ;
expect ( putSpy ) . toHaveBeenCalledWith ( updateUserEndpoint , { view _diffs _file _by _file : value } ) ;
} ,
) ;
2021-02-22 17:27:13 +05:30
} ) ;
2021-03-08 18:12:59 +05:30
describe ( 'reviewFile' , ( ) => {
const file = {
id : '123' ,
2021-12-11 22:18:48 +05:30
file _hash : 'xyz' ,
2021-03-08 18:12:59 +05:30
file _identifier _hash : 'abc' ,
load _collapsed _diff _url : 'gitlab-org/gitlab-test/-/merge_requests/1/diffs' ,
} ;
it . each `
2021-12-11 22:18:48 +05:30
reviews | diffFile | reviewed
$ { { abc : [ '123' , 'hash:xyz' ] } } | $ { file } | $ { true }
$ { { } } | $ { file } | $ { false }
2021-03-08 18:12:59 +05:30
` (
'sets reviews ($reviews) to localStorage and state for file $file if it is marked reviewed=$reviewed' ,
( { reviews , diffFile , reviewed } ) => {
const commitSpy = jest . fn ( ) ;
const getterSpy = jest . fn ( ) . mockReturnValue ( [ ] ) ;
2022-06-21 17:19:12 +05:30
diffActions . reviewFile (
2021-03-08 18:12:59 +05:30
{
commit : commitSpy ,
getters : {
fileReviews : getterSpy ,
} ,
state : {
mrReviews : { abc : [ '123' ] } ,
} ,
} ,
{
file : diffFile ,
reviewed ,
} ,
) ;
expect ( localStorage . setItem ) . toHaveBeenCalledTimes ( 1 ) ;
expect ( localStorage . setItem ) . toHaveBeenCalledWith (
'gitlab-org/gitlab-test/-/merge_requests/1-file-reviews' ,
JSON . stringify ( reviews ) ,
) ;
expect ( commitSpy ) . toHaveBeenCalledWith ( types . SET _MR _FILE _REVIEWS , reviews ) ;
} ,
) ;
} ) ;
2018-11-08 19:23:39 +05:30
} ) ;