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 817f7d1
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 17 deletions.
47 changes: 32 additions & 15 deletions src/utils/turbo_rates_oracle.test.ts
Original file line number Diff line number Diff line change
@@ -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;
});
});
});
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 817f7d1

Please sign in to comment.