summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWinnie Hellmann <winnie@gitlab.com>2018-04-25 21:02:50 +0200
committerWinnie Hellmann <winnie@gitlab.com>2018-04-26 09:26:19 +0200
commitabc623992424d9942e0c38bf6dcb5f6132f66c7d (patch)
tree4319c29a4ee6592d50a74e6e8350217257592a6d
parentddb9693be008bcf681e607710bffcdd7e747db05 (diff)
downloadgitlab-ce-winh-mock-smart-interval.tar.gz
Make sure SmartInterval is mocked in testswinh-mock-smart-interval
-rw-r--r--app/assets/javascripts/smart_interval.js5
-rw-r--r--spec/javascripts/smart_interval_spec.js4
-rw-r--r--spec/javascripts/vue_mr_widget/mr_widget_options_spec.js34
3 files changed, 26 insertions, 17 deletions
diff --git a/app/assets/javascripts/smart_interval.js b/app/assets/javascripts/smart_interval.js
index 8b94303ec9c..d71a82ae603 100644
--- a/app/assets/javascripts/smart_interval.js
+++ b/app/assets/javascripts/smart_interval.js
@@ -1,4 +1,5 @@
import $ from 'jquery';
+import { isTestEnvironment } from '~/environment';
/**
* Instances of SmartInterval extend the functionality of `setInterval`, make it configurable
@@ -20,6 +21,10 @@ export default class SmartInterval {
* be executed before the first interval.
*/
constructor(opts = {}) {
+ if (isTestEnvironment()) {
+ throw new Error('SmartInterval should be mocked in tests!');
+ }
+
this.cfg = {
callback: opts.callback,
startingInterval: opts.startingInterval,
diff --git a/spec/javascripts/smart_interval_spec.js b/spec/javascripts/smart_interval_spec.js
index 00a46d3e07f..2536fb29d6d 100644
--- a/spec/javascripts/smart_interval_spec.js
+++ b/spec/javascripts/smart_interval_spec.js
@@ -27,6 +27,10 @@ describe('SmartInterval', function() {
return new SmartInterval(defaultParams);
}
+ beforeEach(() => {
+ spyOnDependency(SmartInterval, 'isTestEnvironment').and.returnValue(false);
+ });
+
describe('Increment Interval', function() {
beforeEach(function() {
this.smartInterval = createDefaultSmartInterval();
diff --git a/spec/javascripts/vue_mr_widget/mr_widget_options_spec.js b/spec/javascripts/vue_mr_widget/mr_widget_options_spec.js
index c0506337e1c..2c4b21f7db0 100644
--- a/spec/javascripts/vue_mr_widget/mr_widget_options_spec.js
+++ b/spec/javascripts/vue_mr_widget/mr_widget_options_spec.js
@@ -16,8 +16,15 @@ const returnPromise = data =>
describe('mrWidgetOptions', () => {
let vm;
let MrWidgetOptions;
+ let smartIntervalMock;
+ let smartIntervalSpy;
beforeEach(() => {
+ smartIntervalMock = jasmine.createSpyObj('SmartIntervalMock', ['resume', 'stopTimer']);
+ smartIntervalSpy = spyOnDependency(mrWidgetOptions, 'SmartInterval').and.callFake(
+ () => smartIntervalMock,
+ );
+
// Prevent component mounting
delete mrWidgetOptions.el;
@@ -152,28 +159,23 @@ describe('mrWidgetOptions', () => {
describe('initPolling', () => {
it('should call SmartInterval', () => {
- spyOn(vm, 'checkStatus').and.returnValue(Promise.resolve());
- jasmine.clock().install();
vm.initPolling();
- expect(vm.checkStatus).not.toHaveBeenCalled();
-
- jasmine.clock().tick(10000);
-
- expect(vm.pollingInterval).toBeDefined();
- expect(vm.checkStatus).toHaveBeenCalled();
-
- jasmine.clock().uninstall();
+ expect(smartIntervalSpy).toHaveBeenCalled();
+ const smartIntervalOptions = smartIntervalSpy.calls.mostRecent().args[0];
+ expect(smartIntervalOptions.callback).toBe(vm.checkStatus);
+ expect(vm.pollingInterval).toBe(smartIntervalMock);
});
});
describe('initDeploymentsPolling', () => {
it('should call SmartInterval', () => {
- spyOn(vm, 'fetchDeployments').and.returnValue(Promise.resolve());
vm.initDeploymentsPolling();
- expect(vm.deploymentsInterval).toBeDefined();
- expect(vm.fetchDeployments).toHaveBeenCalled();
+ expect(smartIntervalSpy).toHaveBeenCalled();
+ const smartIntervalOptions = smartIntervalSpy.calls.mostRecent().args[0];
+ expect(smartIntervalOptions.callback).toBe(vm.fetchDeployments);
+ expect(vm.deploymentsInterval).toBe(smartIntervalMock);
});
});
@@ -341,18 +343,16 @@ describe('mrWidgetOptions', () => {
describe('resumePolling', () => {
it('should call stopTimer on pollingInterval', () => {
- spyOn(vm.pollingInterval, 'resume');
-
vm.resumePolling();
+
expect(vm.pollingInterval.resume).toHaveBeenCalled();
});
});
describe('stopPolling', () => {
it('should call stopTimer on pollingInterval', () => {
- spyOn(vm.pollingInterval, 'stopTimer');
-
vm.stopPolling();
+
expect(vm.pollingInterval.stopTimer).toHaveBeenCalled();
});
});