-
Notifications
You must be signed in to change notification settings - Fork 117
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
Showing
6 changed files
with
73 additions
and
15 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import {config} from "@onflow/config" | ||
import {setChainIdDefault} from "./get-chain-id" | ||
|
||
/** | ||
* @description | ||
* Watches the config for changes to access node and updates the chain id accordingly | ||
* | ||
* @returns {Function} A function that unsubscribes the listener | ||
* | ||
*/ | ||
export async function watchForChainIdChanges() { | ||
return config.subscribe( | ||
function configSubscriber(config) { | ||
const nextAccessNode = config?.["accessNode.api"] | ||
if (this.prevAccessNode !== nextAccessNode) { | ||
setChainIdDefault() | ||
} | ||
this.prevAccessNode = nextAccessNode | ||
}.bind({}) | ||
) | ||
} |
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,42 @@ | ||
import {watchForChainIdChanges} from "./chain-id-watcher" | ||
import {config} from "@onflow/config" | ||
import * as chainIdUtils from "./get-chain-id" | ||
|
||
describe("chain-id-watcher", () => { | ||
afterEach(() => { | ||
jest.restoreAllMocks() | ||
}) | ||
|
||
test("flow.network.default is correctly set on first call", async () => { | ||
config.overload({"accessNode.api": "https://example.com"}, async () => { | ||
const spy = jest.spyOn(chainIdUtils, "setChainIdDefault") | ||
spy.mockImplementation(() => {}) | ||
|
||
watchForChainIdChanges() | ||
|
||
// Wait for microtask queue to flush | ||
await new Promise(resolve => setTimeout(resolve, 0)) | ||
|
||
expect(chainIdUtils.setChainIdDefault).toHaveBeenCalled() | ||
}) | ||
}) | ||
|
||
test("flow.network.default is correctly set on subsequent changes", async () => { | ||
config.overload({"accessNode.api": "https://example.com"}, async () => { | ||
watchForChainIdChanges() | ||
|
||
// Wait for microtask queue to flush | ||
await new Promise(resolve => setTimeout(resolve, 0)) | ||
|
||
const spy = jest.spyOn(chainIdUtils, "setChainIdDefault") | ||
spy.mockImplementation(() => {}) | ||
|
||
config.put("accessNode.api", "https://example2.com") | ||
|
||
// Wait for microtask queue to flush | ||
await new Promise(resolve => setTimeout(resolve, 0)) | ||
|
||
expect(chainIdUtils.setChainIdDefault).toHaveBeenCalled() | ||
}) | ||
}) | ||
}) |
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
2 changes: 1 addition & 1 deletion
2
packages/fcl/src/utils/getChainId.test.js → packages/fcl/src/utils/get-chain-id.test.js
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