Skip to content

Commit

Permalink
Fix search bar problem (#1042)
Browse files Browse the repository at this point in the history
* [explorer] task: fix jest config and missing extension

* [explorer] fix: search bug

* [explorer] task: add unit test for search bar
  • Loading branch information
AnthonyLaw committed Mar 24, 2022
1 parent 06dc320 commit 466789a
Show file tree
Hide file tree
Showing 12 changed files with 175 additions and 45 deletions.
136 changes: 136 additions & 0 deletions __tests__/store/ui.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
import Helper from '../../src/helper';
import { AccountService, MosaicService, NamespaceService } from '../../src/infrastructure';
import ui from '../../src/store/ui';
import { stub, restore } from 'sinon';

describe('store/ui', () => {
describe('action search should', () => {
let dispatch;
let rootGetters;

beforeEach(() => {
dispatch = jest.fn();
rootGetters = {};
});

afterEach(restore);

it('return block page with height', async () => {
// Arrange:
const height = '10';

// Act:
await ui.actions.search({ dispatch, rootGetters }, height);

// Assert:
expect(dispatch).toHaveBeenCalledTimes(1);
expect(dispatch).toHaveBeenNthCalledWith(1, 'openPage', {
pageName: 'block',
param: height
});
});

it('return account page with public key', async () => {
// Arrange:
const publicKey = 'DC20B243B63246C9E75E4FB5ED236513A005454393E93C8A4CE6EDEE323C2DDB';

const getAccountInfoStub = stub(AccountService, 'getAccountInfo');
getAccountInfoStub.returns(Promise.resolve({}));

// Act:
await ui.actions.search({ dispatch, rootGetters }, publicKey);

// Assert:
expect(dispatch).toHaveBeenCalledTimes(1);
expect(dispatch).toHaveBeenNthCalledWith(1, 'openPage', {
pageName: 'account',
param: publicKey
});
});

it('return account page with address', async () => {
// Arrange:
const address = 'TAR5OQBKR4KSVRVZ3ZBVHNLMBZ4N4Q27WFVMJDI';

// Act:
await ui.actions.search({ dispatch, rootGetters }, address);

// Assert:
expect(dispatch).toHaveBeenCalledTimes(1);
expect(dispatch).toHaveBeenNthCalledWith(1, 'openPage', {
pageName: 'account',
param: address
});
});

it('return transaction page with hash', async () => {
// Arrange:
const hash = '706BBC8F95AF60B22CB38911A645D3BA24DC480FDBE18C197ACCFE0FDE0DC24D';

// Act:
await ui.actions.search({ dispatch, rootGetters }, hash);

// Assert:
expect(dispatch).toHaveBeenCalledTimes(1);
expect(dispatch).toHaveBeenNthCalledWith(1, 'openPage', {
pageName: 'transaction',
param: hash
});
});

it('return mosaic page with mosaic Id', async () => {
// Arrange:
const mosaicId = '3A8416DB2D53B6C8';

const getMosaicInfoStub = stub(MosaicService, 'getMosaicInfo');
getMosaicInfoStub.returns(Promise.resolve({}));

// Act:
await ui.actions.search({ dispatch, rootGetters }, mosaicId);

// Assert:
expect(dispatch).toHaveBeenCalledTimes(1);
expect(dispatch).toHaveBeenNthCalledWith(1, 'openPage', {
pageName: 'mosaic',
param: mosaicId
});
});

it('return namespace page with namespace name', async () => {
// Arrange:
const namespaceName = 'symbol.xym';

const getNamespaceInfoStub = stub(NamespaceService, 'getNamespaceInfo');
getNamespaceInfoStub.returns(Promise.resolve({}));

// Act:
await ui.actions.search({ dispatch, rootGetters }, namespaceName);

// Assert:
expect(dispatch).toHaveBeenCalledTimes(1);
expect(dispatch).toHaveBeenNthCalledWith(1, 'openPage', {
pageName: 'namespace',
param: namespaceName
});
});

it('throw error given Nem address', async () => {
// Arrange:
const nemAddress = 'TALICE546OTUY4YJTQCQZ4HSEP4UM5Y5KRG4JHD7';

// Act + Assert:
return expect(ui.actions.search({ dispatch, rootGetters }, nemAddress))
.rejects.toThrow('errorNisAddressNotAllowed');
});

it('throw error given data not found', async () => {
// Arrange:
const randomText = 'zz';

// Act + Assert:
return expect(ui.actions.search({ dispatch, rootGetters }, randomText))
.rejects.toThrow('errorNothingFound');
});
});

});
3 changes: 1 addition & 2 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ module.exports = {
testMatch: [
'**/__tests__/**/*.spec.js'
],

transformIgnorePatterns: ['/node_modules'],
transformIgnorePatterns: ['/node_modules/(?!vue-material-design-icons|leaflet)'],

moduleNameMapper: {
'^@/(.*)$': '<rootDir>/src/$1'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
</template>

<script>
import TableView from '../tables/TableView';
import TableView from '../tables/TableView.vue';
import LockIcon from '../../styles/img/lock.png';
export default {
Expand Down
2 changes: 1 addition & 1 deletion src/components/graphics/AccountCircle.vue
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@

<script>
import GraphicComponent from './GraphicComponent.vue';
import AccountListPopover from './AccountListPopover';
import AccountListPopover from './AccountListPopover.vue';
export default {
extends: GraphicComponent,
Expand Down
2 changes: 1 addition & 1 deletion src/components/graphics/AccountRemoveCircle.vue
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@

<script>
import GraphicComponent from './GraphicComponent.vue';
import AccountListPopover from './AccountListPopover';
import AccountListPopover from './AccountListPopover.vue';
export default {
extends: GraphicComponent,
Expand Down
2 changes: 1 addition & 1 deletion src/components/graphics/NamespaceCircle.vue
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@

<script>
import GraphicComponent from './GraphicComponent.vue';
import NamespaceListPopover from './NamespaceListPopover';
import NamespaceListPopover from './NamespaceListPopover.vue';
export default {
extends: GraphicComponent,
Expand Down
2 changes: 1 addition & 1 deletion src/components/graphics/NamespaceUnlinkCircle.vue
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@

<script>
import GraphicComponent from './GraphicComponent.vue';
import NamespaceListPopover from './NamespaceListPopover';
import NamespaceListPopover from './NamespaceListPopover.vue';
export default {
extends: GraphicComponent,
Expand Down
2 changes: 1 addition & 1 deletion src/components/graphics/RestrictionMosaicListPopover.vue
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

<script>
import GraphicComponent from './GraphicComponent.vue';
import MosaicListItem from './MosaicListItem';
import MosaicListItem from './MosaicListItem.vue';
export default {
extends: GraphicComponent,
Expand Down
2 changes: 1 addition & 1 deletion src/components/tables/TableListView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ import RestrictionField from '@/components/fields/RestrictionField.vue';
import Loading from '@/components/Loading.vue';
import BlockHeightWithFinalizedStatusField from '@/components/fields/BlockHeightWithFinalizedStatusField.vue';
import ExtendGraphicValueField from '@/components/fields/ExtendGraphicValueField.vue';
import ChainInfo from '@/components/fields/ChainInfo';
import ChainInfo from '@/components/fields/ChainInfo.vue';
import DateField from '@/components/fields/DateField.vue';
import SoftwareVersion from '@/components/fields/SoftwareVersion.vue';
import Harvester from '@/components/fields/Harvester.vue';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
<script>
import GraphicComponent from '../graphics/GraphicComponent.vue';
import AccountIcon from '../graphics/AccountIcon.vue';
import RestrictionOperationCircle from '../graphics/RestrictionOperationCircle';
import RestrictionOperationCircle from '../graphics/RestrictionOperationCircle.vue';
import Arrow from '../graphics/Arrow.vue';
import { TransactionType } from 'symbol-sdk';
Expand Down
2 changes: 1 addition & 1 deletion src/components/transaction-graphic/VrfKeyGraphic.vue
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
<script>
import GraphicComponent from '../graphics/GraphicComponent.vue';
import AccountIcon from '../graphics/AccountIcon.vue';
import KeyCircle from '../graphics/KeyCircle';
import KeyCircle from '../graphics/KeyCircle.vue';
import KeyUnlinkCircle from '../graphics/KeyUnlinkCircle.vue';
import Arrow from '../graphics/Arrow.vue';
import { TransactionType } from 'symbol-sdk';
Expand Down
63 changes: 29 additions & 34 deletions src/store/ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@
import { i18n, keyRedirects } from '../config';
import helper from '../helper';
import { NamespaceService, MosaicService, AccountService } from '../infrastructure';
import http from '../infrastructure/http';
import router from '../router';
import { Address, AccountHttp } from 'symbol-sdk';
import { Address } from 'symbol-sdk';
import Vue from 'vue';

export default {
Expand Down Expand Up @@ -101,54 +102,47 @@ export default {

search: ({ dispatch, rootGetters }, _searchString) => {
return new Promise(async (resolve, reject) => {
if (null !== searchString && '' !== searchString) {
if (null !== _searchString && '' !== _searchString) {
const searchString = _searchString.replace(/\s|-/g, '');
if (helper.isBlockHeight(searchString)) {
dispatch('openPage', {
pageName: 'block',
param: searchString
});
resolve();
} else
if (helper.isAccountPublicKey(searchString)) {
// check the string is a public key of an account

const api = rootGetters['api/currentNode'];

let accountHttp = new AccountHttp(api);
}

let accountAddress;
if (helper.isAccountPublicKey(searchString)) {
// Can be public key or transaction hash
try {
const address = Address.createFromPublicKey(searchString, http.networkType).plain();
const accountInfo = await AccountService.getAccountInfo(address);

let accountInfo;
if (accountInfo) {
dispatch('openPage', {
pageName: 'account',
param: searchString
});

try {
accountInfo = await accountHttp
.getAccountInfo(new Address(searchString))
.toPromise();
accountAddress = accountInfo.address.address;
} catch (e) { }
if (accountAddress) {
dispatch('openPage', {
pageName: 'account',
param: accountAddress
});
resolve();
} else {
// transaction hash
resolve();
}
} catch (e) {
dispatch('openPage', {
pageName: 'transaction',
param: searchString
});
resolve();
}
} else
}

if (helper.isAccountAddress(searchString)) {
dispatch('openPage', {
pageName: 'account',
param: searchString
});
resolve();
} else
}

if (helper.isMosaicOrNamespaceId(searchString)) {
let result = void 0;

Expand All @@ -173,15 +167,16 @@ export default {
});
resolve();
}
} catch (e) {
const isNisAddress = await AccountService.checkNis1Account(searchString);
} catch (e) {}
}

if (isNisAddress)
reject(new Error('errorNisAddressNotAllowed'));
const isNisAddress = await AccountService.checkNis1Account(searchString);

if (isNisAddress)
reject(new Error('errorNisAddressNotAllowed'));

reject(new Error('errorNothingFound'));

reject(new Error('errorNothingFound'));
}
}
} else { reject(new Error('errorNothingFound')); }
});
},
Expand Down

0 comments on commit 466789a

Please sign in to comment.