aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/tensorboard/components/tf_tensorboard/test/autoReloadTests.ts
blob: 0f049d40ab66a3f45912fcb92094c19436993ed9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/* Copyright 2015 The TensorFlow Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/
declare function fixture(id: string): void;
window.HTMLImports.whenReady(() => {
  Polymer({
    is: 'autoreload-test-element',
    behaviors: [TF.TensorBoard.AutoReloadBehavior],
  });

  describe('autoReload-behavior', function() {
    var testElement;
    var ls = window.localStorage;
    var key = TF.TensorBoard.AUTORELOAD_LOCALSTORAGE_KEY;
    var clock;
    var callCount: number;

    beforeEach(function() {
      ls.setItem(key, 'false');  // start it turned off so we can mutate fns
      testElement = fixture('autoReloadFixture');
      callCount = 0;
      testElement.reload = function() { callCount++; };
    });

    before(function() { clock = sinon.useFakeTimers(); });

    after(function() { clock.restore(); });

    it('reads and writes autoReload state from localStorage', function() {
      ls.removeItem(key);
      testElement = fixture('autoReloadFixture');
      chai.assert.isTrue(
          testElement.autoReloadEnabled, 'autoReload defaults to true');
      chai.assert.equal(ls.getItem(key), 'true', 'autoReload setting saved');
      testElement = fixture('autoReloadFixture');
      chai.assert.isTrue(
          testElement.autoReloadEnabled, 'read true from localStorage');
      testElement.autoReloadEnabled = false;
      chai.assert.equal(ls.getItem(key), 'false', 'autoReload setting saved');
      testElement = fixture('autoReloadFixture');
      chai.assert.isFalse(
          testElement.autoReloadEnabled, 'read false setting properly');
      testElement.autoReloadEnabled = true;
      chai.assert.equal(ls.getItem(key), 'true', 'saved true setting');
    });

    it('reloads every interval secs when autoReloading', function() {
      testElement.autoReloadIntervalSecs = 1;
      testElement.autoReloadEnabled = true;
      clock.tick(1000);
      chai.assert.equal(callCount, 1, 'ticking clock triggered call');
      clock.tick(20 * 1000);
      chai.assert.equal(callCount, 21, 'ticking clock 20s triggered 20 calls');
    });

    it('can cancel pending autoReload', function() {
      testElement.autoReloadIntervalSecs = 10;
      testElement.autoReloadEnabled = true;
      clock.tick(5 * 1000);
      testElement.autoReloadEnabled = false;
      clock.tick(20 * 1000);
      chai.assert.equal(callCount, 0, 'callCount is 0');
    });

    it('throws an error in absence of reload method', function() {
      testElement.reload = undefined;
      testElement.autoReloadIntervalSecs = 1;
      testElement.autoReloadEnabled = true;
      chai.assert.throws(function() {
        clock.tick(5000);
      });
    });
  });
});