precommitHandler-helpers.js (4821B)
1 window.testDeferredCommit = async (t, navigationType, mode, destinationIndex = 0) => { 2 let startHash = location.hash; 3 let destinationHash; 4 const err = new Error("boo!"); 5 6 let popstate_fired = false; 7 window.addEventListener("popstate", () => popstate_fired = true, { once : true }); 8 let navigatesuccess_fired = false; 9 navigation.addEventListener("navigatesuccess", () => navigatesuccess_fired = true, { once : true }); 10 let navigateerror_fired = false; 11 navigation.addEventListener("navigateerror", () => navigateerror_fired = true, { once : true }); 12 13 // mode-specific logic for the navigate event handler 14 let navigate_helpers = { 15 rejectBeforeCommit : async (e) => { return Promise.reject("Should never run") }, 16 rejectAfterCommit : async (e) => { 17 assert_equals(location.hash, destinationHash, "hash after commit"); 18 assert_equals(false, popstate_fired, "popstate before handler starts"); 19 await new Promise(resolve => t.step_timeout(resolve, 0)); 20 assert_equals(navigationType == "traverse", popstate_fired, "popstate fired after handler async step"); 21 return Promise.reject(err); 22 }, 23 success : async (e) => { 24 assert_equals(location.hash, destinationHash, "hash after commit"); 25 assert_equals(false, popstate_fired, "popstate before handler starts"); 26 await new Promise(resolve => t.step_timeout(resolve, 0)); 27 assert_equals(navigationType == "traverse", popstate_fired, "popstate fired after handler async step"); 28 return new Promise(resolve => t.step_timeout(resolve, 0)); 29 }, 30 } 31 32 navigation.addEventListener("navigate", e => { 33 e.intercept({ precommitHandler: t.step_func(async () => { 34 assert_equals(e.navigationType, navigationType); 35 assert_equals(location.hash, startHash, "start hash"); 36 assert_false(popstate_fired, "popstate fired at handler start"); 37 38 await new Promise(resolve => t.step_timeout(resolve, 0)); 39 assert_equals(location.hash, startHash, "hash after first async step"); 40 assert_false(popstate_fired, "popstate fired after first async step"); 41 42 if (mode == "rejectBeforeCommit") 43 return Promise.reject(err); 44 }), 45 handler: t.step_func(navigate_helpers[mode]) 46 }); 47 }, { once: true }); 48 49 let startingIndex = navigation.currentEntry.index; 50 let expectedIndexOnCommit; 51 52 let promises; 53 if (navigationType === "push" || navigationType === "replace") { 54 destinationHash = (startHash === "" ? "#" : startHash) + "a"; 55 promises = navigation.navigate(destinationHash, { history: navigationType }); 56 expectedIndexOnCommit = (navigationType === "push") ? startingIndex + 1 57 : startingIndex; 58 } else if (navigationType === "reload") { 59 destinationHash = startHash; 60 promises = navigation.reload(); 61 expectedIndexOnCommit = startingIndex; 62 } else if (navigationType === "traverse") { 63 let destinationEntry = navigation.entries()[destinationIndex]; 64 destinationHash = new URL(destinationEntry.url).hash; 65 promises = navigation.traverseTo(destinationEntry.key); 66 expectedIndexOnCommit = destinationIndex; 67 } 68 69 if (mode === "rejectBeforeCommit") { 70 await assertBothRejectExactly(t, promises, err); 71 assert_equals(location.hash, startHash, "hash after promise resolution"); 72 assert_false(popstate_fired, "popstate fired after promise resolution"); 73 assert_false(navigatesuccess_fired, "navigatesuccess fired"); 74 assert_true(navigateerror_fired, "navigateerror fired"); 75 assert_equals(navigation.currentEntry.index, startingIndex); 76 } else if (mode === "rejectAfterCommit") { 77 await promises.committed; 78 await assertCommittedFulfillsFinishedRejectsExactly(t, promises, navigation.currentEntry, err); 79 assert_equals(location.hash, destinationHash, "hash after promise resolution"); 80 assert_equals(navigationType == "traverse", popstate_fired, "popstate fired after promise resolution"); 81 assert_false(navigatesuccess_fired, "navigatesuccess fired"); 82 assert_true(navigateerror_fired, "navigateerror fired"); 83 assert_equals(navigation.currentEntry.index, expectedIndexOnCommit); 84 } else { 85 await promises.committed; 86 await assertBothFulfill(t, promises, navigation.currentEntry); 87 assert_equals(location.hash, destinationHash, "hash after promise resolution"); 88 assert_equals(navigationType == "traverse", popstate_fired, "popstate fired after promise resolution"); 89 assert_true(navigatesuccess_fired, "navigatesuccess fired"); 90 assert_false(navigateerror_fired, "navigateerror fired"); 91 assert_equals(navigation.currentEntry.index, expectedIndexOnCommit); 92 } 93 }