import Vue from ' vue ' ;
import $ from ' jquery ' ;
import $ from ' jquery ' ;
import _ from ' underscore ' ;
import Api from ' ~/api ' ;
import Api from ' ~/api ' ;
import { TEST_HOST } from ' spec/test_constants ' ;
import { TEST_HOST } from ' spec/test_constants ' ;
import { headersInterceptor } from ' spec/helpers/vue_resource_helper ' ;
import actionsModule , * as actions from ' ~/notes/stores/actions ' ;
import actionsModule , * as actions from ' ~/notes/stores/actions ' ;
import * as mutationTypes from ' ~/notes/stores/mutation_types ' ;
import * as mutationTypes from ' ~/notes/stores/mutation_types ' ;
import * as notesConstants from ' ~/notes/constants ' ;
import * as notesConstants from ' ~/notes/constants ' ;
... @@ -29,6 +26,7 @@ describe('Actions Notes Store', () => {
... @@ -29,6 +26,7 @@ describe('Actions Notes Store', () => {
let state ;
let state ;
let store ;
let store ;
let flashSpy ;
let flashSpy ;
let axiosMock ;
beforeEach (() => {
beforeEach (() => {
store = createStore ();
store = createStore ();
... @@ -36,10 +34,12 @@ describe('Actions Notes Store', () => {
... @@ -36,10 +34,12 @@ describe('Actions Notes Store', () => {
dispatch = jasmine . createSpy ( ' dispatch ' );
dispatch = jasmine . createSpy ( ' dispatch ' );
state = {};
state = {};
flashSpy = spyOnDependency ( actionsModule , ' Flash ' );
flashSpy = spyOnDependency ( actionsModule , ' Flash ' );
axiosMock = new AxiosMockAdapter ( axios );
});
});
afterEach (() => {
afterEach (() => {
resetStore ( store );
resetStore ( store );
axiosMock . restore ();
});
});
describe ( ' setNotesData ' , () => {
describe ( ' setNotesData ' , () => {
... @@ -160,20 +160,8 @@ describe('Actions Notes Store', () => {
... @@ -160,20 +160,8 @@ describe('Actions Notes Store', () => {
});
});
describe ( ' async methods ' , () => {
describe ( ' async methods ' , () => {
const interceptor = ( request , next ) => {
next (
request . respondWith ( JSON . stringify ({}), {
status : 200 ,
}),
);
};
beforeEach (() => {
beforeEach (() => {
Vue . http . interceptors . push ( interceptor );
axiosMock . onAny (). reply ( 200 , {});
});
afterEach (() => {
Vue . http . interceptors = _ . without ( Vue . http . interceptors , interceptor );
});
});
describe ( ' closeIssue ' , () => {
describe ( ' closeIssue ' , () => {
... @@ -259,7 +247,7 @@ describe('Actions Notes Store', () => {
... @@ -259,7 +247,7 @@ describe('Actions Notes Store', () => {
beforeEach ( done => {
beforeEach ( done => {
jasmine . clock (). install ();
jasmine . clock (). install ();
spyOn ( Vue . http , ' get ' ). and . callThrough ();
spyOn ( axios , ' get ' ). and . callThrough ();
store
store
. dispatch ( ' setNotesData ' , notesDataMock )
. dispatch ( ' setNotesData ' , notesDataMock )
... @@ -272,31 +260,15 @@ describe('Actions Notes Store', () => {
... @@ -272,31 +260,15 @@ describe('Actions Notes Store', () => {
});
});
it ( ' calls service with last fetched state ' , done => {
it ( ' calls service with last fetched state ' , done => {
const interceptor = ( request , next ) => {
axiosMock
next (
. onAny ()
request . respondWith (
. reply ( 200 , { notes : [], last_fetched_at : ' 123456 ' }, { ' poll-interval ' : ' 1000 ' });
JSON . stringify ({
notes : [],
last_fetched_at : ' 123456 ' ,
}),
{
status : 200 ,
headers : {
' poll-interval ' : ' 1000 ' ,
},
},
),
);
};
Vue . http . interceptors . push ( interceptor );
Vue . http . interceptors . push ( headersInterceptor );
store
store
. dispatch ( ' poll ' )
. dispatch ( ' poll ' )
. then (() => new Promise ( resolve => requestAnimationFrame ( resolve )))
. then (() => new Promise ( resolve => requestAnimationFrame ( resolve )))
. then (() => {
. then (() => {
expect ( Vue . http . get ). toHaveBeenCalled ();
expect ( axios . get ). toHaveBeenCalled ();
expect ( store . state . lastFetchedAt ). toBe ( ' 123456 ' );
expect ( store . state . lastFetchedAt ). toBe ( ' 123456 ' );
jasmine . clock (). tick ( 1500 );
jasmine . clock (). tick ( 1500 );
... @@ -308,16 +280,12 @@ describe('Actions Notes Store', () => {
... @@ -308,16 +280,12 @@ describe('Actions Notes Store', () => {
}),
}),
)
)
. then (() => {
. then (() => {
expect ( Vue . http . get . calls . count ()). toBe ( 2 );
expect ( axios . get . calls . count ()). toBe ( 2 );
expect ( Vue . http . get . calls . mostRecent (). args [ 1 ]. headers ). toEqual ({
expect ( axios . get . calls . mostRecent (). args [ 1 ]. headers ). toEqual ({
' X-Last-Fetched-At ' : ' 123456 ' ,
' X-Last-Fetched-At ' : ' 123456 ' ,
});
});
})
})
. then (() => store . dispatch ( ' stopPolling ' ))
. then (() => store . dispatch ( ' stopPolling ' ))
. then (() => {
Vue . http . interceptors = _ . without ( Vue . http . interceptors , interceptor );
Vue . http . interceptors = _ . without ( Vue . http . interceptors , headersInterceptor );
})
. then ( done )
. then ( done )
. catch ( done . fail );
. catch ( done . fail );
});
});
... @@ -338,10 +306,8 @@ describe('Actions Notes Store', () => {
... @@ -338,10 +306,8 @@ describe('Actions Notes Store', () => {
describe ( ' removeNote ' , () => {
describe ( ' removeNote ' , () => {
const endpoint = ` ${ TEST_HOST } /note` ;
const endpoint = ` ${ TEST_HOST } /note` ;
let axiosMock ;
beforeEach (() => {
beforeEach (() => {
axiosMock = new AxiosMockAdapter ( axios );
axiosMock . onDelete ( endpoint ). replyOnce ( 200 , {});
axiosMock . onDelete ( endpoint ). replyOnce ( 200 , {});
$ ( ' body ' ). attr ( ' data-page ' , '' );
$ ( ' body ' ). attr ( ' data-page ' , '' );
... @@ -411,10 +377,8 @@ describe('Actions Notes Store', () => {
... @@ -411,10 +377,8 @@ describe('Actions Notes Store', () => {
describe ( ' deleteNote ' , () => {
describe ( ' deleteNote ' , () => {
const endpoint = ` ${ TEST_HOST } /note` ;
const endpoint = ` ${ TEST_HOST } /note` ;
let axiosMock ;
beforeEach (() => {
beforeEach (() => {
axiosMock = new AxiosMockAdapter ( axios );
axiosMock . onDelete ( endpoint ). replyOnce ( 200 , {});
axiosMock . onDelete ( endpoint ). replyOnce ( 200 , {});
$ ( ' body ' ). attr ( ' data-page ' , '' );
$ ( ' body ' ). attr ( ' data-page ' , '' );
... @@ -454,20 +418,9 @@ describe('Actions Notes Store', () => {
... @@ -454,20 +418,9 @@ describe('Actions Notes Store', () => {
id : 1 ,
id : 1 ,
valid : true ,
valid : true ,
};
};
const interceptor = ( request , next ) => {
next (
request . respondWith ( JSON . stringify ( res ), {
status : 200 ,
}),
);
};
beforeEach (() => {
beforeEach (() => {
Vue . http . interceptors . push ( interceptor );
axiosMock . onAny (). reply ( 200 , res );
});
afterEach (() => {
Vue . http . interceptors = _ . without ( Vue . http . interceptors , interceptor );
});
});
it ( ' commits ADD_NEW_NOTE and dispatches updateMergeRequestWidget ' , done => {
it ( ' commits ADD_NEW_NOTE and dispatches updateMergeRequestWidget ' , done => {
... @@ -501,20 +454,9 @@ describe('Actions Notes Store', () => {
... @@ -501,20 +454,9 @@ describe('Actions Notes Store', () => {
const res = {
const res = {
errors : [ ' error ' ],
errors : [ ' error ' ],
};
};
const interceptor = ( request , next ) => {
next (
request . respondWith ( JSON . stringify ( res ), {
status : 200 ,
}),
);
};
beforeEach (() => {
beforeEach (() => {
Vue . http . interceptors . push ( interceptor );
axiosMock . onAny (). replyOnce ( 200 , res );
});
afterEach (() => {
Vue . http . interceptors = _ . without ( Vue . http . interceptors , interceptor );
});
});
it ( ' does not commit ADD_NEW_NOTE or dispatch updateMergeRequestWidget ' , done => {
it ( ' does not commit ADD_NEW_NOTE or dispatch updateMergeRequestWidget ' , done => {
... @@ -534,20 +476,9 @@ describe('Actions Notes Store', () => {
... @@ -534,20 +476,9 @@ describe('Actions Notes Store', () => {
const res = {
const res = {
resolved : true ,
resolved : true ,
};
};
const interceptor = ( request , next ) => {
next (
request . respondWith ( JSON . stringify ( res ), {
status : 200 ,
}),
);
};
beforeEach (() => {
beforeEach (() => {
Vue . http . interceptors . push ( interceptor );
axiosMock . onAny (). reply ( 200 , res );
});
afterEach (() => {
Vue . http . interceptors = _ . without ( Vue . http . interceptors , interceptor );
});
});
describe ( ' as note ' , () => {
describe ( ' as note ' , () => {
... @@ -720,32 +651,19 @@ describe('Actions Notes Store', () => {
... @@ -720,32 +651,19 @@ describe('Actions Notes Store', () => {
});
});
describe ( ' replyToDiscussion ' , () => {
describe ( ' replyToDiscussion ' , () => {
let res = { discussion : { notes : [] } };
const payload = { endpoint : TEST_HOST , data : {} };
const payload = { endpoint : TEST_HOST , data : {} };
const interceptor = ( request , next ) => {
next (
request . respondWith ( JSON . stringify ( res ), {
status : 200 ,
}),
);
};
beforeEach (() => {
Vue . http . interceptors . push ( interceptor );
});
afterEach (() => {
Vue . http . interceptors = _ . without ( Vue . http . interceptors , interceptor );
});
it ( ' updates discussion if response contains disussion ' , done => {
it ( ' updates discussion if response contains disussion ' , done => {
const discussion = { notes : [] };
axiosMock . onAny (). reply ( 200 , { discussion });
testAction (
testAction (
actions . replyToDiscussion ,
actions . replyToDiscussion ,
payload ,
payload ,
{
{
notesById : {},
notesById : {},
},
},
[{ type : mutationTypes . UPDATE_DISCUSSION , payload : res . discussion }],
[{ type : mutationTypes . UPDATE_DISCUSSION , payload : discussion }],
[
[
{ type : ' updateMergeRequestWidget ' },
{ type : ' updateMergeRequestWidget ' },
{ type : ' startTaskList ' },
{ type : ' startTaskList ' },
... @@ -756,7 +674,8 @@ describe('Actions Notes Store', () => {
... @@ -756,7 +674,8 @@ describe('Actions Notes Store', () => {
});
});
it ( ' adds a reply to a discussion ' , done => {
it ( ' adds a reply to a discussion ' , done => {
res = {};
const res = {};
axiosMock . onAny (). reply ( 200 , res );
testAction (
testAction (
actions . replyToDiscussion ,
actions . replyToDiscussion ,
...
...
... ...