test(NetworkConfig): fully cover NetworkConfig.js

This commit is contained in:
David Laganiere 2023-04-29 11:44:59 -04:00
commit 4bc2a251cf
No known key found for this signature in database
GPG key ID: 18C4B80581C28747
2 changed files with 32 additions and 2 deletions

View file

@ -0,0 +1,30 @@
import * as NetworkConfig from "utils/NetworkConfig";
jest.spyOn(Math, "random").mockReturnValue(0.5);
jest.spyOn(Date, "now").mockImplementation(() => 1487076708000);
describe("NetworkConfig", () => {
it("getRandomInt()", () => {
expect(NetworkConfig.getRandomInt(1, 10)).toBe(5);
expect(NetworkConfig.getRandomInt(2, 2)).toBe(2);
expect(NetworkConfig.getRandomInt("test", 10)).toBe(NaN);
expect(NetworkConfig.getRandomInt(1, "test")).toBe(NaN);
expect(NetworkConfig.getRandomInt("test", "test")).toBe(NaN);
});
it("generateNetworkConfig()", () => {
expect(NetworkConfig.generateNetworkConfig()).toStrictEqual({
config: {
name: "new-net-08000",
private: true,
v6AssignMode: { "6plane": false, rfc4193: false, zt: false },
v4AssignMode: { zt: true },
routes: [{ flags: 0, metric: 0, target: "172.30.127.0/24", via: null }],
ipAssignmentPools: [
{ ipRangeEnd: "172.30.127.254", ipRangeStart: "172.30.127.1" },
],
enableBroadcast: true,
},
});
});
});

View file

@ -1,6 +1,6 @@
export function generateNetworkConfig() { export function generateNetworkConfig() {
const randSubnetPart = getRandomInt(0, 254).toString(); const randSubnetPart = getRandomInt(0, 254).toString();
const randNamePart = new Date().getTime(); const randNamePart = new Date(Date.now()).getTime();
return { return {
config: { config: {
name: "new-net-" + randNamePart.toString().substring(8), name: "new-net-" + randNamePart.toString().substring(8),
@ -26,7 +26,7 @@ export function generateNetworkConfig() {
}; };
} }
function getRandomInt(min, max) { export function getRandomInt(min, max) {
min = Math.ceil(min); min = Math.ceil(min);
max = Math.floor(max); max = Math.floor(max);
return Math.floor(Math.random() * (max - min)) + min; return Math.floor(Math.random() * (max - min)) + min;