updater_test.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. beforeEach(function () {
  22. this.clock = sinon.useFakeTimers();
  23. sinon.stub(App.router, "get").returns('test');
  24. App.updater.run.restore();
  25. sinon.spy(App.updater, 'run');
  26. App.updater.immediateRun.restore();
  27. sinon.spy(App.updater, 'immediateRun');
  28. });
  29. var tests = {
  30. t1: {
  31. obj: Em.Object.create({
  32. method: sinon.spy(),
  33. isWorking: true
  34. }),
  35. m: 'method called once with default interval in 15 000 ms'
  36. },
  37. t2: {
  38. obj: Em.Object.create({
  39. method: function () {
  40. }
  41. }),
  42. m: 'should return false if key name is invalid or absent'
  43. },
  44. t3: {
  45. obj: Em.Object.create({
  46. method2: sinon.spy(),
  47. isWorking: true
  48. }),
  49. m: 'method should be called immediately'
  50. },
  51. t4: {
  52. obj: Em.Object.create({
  53. method3: sinon.spy(),
  54. isWorking: true
  55. }),
  56. m: 'method call should be ignored if `isWorking` set to false'
  57. },
  58. t5: {
  59. obj: Em.Object.create({
  60. method4: sinon.spy(),
  61. isWorking: true
  62. }),
  63. m: 'method call should be ignored if urlPattern is not matching router location'
  64. }
  65. };
  66. it(tests.t1.m, function () {
  67. App.updater.run(tests.t1.obj, 'method', 'isWorking');
  68. this.clock.tick(3600000 * 1.5);
  69. expect(tests.t1.obj.method.called).to.be.ok;
  70. });
  71. it(tests.t2.m, function () {
  72. var methodCall = App.updater.run(tests.t2.obj, 'method', 'isWorking');
  73. expect(methodCall).to.be.false;
  74. });
  75. it(tests.t3.m, function () {
  76. App.updater.run(tests.t3.obj, 'method2', 'isWorking');
  77. App.updater.immediateRun('method2');
  78. expect(tests.t3.obj.method2.called).to.be.ok;
  79. });
  80. it(tests.t4.m, function () {
  81. App.updater.run(tests.t4.obj, 'method3', 'isWorking');
  82. this.clock.tick(10000);
  83. tests.t4.obj.set('isWorking', false);
  84. this.clock.tick(5000);
  85. expect(tests.t4.obj.method3.called).to.be.false;
  86. });
  87. it(tests.t5.m, function () {
  88. App.updater.run(tests.t5.obj, 'method4', 'isWorking', 15000, 'pattern');
  89. this.clock.tick(15000);
  90. expect(tests.t5.obj.method4.called).to.be.false;
  91. });
  92. afterEach(function () {
  93. this.clock.restore();
  94. App.router.get.restore();
  95. });
  96. });