-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3762a6e
commit d22622e
Showing
4 changed files
with
322 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -150,11 +150,11 @@ | |
"vite": "^5.4.8", | ||
"vite-plugin-checker": "^0.8.0", | ||
"vite-plugin-node-polyfills": "^0.17.0", | ||
"zod": "^3.23.8" | ||
"zod": "^3.23.8", | ||
"@hookform/resolvers": "^3.9.0" | ||
}, | ||
"packageManager": "[email protected]", | ||
"dependencies": { | ||
"electron-updater": "6.3.9", | ||
"@hookform/resolvers": "^3.9.0" | ||
"electron-updater": "6.3.9" | ||
} | ||
} |
115 changes: 115 additions & 0 deletions
115
packages/components/src/utils/validationSchemes.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
import { type Network } from "@umami/tezos"; | ||
|
||
import { getNetworkValidationScheme } from "./validationSchemes"; | ||
|
||
describe("getNetworkValidationScheme", () => { | ||
const mockAvailableNetworks: Network[] = [ | ||
{ | ||
name: "Existing Network", | ||
rpcUrl: "https://example.com", | ||
tzktApiUrl: "https://api.example.com", | ||
tzktExplorerUrl: "https://explorer.example.com", | ||
}, | ||
]; | ||
|
||
it("should validate a correct network input", () => { | ||
const schema = getNetworkValidationScheme(mockAvailableNetworks); | ||
const validInput = { | ||
name: "New Network", | ||
rpcUrl: "https://rpc.example.com", | ||
tzktApiUrl: "https://api.tzkt.example.com", | ||
tzktExplorerUrl: "https://explorer.tzkt.example.com", | ||
buyTezUrl: "https://buy.example.com", | ||
}; | ||
|
||
const result = schema.safeParse(validInput); | ||
expect(result.success).toBe(true); | ||
}); | ||
|
||
it("should reject a network with an existing name", () => { | ||
const schema = getNetworkValidationScheme(mockAvailableNetworks); | ||
const invalidInput = { | ||
name: "Existing Network", | ||
rpcUrl: "https://rpc.example.com", | ||
tzktApiUrl: "https://api.tzkt.example.com", | ||
tzktExplorerUrl: "https://explorer.tzkt.example.com", | ||
}; | ||
|
||
const result = schema.safeParse(invalidInput); | ||
expect(result.success).toBe(false); | ||
if (!result.success) { | ||
expect(result.error.issues[0].message).toBe("Network with this name already exists"); | ||
} | ||
}); | ||
|
||
it("should reject invalid URLs", () => { | ||
const schema = getNetworkValidationScheme(mockAvailableNetworks); | ||
const invalidInput = { | ||
name: "Invalid URL Network", | ||
rpcUrl: "http://insecure.com", | ||
tzktApiUrl: "not-a-url", | ||
tzktExplorerUrl: "ftp://invalid-protocol.com", | ||
}; | ||
|
||
const result = schema.safeParse(invalidInput); | ||
expect(result.success).toBe(false); | ||
if (!result.success) { | ||
expect(result.error.issues).toEqual( | ||
expect.arrayContaining([ | ||
expect.objectContaining({ message: "RPC URL must be secure and start with 'https://'" }), | ||
expect.objectContaining({ message: "Enter a valid Tzkt API URL" }), | ||
expect.objectContaining({ | ||
message: "Tzkt Explorer URL must be secure and start with 'https://'", | ||
}), | ||
]) | ||
); | ||
} | ||
}); | ||
|
||
it("should allow empty buyTezUrl", () => { | ||
const schema = getNetworkValidationScheme(mockAvailableNetworks); | ||
const validInput = { | ||
name: "No Buy Tez URL", | ||
rpcUrl: "https://rpc.example.com", | ||
tzktApiUrl: "https://api.tzkt.example.com", | ||
tzktExplorerUrl: "https://explorer.tzkt.example.com", | ||
buyTezUrl: "", | ||
}; | ||
|
||
const result = schema.safeParse(validInput); | ||
expect(result.success).toBe(true); | ||
}); | ||
|
||
it("should make name optional when editing an existing network", () => { | ||
const existingNetwork: Network = { | ||
name: "Existing Network", | ||
rpcUrl: "https://rpc.example.com", | ||
tzktApiUrl: "https://api.tzkt.example.com", | ||
tzktExplorerUrl: "https://explorer.tzkt.example.com", | ||
}; | ||
const schema = getNetworkValidationScheme(mockAvailableNetworks, existingNetwork); | ||
const validInput = { | ||
rpcUrl: "https://new-rpc.example.com", | ||
tzktApiUrl: "https://new-api.tzkt.example.com", | ||
tzktExplorerUrl: "https://new-explorer.tzkt.example.com", | ||
}; | ||
|
||
const result = schema.safeParse(validInput); | ||
expect(result.success).toBe(true); | ||
}); | ||
|
||
it("should require name when adding a new network", () => { | ||
const schema = getNetworkValidationScheme(mockAvailableNetworks); | ||
const invalidInput = { | ||
rpcUrl: "https://rpc.example.com", | ||
tzktApiUrl: "https://api.tzkt.example.com", | ||
tzktExplorerUrl: "https://explorer.tzkt.example.com", | ||
}; | ||
|
||
const result = schema.safeParse(invalidInput); | ||
expect(result.success).toBe(false); | ||
if (!result.success) { | ||
expect(result.error.issues[0].message).toBe("Name is required"); | ||
} | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.