Skip to content

Commit

Permalink
fix(tests): update test to use stubbed turbo client
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
dtfiedler committed Nov 16, 2023
1 parent d001c08 commit 739d718
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 17 deletions.
46 changes: 31 additions & 15 deletions src/utils/turbo_rates_oracle.test.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,48 @@
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 = {

Check failure on line 11 in src/utils/turbo_rates_oracle.test.ts

View workflow job for this annotation

GitHub Actions / build

Type '{ winc: string; fiat: { aud: number; brl: number; cad: number; eur: number; gbp: number; hkd: number; inr: number; jpy: number; sgd: number; usd: number; }; }' is not assignable to type '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
}
};

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;
});
});
});
10 changes: 8 additions & 2 deletions src/utils/turbo_rates_oracle.ts
Original file line number Diff line number Diff line change
@@ -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<TurboRatesResponse> {
return this.turbo.getFiatRates();
Expand Down

0 comments on commit 739d718

Please sign in to comment.