-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
## Description Add sdk react hook tests. ## What type of PR is this? (check all applicable) - [ ] 🍕 Feature (`feat:`) - [ ] 🐛 Bug Fix (`fix:`) - [ ] 📝 Documentation Update (`docs:`) - [ ] 🎨 Style (`style:`) - [ ] 🧑💻 Code Refactor (`refactor:`) - [ ] 🔥 Performance Improvements (`perf:`) - [x] ✅ Test (`test:`) - [ ] 🤖 Build (`build:`) - [ ] 🔁 CI (`ci:`) - [ ] 📦 Chore (`chore:`) - [ ] ⏩ Revert (`revert:`) - [ ] 🚀 Breaking Changes (`BREAKING CHANGE:`)
- Loading branch information
Showing
43 changed files
with
1,196 additions
and
471 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 |
---|---|---|
@@ -1,77 +1,37 @@ | ||
"use client"; | ||
|
||
import React, { | ||
createContext, | ||
PropsWithChildren, | ||
useEffect, | ||
useState | ||
} from "react"; | ||
import React, { createContext, PropsWithChildren, useMemo } from "react"; | ||
|
||
import { useAccount, useNetwork, useProvider } from "@starknet-react/core"; | ||
import { useNetwork, useProvider } from "@starknet-react/core"; | ||
|
||
import { | ||
Config, | ||
createConfig, | ||
CreateConfigParameters, | ||
Network | ||
CreateConfigParameters | ||
} from "@ark-project/core"; | ||
|
||
import { getOwner } from "../lib/getOwner"; | ||
|
||
const OwnerDataContext = createContext<string | undefined>(undefined); | ||
const ConfigDataContext = createContext<Config | undefined>(undefined); | ||
const ArkContext = createContext<Config | undefined>(undefined); | ||
|
||
export type ArkProviderProviderProps = { | ||
config: CreateConfigParameters; | ||
}; | ||
|
||
function ArkProvider(props: PropsWithChildren<ArkProviderProviderProps>) { | ||
const { children, config: baseConfig } = props; | ||
const [owner, setOwner] = useState<string | undefined>(undefined); | ||
const { provider: starknetProvider } = useProvider(); | ||
const { chain: starknetChain } = useNetwork(); | ||
const [config, setConfig] = useState<Config>( | ||
createConfig({ | ||
starknetProvider: starknetProvider, | ||
starknetNetwork: baseConfig.starknetNetwork as Network | ||
}) | ||
const { provider } = useProvider(); | ||
const { chain } = useNetwork(); | ||
|
||
const config = useMemo( | ||
() => | ||
createConfig({ | ||
...baseConfig, | ||
starknetProvider: provider, | ||
starknetNetwork: baseConfig.starknetNetwork | ||
}), | ||
[chain, provider, baseConfig.starknetNetwork] | ||
); | ||
|
||
useEffect(() => { | ||
const newConfig = createConfig({ | ||
starknetProvider: starknetProvider, | ||
starknetNetwork: baseConfig.starknetNetwork as Network | ||
}); | ||
setConfig(newConfig); | ||
}, [starknetProvider, starknetChain, baseConfig]); | ||
|
||
const { address, connector } = useAccount(); | ||
|
||
useEffect(() => { | ||
const fetchOwner = async () => { | ||
if (address && config.starknetProvider && connector?.id) { | ||
const owner = await getOwner( | ||
address, | ||
config.starknetProvider, | ||
connector?.id | ||
); | ||
if (Array.isArray(owner) && owner[0]) { | ||
setOwner(owner[0]); | ||
} | ||
} else { | ||
setOwner(undefined); | ||
} | ||
}; | ||
fetchOwner(); | ||
}, [address, config.starknetProvider, connector]); | ||
|
||
return ( | ||
<ConfigDataContext.Provider value={config}> | ||
<OwnerDataContext.Provider value={owner}> | ||
{children} | ||
</OwnerDataContext.Provider> | ||
</ConfigDataContext.Provider> | ||
); | ||
return <ArkContext.Provider value={config}>{children}</ArkContext.Provider>; | ||
} | ||
|
||
export { ArkProvider, ConfigDataContext, OwnerDataContext }; | ||
export { ArkContext, ArkProvider }; |
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 |
---|---|---|
@@ -1,12 +1,20 @@ | ||
import { expect, it } from "vitest"; | ||
import { describe, expect, it } from "vitest"; | ||
|
||
import { renderHook } from "../test/react"; | ||
import { renderHook, waitFor } from "../../../test/src/react"; | ||
import { useArkFees } from "./useArkFees"; | ||
|
||
it("default", async () => { | ||
const { result, rerender } = renderHook(() => useArkFees()); | ||
describe("useArkFees", () => { | ||
it("default", async () => { | ||
const { result } = renderHook(() => useArkFees()); | ||
|
||
expect(result.current.isSuccess).toBe(false); | ||
rerender(); | ||
expect(result.current.isSuccess).toBe(true); | ||
await waitFor(() => expect(result.current.isSuccess).toBe(true)); | ||
|
||
expect(result.current.data).toMatchInlineSnapshot(` | ||
{ | ||
"denominator": 1n, | ||
"formatted": "0.00", | ||
"numerator": 0n, | ||
} | ||
`); | ||
}); | ||
}); |
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,26 @@ | ||
import { describe, expect, it } from "vitest"; | ||
|
||
import { accounts } from "@ark-project/test"; | ||
|
||
import { renderHook, waitFor } from "../../../test/src/react"; | ||
import { useBrokerFees } from "./useBrokerFees"; | ||
|
||
describe("useBrokerFees", () => { | ||
it("default", async () => { | ||
const { listingBroker } = accounts; | ||
|
||
const { result } = renderHook(() => | ||
useBrokerFees({ brokerAdress: listingBroker.address }) | ||
); | ||
|
||
await waitFor(() => expect(result.current.isSuccess).toBe(true)); | ||
|
||
expect(result.current.data).toMatchInlineSnapshot(` | ||
{ | ||
"denominator": 1n, | ||
"formatted": "0.00", | ||
"numerator": 0n, | ||
} | ||
`); | ||
}); | ||
}); |
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.