aboutsummaryrefslogtreecommitdiffstats
path: root/packages/asset-buyer/test/utils/mocks.ts
blob: d3e1c09c44ead51f0133ec8a61a87596a4a2fe38 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import { Web3ProviderEngine } from '@0x/subproviders';
import * as TypeMoq from 'typemoq';

import { AssetBuyer } from '../../src/asset_buyer';
import { OrderProvider, OrderProviderResponse, OrdersAndFillableAmounts } from '../../src/types';

// tslint:disable:promise-function-async

// Implementing dummy class for using in mocks, see https://github.com/florinn/typemoq/issues/3
class OrderProviderClass implements OrderProvider {
    // tslint:disable-next-line:prefer-function-over-method
    public async getOrdersAsync(): Promise<OrderProviderResponse> {
        return Promise.resolve({ orders: [] });
    }
    // tslint:disable-next-line:prefer-function-over-method
    public async getAvailableMakerAssetDatasAsync(takerAssetData: string): Promise<string[]> {
        return Promise.resolve([]);
    }
}

export const orderProviderMock = () => {
    return TypeMoq.Mock.ofType(OrderProviderClass, TypeMoq.MockBehavior.Strict);
};

export const mockAvailableAssetDatas = (
    mockOrderProvider: TypeMoq.IMock<OrderProviderClass>,
    assetData: string,
    availableAssetDatas: string[],
) => {
    mockOrderProvider
        .setup(op => op.getAvailableMakerAssetDatasAsync(TypeMoq.It.isValue(assetData)))
        .returns(() => {
            return Promise.resolve(availableAssetDatas);
        })
        .verifiable(TypeMoq.Times.once());
};

const partiallyMockedAssetBuyer = (
    provider: Web3ProviderEngine,
    orderProvider: OrderProvider,
): TypeMoq.IMock<AssetBuyer> => {
    const rawAssetBuyer = new AssetBuyer(provider, orderProvider);
    const mockedAssetBuyer = TypeMoq.Mock.ofInstance(rawAssetBuyer, TypeMoq.MockBehavior.Loose, false);
    mockedAssetBuyer.callBase = true;
    return mockedAssetBuyer;
};

const mockGetOrdersAndAvailableAmounts = (
    mockedAssetBuyer: TypeMoq.IMock<AssetBuyer>,
    assetData: string,
    ordersAndFillableAmounts: OrdersAndFillableAmounts,
): void => {
    mockedAssetBuyer
        .setup(a => a.getOrdersAndFillableAmountsAsync(assetData, false))
        .returns(() => Promise.resolve(ordersAndFillableAmounts))
        .verifiable(TypeMoq.Times.once());
};

export const mockedAssetBuyerWithOrdersAndFillableAmounts = (
    provider: Web3ProviderEngine,
    orderProvider: OrderProvider,
    assetData: string,
    ordersAndFillableAmounts: OrdersAndFillableAmounts,
): TypeMoq.IMock<AssetBuyer> => {
    const mockedAssetBuyer = partiallyMockedAssetBuyer(provider, orderProvider);
    mockGetOrdersAndAvailableAmounts(mockedAssetBuyer, assetData, ordersAndFillableAmounts);
    return mockedAssetBuyer;
};