Skip to content

Commit

Permalink
SocketProviderAdapter mock class renamed to SocketProvider and improv…
Browse files Browse the repository at this point in the history
…ed and AbstractSubscriptionTest and LogSubscriptionTest updated.
  • Loading branch information
Samuel Furter committed Apr 26, 2019
1 parent 8be71d1 commit b9fe454
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default class SocketProvider {
constructor() {}
on() {}
once() {}
subscribe() {}
removeAllListeners() {}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ import * as Utils from 'web3-utils';
import {formatters} from 'web3-core-helpers';
import AbstractSubscription from '../../../lib/subscriptions/AbstractSubscription';
import AbstractWeb3Module from '../../__mocks__/AbstractWeb3Module';
import SocketProvider from '../../__mocks__/SocketProvider';

// Mocks
jest.mock('web3-utils');
jest.mock('web3-core-helpers');
jest.mock('../../__mocks__/SocketProvider');

/**
* AbstractSubscription test
Expand All @@ -15,7 +17,7 @@ describe('AbstractSubscriptionTest', () => {

beforeEach(() => {
moduleInstanceMock = new AbstractWeb3Module();
moduleInstanceMock.currentProvider.once = jest.fn();
moduleInstanceMock.currentProvider = new SocketProvider();
moduleInstanceMock.currentProvider.subscribe = jest.fn((type, method, parameters) => {
expect(type).toEqual(abstractSubscription.type);

Expand Down Expand Up @@ -63,8 +65,6 @@ describe('AbstractSubscriptionTest', () => {
});

it('calls subscribe and emits a error from the provider error listener', (done) => {
moduleInstanceMock.currentProvider.removeAllListeners = jest.fn();
moduleInstanceMock.currentProvider.on = jest.fn();
moduleInstanceMock.currentProvider.once = jest.fn((event, callback) => {
expect(event).toEqual('error');

Expand All @@ -83,7 +83,6 @@ describe('AbstractSubscriptionTest', () => {
});

it('calls subscribe and emits a error because of the provider subscribe method', (done) => {
moduleInstanceMock.currentProvider.removeAllListeners = jest.fn();
moduleInstanceMock.currentProvider.subscribe = jest.fn(() => {
return Promise.reject(new Error('ERROR'));
});
Expand All @@ -98,7 +97,6 @@ describe('AbstractSubscriptionTest', () => {
});

it('calls subscribe and returns a error because of the provider subscribe method', (done) => {
moduleInstanceMock.currentProvider.removeAllListeners = jest.fn();
moduleInstanceMock.currentProvider.subscribe = jest.fn(() => {
return Promise.reject(new Error('ERROR'));
});
Expand All @@ -111,8 +109,6 @@ describe('AbstractSubscriptionTest', () => {
});

it('calls subscribe and returns a error from the provider error listener', (done) => {
moduleInstanceMock.currentProvider.removeAllListeners = jest.fn();
moduleInstanceMock.currentProvider.on = jest.fn();
moduleInstanceMock.currentProvider.once = jest.fn((event, callback) => {
expect(event).toEqual('error');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {formatters} from 'web3-core-helpers';
import LogSubscription from '../../../../src/subscriptions/eth/LogSubscription';
import AbstractWeb3Module from '../../../__mocks__/AbstractWeb3Module';
import GetPastLogsMethod from '../../../__mocks__/GetPastLogsMethod';
import SocketProviderAdapter from '../../../__mocks__/SocketProviderAdapter';
import SocketProvider from '../../../__mocks__/SocketProvider';

// Mocks
jest.mock('web3-utils');
Expand All @@ -13,11 +13,11 @@ jest.mock('web3-core-helpers');
* LogSubscription test
*/
describe('LogSubscriptionTest', () => {
let logSubscription, moduleInstanceMock, getPastLogsMethodMock, socketProviderAdapterMock;
let logSubscription, moduleInstanceMock, getPastLogsMethodMock, socketProviderMock;

beforeEach(() => {
moduleInstanceMock = new AbstractWeb3Module();
socketProviderAdapterMock = new SocketProviderAdapter();
socketProviderMock = new SocketProvider();
getPastLogsMethodMock = new GetPastLogsMethod();
getPastLogsMethodMock.execute = jest.fn();

Expand Down Expand Up @@ -45,7 +45,7 @@ describe('LogSubscriptionTest', () => {

getPastLogsMethodMock.execute.mockReturnValueOnce(Promise.resolve([0]));

socketProviderAdapterMock.subscribe = jest.fn((type, method, parameters) => {
socketProviderMock.subscribe = jest.fn((type, method, parameters) => {
expect(type).toEqual('eth_subscribe');

expect(method).toEqual('logs');
Expand All @@ -55,13 +55,13 @@ describe('LogSubscriptionTest', () => {
return Promise.resolve('MY_ID');
});

socketProviderAdapterMock.on = jest.fn((subscriptionId, callback) => {
socketProviderMock.on = jest.fn((subscriptionId, callback) => {
expect(subscriptionId).toEqual('MY_ID');

callback(false, 'SUBSCRIPTION_ITEM');
});

moduleInstanceMock.currentProvider = socketProviderAdapterMock;
moduleInstanceMock.currentProvider = socketProviderMock;

let second = false;
logSubscription.options.fromBlock = 0;
Expand Down Expand Up @@ -124,7 +124,7 @@ describe('LogSubscriptionTest', () => {
it('calls subscribe and calls the callback once', (done) => {
formatters.outputLogFormatter.mockReturnValueOnce('ITEM');

socketProviderAdapterMock.subscribe = jest.fn((type, method, parameters) => {
socketProviderMock.subscribe = jest.fn((type, method, parameters) => {
expect(type).toEqual('eth_subscribe');

expect(method).toEqual('logs');
Expand All @@ -134,13 +134,13 @@ describe('LogSubscriptionTest', () => {
return Promise.resolve('MY_ID');
});

socketProviderAdapterMock.on = jest.fn((subscriptionId, callback) => {
socketProviderMock.on = jest.fn((subscriptionId, callback) => {
expect(subscriptionId).toEqual('MY_ID');

callback(false, 'SUBSCRIPTION_ITEM');
});

moduleInstanceMock.currentProvider = socketProviderAdapterMock;
moduleInstanceMock.currentProvider = socketProviderMock;

const subscription = logSubscription.subscribe((error, response) => {
expect(error).toEqual(false);
Expand All @@ -158,11 +158,11 @@ describe('LogSubscriptionTest', () => {
it('calls subscribe and it returns with an Subscription object that calls the callback with an error', (done) => {
formatters.inputLogFormatter.mockReturnValueOnce({});

socketProviderAdapterMock.subscribe = jest.fn(() => {
socketProviderMock.subscribe = jest.fn(() => {
return Promise.reject(new Error('ERROR'));
});

moduleInstanceMock.currentProvider = socketProviderAdapterMock;
moduleInstanceMock.currentProvider = socketProviderMock;

expect(
logSubscription.subscribe((error, response) => {
Expand Down

0 comments on commit b9fe454

Please sign in to comment.