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
a94ee468
Commit
a94ee468
authored
Mar 21, 2017
by
Filipa Lacerda
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Adds tests and changes after review
parent
ca5982af
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
136 additions
and
7 deletions
+136
-7
app/assets/javascripts/lib/utils/poll.js
app/assets/javascripts/lib/utils/poll.js
+11
-7
spec/javascripts/lib/utils/poll_spec.js
spec/javascripts/lib/utils/poll_spec.js
+125
-0
No files found.
app/assets/javascripts/lib/utils/poll.js
View file @
a94ee468
...
@@ -8,6 +8,7 @@ import httpStatusCodes from './http_status';
...
@@ -8,6 +8,7 @@ import httpStatusCodes from './http_status';
* new poll({
* new poll({
* resource: resource,
* resource: resource,
* method: 'name',
* method: 'name',
* data: {page: 1, scope: 'all'},
* successCallback: () => {},
* successCallback: () => {},
* errorCallback: () => {},
* errorCallback: () => {},
* }).makeRequest();
* }).makeRequest();
...
@@ -16,6 +17,7 @@ import httpStatusCodes from './http_status';
...
@@ -16,6 +17,7 @@ import httpStatusCodes from './http_status';
* new poll({
* new poll({
* resource: this.service,
* resource: this.service,
* method: 'get',
* method: 'get',
* data: {page: 1, scope: 'all'},
* successCallback: () => {},
* successCallback: () => {},
* errorCallback: () => {},
* errorCallback: () => {},
* }).makeRequest();
* }).makeRequest();
...
@@ -30,7 +32,9 @@ import httpStatusCodes from './http_status';
...
@@ -30,7 +32,9 @@ import httpStatusCodes from './http_status';
*/
*/
export
default
class
poll
{
export
default
class
poll
{
constructor
(
options
=
{})
{
constructor
(
options
=
{})
{
this
.
options
=
options
;
this
.
options
=
Object
.
assign
({},
{
data
:
{},
},
options
);
this
.
intervalHeader
=
'
POLL-INTERVAL
'
;
this
.
intervalHeader
=
'
POLL-INTERVAL
'
;
}
}
...
@@ -42,9 +46,7 @@ export default class poll {
...
@@ -42,9 +46,7 @@ export default class poll {
if
(
pollInterval
>
0
&&
response
.
status
===
httpStatusCodes
.
OK
)
{
if
(
pollInterval
>
0
&&
response
.
status
===
httpStatusCodes
.
OK
)
{
this
.
options
.
successCallback
(
response
);
this
.
options
.
successCallback
(
response
);
setTimeout
(()
=>
{
setTimeout
(()
=>
{
this
.
makeRequest
()
this
.
makeRequest
();
.
then
(
this
.
checkConditions
)
.
catch
(
error
=>
this
.
options
.
errorCallback
(
error
));
},
pollInterval
);
},
pollInterval
);
}
else
{
}
else
{
this
.
options
.
successCallback
(
response
);
this
.
options
.
successCallback
(
response
);
...
@@ -52,8 +54,10 @@ export default class poll {
...
@@ -52,8 +54,10 @@ export default class poll {
}
}
makeRequest
()
{
makeRequest
()
{
return
this
.
options
.
resource
[
this
.
options
.
method
]()
const
{
resource
,
method
,
data
,
errorCallback
}
=
this
.
options
;
.
then
(
this
.
checkConditions
.
bind
(
this
))
.
catch
(
error
=>
this
.
options
.
errorCallback
(
error
));
return
resource
[
method
](
data
)
.
then
(
response
=>
this
.
checkConditions
(
response
))
.
catch
(
error
=>
errorCallback
(
error
));
}
}
}
}
spec/javascripts/lib/utils/poll_spec.js
0 → 100644
View file @
a94ee468
import
Vue
from
'
vue
'
;
import
VueResource
from
'
vue-resource
'
;
import
Poll
from
'
~/lib/utils/poll
'
;
Vue
.
use
(
VueResource
);
class
ServiceMock
{
constructor
(
endpoint
)
{
this
.
service
=
Vue
.
resource
(
endpoint
);
}
fetch
()
{
return
this
.
service
.
get
();
}
}
describe
(
'
Poll
'
,
()
=>
{
let
callbacks
;
beforeEach
(()
=>
{
callbacks
=
{
success
:
()
=>
{},
error
:
()
=>
{},
};
spyOn
(
callbacks
,
'
success
'
);
spyOn
(
callbacks
,
'
error
'
);
});
it
(
'
calls the success callback when no header for interval is provided
'
,
(
done
)
=>
{
const
successInterceptor
=
(
request
,
next
)
=>
{
next
(
request
.
respondWith
(
JSON
.
stringify
([]),
{
status
:
200
}));
};
Vue
.
http
.
interceptors
.
push
(
successInterceptor
);
new
Poll
({
resource
:
new
ServiceMock
(
'
endpoint
'
),
method
:
'
fetch
'
,
successCallback
:
callbacks
.
success
,
errorCallback
:
callbacks
.
error
,
}).
makeRequest
();
setTimeout
(()
=>
{
expect
(
callbacks
.
success
).
toHaveBeenCalled
();
expect
(
callbacks
.
error
).
not
.
toHaveBeenCalled
();
done
();
},
0
);
Vue
.
http
.
interceptors
=
_
.
without
(
Vue
.
http
.
interceptors
,
successInterceptor
);
});
it
(
'
calls the error callback whe the http request returns an error
'
,
(
done
)
=>
{
const
errorInterceptor
=
(
request
,
next
)
=>
{
next
(
request
.
respondWith
(
JSON
.
stringify
([]),
{
status
:
500
}));
};
Vue
.
http
.
interceptors
.
push
(
errorInterceptor
);
new
Poll
({
resource
:
new
ServiceMock
(
'
endpoint
'
),
method
:
'
fetch
'
,
successCallback
:
callbacks
.
success
,
errorCallback
:
callbacks
.
error
,
}).
makeRequest
();
setTimeout
(()
=>
{
expect
(
callbacks
.
success
).
not
.
toHaveBeenCalled
();
expect
(
callbacks
.
error
).
toHaveBeenCalled
();
done
();
},
0
);
Vue
.
http
.
interceptors
=
_
.
without
(
Vue
.
http
.
interceptors
,
errorInterceptor
);
});
it
(
'
should call the success callback when the interval header is -1
'
,
(
done
)
=>
{
const
intervalInterceptor
=
(
request
,
next
)
=>
{
next
(
request
.
respondWith
(
JSON
.
stringify
([]),
{
status
:
200
,
headers
:
{
'
poll-interval
'
:
-
1
}
}));
};
Vue
.
http
.
interceptors
.
push
(
intervalInterceptor
);
new
Poll
({
resource
:
new
ServiceMock
(
'
endpoint
'
),
method
:
'
fetch
'
,
successCallback
:
callbacks
.
success
,
errorCallback
:
callbacks
.
error
,
}).
makeRequest
();
setTimeout
(()
=>
{
expect
(
callbacks
.
success
).
toHaveBeenCalled
();
expect
(
callbacks
.
error
).
not
.
toHaveBeenCalled
();
done
();
},
0
);
Vue
.
http
.
interceptors
=
_
.
without
(
Vue
.
http
.
interceptors
,
intervalInterceptor
);
});
it
(
'
starts polling when http status is 200 and interval header is provided
'
,
(
done
)
=>
{
const
pollInterceptor
=
(
request
,
next
)
=>
{
next
(
request
.
respondWith
(
JSON
.
stringify
([]),
{
status
:
200
,
headers
:
{
'
poll-interval
'
:
2
}
}));
};
Vue
.
http
.
interceptors
.
push
(
pollInterceptor
);
const
service
=
new
ServiceMock
(
'
endpoint
'
);
spyOn
(
service
,
'
fetch
'
).
and
.
callThrough
();
new
Poll
({
resource
:
service
,
method
:
'
fetch
'
,
successCallback
:
callbacks
.
success
,
errorCallback
:
callbacks
.
error
,
}).
makeRequest
();
setTimeout
(()
=>
{
expect
(
service
.
fetch
.
calls
.
count
()).
toEqual
(
2
);
expect
(
callbacks
.
success
).
toHaveBeenCalled
();
expect
(
callbacks
.
error
).
not
.
toHaveBeenCalled
();
done
();
},
5
);
Vue
.
http
.
interceptors
=
_
.
without
(
Vue
.
http
.
interceptors
,
pollInterceptor
);
});
});
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