Skip to content

Commit 24d117b

Browse files
authored
update js dependencies (#3849)
* update js dependencies * fix flaky test
1 parent f55d8d9 commit 24d117b

20 files changed

+2487
-1878
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ jobs:
169169

170170
runs-on: ubuntu-latest
171171
container:
172-
image: mcr.microsoft.com/playwright:v1.51.1-jammy
172+
image: mcr.microsoft.com/playwright:v1.53.0-jammy
173173
env:
174174
ImageOS: ubuntu22
175175
HOME: /root

assets/js/phoenix_live_view/browser.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,17 @@ const Browser = {
7979
document.cookie = `${name}=; max-age=-1; path=/`;
8080
},
8181

82-
redirect(toURL, flash) {
82+
redirect(
83+
toURL,
84+
flash,
85+
navigate = (url) => {
86+
window.location.href = url;
87+
},
88+
) {
8389
if (flash) {
8490
this.setCookie("__phoenix_flash__", flash, 60);
8591
}
86-
window.location.href = toURL;
92+
navigate(toURL);
8793
},
8894

8995
localKey(namespace, subkey) {

assets/test/browser_test.ts

Lines changed: 6 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -30,59 +30,22 @@ describe("Browser", () => {
3030
});
3131

3232
describe("redirect", () => {
33-
let originalLocation: Location;
34-
let mockHrefSetter: jest.Mock;
35-
let currentHref: string;
36-
37-
beforeAll(() => {
38-
originalLocation = window.location;
39-
});
40-
41-
beforeEach(() => {
42-
currentHref = "https://example.com"; // Initial mocked URL
43-
mockHrefSetter = jest.fn((newHref: string) => {
44-
currentHref = newHref;
45-
});
46-
47-
Object.defineProperty(window, "location", {
48-
writable: true,
49-
configurable: true,
50-
value: {
51-
get href() {
52-
return currentHref;
53-
},
54-
set href(url: string) {
55-
mockHrefSetter(url);
56-
},
57-
},
58-
});
59-
});
60-
61-
afterAll(() => {
62-
// Restore the original window.location object
63-
Object.defineProperty(window, "location", {
64-
writable: true,
65-
configurable: true,
66-
value: originalLocation,
67-
});
68-
});
69-
7033
test("redirects to a new URL", () => {
34+
const navigate = jest.fn();
7135
const targetUrl = "https://phoenixframework.com";
72-
Browser.redirect(targetUrl);
73-
expect(mockHrefSetter).toHaveBeenCalledWith(targetUrl);
74-
expect(window.location.href).toEqual(targetUrl);
36+
Browser.redirect(targetUrl, null, navigate);
37+
expect(navigate).toHaveBeenCalledWith(targetUrl);
7538
});
7639

7740
test("sets a flash cookie before redirecting", () => {
41+
const navigate = jest.fn();
7842
const targetUrl = "https://phoenixframework.com";
7943
const flashMessage = "mango";
80-
Browser.redirect(targetUrl, flashMessage);
44+
Browser.redirect(targetUrl, flashMessage, navigate);
8145

8246
expect(document.cookie).toContain("__phoenix_flash__");
8347
expect(document.cookie).toContain(flashMessage);
84-
expect(mockHrefSetter).toHaveBeenCalledWith(targetUrl);
85-
expect(window.location.href).toEqual(targetUrl);
48+
expect(navigate).toHaveBeenCalledWith(targetUrl);
8649
});
8750
});
8851
});

assets/test/globals.d.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
declare global {
2-
function setStartSystemTime(): void;
3-
function advanceTimersToNextFrame(): void;
42
let LV_VSN: string;
53
}
64

assets/test/js_test.ts

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ describe("JS", () => {
2222
beforeEach(() => {
2323
global.document.body.innerHTML = "";
2424
jest.useFakeTimers();
25-
setStartSystemTime();
2625
});
2726

2827
afterEach(() => {
@@ -301,27 +300,27 @@ describe("JS", () => {
301300
JS.exec(event, "click", click.getAttribute("phx-click"), view, click);
302301
expect(hideStartCalled).toBe(true);
303302
// first tick: waiting for start classes to be set
304-
advanceTimersToNextFrame();
303+
jest.advanceTimersToNextFrame();
305304
expect(modal.classList.contains("fade-out-start")).toBe(true);
306305
expect(modal.classList.contains("fade-out")).toBe(false);
307306
// second tick: waiting for out classes to be set
308-
advanceTimersToNextFrame();
307+
jest.advanceTimersToNextFrame();
309308
expect(modal.classList.contains("fade-out-start")).toBe(true);
310309
expect(modal.classList.contains("fade-out")).toBe(true);
311310
// third tick: waiting for outEndClasses
312-
advanceTimersToNextFrame();
311+
jest.advanceTimersToNextFrame();
313312
expect(modal.classList.contains("fade-out-start")).toBe(false);
314313
expect(modal.classList.contains("fade-out")).toBe(true);
315314
expect(modal.classList.contains("fade-out-end")).toBe(true);
316315
// wait for onEnd
317316
jest.runAllTimers();
318-
advanceTimersToNextFrame();
317+
jest.advanceTimersToNextFrame();
319318
// fifth tick: display: none
320-
advanceTimersToNextFrame();
319+
jest.advanceTimersToNextFrame();
321320
expect(hideEndCalled).toBe(true);
322321
expect(modal.style.display).toEqual("none");
323322
// sixth tick, removed end classes
324-
advanceTimersToNextFrame();
323+
jest.advanceTimersToNextFrame();
325324
expect(modal.classList.contains("fade-out-start")).toBe(false);
326325
expect(modal.classList.contains("fade-out")).toBe(false);
327326
expect(modal.classList.contains("fade-out-end")).toBe(false);
@@ -330,24 +329,24 @@ describe("JS", () => {
330329
JS.exec(event, "click", click.getAttribute("phx-click"), view, click);
331330
expect(showStartCalled).toBe(true);
332331
// first tick: waiting for start classes to be set
333-
advanceTimersToNextFrame();
332+
jest.advanceTimersToNextFrame();
334333
expect(modal.classList.contains("fade-in-start")).toBe(true);
335334
expect(modal.classList.contains("fade-in")).toBe(false);
336335
expect(modal.style.display).toEqual("none");
337336
// second tick: waiting for in classes to be set
338-
advanceTimersToNextFrame();
337+
jest.advanceTimersToNextFrame();
339338
expect(modal.classList.contains("fade-in-start")).toBe(true);
340339
expect(modal.classList.contains("fade-in")).toBe(true);
341340
expect(modal.classList.contains("fade-in-end")).toBe(false);
342341
expect(modal.style.display).toEqual("block");
343342
// third tick: waiting for inEndClasses
344-
advanceTimersToNextFrame();
343+
jest.advanceTimersToNextFrame();
345344
expect(modal.classList.contains("fade-in-start")).toBe(false);
346345
expect(modal.classList.contains("fade-in")).toBe(true);
347346
expect(modal.classList.contains("fade-in-end")).toBe(true);
348347
// wait for onEnd
349348
jest.runAllTimers();
350-
advanceTimersToNextFrame();
349+
jest.advanceTimersToNextFrame();
351350
expect(showEndCalled).toBe(true);
352351
// sixth tick, removed end classes
353352
expect(modal.classList.contains("fade-in-start")).toBe(false);

jest.config.js

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* https://jestjs.io/docs/configuration
44
*/
55

6-
import {default as packageJson} from "./package.json" with {type: "json"}
6+
import { default as packageJson } from "./package.json" with { type: "json" };
77

88
export default {
99
// All imported modules in your tests should be mocked automatically
@@ -28,10 +28,7 @@ export default {
2828
// coverageDirectory: undefined,
2929

3030
// An array of regexp pattern strings used to skip coverage collection
31-
coveragePathIgnorePatterns: [
32-
"/node_modules/",
33-
"/assets/test/"
34-
],
31+
coveragePathIgnorePatterns: ["/node_modules/", "/assets/test/"],
3532

3633
collectCoverage: true,
3734

@@ -85,7 +82,7 @@ export default {
8582
// A map from regular expressions to module names or to arrays of module names that allow to stub out resources with a single module
8683
moduleNameMapper: {
8784
"^phoenix_live_view$": "<rootDir>/assets/js/phoenix_live_view/index.ts",
88-
"^phoenix_live_view/(.*)$": "<rootDir>/assets/js/phoenix_live_view/$1"
85+
"^phoenix_live_view/(.*)$": "<rootDir>/assets/js/phoenix_live_view/$1",
8986
},
9087

9188
// An array of regexp pattern strings, matched against all module paths before considered 'visible' to the module loader
@@ -106,15 +103,14 @@ export default {
106103
// Use this configuration option to add custom reporters to Jest
107104
reporters: [
108105
"default",
109-
["jest-monocart-coverage", {
110-
name: "Phoenix LiveView JS Unit Coverage",
111-
reports: [
112-
["raw", {outputDir: "./raw"}],
113-
["v8"],
114-
["console-summary"]
115-
],
116-
outputDir: "./coverage"
117-
}]
106+
[
107+
"jest-monocart-coverage",
108+
{
109+
name: "Phoenix LiveView JS Unit Coverage",
110+
reports: [["raw", { outputDir: "./raw" }], ["v8"], ["console-summary"]],
111+
outputDir: "./coverage",
112+
},
113+
],
118114
],
119115

120116
// Automatically reset mock state between every test
@@ -141,14 +137,12 @@ export default {
141137
// runner: "jest-runner",
142138

143139
// The paths to modules that run some code to configure or set up the testing environment before each test
144-
setupFiles: [
145-
"<rootDir>/setupTests.js"
146-
],
140+
setupFiles: ["<rootDir>/setupTests.js"],
147141

148142
// A list of paths to modules that run some code to configure or set up the testing framework before each test
149-
setupFilesAfterEnv: [
150-
"<rootDir>/setupTestsAfterEnv.js"
151-
],
143+
// setupFilesAfterEnv: [
144+
// "<rootDir>/setupTestsAfterEnv.js"
145+
// ],
152146

153147
// The number of seconds after which a test is considered as slow and reported as such in the results.
154148
// slowTestThreshold: 5,
@@ -211,4 +205,4 @@ export default {
211205

212206
// Whether to use watchman for file crawling
213207
// watchman: true,
214-
}
208+
};

0 commit comments

Comments
 (0)