From 817f7d12cf295a45598c9f68aa39233d0a454d82 Mon Sep 17 00:00:00 2001 From: dtfiedler Date: Thu, 16 Nov 2023 12:00:33 -0600 Subject: [PATCH] fix(tests): update test to use stubbed turbo client We allow a TurboAuthenticatedClient as an optional constructor param for the TurboRatesOracle. This allows us to stub the client for tests, and validate the reponse returned from the SDK is what we return from our wrapper class. --- src/utils/turbo_rates_oracle.test.ts | 47 +++++++++++++++++++--------- src/utils/turbo_rates_oracle.ts | 10 ++++-- 2 files changed, 40 insertions(+), 17 deletions(-) diff --git a/src/utils/turbo_rates_oracle.test.ts b/src/utils/turbo_rates_oracle.test.ts index 514eed4..d0251b2 100644 --- a/src/utils/turbo_rates_oracle.test.ts +++ b/src/utils/turbo_rates_oracle.test.ts @@ -1,32 +1,49 @@ import chai, { expect } from 'chai'; +import sinon from 'sinon'; import chaiAsPromised from 'chai-as-promised'; -import type { FiatID } from './fiat_oracle_types'; import { TurboRatesOracle } from './turbo_rates_oracle'; import { beforeEach, describe, it } from 'vitest'; +import { + TurboUnauthenticatedClient, + TurboRatesResponse +} from '@ardrive/turbo-sdk'; -chai.use(chaiAsPromised); +const turboStubbedResponse: TurboRatesResponse = { + winc: '10000', + fiat: { + aud: 10.123, + brl: 10.123, + cad: 10.123, + eur: 10.123, + gbp: 10.123, + hkd: 10.123, + inr: 10.123, + jpy: 10.123, + sgd: 10.123, + usd: 10.123 + }, + adjustments: [] +}; -const fiat: FiatID = 'usd'; -const examplePriceValue = 15.05; +chai.use(chaiAsPromised); -const TurboRatesResponseSample = `{ - "winc": 1, - "fiat": { - "${fiat}": ${examplePriceValue} - } -}`; describe('The TurboRatesOracle class', () => { let turboRatesOracle: TurboRatesOracle; + let turboSpy: TurboUnauthenticatedClient; beforeEach(() => { - turboRatesOracle = new TurboRatesOracle(); + turboSpy = sinon.createStubInstance(TurboUnauthenticatedClient, { + getFiatRates: Promise.resolve(turboStubbedResponse) + }); + turboRatesOracle = new TurboRatesOracle(turboSpy); }); describe('getFiatRatesForToken function', () => { - it('returns the expected response after a single fetch', async () => { - expect(await turboRatesOracle.getTurboRates()).to.deep.equal( - TurboRatesResponseSample - ); + it('returns the expected response from turbo', async () => { + const rates = await turboRatesOracle.getTurboRates(); + expect(rates).to.deep.equal(turboStubbedResponse); + expect((turboSpy.getFiatRates as sinon.SinonStub).calledOnce).to.be + .true; }); }); }); diff --git a/src/utils/turbo_rates_oracle.ts b/src/utils/turbo_rates_oracle.ts index edeeb01..94345dc 100644 --- a/src/utils/turbo_rates_oracle.ts +++ b/src/utils/turbo_rates_oracle.ts @@ -1,7 +1,13 @@ -import { TurboFactory, TurboRatesResponse } from '@ardrive/turbo-sdk'; +import { + TurboFactory, + TurboRatesResponse, + TurboUnauthenticatedClient +} from '@ardrive/turbo-sdk'; export class TurboRatesOracle implements RatesOracle { - private turbo = TurboFactory.unauthenticated(); + constructor( + private turbo: TurboUnauthenticatedClient = TurboFactory.unauthenticated() + ) {} public async getTurboRates(): Promise { return this.turbo.getFiatRates();