summaryrefslogtreecommitdiffstats
path: root/browser/components/places/tests/unit/test_PUIU_batchUpdatesForNode.js
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 17:32:43 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 17:32:43 +0000
commit6bf0a5cb5034a7e684dcc3500e841785237ce2dd (patch)
treea68f146d7fa01f0134297619fbe7e33db084e0aa /browser/components/places/tests/unit/test_PUIU_batchUpdatesForNode.js
parentInitial commit. (diff)
downloadthunderbird-upstream.tar.xz
thunderbird-upstream.zip
Adding upstream version 1:115.7.0.upstream/1%115.7.0upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'browser/components/places/tests/unit/test_PUIU_batchUpdatesForNode.js')
-rw-r--r--browser/components/places/tests/unit/test_PUIU_batchUpdatesForNode.js107
1 files changed, 107 insertions, 0 deletions
diff --git a/browser/components/places/tests/unit/test_PUIU_batchUpdatesForNode.js b/browser/components/places/tests/unit/test_PUIU_batchUpdatesForNode.js
new file mode 100644
index 0000000000..58b68d7574
--- /dev/null
+++ b/browser/components/places/tests/unit/test_PUIU_batchUpdatesForNode.js
@@ -0,0 +1,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"
+ );
+});