-
Notifications
You must be signed in to change notification settings - Fork 162
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: disable reward button when already claimed (#499)
* feat: disable reward button when already claimed * using unclaimed rewards * fix * fix: showing claimed when not claimable
- Loading branch information
1 parent
1dbe7b9
commit dd401d3
Showing
5 changed files
with
155 additions
and
33 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
import { retrieveAssets, getNfts } from "../../utils/assets"; | ||
|
||
global.fetch = jest.fn(); | ||
|
||
describe("retrieveAssets", () => { | ||
beforeEach(() => { | ||
fetch.mockClear(); | ||
}); | ||
it("successfully retrieves assets without pagination", async () => { | ||
const mockAssets = { | ||
data: [ | ||
{ id: "1", name: "Asset 1" }, | ||
{ id: "2", name: "Asset 2" }, | ||
], | ||
}; | ||
fetch.mockResolvedValueOnce({ | ||
json: () => Promise.resolve(mockAssets), | ||
}); | ||
|
||
const url = "https://api.example.com/assets"; | ||
const result = await retrieveAssets(url); | ||
|
||
expect(result.data).toEqual(mockAssets.data); | ||
expect(fetch).toHaveBeenCalledTimes(1); | ||
expect(fetch).toHaveBeenCalledWith(url, { | ||
method: "GET", | ||
headers: { | ||
"Content-Type": "application/json", | ||
"x-api-key": expect.any(String), | ||
}, | ||
}); | ||
}); | ||
|
||
it("handles pagination correctly", async () => { | ||
const firstPageAssets = { | ||
data: [ | ||
{ id: "1", name: "Asset 1" }, | ||
{ id: "2", name: "Asset 2" }, | ||
], | ||
next_url: "https://api.example.com/assets?page=2", | ||
}; | ||
const secondPageAssets = { | ||
data: [ | ||
{ id: "3", name: "Asset 3" }, | ||
{ id: "4", name: "Asset 4" }, | ||
], | ||
}; | ||
fetch.mockResolvedValueOnce({ | ||
json: () => Promise.resolve(firstPageAssets), | ||
}); | ||
fetch.mockResolvedValueOnce({ | ||
json: () => Promise.resolve(secondPageAssets), | ||
}); | ||
const url = "https://api.example.com/assets"; | ||
const result = await retrieveAssets(url); | ||
|
||
expect(result.data).toEqual([ | ||
...firstPageAssets.data, | ||
...secondPageAssets.data, | ||
]); | ||
expect(fetch).toHaveBeenCalledTimes(2); | ||
}); | ||
}); | ||
|
||
describe("getNfts", () => { | ||
beforeEach(() => { | ||
fetch.mockClear(); | ||
}); | ||
it("retrieves NFTs for a given address on the mainnet", async () => { | ||
const mockNfts = { | ||
data: [ | ||
{ id: "1", name: "NFT 1" }, | ||
{ id: "2", name: "NFT 2" }, | ||
], | ||
}; | ||
fetch.mockResolvedValueOnce({ | ||
json: () => Promise.resolve(mockNfts), | ||
}); | ||
|
||
const address = "0x123"; | ||
const network = "MAINNET"; | ||
const nfts = await getNfts(address, network); | ||
|
||
expect(nfts).toEqual(mockNfts.data); | ||
expect(fetch).toHaveBeenCalledWith( | ||
expect.stringContaining("api.starkscan.co"), | ||
expect.any(Object) | ||
); | ||
}); | ||
|
||
it("retrieves NFTs for a given address on the testnet", async () => { | ||
const mockNfts = { | ||
data: [ | ||
{ id: "1", name: "NFT 1" }, | ||
{ id: "2", name: "NFT 2" }, | ||
], | ||
}; | ||
fetch.mockResolvedValueOnce({ | ||
json: () => Promise.resolve(mockNfts), | ||
}); | ||
|
||
const address = "0x456"; | ||
const network = "TESTNET"; | ||
const nfts = await getNfts(address, network); | ||
|
||
expect(nfts).toEqual(mockNfts.data); | ||
expect(fetch).toHaveBeenCalledWith( | ||
expect.stringContaining("api-testnet.starkscan.co"), | ||
expect.any(Object) | ||
); | ||
}); | ||
}); |
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,34 @@ | ||
export const retrieveAssets = async ( | ||
url: string, | ||
accumulatedAssets: StarkscanNftProps[] = [] | ||
): Promise<StarkscanApiResult> => { | ||
return fetch(url, { | ||
method: "GET", | ||
headers: { | ||
"Content-Type": "application/json", | ||
"x-api-key": `${process.env.NEXT_PUBLIC_STARKSCAN}`, | ||
}, | ||
}) | ||
.then((res) => res.json()) | ||
.then((data) => { | ||
const assets = [...accumulatedAssets, ...data.data]; | ||
if (data.next_url) { | ||
return retrieveAssets(data.next_url, assets); | ||
} else { | ||
return { | ||
data: assets, | ||
}; | ||
} | ||
}); | ||
}; | ||
|
||
export const getNfts = async ( | ||
address: string, | ||
network: string | ||
): Promise<StarkscanNftProps[]> => { | ||
const url = `https://${ | ||
network === "TESTNET" ? "api-testnet" : "api" | ||
}.starkscan.co/api/v0/nfts?owner_address=${address}`; | ||
const assets = await retrieveAssets(url); | ||
return assets.data; | ||
}; |