2018-03-17 18:26:18 +05:30
import Vue from 'vue' ;
2020-06-23 00:09:42 +05:30
import mountComponent from 'helpers/vue_mount_component_helper' ;
2018-03-17 18:26:18 +05:30
import eventHub from '~/vue_merge_request_widget/event_hub' ;
import component from '~/vue_merge_request_widget/components/states/mr_widget_rebase.vue' ;
describe ( 'Merge request widget rebase component' , ( ) => {
let Component ;
let vm ;
2021-01-03 14:25:43 +05:30
const findRebaseMessageEl = ( ) => vm . $el . querySelector ( '[data-testid="rebase-message"]' ) ;
const findRebaseMessageElText = ( ) => findRebaseMessageEl ( ) . textContent . trim ( ) ;
2018-03-17 18:26:18 +05:30
beforeEach ( ( ) => {
Component = Vue . extend ( component ) ;
} ) ;
afterEach ( ( ) => {
vm . $destroy ( ) ;
} ) ;
describe ( 'While rebasing' , ( ) => {
it ( 'should show progress message' , ( ) => {
vm = mountComponent ( Component , {
mr : { rebaseInProgress : true } ,
service : { } ,
} ) ;
2021-01-03 14:25:43 +05:30
expect ( findRebaseMessageElText ( ) ) . toContain ( 'Rebase in progress' ) ;
2018-03-17 18:26:18 +05:30
} ) ;
} ) ;
describe ( 'With permissions' , ( ) => {
beforeEach ( ( ) => {
vm = mountComponent ( Component , {
mr : {
rebaseInProgress : false ,
canPushToSourceBranch : true ,
} ,
service : { } ,
} ) ;
} ) ;
it ( 'it should render rebase button and warning message' , ( ) => {
2021-01-03 14:25:43 +05:30
const text = findRebaseMessageElText ( ) ;
2018-12-13 13:39:08 +05:30
2018-03-17 18:26:18 +05:30
expect ( text ) . toContain ( 'Fast-forward merge is not possible.' ) ;
2019-02-15 15:39:39 +05:30
expect ( text . replace ( /\s\s+/g , ' ' ) ) . toContain (
2020-06-23 00:09:42 +05:30
'Rebase the source branch onto the target branch.' ,
2019-02-15 15:39:39 +05:30
) ;
2018-03-17 18:26:18 +05:30
} ) ;
2018-12-13 13:39:08 +05:30
it ( 'it should render error message when it fails' , done => {
2018-03-17 18:26:18 +05:30
vm . rebasingError = 'Something went wrong!' ;
Vue . nextTick ( ( ) => {
2021-01-03 14:25:43 +05:30
expect ( findRebaseMessageElText ( ) ) . toContain ( 'Something went wrong!' ) ;
2018-03-17 18:26:18 +05:30
done ( ) ;
} ) ;
} ) ;
} ) ;
describe ( 'Without permissions' , ( ) => {
it ( 'should render a message explaining user does not have permissions' , ( ) => {
vm = mountComponent ( Component , {
mr : {
rebaseInProgress : false ,
canPushToSourceBranch : false ,
targetBranch : 'foo' ,
} ,
service : { } ,
} ) ;
2021-01-03 14:25:43 +05:30
const text = findRebaseMessageElText ( ) ;
2018-03-17 18:26:18 +05:30
expect ( text ) . toContain ( 'Fast-forward merge is not possible.' ) ;
expect ( text ) . toContain ( 'Rebase the source branch onto' ) ;
expect ( text ) . toContain ( 'foo' ) ;
2019-02-15 15:39:39 +05:30
expect ( text . replace ( /\s\s+/g , ' ' ) ) . toContain ( 'to allow this merge request to be merged.' ) ;
2018-03-17 18:26:18 +05:30
} ) ;
2019-12-04 20:38:33 +05:30
it ( 'should render the correct target branch name' , ( ) => {
const targetBranch = 'fake-branch-to-test-with' ;
vm = mountComponent ( Component , {
mr : {
rebaseInProgress : false ,
canPushToSourceBranch : false ,
targetBranch ,
} ,
service : { } ,
} ) ;
2021-01-03 14:25:43 +05:30
const elem = findRebaseMessageEl ( ) ;
2019-12-04 20:38:33 +05:30
expect ( elem . innerHTML ) . toContain (
` Fast-forward merge is not possible. Rebase the source branch onto <span class="label-branch"> ${ targetBranch } </span> to allow this merge request to be merged. ` ,
) ;
} ) ;
2018-03-17 18:26:18 +05:30
} ) ;
describe ( 'methods' , ( ) => {
2018-12-13 13:39:08 +05:30
it ( 'checkRebaseStatus' , done => {
2020-06-23 00:09:42 +05:30
jest . spyOn ( eventHub , '$emit' ) . mockImplementation ( ( ) => { } ) ;
2018-03-17 18:26:18 +05:30
vm = mountComponent ( Component , {
mr : { } ,
service : {
rebase ( ) {
return Promise . resolve ( ) ;
} ,
poll ( ) {
return Promise . resolve ( {
data : {
rebase _in _progress : false ,
merge _error : null ,
} ,
} ) ;
} ,
} ,
} ) ;
vm . rebase ( ) ;
// Wait for the rebase request
vm . $nextTick ( )
// Wait for the polling request
. then ( vm . $nextTick ( ) )
// Wait for the eventHub to be called
. then ( vm . $nextTick ( ) )
. then ( ( ) => {
2019-02-15 15:39:39 +05:30
expect ( eventHub . $emit ) . toHaveBeenCalledWith ( 'MRWidgetRebaseSuccess' ) ;
2018-03-17 18:26:18 +05:30
} )
. then ( done )
. catch ( done . fail ) ;
} ) ;
} ) ;
} ) ;