forked from gitcoinco/grants-stack
-
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.
add ipfs collections (gitcoinco#3315)
* add ipfs collections * return empty collection instead of selecting the first 100 if collection file is empty * fix collection tests * use unknown before parsing collection
- Loading branch information
1 parent
238b428
commit f414231
Showing
10 changed files
with
374 additions
and
35 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
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
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
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
138 changes: 138 additions & 0 deletions
138
packages/grant-explorer/src/features/collections/__tests__/collections.test.tsx
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,138 @@ | ||
import { parseCollection } from "../collections"; | ||
import { ZodError } from "zod"; | ||
|
||
describe("parseCollection", () => { | ||
beforeEach(() => { | ||
vi.clearAllMocks(); | ||
}); | ||
|
||
it("should parse a valid collection", async () => { | ||
const data = { | ||
version: "1.0.0", | ||
applications: [ | ||
{ | ||
chainId: 1, | ||
roundId: "0x05df76bc446cee7ad536e2d23c128c9c8909cf7b", | ||
id: "0", | ||
}, | ||
{ | ||
chainId: 10, | ||
roundId: "8", | ||
id: "7", | ||
}, | ||
], | ||
}; | ||
|
||
const response = parseCollection(data); | ||
expect(response).toEqual(data); | ||
}); | ||
|
||
it("requires a valid version", async () => { | ||
const data = { | ||
version: "xyz", | ||
applications: [], | ||
}; | ||
|
||
const expectedError = new ZodError([ | ||
{ | ||
received: "xyz", | ||
code: "invalid_enum_value", | ||
options: ["1.0.0"], | ||
path: ["version"], | ||
message: "Invalid enum value. Expected '1.0.0', received 'xyz'", | ||
}, | ||
]); | ||
|
||
expect(() => parseCollection(data)).toThrowError(expectedError); | ||
}); | ||
|
||
it("requires applications", async () => { | ||
const data = { | ||
version: "1.0.0", | ||
}; | ||
|
||
const expectedError = new ZodError([ | ||
{ | ||
code: "invalid_type", | ||
expected: "array", | ||
received: "undefined", | ||
path: ["applications"], | ||
message: "Required", | ||
}, | ||
]); | ||
|
||
expect(() => parseCollection(data)).toThrowError(expectedError); | ||
}); | ||
|
||
it("requires chainId in applications", async () => { | ||
const data = { | ||
version: "1.0.0", | ||
applications: [ | ||
{ | ||
roundId: "1", | ||
id: "0", | ||
}, | ||
], | ||
}; | ||
|
||
const expectedError = new ZodError([ | ||
{ | ||
code: "invalid_type", | ||
expected: "number", | ||
received: "undefined", | ||
path: ["applications", 0, "chainId"], | ||
message: "Required", | ||
}, | ||
]); | ||
|
||
expect(() => parseCollection(data)).toThrowError(expectedError); | ||
}); | ||
|
||
it("requires roundId in applications", async () => { | ||
const data = { | ||
version: "1.0.0", | ||
applications: [ | ||
{ | ||
chainId: 1, | ||
id: "0", | ||
}, | ||
], | ||
}; | ||
|
||
const expectedError = new ZodError([ | ||
{ | ||
code: "invalid_type", | ||
expected: "string", | ||
received: "undefined", | ||
path: ["applications", 0, "roundId"], | ||
message: "Required", | ||
}, | ||
]); | ||
|
||
expect(() => parseCollection(data)).toThrowError(expectedError); | ||
}); | ||
|
||
it("requires id in applications", async () => { | ||
const data = { | ||
version: "1.0.0", | ||
applications: [ | ||
{ | ||
chainId: 1, | ||
roundId: "0", | ||
}, | ||
], | ||
}; | ||
|
||
const expectedError = new ZodError([ | ||
{ | ||
code: "invalid_type", | ||
expected: "string", | ||
received: "undefined", | ||
path: ["applications", 0, "id"], | ||
message: "Required", | ||
}, | ||
]); | ||
|
||
expect(() => parseCollection(data)).toThrowError(expectedError); | ||
}); | ||
}); |
21 changes: 21 additions & 0 deletions
21
packages/grant-explorer/src/features/collections/collections.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,21 @@ | ||
import { z } from "zod"; | ||
|
||
const collectionSchemaV1 = z.object({ | ||
version: z.enum(["1.0.0"]), | ||
name: z.string().optional(), | ||
description: z.string().optional(), | ||
author: z.string().optional(), | ||
applications: z.array( | ||
z.object({ | ||
chainId: z.number(), | ||
roundId: z.string(), | ||
id: z.string(), | ||
}) | ||
), | ||
}); | ||
|
||
export type CollectionV1 = z.infer<typeof collectionSchemaV1>; | ||
|
||
export function parseCollection(json: unknown) { | ||
return collectionSchemaV1.parse(json); | ||
} |
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.