Skip to content

Commit

Permalink
test: fix
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait committed Dec 5, 2024
1 parent f989b56 commit 5e943c4
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 127 deletions.
2 changes: 2 additions & 0 deletions client-src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -721,3 +721,5 @@ const createSocketURL = (parsedURL) => {
const socketURL = createSocketURL(parsedResourceQuery);

socket(socketURL, onSocketMessage, options.reconnect);

export { getCurrentScriptSource, parseURL, createSocketURL };
1 change: 1 addition & 0 deletions lib/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,7 @@ class Server {
let host;

const networks = Object.values(os.networkInterfaces())
// eslint-disable-next-line no-shadow
.flatMap((networks) => networks ?? [])
.filter((network) => {
if (!network || !network.address) {
Expand Down
31 changes: 22 additions & 9 deletions test/client/utils/createSocketURL.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
"use strict";

describe("'createSocketURL' function ", () => {
global.__webpack_hash__ = "hash";

const samples = [
// __resourceQuery, location and socket URL
// // __resourceQuery, location and socket URL
[
"?hostname=example.com&pathname=/ws",
"http://example.com",
Expand Down Expand Up @@ -103,16 +105,20 @@ describe("'createSocketURL' function ", () => {
];

samples.forEach(([__resourceQuery, location, expected]) => {
jest.doMock(
"../../../client-src/utils/getCurrentScriptSource",
() => () => new URL("./entry.js", location).toString(),
);
test(`should return '${expected}' socket URL when '__resourceQuery' is '${__resourceQuery}' and 'self.location' is '${location}'`, () => {
global.__resourceQuery = __resourceQuery;

const createSocketURL =
require("../../../client-src/utils/createSocketURL").default;
const parseURL = require("../../../client-src/utils/parseURL").default;
if (__resourceQuery === null) {
Object.defineProperty(document, "currentScript", {
value: document.createElement("script"),
configurable: true,
});
}

const client = require("../../../client-src/index");
const createSocketURL = client.createSocketURL;
const parseURL = client.parseURL;

test(`should return '${expected}' socket URL when '__resourceQuery' is '${__resourceQuery}' and 'self.location' is '${location}'`, () => {
const selfLocation = new URL(location);

delete window.location;
Expand All @@ -121,6 +127,13 @@ describe("'createSocketURL' function ", () => {

const parsedURL = parseURL(__resourceQuery);

if (__resourceQuery === null) {
Object.defineProperty(document, "currentScript", {
value: null,
configurable: true,
});
}

expect(createSocketURL(parsedURL)).toBe(expected);
});

Expand Down
13 changes: 10 additions & 3 deletions test/client/utils/getCurrentScriptSource.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,17 @@

"use strict";

const getCurrentScriptSource =
require("../../../client-src/utils/getCurrentScriptSource").default;

describe("'getCurrentScriptSource' function", () => {
let getCurrentScriptSource;

beforeEach(() => {
global.__webpack_hash__ = "mock-hash";
global.__resourceQuery = "?protocol=ws&hostname=0.0.0.0";

getCurrentScriptSource =
require("../../../client-src/index").getCurrentScriptSource;
});

afterEach(() => {
Object.defineProperty(document, "currentScript", {
// eslint-disable-next-line no-undefined
Expand Down
115 changes: 0 additions & 115 deletions test/client/utils/reloadApp.test.js

This file was deleted.

0 comments on commit 5e943c4

Please sign in to comment.