Skip to content
This repository has been archived by the owner on Jun 29, 2020. It is now read-only.

Commit

Permalink
Fix frontend tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fvictorio committed Apr 23, 2018
1 parent cdf639f commit 86c2c95
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 77 deletions.
169 changes: 96 additions & 73 deletions web-dapp/src/components/ConfirmationPage.test.js
Expand Up @@ -2,8 +2,14 @@ import React from 'react';
import { mount } from 'enzyme';

import ConfirmationPage from './ConfirmationPage';

const web3 = { eth: { accounts: ['0x1aa2d288d03d8397c193d2327ee7a7443d4ec3a1'] } };
import * as waitForTransaction from '../waitForTransaction';

const web3 = {
eth: {
accounts: ['0x1aa2d288d03d8397c193d2327ee7a7443d4ec3a1'],
getTransactionReceipt: jest.fn()
}
};
const contract = require('../ProofOfPhysicalAddress.json');

window.$ = { ajax: jest.fn() };
Expand Down Expand Up @@ -348,7 +354,7 @@ describe('<ConfirmationPage />', () => {
);
});

it('displays a message if there was an error confirming address', () => {
it('displays a message if there was an error confirming address', (done) => {
const page = mount(
<ConfirmationPage
my_web3={web3}
Expand All @@ -368,24 +374,27 @@ describe('<ConfirmationPage />', () => {
return callback(null, { found: true, ...addressDetails });
});

confirmAddress.mockImplementationOnce((opts, callback) => {
return callback({ message: 'fake confirmation error' });
confirmAddress.mockImplementationOnce((opts) => {
return Promise.reject({ message: 'fake confirmation error' });
});

page.find('.postcard-button').simulate('click');

expect(checkUserExists).toHaveBeenCalled();
expect(checkWalletSame).toHaveBeenCalled();
expect(ajaxCall).toHaveBeenCalled();
expect(findAddress).toHaveBeenCalled();
expect(showAlert).toHaveBeenLastCalledWith(
'error',
'Confirming address',
[['Error', 'fake confirmation error']]
);
setTimeout(() => {
expect(checkUserExists).toHaveBeenCalled();
expect(checkWalletSame).toHaveBeenCalled();
expect(ajaxCall).toHaveBeenCalled();
expect(findAddress).toHaveBeenCalled();
expect(showAlert).toHaveBeenLastCalledWith(
'error',
'Confirming address',
[['Error', 'fake confirmation error']]
);
done();
})
});

it('displays a message when received an unexpected JSON RPC response', () => {
it('displays a message when received an unexpected JSON RPC response', (done) => {
const page = mount(
<ConfirmationPage
my_web3={web3}
Expand All @@ -404,23 +413,26 @@ describe('<ConfirmationPage />', () => {
});

confirmAddress.mockImplementationOnce((opts, callback) => {
return callback();
return Promise.resolve();
});

page.find('.postcard-button').simulate('click');

expect(checkUserExists).toHaveBeenCalled();
expect(checkWalletSame).toHaveBeenCalled();
expect(ajaxCall).toHaveBeenCalled();
expect(findAddress).toHaveBeenCalled();
expect(showAlert).toHaveBeenLastCalledWith(
'error',
'Confirming address',
'Error is empty but txId is also empty'
);
setTimeout(() => {
expect(checkUserExists).toHaveBeenCalled();
expect(checkWalletSame).toHaveBeenCalled();
expect(ajaxCall).toHaveBeenCalled();
expect(findAddress).toHaveBeenCalled();
expect(showAlert).toHaveBeenLastCalledWith(
'error',
'Confirming address',
'Error is empty but txId is also empty'
);
done();
})
});

it('displays a message if there was an error estimating gas for transaction', () => {
it('displays a message if there was an error estimating gas for transaction', (done) => {
const page = mount(
<ConfirmationPage
my_web3={web3}
Expand Down Expand Up @@ -456,16 +468,19 @@ describe('<ConfirmationPage />', () => {

page.find('.postcard-button').simulate('click');

expect(checkUserExists).toHaveBeenCalled();
expect(checkWalletSame).toHaveBeenCalled();
expect(ajaxCall).toHaveBeenCalled();
expect(findAddress).toHaveBeenCalled();
expect(showAlert).toHaveBeenLastCalledWith(
'error',
'Confirming address',
[['Error', 'fake error']]
);
});
setTimeout(() => {
expect(checkUserExists).toHaveBeenCalled();
expect(checkWalletSame).toHaveBeenCalled();
expect(ajaxCall).toHaveBeenCalled();
expect(findAddress).toHaveBeenCalled();
expect(showAlert).toHaveBeenLastCalledWith(
'error',
'Confirming address',
[['Error', 'fake error']]
);
done();
})
})

it('displays a message when a address was confirmed successfully', () => {
const contract = {
Expand Down Expand Up @@ -504,26 +519,29 @@ describe('<ConfirmationPage />', () => {

page.find('.postcard-button').simulate('click');

expect(checkUserExists).toHaveBeenCalled();
expect(checkWalletSame).toHaveBeenCalled();
expect(ajaxCall).toHaveBeenCalled();
expect(findAddress).toHaveBeenCalled();
expect(showAlert).toHaveBeenLastCalledWith(
'success',
'Address confirmed!',
[
['Transaction to confirm address was submitted'],
['Transaction ID', '0xfd3c97d14b3979cc6356a92b79b3ac8038f0065fc5079c6a0a0ff9b0c0786291'],
['Country', 'US'],
['State', 'NM'],
['City', 'ALBUQUERQUE'],
['Address', '3828 PIERMONT DR NE'],
['ZIP code', '87111']
]
);
setTimeout(() => {
expect(checkUserExists).toHaveBeenCalled();
expect(checkWalletSame).toHaveBeenCalled();
expect(ajaxCall).toHaveBeenCalled();
expect(findAddress).toHaveBeenCalled();
expect(showAlert).toHaveBeenLastCalledWith(
'success',
'Address confirmed!',
[
['Transaction to confirm address was submitted'],
['Transaction ID', '0xfd3c97d14b3979cc6356a92b79b3ac8038f0065fc5079c6a0a0ff9b0c0786291'],
['Country', 'US'],
['State', 'NM'],
['City', 'ALBUQUERQUE'],
['Address', '3828 PIERMONT DR NE'],
['ZIP code', '87111']
]
);
done();
})
});

it('displays a message when a address was confirmed successfully', () => {
it('displays a message when a address was confirmed successfully', (done) => {
const page = mount(
<ConfirmationPage
my_web3={web3}
Expand All @@ -541,28 +559,33 @@ describe('<ConfirmationPage />', () => {
return callback(null, { found: true, ...addressDetails });
});

confirmAddress.mockImplementationOnce((opts, callback) => {
return callback(null, '0xfd3c97d14b3979cc6356a92b79b3ac8038f0065fc5079c6a0a0ff9b0c0786291');
confirmAddress.mockImplementationOnce((opts) => {
return Promise.resolve('0xfd3c97d14b3979cc6356a92b79b3ac8038f0065fc5079c6a0a0ff9b0c0786291');
});

waitForTransaction.default = jest.fn().mockImplementationOnce(() => Promise.resolve())

page.find('.postcard-button').simulate('click');

expect(checkUserExists).toHaveBeenCalled();
expect(checkWalletSame).toHaveBeenCalled();
expect(ajaxCall).toHaveBeenCalled();
expect(findAddress).toHaveBeenCalled();
expect(showAlert).toHaveBeenLastCalledWith(
'success',
'Address confirmed!',
[
['Transaction to confirm address was submitted'],
['Transaction ID', '0xfd3c97d14b3979cc6356a92b79b3ac8038f0065fc5079c6a0a0ff9b0c0786291'],
['Country', 'US'],
['State', 'NM'],
['City', 'ALBUQUERQUE'],
['Address', '3828 PIERMONT DR NE'],
['ZIP code', '87111']
]
);
setTimeout(() => {
expect(checkUserExists).toHaveBeenCalled();
expect(checkWalletSame).toHaveBeenCalled();
expect(ajaxCall).toHaveBeenCalled();
expect(findAddress).toHaveBeenCalled();
expect(showAlert).toHaveBeenLastCalledWith(
'success',
'Address confirmed!',
[
['Transaction to confirm address was submitted'],
['Transaction ID', '0xfd3c97d14b3979cc6356a92b79b3ac8038f0065fc5079c6a0a0ff9b0c0786291'],
['Country', 'US'],
['State', 'NM'],
['City', 'ALBUQUERQUE'],
['Address', '3828 PIERMONT DR NE'],
['ZIP code', '87111']
]
);
done();
})
});
});
14 changes: 10 additions & 4 deletions web-dapp/src/components/MyAddressesPage.test.js
Expand Up @@ -2,6 +2,7 @@ import React from 'react';
import { mount } from 'enzyme';

import MyAddressesPage from './MyAddressesPage';
import * as waitForTransaction from '../waitForTransaction';

const web3 = { eth: { accounts: ['0x1aa2d288d03d8397c193d2327ee7a7443d4ec3a1'] } };

Expand All @@ -24,18 +25,23 @@ describe('<MyAddressesPage />', () => {
expect(page.state('addresses').length).toBe(2)
})

it('should reload page after removing an address', () => {
it('should reload page after removing an address', (done) => {
const reloadSpy = jest.spyOn(window.location, 'reload').mockImplementation(() => {})
const contractMock = buildContractMock({ count: 2 })
const page = mount(<MyAddressesPage my_web3={web3} contract={contractMock}/>);

waitForTransaction.default = jest.fn().mockImplementationOnce(() => Promise.resolve())

const e = { preventDefault: jest.fn() }
page.instance().remove(e, 'country', 'state', 'city', 'location', 'zip')

expect(reloadSpy).toHaveBeenCalled()
setTimeout(() => {
expect(reloadSpy).toHaveBeenCalled()

reloadSpy.mockReset()
reloadSpy.mockRestore()
reloadSpy.mockReset()
reloadSpy.mockRestore()
done();
})
})
});

Expand Down

0 comments on commit 86c2c95

Please sign in to comment.