updater_test.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. var App = require('app');
  19. require('utils/updater');
  20. describe('utils/updater', function() {
  21. describe('#App.updater', function() {
  22. beforeEach(function() {
  23. this.clock = sinon.useFakeTimers();
  24. });
  25. var tests = {
  26. t1: {
  27. obj: Em.Object.create({
  28. method: sinon.spy(),
  29. isWorking: true
  30. }),
  31. m: 'method called once with default interval in 15 000 ms'
  32. },
  33. t2: {
  34. obj: Em.Object.create({
  35. method: function() {}
  36. }),
  37. m: 'should return false if key name is invalid or absent'
  38. },
  39. t3: {
  40. obj: Em.Object.create({
  41. method2: sinon.spy(),
  42. isWorking: true
  43. }),
  44. m: 'method should be called immediately'
  45. },
  46. t4: {
  47. obj: Em.Object.create({
  48. method3: sinon.spy(),
  49. isWorking: true
  50. }),
  51. m: 'method call should be ignored if `isWorking` set to false'
  52. }
  53. };
  54. it(tests.t1.m, function() {
  55. App.updater.run(tests.t1.obj, 'method', 'isWorking');
  56. this.clock.tick(15000);
  57. expect(tests.t1.obj.method.called).to.be.ok;
  58. });
  59. it(tests.t2.m, function() {
  60. var methodCall = App.updater.run(tests.t2.obj, 'method', 'isWorking');
  61. expect(methodCall).to.be.false;
  62. });
  63. it(tests.t3.m, function() {
  64. App.updater.run(tests.t3.obj, 'method2', 'isWorking');
  65. App.updater.immediateRun('method2');
  66. expect(tests.t3.obj.method2.called).to.be.ok;
  67. });
  68. it(tests.t4.m, function() {
  69. App.updater.run(tests.t4.obj, 'method3', 'isWorking');
  70. this.clock.tick(10000);
  71. tests.t4.obj.set('isWorking', false);
  72. this.clock.tick(5000);
  73. expect(tests.t4.obj.method3.called).to.be.false;
  74. });
  75. afterEach(function() {
  76. this.clock.restore();
  77. });
  78. });
  79. });