summaryrefslogtreecommitdiffstats
path: root/browser/components/places/tests/unit/test_PUIU_batchUpdatesForNode.js
blob: 58b68d7574b746fe4dc1830a8f7b69a3c6b60eab (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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
const { sinon } = ChromeUtils.importESModule(
  "resource://testing-common/Sinon.sys.mjs"
);

/* eslint-disable mozilla/use-chromeutils-generateqi */

add_task(async function test_no_result_node() {
  let functionSpy = sinon.stub().returns(Promise.resolve());

  await PlacesUIUtils.batchUpdatesForNode(null, 1, functionSpy);

  Assert.ok(
    functionSpy.calledOnce,
    "Passing a null result node should still call the wrapped function"
  );
});

add_task(async function test_under_batch_threshold() {
  let functionSpy = sinon.stub().returns(Promise.resolve());
  let resultNode = {
    QueryInterface() {
      return this;
    },
    onBeginUpdateBatch: sinon.spy(),
    onEndUpdateBatch: sinon.spy(),
  };

  await PlacesUIUtils.batchUpdatesForNode(resultNode, 1, functionSpy);

  Assert.ok(functionSpy.calledOnce, "Wrapped function should be called once");
  Assert.ok(
    resultNode.onBeginUpdateBatch.notCalled,
    "onBeginUpdateBatch should not have been called"
  );
  Assert.ok(
    resultNode.onEndUpdateBatch.notCalled,
    "onEndUpdateBatch should not have been called"
  );
});

add_task(async function test_over_batch_threshold() {
  let functionSpy = sinon.stub().callsFake(() => {
    Assert.ok(
      resultNode.onBeginUpdateBatch.calledOnce,
      "onBeginUpdateBatch should have been called before the function"
    );
    Assert.ok(
      resultNode.onEndUpdateBatch.notCalled,
      "onEndUpdateBatch should not have been called before the function"
    );

    return Promise.resolve();
  });
  let resultNode = {
    QueryInterface() {
      return this;
    },
    onBeginUpdateBatch: sinon.spy(),
    onEndUpdateBatch: sinon.spy(),
  };

  await PlacesUIUtils.batchUpdatesForNode(resultNode, 100, functionSpy);

  Assert.ok(functionSpy.calledOnce, "Wrapped function should be called once");
  Assert.ok(
    resultNode.onBeginUpdateBatch.calledOnce,
    "onBeginUpdateBatch should have been called"
  );
  Assert.ok(
    resultNode.onEndUpdateBatch.calledOnce,
    "onEndUpdateBatch should have been called"
  );
});

add_task(async function test_wrapped_function_throws() {
  let error = new Error("Failed!");
  let functionSpy = sinon.stub().throws(error);
  let resultNode = {
    QueryInterface() {
      return this;
    },
    onBeginUpdateBatch: sinon.spy(),
    onEndUpdateBatch: sinon.spy(),
  };

  let raisedError;
  try {
    await PlacesUIUtils.batchUpdatesForNode(resultNode, 100, functionSpy);
  } catch (ex) {
    raisedError = ex;
  }

  Assert.ok(functionSpy.calledOnce, "Wrapped function should be called once");
  Assert.ok(
    resultNode.onBeginUpdateBatch.calledOnce,
    "onBeginUpdateBatch should have been called"
  );
  Assert.ok(
    resultNode.onEndUpdateBatch.calledOnce,
    "onEndUpdateBatch should have been called"
  );
  Assert.equal(
    raisedError,
    error,
    "batchUpdatesForNode should have raised the error from the wrapped function"
  );
});