diff --git a/chapter6/4_testing_and_browser_apis/2_history_api/main.test.js b/chapter6/4_testing_and_browser_apis/2_history_api/main.test.js index 6508b51..193f776 100644 --- a/chapter6/4_testing_and_browser_apis/2_history_api/main.test.js +++ b/chapter6/4_testing_and_browser_apis/2_history_api/main.test.js @@ -11,17 +11,14 @@ beforeEach(() => localStorage.clear()); beforeEach(() => { document.body.innerHTML = initialHtml; + jest.spyOn(window, "addEventListener"); + // You must execute main.js again so that it can attach the // event listener to the form every time the body changes. // Here you must use `jest.resetModules` because otherwise // Jest will have cached `main.js` and it will _not_ run again. jest.resetModules(); - require("./main"); - - // You can only spy on `window.addEventListener` after `main.js` - // has been executed. Otherwise `detachPopstateHandlers` will - // also detach the handlers that `main.js` attached to the page. - jest.spyOn(window, "addEventListener"); + require("./main"); }); afterEach(detachPopstateHandlers); diff --git a/chapter6/5_web_sockets_and_http_requests/1_http_requests/main.test.js b/chapter6/5_web_sockets_and_http_requests/1_http_requests/main.test.js index b62bbbe..323ad4b 100644 --- a/chapter6/5_web_sockets_and_http_requests/1_http_requests/main.test.js +++ b/chapter6/5_web_sockets_and_http_requests/1_http_requests/main.test.js @@ -13,6 +13,8 @@ beforeEach(() => localStorage.clear()); beforeEach(async () => { document.body.innerHTML = initialHtml; + jest.spyOn(window, "addEventListener"); + // You must execute main.js again so that it can attach the // event listener to the form every time the body changes. // Here you must use `jest.resetModules` because otherwise @@ -22,12 +24,7 @@ beforeEach(async () => { nock(API_ADDR) .get("/inventory") .replyWithError({ code: 500 }); - await require("./main"); - - // You can only spy on `window.addEventListener` after `main.js` - // has been executed. Otherwise `detachPopstateHandlers` will - // also detach the handlers that `main.js` attached to the page. - jest.spyOn(window, "addEventListener"); + await require("./main"); }); afterEach(detachPopstateHandlers); diff --git a/chapter6/5_web_sockets_and_http_requests/2_web_sockets/main.test.js b/chapter6/5_web_sockets_and_http_requests/2_web_sockets/main.test.js index 59ec889..ff570c6 100644 --- a/chapter6/5_web_sockets_and_http_requests/2_web_sockets/main.test.js +++ b/chapter6/5_web_sockets_and_http_requests/2_web_sockets/main.test.js @@ -13,6 +13,8 @@ beforeEach(() => localStorage.clear()); beforeEach(async () => { document.body.innerHTML = initialHtml; + jest.spyOn(window, "addEventListener"); + // You must execute main.js again so that it can attach the // event listener to the form every time the body changes. // Here you must use `jest.resetModules` because otherwise @@ -22,12 +24,7 @@ beforeEach(async () => { nock(API_ADDR) .get("/inventory") .replyWithError({ code: 500 }); - await require("./main"); - - // You can only spy on `window.addEventListener` after `main.js` - // has been executed. Otherwise `detachPopstateHandlers` will - // also detach the handlers that `main.js` attached to the page. - jest.spyOn(window, "addEventListener"); + await require("./main"); }); afterEach(detachPopstateHandlers);