Skip to content

Commit

Permalink
fix: provider connect method accept options (#1388)
Browse files Browse the repository at this point in the history
Co-authored-by: Dhaiwat <[email protected]>
  • Loading branch information
LuizAsFight and Dhaiwat10 authored Nov 5, 2023
1 parent c07b065 commit fe579dc
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/khaki-years-compare.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@fuel-ts/providers": patch
---

You can now pass in `ProviderOptions` to `Provider.connect`
4 changes: 2 additions & 2 deletions packages/providers/src/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -310,9 +310,9 @@ export default class Provider {
/**
* Updates the URL for the provider and fetches the consensus parameters for the new URL, if needed.
*/
async connect(url: string) {
async connect(url: string, options?: ProviderOptions) {
this.url = url;
this.operations = this.createOperations(url);
this.operations = this.createOperations(url, options ?? this.options);
await this.fetchChainAndNodeInfo();
}

Expand Down
32 changes: 32 additions & 0 deletions packages/providers/test/provider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,38 @@ describe('Provider', () => {
const provider = await Provider.create(providerUrl, {
fetch: getCustomFetch('getVersion', { nodeInfo: { nodeVersion: '0.30.0' } }),
});

expect(await provider.getVersion()).toEqual('0.30.0');
});

it('can accept options override in connect method', async () => {
const providerUrl = FUEL_NETWORK_URL;

/**
* Mocking and initializing Provider with an invalid fetcher just
* to ensure it'll be properly overriden in `connect` method below
*/
const fetchChainAndNodeInfo = jest
.spyOn(Provider.prototype, 'fetchChainAndNodeInfo')
.mockImplementation();

const provider = await Provider.create(providerUrl, {
fetch: () => {
throw new Error('This should never happen');
},
});

expect(fetchChainAndNodeInfo).toHaveBeenCalledTimes(1);

/**
* Restore mock and call connect with a proper fetch override
*/
fetchChainAndNodeInfo.mockRestore();

await provider.connect(providerUrl, {
fetch: getCustomFetch('getVersion', { nodeInfo: { nodeVersion: '0.30.0' } }),
});

expect(await provider.getVersion()).toEqual('0.30.0');
});

Expand Down

0 comments on commit fe579dc

Please sign in to comment.