Skip to content

Commit

Permalink
Merge pull request #476 from Bit-Nation/feature/dapp-components
Browse files Browse the repository at this point in the history
Feature/dapp components
  • Loading branch information
seland committed Aug 17, 2018
2 parents a967bed + d21a9d7 commit 85e8797
Show file tree
Hide file tree
Showing 84 changed files with 2,351 additions and 8,438 deletions.
256 changes: 256 additions & 0 deletions __tests__/src/DAppsSDK/0.0.1/utils/renderer.js
@@ -0,0 +1,256 @@
import React from 'react';

import { getTypeElementFromText, renderJSON, validateProps } from '../../../../../src/DAppsSDK/0.0.1/utils/renderer';
import View from '../../../../../src/DAppsSDK/0.0.1/components/View';
import Text from '../../../../../src/DAppsSDK/0.0.1/components/Text';
import TextInput from '../../../../../src/DAppsSDK/0.0.1/components/TextInput';
import Button from '../../../../../src/DAppsSDK/0.0.1/components/Button';

React.createElement = jest.fn().mockImplementation((component, props, children) => ({
component,
props,
children,
}));

test('getTypeElementFromText', () => {
expect(getTypeElementFromText('text')).toEqual(Text);
expect(getTypeElementFromText('view')).toEqual(View);
expect(getTypeElementFromText('textInput')).toEqual(TextInput);
expect(getTypeElementFromText('button')).toEqual(Button);
expect(getTypeElementFromText('something unknown')).toBeUndefined();
});

test('validateProps', () => {
expect(validateProps(
{
allowedNativeProp1: 'ALLOWED_NATIVE_PROP_1',
allowedNativeProp2: 'ALLOWED_NATIVE_PROP_2',
disallowedNativeProp3: 'DISALLOWED_NATIVE_PROP_3',
someCustomProp1: 'SOME_CUSTOM_PROP_1',
someCustomProp2: 'SOME_CUSTOM_PROP_2',
badCustomProp: 'BAD_CUSTOM_PROP',
onPress: 'ON_PRESS',
badCallback: 'BAD_CALLBACK',
}, {
native: ['allowedNativeProp1', 'allowedNativeProp2'],
custom: ['someCustomProp1', 'someCustomProp2'],
callbacks: ['onPress'],
},
'COMPONENT_TYPE',
)).toEqual({
nativeProps: {
allowedNativeProp1: 'ALLOWED_NATIVE_PROP_1',
allowedNativeProp2: 'ALLOWED_NATIVE_PROP_2',
},
customProps: {
someCustomProp1: 'SOME_CUSTOM_PROP_1',
someCustomProp2: 'SOME_CUSTOM_PROP_2',
},
callbackProps: {
onPress: 'ON_PRESS',
},
});
});

describe('renderJSON', () => {
test('single view', () => {
expect(renderJSON({
type: 'view',
props: {},
children: null,
}, undefined, () => ({}))).toEqual({
component: View,
props: { nativeProps: {} },
children: null,
});
});

test('unknown component', () => {
expect(renderJSON({ type: 'Something unknown' }, undefined, () => ({}))).toBeNull();
});

test('string literal outside text', () => {
expect(renderJSON({ type: 'view', children: ['Something unknown'], props: {} }, undefined, () => ({}))).toEqual({
component: View,
props: { nativeProps: {} },
children: [null],
});

expect(renderJSON({ type: 'view', children: 'Something unknown', props: {} }, undefined, () => ({}))).toEqual({
component: View,
props: { nativeProps: {} },
children: null,
});
});

test('string literal inside text', () => {
expect(renderJSON({ type: 'text', children: 'Something unknown', props: {} }, undefined, () => ({}))).toEqual({
component: Text,
props: { nativeProps: {} },
children: 'Something unknown',
});
});

test('number literal inside text', () => {
expect(renderJSON({ type: 'text', children: 5, props: {} }, undefined, () => ({}))).toEqual({
component: Text,
props: { nativeProps: {} },
children: 5,
});
});

test('number literal outside text', () => {
expect(renderJSON({ type: 'view', children: 5, props: {} }, undefined, () => ({}))).toEqual({
component: View,
props: { nativeProps: {} },
children: null,
});
});

test('complex JSON', () => {
const json = {
type: 'view',
props: { style: { backgroundColor: 'yellow', flex: 1 } },
children: [
{
type: 'text',
props: { style: { color: 'red' } },
children: [
'Red text',
],
},
{
type: 'view',
props: { style: { backgroundColor: 'blue' } },
children: [
{
type: 'text',
props: { style: { color: 'white' } },
children: [{
type: 'text',
props: { style: { color: 'red' } },
children: [
'White bold text',
],
},
'And text',
],
},
],
},
{
type: 'textInput',
props: {
style: { width: 200, height: 50 },
onEndEditing: 1,
},
},
{
type: 'textInput',
props: { style: { width: 200, height: 50 } },
},
{
type: 'button',
props: {
style: { width: 100, height: 50, backgroundColor: 'green' },
title: 'Hey',
onPress: 2,
},
},
],
};

const customPropsProvider = (component, ownProps) => ({
ownPropsCount: Object.keys(ownProps).length,
});

expect(renderJSON(json, undefined, customPropsProvider)).toEqual({
component: View,
props: {
nativeProps: { style: { backgroundColor: 'yellow', flex: 1 } },
key: undefined,
ownPropsCount: 1,
},
children: [
{
component: Text,
props: {
nativeProps: {},
style: { color: 'red' },
key: '0',
ownPropsCount: 1,
},
children: [
'Red text',
],
},
{
component: View,
props: {
nativeProps: { style: { backgroundColor: 'blue' } },
key: '1',
ownPropsCount: 1,
},
children: [
{
component: Text,
props: {
nativeProps: {},
style: { color: 'white' },
key: '0',
ownPropsCount: 1,
},
children: [{
component: Text,
props: {
nativeProps: {},
style: { color: 'red' },
key: '0',
ownPropsCount: 1,
},
children: [
'White bold text',
],
},
'And text',
],
},
],
},
{
children: null,
component: TextInput,
props: {
nativeProps: {
style: { width: 200, height: 50 },
},
key: '2',
ownPropsCount: 2,
},
},
{
children: null,
component: TextInput,
props: {
nativeProps: {
style: { width: 200, height: 50 },
},
key: '3',
ownPropsCount: 1,
},
},
{
children: null,
component: Button,
props: {
nativeProps: {},
style: { width: 100, height: 50, backgroundColor: 'green' },
title: 'Hey',
key: '4',
ownPropsCount: 3,
},
},
],
});
});
});
6 changes: 3 additions & 3 deletions __tests__/src/sagas/activity/sagas.js
Expand Up @@ -26,7 +26,7 @@ describe('onCurrentAccountChange', () => {
version: 3,
display: true,
interpret: true,
created_at: new Date(),
createdAt: new Date(),
};

const gen = onCurrentAccountChange([mockMessage]);
Expand Down Expand Up @@ -64,7 +64,7 @@ describe('addNewMessageSaga', () => {
const mockAction = {
type: ADD_NEW_MESSAGE,
message: 'Test Message',
params: '',
params: {},
interpret: true,
callback: jest.fn(),
};
Expand All @@ -82,7 +82,7 @@ describe('addNewMessageSaga', () => {

const mockMessage = buildMessageObject(1, testAccountId, mockAction.message, mockAction.params, mockAction.interpret);
const convertedMessage = convertToDatabase(mockMessage);
delete convertedMessage.created_at;
delete convertedMessage.createdAt;
delete convertedMessage.params;
expect(realm.objects('MessageJob').filtered(`accountId == '${testAccountId}'`)[0])
.toMatchObject({
Expand Down
3 changes: 2 additions & 1 deletion __tests__/src/sagas/index.js
Expand Up @@ -13,9 +13,9 @@ import serviceContainer from '../../../src/sagas/serviceContainer';
import modifyNation from '../../../src/sagas/modifyNation';
import nations from '../../../src/sagas/nations';
import txProcessor from '../../../src/sagas/txProcessor';
import dApps from '../../../src/sagas/nativeDApps';
import chat from '../../../src/sagas/chat';
import migration from '../../../src/sagas/migration';
import dApps from '../../../src/sagas/dApps';
import upstream from '../../../src/sagas/upstream';

test('rootSaga', () => {
Expand All @@ -32,6 +32,7 @@ test('rootSaga', () => {
call(modifyNation),
call(nations),
call(txProcessor),
call(migration),
call(dApps),
call(migration),
call(upstream),
Expand Down
2 changes: 0 additions & 2 deletions __tests__/src/screens/Dashboard/__snapshots__/index.js.snap
Expand Up @@ -55,7 +55,6 @@ ShallowWrapper {
},
"onAddDummyMessage": [Function],
"onSelectNation": [Function],
"openDApp": [Function],
"store": Object {
"clearActions": [Function],
"dispatch": [Function],
Expand Down Expand Up @@ -119,7 +118,6 @@ ShallowWrapper {
},
"onAddDummyMessage": [Function],
"onSelectNation": [Function],
"openDApp": [Function],
"store": Object {
"clearActions": [Function],
"dispatch": [Function],
Expand Down
31 changes: 25 additions & 6 deletions __tests__/src/services/database/index.js
Expand Up @@ -42,23 +42,42 @@ describe('db', () => {
realm2.close();
});

test('schema v3 - v4', async () => {
expect.assertions(4);
test('schema v3 - v6', async () => {
expect.assertions(5);
const dbPath = randomDbPath();
const databaseGenerator = factory(dbPath, 3);

const realm3 = await databaseGenerator.next().value;
const realm3: Realm = await databaseGenerator.next().value;
expect(Realm.schemaVersion(dbPath)).toBe(3);

const realm4 = await databaseGenerator.next(realm3).value;
const realm4: Realm = await databaseGenerator.next(realm3).value;
expect(Realm.schemaVersion(dbPath)).toBe(4);

const realm5 = await databaseGenerator.next(realm4).value;
const realm5: Realm = await databaseGenerator.next(realm4).value;
expect(Realm.schemaVersion(dbPath)).toBe(5);

const realm6 = await databaseGenerator.next(realm5).value;
// Add profile to DB
realm5.write(() => {
realm5.create('Profile', {
name: 'name',
location: 'location',
image: 'image',
identity_pub_key: 'identity_pub_key',
ethereum_pub_Key: 'ethereum_pub_Key',
chat_id_key: 'chat_id_key',
timestamp: new Date(),
version: 0,
identity_key_signature: 'identity_key_signature',
ethereum_key_signature: 'ethereum_key_signature',
});
});

const realm6: Realm = await databaseGenerator.next(realm5).value;
expect(Realm.schemaVersion(dbPath)).toBe(6);

// Check that profile was deleted correctly.
expect(realm6.objects('Profile')).toHaveLength(0);

realm6.close();
});
});
Expand Down
15 changes: 0 additions & 15 deletions __tests__/src/services/ethereum/index.js
Expand Up @@ -5,7 +5,6 @@
import ethers from 'ethers';
import providers from 'ethers/providers';
import Ethereum from '../../../../src/services/ethereum/index';
import ContractInfo from '../../../../src/dapps/escrow/ERC20TokenEscrow.json';

describe('ethereum', () => {
let ethereum;
Expand Down Expand Up @@ -41,18 +40,4 @@ describe('ethereum', () => {
const numCitizens = await nationsObject.getNumCitizens(0);
expect(numCitizens.toNumber()).toEqual(0);
});
test('Create contract test', async () => {
const wallet = new ethers.Wallet('0xefc27ba5330258fcfb75e28e4e6efd88458751086998bbfad99257035fb3e160');
wallet.provider = new providers.InfuraProvider('rinkeby');
ethereum = new Ethereum(wallet, 'dev');
const txReceipt = await ethereum.deployContract(
ContractInfo.bytecode,
ContractInfo.abi,
'0',
'0xC3830A6206fB9d089D1ce824598978532D14d8Aa',
'0',
'0',
'0xcd4dd4fd12acD06fD49509516Bb136A0B496d451',
);
});
});

0 comments on commit 85e8797

Please sign in to comment.