test_PUIU_batchUpdatesForNode.js (2919B)
1 const { sinon } = ChromeUtils.importESModule( 2 "resource://testing-common/Sinon.sys.mjs" 3 ); 4 5 /* eslint-disable mozilla/use-chromeutils-generateqi */ 6 7 add_task(async function test_no_result_node() { 8 let functionSpy = sinon.stub().returns(Promise.resolve()); 9 10 await PlacesUIUtils.batchUpdatesForNode(null, 1, functionSpy); 11 12 Assert.ok( 13 functionSpy.calledOnce, 14 "Passing a null result node should still call the wrapped function" 15 ); 16 }); 17 18 add_task(async function test_under_batch_threshold() { 19 let functionSpy = sinon.stub().returns(Promise.resolve()); 20 let resultNode = { 21 QueryInterface() { 22 return this; 23 }, 24 onBeginUpdateBatch: sinon.spy(), 25 onEndUpdateBatch: sinon.spy(), 26 }; 27 28 await PlacesUIUtils.batchUpdatesForNode(resultNode, 1, functionSpy); 29 30 Assert.ok(functionSpy.calledOnce, "Wrapped function should be called once"); 31 Assert.ok( 32 resultNode.onBeginUpdateBatch.notCalled, 33 "onBeginUpdateBatch should not have been called" 34 ); 35 Assert.ok( 36 resultNode.onEndUpdateBatch.notCalled, 37 "onEndUpdateBatch should not have been called" 38 ); 39 }); 40 41 add_task(async function test_over_batch_threshold() { 42 let functionSpy = sinon.stub().callsFake(() => { 43 Assert.ok( 44 resultNode.onBeginUpdateBatch.calledOnce, 45 "onBeginUpdateBatch should have been called before the function" 46 ); 47 Assert.ok( 48 resultNode.onEndUpdateBatch.notCalled, 49 "onEndUpdateBatch should not have been called before the function" 50 ); 51 52 return Promise.resolve(); 53 }); 54 let resultNode = { 55 QueryInterface() { 56 return this; 57 }, 58 onBeginUpdateBatch: sinon.spy(), 59 onEndUpdateBatch: sinon.spy(), 60 }; 61 62 await PlacesUIUtils.batchUpdatesForNode(resultNode, 100, functionSpy); 63 64 Assert.ok(functionSpy.calledOnce, "Wrapped function should be called once"); 65 Assert.ok( 66 resultNode.onBeginUpdateBatch.calledOnce, 67 "onBeginUpdateBatch should have been called" 68 ); 69 Assert.ok( 70 resultNode.onEndUpdateBatch.calledOnce, 71 "onEndUpdateBatch should have been called" 72 ); 73 }); 74 75 add_task(async function test_wrapped_function_throws() { 76 let error = new Error("Failed!"); 77 let functionSpy = sinon.stub().throws(error); 78 let resultNode = { 79 QueryInterface() { 80 return this; 81 }, 82 onBeginUpdateBatch: sinon.spy(), 83 onEndUpdateBatch: sinon.spy(), 84 }; 85 86 let raisedError; 87 try { 88 await PlacesUIUtils.batchUpdatesForNode(resultNode, 100, functionSpy); 89 } catch (ex) { 90 raisedError = ex; 91 } 92 93 Assert.ok(functionSpy.calledOnce, "Wrapped function should be called once"); 94 Assert.ok( 95 resultNode.onBeginUpdateBatch.calledOnce, 96 "onBeginUpdateBatch should have been called" 97 ); 98 Assert.ok( 99 resultNode.onEndUpdateBatch.calledOnce, 100 "onEndUpdateBatch should have been called" 101 ); 102 Assert.equal( 103 raisedError, 104 error, 105 "batchUpdatesForNode should have raised the error from the wrapped function" 106 ); 107 });