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
'
;
import
Cache
from
'
./cache
'
;
class
AjaxCache
extends
Cache
{
class
AjaxCache
extends
Cache
{
...
@@ -18,22 +19,15 @@ class AjaxCache extends Cache {
...
@@ -18,22 +19,15 @@ class AjaxCache extends Cache {
let
pendingRequest
=
this
.
pendingRequests
[
endpoint
];
let
pendingRequest
=
this
.
pendingRequests
[
endpoint
];
if
(
!
pendingRequest
)
{
if
(
!
pendingRequest
)
{
pendingRequest
=
new
Promise
((
resolve
,
reject
)
=>
{
pendingRequest
=
axios
.
get
(
endpoint
)
// jQuery 2 is not Promises/A+ compatible (missing catch)
.
then
(({
data
})
=>
{
$
.
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
)
=>
{
this
.
internalStorage
[
endpoint
]
=
data
;
this
.
internalStorage
[
endpoint
]
=
data
;
delete
this
.
pendingRequests
[
endpoint
];
delete
this
.
pendingRequests
[
endpoint
];
})
})
.
catch
((
error
)
=>
{
.
catch
((
e
)
=>
{
const
error
=
new
Error
(
`
${
endpoint
}
:
${
e
.
message
}
`
);
error
.
textStatus
=
e
.
message
;
delete
this
.
pendingRequests
[
endpoint
];
delete
this
.
pendingRequests
[
endpoint
];
throw
error
;
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
'
;
import
AjaxCache
from
'
~/lib/utils/ajax_cache
'
;
describe
(
'
AjaxCache
'
,
()
=>
{
describe
(
'
AjaxCache
'
,
()
=>
{
...
@@ -86,67 +88,55 @@ describe('AjaxCache', () => {
...
@@ -86,67 +88,55 @@ describe('AjaxCache', () => {
});
});
});
});
describe
(
'
retrieve
'
,
()
=>
{
f
describe
(
'
retrieve
'
,
()
=>
{
let
ajaxSpy
;
let
ajaxSpy
;
let
mock
;
beforeEach
(()
=>
{
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
)
=>
{
it
(
'
stores and returns data from Ajax call if cache is empty
'
,
(
done
)
=>
{
ajaxSpy
=
(
url
)
=>
{
mock
.
onGet
(
dummyEndpoint
).
reply
(
200
,
dummyResponse
);
expect
(
url
).
toBe
(
dummyEndpoint
);
const
deferred
=
$
.
Deferred
();
deferred
.
resolve
(
dummyResponse
);
return
deferred
.
promise
();
};
AjaxCache
.
retrieve
(
dummyEndpoint
)
AjaxCache
.
retrieve
(
dummyEndpoint
)
.
then
((
data
)
=>
{
.
then
((
data
)
=>
{
expect
(
data
).
to
Be
(
dummyResponse
);
expect
(
data
).
to
Equal
(
dummyResponse
);
expect
(
AjaxCache
.
internalStorage
[
dummyEndpoint
]).
to
Be
(
dummyResponse
);
expect
(
AjaxCache
.
internalStorage
[
dummyEndpoint
]).
to
Equal
(
dummyResponse
);
})
})
.
then
(
done
)
.
then
(
done
)
.
catch
(
fail
);
.
catch
(
fail
);
});
});
it
(
'
makes no Ajax call if request is pending
'
,
()
=>
{
it
(
'
makes no Ajax call if request is pending
'
,
(
done
)
=>
{
const
responseDeferred
=
$
.
Deferred
();
mock
.
onGet
(
dummyEndpoint
).
reply
(
200
,
dummyResponse
);
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
}
`
);
AjaxCache
.
retrieve
(
dummyEndpoint
)
AjaxCache
.
retrieve
(
dummyEndpoint
)
.
then
(
unexpectedRespons
e
)
.
then
(
don
e
)
.
catch
(
fail
);
.
catch
(
fail
);
AjaxCache
.
retrieve
(
dummyEndpoint
)
AjaxCache
.
retrieve
(
dummyEndpoint
)
.
then
(
unexpectedRespons
e
)
.
then
(
don
e
)
.
catch
(
fail
);
.
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
)
=>
{
it
(
'
returns undefined if Ajax call fails and cache is empty
'
,
(
done
)
=>
{
const
dummyStatusText
=
'
exploded
'
;
const
errorMessage
=
'
Network Error
'
;
const
dummyErrorMessage
=
'
server exploded
'
;
mock
.
onGet
(
dummyEndpoint
).
networkError
();
ajaxSpy
=
(
url
)
=>
{
expect
(
url
).
toBe
(
dummyEndpoint
);
const
deferred
=
$
.
Deferred
();
deferred
.
reject
(
null
,
dummyStatusText
,
dummyErrorMessage
);
return
deferred
.
promise
();
};
AjaxCache
.
retrieve
(
dummyEndpoint
)
AjaxCache
.
retrieve
(
dummyEndpoint
)
.
then
(
data
=>
fail
(
`Received unexpected data:
${
JSON
.
stringify
(
data
)}
`
))
.
then
(
data
=>
fail
(
`Received unexpected data:
${
JSON
.
stringify
(
data
)}
`
))
.
catch
((
error
)
=>
{
.
catch
((
error
)
=>
{
expect
(
error
.
message
).
toBe
(
`
${
dummyEndpoint
}
:
${
dummyE
rrorMessage
}
`
);
expect
(
error
.
message
).
toBe
(
`
${
dummyEndpoint
}
:
${
e
rrorMessage
}
`
);
expect
(
error
.
textStatus
).
toBe
(
dummyStatusText
);
expect
(
error
.
textStatus
).
toBe
(
errorMessage
);
done
();
done
();
})
})
.
catch
(
fail
);
.
catch
(
fail
);
...
@@ -154,7 +144,9 @@ describe('AjaxCache', () => {
...
@@ -154,7 +144,9 @@ describe('AjaxCache', () => {
it
(
'
makes no Ajax call if matching data exists
'
,
(
done
)
=>
{
it
(
'
makes no Ajax call if matching data exists
'
,
(
done
)
=>
{
AjaxCache
.
internalStorage
[
dummyEndpoint
]
=
dummyResponse
;
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
)
AjaxCache
.
retrieve
(
dummyEndpoint
)
.
then
((
data
)
=>
{
.
then
((
data
)
=>
{
...
@@ -171,12 +163,7 @@ describe('AjaxCache', () => {
...
@@ -171,12 +163,7 @@ describe('AjaxCache', () => {
AjaxCache
.
internalStorage
[
dummyEndpoint
]
=
oldDummyResponse
;
AjaxCache
.
internalStorage
[
dummyEndpoint
]
=
oldDummyResponse
;
ajaxSpy
=
(
url
)
=>
{
mock
.
onGet
(
dummyEndpoint
).
reply
(
200
,
dummyResponse
);
expect
(
url
).
toBe
(
dummyEndpoint
);
const
deferred
=
$
.
Deferred
();
deferred
.
resolve
(
dummyResponse
);
return
deferred
.
promise
();
};
// Call without forceRetrieve param
// Call without forceRetrieve param
AjaxCache
.
retrieve
(
dummyEndpoint
)
AjaxCache
.
retrieve
(
dummyEndpoint
)
...
@@ -189,7 +176,7 @@ describe('AjaxCache', () => {
...
@@ -189,7 +176,7 @@ describe('AjaxCache', () => {
// Call with forceRetrieve param
// Call with forceRetrieve param
AjaxCache
.
retrieve
(
dummyEndpoint
,
true
)
AjaxCache
.
retrieve
(
dummyEndpoint
,
true
)
.
then
((
data
)
=>
{
.
then
((
data
)
=>
{
expect
(
data
).
to
Be
(
dummyResponse
);
expect
(
data
).
to
Equal
(
dummyResponse
);
})
})
.
then
(
done
)
.
then
(
done
)
.
catch
(
fail
);
.
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