Skip to content

Commit

Permalink
test: reject promises test
Browse files Browse the repository at this point in the history
  • Loading branch information
0xsambugs committed Dec 28, 2024
1 parent 2b6966b commit 8863e38
Showing 1 changed file with 75 additions and 0 deletions.
75 changes: 75 additions & 0 deletions test/unit/services/quotes/source-lists/local-source-list.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import chai, { expect } from 'chai';
import chaiAsPromised from 'chai-as-promised';
import { LocalSourceList } from '@services/quotes/source-lists/local-source-list';
import { SourceListQuoteRequest } from '@services/quotes/source-lists/types';
import { then, when } from '@test-utils/bdd';
import { IProviderService } from '@services/providers';
import { IFetchService } from '@services/fetch';
chai.use(chaiAsPromised);

describe('Local Source List', () => {
describe('Rejected Promises', () => {
when('asking for unknown source', () => {
then('promise is rejected', async () => {
const sourceList = new LocalSourceList({
providerService: PROVIDER_SERVICE,
fetchService: FETCH_SERVICE,
});
const quotes = sourceList.getQuotes({
...REQUEST,
sources: ['unknown'],
order: { type: 'sell', sellAmount: 100 },
});
expect(Object.keys(quotes)).to.have.lengthOf(1);
await expect(quotes['unknown']).to.have.rejectedWith(`Could not find a source with id 'unknown'`);
});
});

when('executing a buy order for a source that does not support it', () => {
then('promise is rejected', async () => {
const sourceList = new LocalSourceList({
providerService: PROVIDER_SERVICE,
fetchService: FETCH_SERVICE,
});
const quotes = sourceList.getQuotes({
...REQUEST,
order: { type: 'buy', buyAmount: 100 },
sources: ['odos'],
});
expect(Object.keys(quotes)).to.have.lengthOf(1);
await expect(quotes['odos']).to.have.rejectedWith(`Source with id 'odos' does not support buy orders`);
});
});

when('context/config is invalid for a source', () => {
then('promise is rejected', async () => {
const sourceList = new LocalSourceList({
providerService: PROVIDER_SERVICE,
fetchService: FETCH_SERVICE,
});
const quotes = sourceList.getQuotes({
...REQUEST,
order: { type: 'sell', sellAmount: 100 },
sources: ['enso'],
});
expect(Object.keys(quotes)).to.have.lengthOf(1);
await expect(quotes['enso']).to.have.rejectedWith(`The current context or config is not valid for source with id 'enso'`);
});
});
});
});

const REQUEST: Omit<SourceListQuoteRequest, 'sources' | 'order'> = {
chainId: 1,
sellToken: '0x0000000000000000000000000000000000000001',
buyToken: '0x0000000000000000000000000000000000000002',
slippagePercentage: 0.03,
takerAddress: '0x0000000000000000000000000000000000000003',
external: {
tokenData: {} as any,
gasPrice: {} as any,
},
};

const PROVIDER_SERVICE: IProviderService = {} as any;
const FETCH_SERVICE: IFetchService = {} as any;

0 comments on commit 8863e38

Please sign in to comment.