BigW Consortium Gitlab
Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
G
gitlab-ce
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
Forest Godfrey
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
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
136 additions
and
7 deletions
+136
-7
poll.js
app/assets/javascripts/lib/utils/poll.js
+11
-7
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';
* new poll({
* resource: resource,
* method: 'name',
* data: {page: 1, scope: 'all'},
* successCallback: () => {},
* errorCallback: () => {},
* }).makeRequest();
...
...
@@ -16,6 +17,7 @@ import httpStatusCodes from './http_status';
* new poll({
* resource: this.service,
* method: 'get',
* data: {page: 1, scope: 'all'},
* successCallback: () => {},
* errorCallback: () => {},
* }).makeRequest();
...
...
@@ -30,7 +32,9 @@ import httpStatusCodes from './http_status';
*/
export
default
class
poll
{
constructor
(
options
=
{})
{
this
.
options
=
options
;
this
.
options
=
Object
.
assign
({},
{
data
:
{},
},
options
);
this
.
intervalHeader
=
'POLL-INTERVAL'
;
}
...
...
@@ -42,9 +46,7 @@ export default class poll {
if
(
pollInterval
>
0
&&
response
.
status
===
httpStatusCodes
.
OK
)
{
this
.
options
.
successCallback
(
response
);
setTimeout
(()
=>
{
this
.
makeRequest
()
.
then
(
this
.
checkConditions
)
.
catch
(
error
=>
this
.
options
.
errorCallback
(
error
));
this
.
makeRequest
();
},
pollInterval
);
}
else
{
this
.
options
.
successCallback
(
response
);
...
...
@@ -52,8 +54,10 @@ export default class poll {
}
makeRequest
()
{
return
this
.
options
.
resource
[
this
.
options
.
method
]()
.
then
(
this
.
checkConditions
.
bind
(
this
))
.
catch
(
error
=>
this
.
options
.
errorCallback
(
error
));
const
{
resource
,
method
,
data
,
errorCallback
}
=
this
.
options
;
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