Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Boxiang Sun
gitlab-ce
Commits
ae401d03
Commit
ae401d03
authored
Jan 31, 2018
by
Phil Hughes
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Converted ajax_cache to axios
parent
3ae2d90b
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
41 additions
and
60 deletions
+41
-60
app/assets/javascripts/lib/utils/ajax_cache.js
app/assets/javascripts/lib/utils/ajax_cache.js
+13
-19
spec/javascripts/lib/utils/ajax_cache_spec.js
spec/javascripts/lib/utils/ajax_cache_spec.js
+28
-41
No files found.
app/assets/javascripts/lib/utils/ajax_cache.js
View file @
ae401d03
import
axios
from
'
./axios_utils
'
;
import
Cache
from
'
./cache
'
;
class
AjaxCache
extends
Cache
{
...
...
@@ -18,22 +19,15 @@ class AjaxCache extends Cache {
let
pendingRequest
=
this
.
pendingRequests
[
endpoint
];
if
(
!
pendingRequest
)
{
pendingRequest
=
new
Promise
((
resolve
,
reject
)
=>
{
// jQuery 2 is not Promises/A+ compatible (missing catch)
$
.
ajax
(
endpoint
)
// eslint-disable-line promise/catch-or-return
.
then
(
data
=>
resolve
(
data
),
(
jqXHR
,
textStatus
,
errorThrown
)
=>
{
const
error
=
new
Error
(
`
${
endpoint
}
:
${
errorThrown
}
`
);
error
.
textStatus
=
textStatus
;
reject
(
error
);
},
);
})
.
then
((
data
)
=>
{
pendingRequest
=
axios
.
get
(
endpoint
)
.
then
(({
data
})
=>
{
this
.
internalStorage
[
endpoint
]
=
data
;
delete
this
.
pendingRequests
[
endpoint
];
})
.
catch
((
error
)
=>
{
.
catch
((
e
)
=>
{
const
error
=
new
Error
(
`
${
endpoint
}
:
${
e
.
message
}
`
);
error
.
textStatus
=
e
.
message
;
delete
this
.
pendingRequests
[
endpoint
];
throw
error
;
});
...
...
spec/javascripts/lib/utils/ajax_cache_spec.js
View file @
ae401d03
import
MockAdapter
from
'
axios-mock-adapter
'
;
import
axios
from
'
~/lib/utils/axios_utils
'
;
import
AjaxCache
from
'
~/lib/utils/ajax_cache
'
;
describe
(
'
AjaxCache
'
,
()
=>
{
...
...
@@ -86,67 +88,55 @@ describe('AjaxCache', () => {
});
});
describe
(
'
retrieve
'
,
()
=>
{
f
describe
(
'
retrieve
'
,
()
=>
{
let
ajaxSpy
;
let
mock
;
beforeEach
(()
=>
{
spyOn
(
jQuery
,
'
ajax
'
).
and
.
callFake
(
url
=>
ajaxSpy
(
url
));
mock
=
new
MockAdapter
(
axios
);
spyOn
(
axios
,
'
get
'
).
and
.
callThrough
();
});
afterEach
(()
=>
{
mock
.
restore
();
});
it
(
'
stores and returns data from Ajax call if cache is empty
'
,
(
done
)
=>
{
ajaxSpy
=
(
url
)
=>
{
expect
(
url
).
toBe
(
dummyEndpoint
);
const
deferred
=
$
.
Deferred
();
deferred
.
resolve
(
dummyResponse
);
return
deferred
.
promise
();
};
mock
.
onGet
(
dummyEndpoint
).
reply
(
200
,
dummyResponse
);
AjaxCache
.
retrieve
(
dummyEndpoint
)
.
then
((
data
)
=>
{
expect
(
data
).
to
Be
(
dummyResponse
);
expect
(
AjaxCache
.
internalStorage
[
dummyEndpoint
]).
to
Be
(
dummyResponse
);
expect
(
data
).
to
Equal
(
dummyResponse
);
expect
(
AjaxCache
.
internalStorage
[
dummyEndpoint
]).
to
Equal
(
dummyResponse
);
})
.
then
(
done
)
.
catch
(
fail
);
});
it
(
'
makes no Ajax call if request is pending
'
,
()
=>
{
const
responseDeferred
=
$
.
Deferred
();
ajaxSpy
=
(
url
)
=>
{
expect
(
url
).
toBe
(
dummyEndpoint
);
// neither reject nor resolve to keep request pending
return
responseDeferred
.
promise
();
};
const
unexpectedResponse
=
data
=>
fail
(
`Did not expect response:
${
data
}
`
);
it
(
'
makes no Ajax call if request is pending
'
,
(
done
)
=>
{
mock
.
onGet
(
dummyEndpoint
).
reply
(
200
,
dummyResponse
);
AjaxCache
.
retrieve
(
dummyEndpoint
)
.
then
(
unexpectedRespons
e
)
.
then
(
don
e
)
.
catch
(
fail
);
AjaxCache
.
retrieve
(
dummyEndpoint
)
.
then
(
unexpectedRespons
e
)
.
then
(
don
e
)
.
catch
(
fail
);
expect
(
$
.
ajax
.
calls
.
count
()).
toBe
(
1
);
expect
(
axios
.
get
.
calls
.
count
()).
toBe
(
1
);
});
it
(
'
returns undefined if Ajax call fails and cache is empty
'
,
(
done
)
=>
{
const
dummyStatusText
=
'
exploded
'
;
const
dummyErrorMessage
=
'
server exploded
'
;
ajaxSpy
=
(
url
)
=>
{
expect
(
url
).
toBe
(
dummyEndpoint
);
const
deferred
=
$
.
Deferred
();
deferred
.
reject
(
null
,
dummyStatusText
,
dummyErrorMessage
);
return
deferred
.
promise
();
};
const
errorMessage
=
'
Network Error
'
;
mock
.
onGet
(
dummyEndpoint
).
networkError
();
AjaxCache
.
retrieve
(
dummyEndpoint
)
.
then
(
data
=>
fail
(
`Received unexpected data:
${
JSON
.
stringify
(
data
)}
`
))
.
catch
((
error
)
=>
{
expect
(
error
.
message
).
toBe
(
`
${
dummyEndpoint
}
:
${
dummyE
rrorMessage
}
`
);
expect
(
error
.
textStatus
).
toBe
(
dummyStatusText
);
expect
(
error
.
message
).
toBe
(
`
${
dummyEndpoint
}
:
${
e
rrorMessage
}
`
);
expect
(
error
.
textStatus
).
toBe
(
errorMessage
);
done
();
})
.
catch
(
fail
);
...
...
@@ -154,7 +144,9 @@ describe('AjaxCache', () => {
it
(
'
makes no Ajax call if matching data exists
'
,
(
done
)
=>
{
AjaxCache
.
internalStorage
[
dummyEndpoint
]
=
dummyResponse
;
ajaxSpy
=
()
=>
fail
(
new
Error
(
'
expected no Ajax call!
'
));
mock
.
onGet
(
dummyEndpoint
).
reply
(()
=>
{
fail
(
new
Error
(
'
expected no Ajax call!
'
));
});
AjaxCache
.
retrieve
(
dummyEndpoint
)
.
then
((
data
)
=>
{
...
...
@@ -171,12 +163,7 @@ describe('AjaxCache', () => {
AjaxCache
.
internalStorage
[
dummyEndpoint
]
=
oldDummyResponse
;
ajaxSpy
=
(
url
)
=>
{
expect
(
url
).
toBe
(
dummyEndpoint
);
const
deferred
=
$
.
Deferred
();
deferred
.
resolve
(
dummyResponse
);
return
deferred
.
promise
();
};
mock
.
onGet
(
dummyEndpoint
).
reply
(
200
,
dummyResponse
);
// Call without forceRetrieve param
AjaxCache
.
retrieve
(
dummyEndpoint
)
...
...
@@ -189,7 +176,7 @@ describe('AjaxCache', () => {
// Call with forceRetrieve param
AjaxCache
.
retrieve
(
dummyEndpoint
,
true
)
.
then
((
data
)
=>
{
expect
(
data
).
to
Be
(
dummyResponse
);
expect
(
data
).
to
Equal
(
dummyResponse
);
})
.
then
(
done
)
.
catch
(
fail
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment