From d649d2a6135e91954c175d8dde912be61dedc1d2 Mon Sep 17 00:00:00 2001 From: Rasil Baidar Date: Mon, 6 Dec 2021 16:51:57 +0545 Subject: [PATCH 1/6] list transaction test done and and index db listtx metho refactored --- src/services/db.js | 4 +++- tests/indexDbTest/db.spec.js | 24 ++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/services/db.js b/src/services/db.js index cb6aa99..8b5b152 100644 --- a/src/services/db.js +++ b/src/services/db.js @@ -141,7 +141,9 @@ const DataService = { listTx(type) { if (!type) return db.transactions.orderBy('timestamp').reverse().toArray(); - return db.transactions.get({ type }).orderBy('timestamp').reverse(); + return db.transactions.where({ type: type }).reverse().sortBy('timestamp'); + + // return db.transactions.get({ type }).orderBy('timestamp').reverse(); }, async addNft(nft) { diff --git a/tests/indexDbTest/db.spec.js b/tests/indexDbTest/db.spec.js index ab0fd0c..4182285 100644 --- a/tests/indexDbTest/db.spec.js +++ b/tests/indexDbTest/db.spec.js @@ -100,4 +100,28 @@ describe('Testing Index DB', () => { }); }); }); + + //Trasnaction + + describe('Tests major functions of index db in transaction table', () => { + const mockTrxn = { + hash: '0x4b69fb35f7f337eaef7a98cc4ad265b5f110220e82058b4a7d13c367ce07e0cf', + type: 'tokenRecieved', + timestamp: Date.now(), + amount: 10, + to: 2222, + from: '0xa1c9753e7181313585b07bcd88a64a8ebd808ed7', + status: 'success' + }; + it('adds and gets trasnaction properly', async () => { + await DataService.addTx(mockTrxn); + const savedTxn = await DataService.getTx(mockTrxn.hash); + expect(savedTxn).toMatchObject(mockTrxn); + }); + + it('lists trasnaction properly', async () => { + const txnList = await DataService.listTx(mockTrxn.type); + expect(txnList[0]).toMatchObject(mockTrxn); + }); + }); }); From be9e164047eabacda4dc90464b2d52e5bbe1884e Mon Sep 17 00:00:00 2001 From: Rasil Baidar Date: Tue, 7 Dec 2021 13:38:53 +0545 Subject: [PATCH 2/6] refactored index db addNft method, wrote test for testing index db nfts table methods --- src/services/db.js | 5 ++++- tests/indexDbTest/db.spec.js | 27 +++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/src/services/db.js b/src/services/db.js index 8b5b152..fab439c 100644 --- a/src/services/db.js +++ b/src/services/db.js @@ -60,6 +60,9 @@ const DataService = { await db.data.clear(); await db.assets.clear(); await db.documents.clear(); + await db.nfts.clear(); + await db.transactions.clear(); + await db.agencies.clear(); }, saveNetwork(network) { @@ -149,7 +152,7 @@ const DataService = { async addNft(nft) { const { tokenId } = nft; const storedNft = await this.getNft(tokenId); - if (!storedNft) return db.nfts.put({ ...nft, amount: 1 }); + if (!storedNft) return db.nfts.put({ ...nft, amount: nft.amount ? nft.amount : 1 }); storedNft.amount++; return db.nfts.put(storedNft); }, diff --git a/tests/indexDbTest/db.spec.js b/tests/indexDbTest/db.spec.js index 4182285..fc13a26 100644 --- a/tests/indexDbTest/db.spec.js +++ b/tests/indexDbTest/db.spec.js @@ -124,4 +124,31 @@ describe('Testing Index DB', () => { expect(txnList[0]).toMatchObject(mockTrxn); }); }); + + //NFT + + describe('Tests major functions of index db in nft table', () => { + const mockNft = { + name: 'RICE', + symbol: 'RIC', + amount: 3, + imageUri: 'QmRBf9ZJgynFakt19JS5Y2i4qSXjoCCpUtshFqqL9ZoDWA', + tokenId: 1, + value: 370, + description: 'Mock description', + metadataURI: 'QmPhqCqwJbDSp8GkPmmhUPA6NCzsYZ4sh2qFEnojd9SFRe' + }; + it('adds and gets nft properly', async () => { + await DataService.addNft(mockNft); + const savedNft = await DataService.getNft(mockNft.tokenId); + console.log({ savedNft }); + expect(savedNft).toMatchObject(mockNft); + }); + + it('lists nfts properly', async () => { + const nftList = await DataService.listNft(); + + expect(nftList[0]).toMatchObject(mockNft); + }); + }); }); From f9e0c38449aafc7550233aff6355270ad05c800a Mon Sep 17 00:00:00 2001 From: Rasil Baidar Date: Tue, 7 Dec 2021 15:14:26 +0545 Subject: [PATCH 3/6] wrote test for index db asset table --- tests/indexDbTest/db.spec.js | 61 +++++++++++++++++++++++++++++++++++- 1 file changed, 60 insertions(+), 1 deletion(-) diff --git a/tests/indexDbTest/db.spec.js b/tests/indexDbTest/db.spec.js index fc13a26..2380306 100644 --- a/tests/indexDbTest/db.spec.js +++ b/tests/indexDbTest/db.spec.js @@ -141,7 +141,6 @@ describe('Testing Index DB', () => { it('adds and gets nft properly', async () => { await DataService.addNft(mockNft); const savedNft = await DataService.getNft(mockNft.tokenId); - console.log({ savedNft }); expect(savedNft).toMatchObject(mockNft); }); @@ -151,4 +150,64 @@ describe('Testing Index DB', () => { expect(nftList[0]).toMatchObject(mockNft); }); }); + + //Assets + + describe('Tests major functions of index db in assests table', () => { + const mockAsset = { + address: 'default', + balance: 0, + decimal: 18, + name: 'Ether', + symbol: 'ETH' + }; + const secondaryAsset = { + address: 'secondary', + balance: 0, + decimal: 18, + name: 'Rasil', + symbol: 'RAS' + }; + it('adds and gets default assets properly', async () => { + await DataService.addDefaultAsset(mockAsset.symbol, mockAsset.name); + const savedAsset = await DataService.getAsset(mockAsset.address); + + expect(savedAsset).toMatchObject(mockAsset); + }); + it(' gets assets by symbol properly', async () => { + const savedAsset = await DataService.getAssetBySymbol(mockAsset.symbol); + + expect(savedAsset).toMatchObject(mockAsset); + }); + it(' saves multiple assets properly and lists all of them properly', async () => { + await DataService.clearAll(); + await DataService.addMultiAssets([mockAsset, secondaryAsset]); + const assetsList = await DataService.listAssets(); + expect(assetsList).toEqual( + expect.arrayContaining([ + expect.objectContaining({ + name: mockAsset.name + }) + ]) + ); + expect(assetsList).toEqual( + expect.arrayContaining([ + expect.objectContaining({ + name: secondaryAsset.name + }) + ]) + ); + }); + + it('updates assets properly', async () => { + const update = { name: 'updatedAsset' }; + await DataService.updateAsset(secondaryAsset.address, { + ...secondaryAsset, + name: update.name + }); + + const updatedAsset = await DataService.getAsset(secondaryAsset.address); + expect(updatedAsset).toMatchObject({ ...secondaryAsset, name: update.name }); + }); + }); }); From 01b8844e8876c781053d84d649de4b6a85c7586c Mon Sep 17 00:00:00 2001 From: Rasil Baidar Date: Tue, 7 Dec 2021 16:21:03 +0545 Subject: [PATCH 4/6] wrote test for document table in index db --- tests/indexDbTest/db.spec.js | 64 ++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/tests/indexDbTest/db.spec.js b/tests/indexDbTest/db.spec.js index 2380306..31146ba 100644 --- a/tests/indexDbTest/db.spec.js +++ b/tests/indexDbTest/db.spec.js @@ -210,4 +210,68 @@ describe('Testing Index DB', () => { expect(updatedAsset).toMatchObject({ ...secondaryAsset, name: update.name }); }); }); + + //Document + + describe('Tests major functions of index db in document table', () => { + const mockDocument = { + hash: 'hash1', + type: 'type', + name: 'name', + file: 'file', + encryptedFile: 'encryptedFile', + createdAt: 'createdAt', + inIpfs: 'inIpfs' + }; + const secondMockDocument = { + hash: 'hash2', + type: 'type', + name: 'name', + file: 'file', + encryptedFile: 'encryptedFile', + createdAt: 'createdAt', + inIpfs: 'inIpfs' + }; + + it('saves and gets document properly', async () => { + await DataService.saveDocuments(mockDocument); + + let document = await DataService.getDocument(mockDocument.hash); + + expect(document).toMatchObject(mockDocument); + let documentList; + + documentList = await DataService.listDocuments(); + expect(documentList).toEqual( + expect.arrayContaining([ + expect.objectContaining({ + hash: mockDocument.hash + }) + ]) + ); + + await DataService.clearAll(); + + await DataService.saveDocuments([mockDocument, secondMockDocument]); + documentList = await DataService.listDocuments(); + expect(documentList.length).toBeGreaterThan(1); + expect(documentList).toEqual( + expect.arrayContaining([ + expect.objectContaining({ + hash: secondMockDocument.hash + }) + ]) + ); + }); + + it('updates document properly', async () => { + const update = { + name: 'Edited name' + }; + await DataService.updateDocument(secondMockDocument.hash, { ...secondMockDocument, ...update }); + + const updatedDoc = await DataService.getDocument(secondMockDocument.hash); + expect(updatedDoc.name).not.toEqual(secondMockDocument.name); + }); + }); }); From 0ae1970268155c1a1ad2279fea02c0f920fbfdaf Mon Sep 17 00:00:00 2001 From: Rasil Baidar Date: Wed, 8 Dec 2021 15:38:44 +0545 Subject: [PATCH 5/6] wrote test for all the methos in db.js file --- jest.config.js | 2 +- tests/indexDbTest/db.spec.js | 154 ++++++++++++++++++++++++++++++++++- 2 files changed, 153 insertions(+), 3 deletions(-) diff --git a/jest.config.js b/jest.config.js index ea26d60..4cbe007 100644 --- a/jest.config.js +++ b/jest.config.js @@ -118,7 +118,7 @@ module.exports = { // runner: "jest-runner", // The paths to modules that run some code to configure or set up the testing environment before each test - setupFiles: ['fake-indexeddb/auto', '/jest/setEnvVars.js'], + setupFiles: ['fake-indexeddb/auto', '/jest/setEnvVars.js', '/jest/MockLocalStorage.js'], // A list of paths to modules that run some code to configure or set up the testing framework before each test // setupFilesAfterEnv: [], diff --git a/tests/indexDbTest/db.spec.js b/tests/indexDbTest/db.spec.js index 31146ba..4fa7724 100644 --- a/tests/indexDbTest/db.spec.js +++ b/tests/indexDbTest/db.spec.js @@ -1,7 +1,7 @@ import DataService from '../../src/services/db'; import 'fake-indexeddb/auto'; import 'regenerator-runtime/runtime'; - +import { NETWORKS } from '../../src/constants/networks'; describe('Testing Index DB', () => { //Data Table describe('Tests major function in indx db data table', () => { @@ -30,6 +30,116 @@ describe('Testing Index DB', () => { const initApp = await DataService.initAppData(); expect(initApp).toMatchObject({ ...data }); }); + it('gets removes data properly', async () => { + const mockData = { + name: 'testData', + data: 'Hello world' + }; + await DataService.save(mockData.name, mockData.data); + + const saveData = await DataService.get(mockData.name); + expect(saveData).toBe(mockData.data); + + await DataService.remove(mockData.name); + + const removedData = await DataService.get(mockData.name); + + expect(removedData).toBeNull(); + }); + + it('lists data table properly', async () => { + const mockData = { + name: 'Test Data', + data: { + 0: 'abx', + 1: 'qwery' + } + }; + const list = await DataService.list(); + expect(list).toEqual( + expect.arrayContaining([ + expect.objectContaining({ + name: mockData.name + }) + ]) + ); + }); + it('saves profile and gets properly', async () => { + const mockProfileData = { + name: 'Test Data', + email: 'test@gmail.com', + imageUri: 'QmRBf9ZJgynFakt19JS5Y2i4qSXjoCCpUtshFqqL9ZoDWA' + }; + await DataService.saveProfile(mockProfileData); + + const profile = await DataService.getProfile(); + expect(profile).toMatchObject(mockProfileData); + }); + + it('saves profile image properly', async () => { + const mockProfileImg = 'QmRBf9ZJgynFakt19JS5Y2i4qSXjoCCpUtshFqqL9ZoDWA'; + + await DataService.saveProfileImage(mockProfileImg); + + const profileImg = await DataService.get('profileImage'); + expect(profileImg).toEqual(mockProfileImg); + }); + + it('saves profile id card properly', async () => { + const mockProfileIdCard = 'QmRBf9ZJgynFakt19JS5Y2i4qSXjoCCpUtshFqqL9ZoDWA'; + + await DataService.saveProfileIdCard(mockProfileIdCard); + + const profileIdCard = await DataService.get('profileIdCard'); + expect(profileIdCard).toEqual(mockProfileIdCard); + }); + + it('saves and gets network properly', async () => { + // await DataService.clearAll(); + const network = NETWORKS.filter(netwrk => netwrk.name === 'rumsan'); + await DataService.saveNetwork(network); + + const savedNetwork = await DataService.getNetwork(); + expect(savedNetwork).toMatchObject(network); + }); + it('saves and gets ipfsUrl properly', async () => { + const mockUrl = process.env.REACT_APP_DEFAULT_IPFS; + const mockDownloadUrl = process.env.REACT_APP_DEFAULT_IPFS_DOWNLOAD; + await DataService.saveIpfsUrl(mockUrl); + + const saveUrl = await DataService.getIpfs(); + expect(saveUrl).toMatchObject({ ipfsUrl: mockUrl, ipfsDownloadUrl: mockDownloadUrl }); + }); + + it('saves and gets ipfsDownloadUrl properly', async () => { + const mockDownloadUrl = process.env.REACT_APP_DEFAULT_IPFS_DOWNLOAD; + await DataService.saveIpfsDownloadUrl(mockDownloadUrl); + + const savedUrl = await DataService.get('ipfsUrlDownload'); + expect(savedUrl).toEqual(mockDownloadUrl); + }); + it('saves and gets address properly', async () => { + const mockAddress = 'banepa123'; + await DataService.saveAddress(mockAddress); + + const savedAddress = await DataService.getAddress(); + expect(savedAddress).toEqual(mockAddress); + + const locallySavedAddress = DataService.getAddressFromLocal(); + expect(locallySavedAddress).toEqual(mockAddress); + }); + + it('saves wallet properly', async () => { + const mockWallet = { + address: '0xeddA7538FB64f60589605AFeFC90c510d2cAfA18', + network: 'https://testnetwork.esatya.io' + }; + + await DataService.saveWallet(mockWallet); + + const savedWallet = await DataService.getWallet(); + expect(savedWallet).toMatchObject(mockWallet); + }); }); //Agency Table @@ -52,6 +162,9 @@ describe('Testing Index DB', () => { const savedAgency = await DataService.getAgency(mockAgency.address); expect(savedAgency).toMatchObject(mockAgency); + + const defaultAgency = await DataService.getDefaultAgency(); + expect(defaultAgency).toMatchObject(mockAgency); }); it('Updates agency properly', async () => { const mockAgency = { @@ -138,13 +251,29 @@ describe('Testing Index DB', () => { description: 'Mock description', metadataURI: 'QmPhqCqwJbDSp8GkPmmhUPA6NCzsYZ4sh2qFEnojd9SFRe' }; + const secondNft = { + name: 'RICE', + symbol: 'RIC', + imageUri: 'QmRBf9ZJgynFakt19JS5Y2i4qSXjoCCpUtshFqqL9ZoDWA', + tokenId: 1, + value: 370, + description: 'Mock description', + metadataURI: 'QmPhqCqwJbDSp8GkPmmhUPA6NCzsYZ4sh2qFEnojd9SFRe' + }; it('adds and gets nft properly', async () => { await DataService.addNft(mockNft); const savedNft = await DataService.getNft(mockNft.tokenId); expect(savedNft).toMatchObject(mockNft); + await DataService.addNft(secondNft); + const savedSecondNft = await DataService.getNft(mockNft.tokenId); + + expect(savedSecondNft).toMatchObject({ ...savedNft, amount: savedNft.amount + 1 }); }); it('lists nfts properly', async () => { + await DataService.clearAll(); + + await DataService.addNft(mockNft); const nftList = await DataService.listNft(); expect(nftList[0]).toMatchObject(mockNft); @@ -168,6 +297,14 @@ describe('Testing Index DB', () => { name: 'Rasil', symbol: 'RAS' }; + const assetWithNetwork = { + address: 'third', + balance: 0, + decimal: 18, + name: 'Rasilo', + symbol: 'RASO', + network: NETWORKS[0] + }; it('adds and gets default assets properly', async () => { await DataService.addDefaultAsset(mockAsset.symbol, mockAsset.name); const savedAsset = await DataService.getAsset(mockAsset.address); @@ -178,10 +315,15 @@ describe('Testing Index DB', () => { const savedAsset = await DataService.getAssetBySymbol(mockAsset.symbol); expect(savedAsset).toMatchObject(mockAsset); + + await DataService.saveAsset(assetWithNetwork); + const savedAssetWithNetwork = await DataService.getAssetBySymbol(assetWithNetwork.symbol, NETWORKS[0].name); + + expect(savedAssetWithNetwork).toMatchObject(assetWithNetwork); }); it(' saves multiple assets properly and lists all of them properly', async () => { await DataService.clearAll(); - await DataService.addMultiAssets([mockAsset, secondaryAsset]); + await DataService.addMultiAssets([mockAsset, secondaryAsset, assetWithNetwork]); const assetsList = await DataService.listAssets(); expect(assetsList).toEqual( expect.arrayContaining([ @@ -197,6 +339,14 @@ describe('Testing Index DB', () => { }) ]) ); + const assetsListWIthNetwork = await DataService.listAssets(NETWORKS[0].name); + expect(assetsListWIthNetwork).toEqual( + expect.arrayContaining([ + expect.objectContaining({ + name: assetWithNetwork.name + }) + ]) + ); }); it('updates assets properly', async () => { From fefeed3d031e5227342be0471b1792ad5ffbadc5 Mon Sep 17 00:00:00 2001 From: Rasil Baidar Date: Wed, 8 Dec 2021 16:06:14 +0545 Subject: [PATCH 6/6] removed jest folder from gitignore, changed envVariables to dummy variables except testnetwork url --- .gitignore | 2 +- jest/MockLocalStorage.js | 19 +++++++++++++++++++ jest/setEnvVars.js | 10 +++++----- 3 files changed, 25 insertions(+), 6 deletions(-) create mode 100644 jest/MockLocalStorage.js diff --git a/.gitignore b/.gitignore index 237abca..6768015 100644 --- a/.gitignore +++ b/.gitignore @@ -5,7 +5,7 @@ config/ public/js/app yarn.lock coverage/ -jest/ +# jest/ # Logs logs diff --git a/jest/MockLocalStorage.js b/jest/MockLocalStorage.js new file mode 100644 index 0000000..3d86fb9 --- /dev/null +++ b/jest/MockLocalStorage.js @@ -0,0 +1,19 @@ +class LocalStorage { + constructor() { + this.store = {}; + } + clear() { + this.store = {}; + } + getItem(key) { + return this.store[key] || null; + } + setItem(key, value) { + this.store[key] = String(value); + } + removeItem(key) { + delete this.store[key]; + } +} + +global.localStorage = new LocalStorage(); diff --git a/jest/setEnvVars.js b/jest/setEnvVars.js index d3761e1..1e866fc 100644 --- a/jest/setEnvVars.js +++ b/jest/setEnvVars.js @@ -1,6 +1,6 @@ -process.env.REACT_APP_GOOGLE_REDIRECT_URL = 'https://wallet.dev.rumsan.net/google'; -process.env.REACT_APP_GOOGLE_CLIENT_ID = '1037282848942-iorpo75krbdj1p40rqncs1i8504lgger.apps.googleusercontent.com'; +process.env.REACT_APP_GOOGLE_REDIRECT_URL = 'https://googleRedirectUrl.com/google'; +process.env.REACT_APP_GOOGLE_CLIENT_ID = '123456789-googleClientId.apps.googleusercontent.com'; process.env.REACT_APP_DEFAULT_NETWORK = 'https://testnetwork.esatya.io'; -process.env.REACT_APP_DEFAULT_IPFS = 'https://upload.dev.rumsan.net'; -process.env.REACT_APP_DEFAULT_IPFS_DOWNLOAD = 'https://ipfs.dev.rumsan.net/ipfs'; -process.env.REACT_APP_DEFAULT_AGENCY_API = 'https://agency-nft.rahat.io/api/v1'; +process.env.REACT_APP_DEFAULT_IPFS = 'https://upload.test.rumsan.net'; +process.env.REACT_APP_DEFAULT_IPFS_DOWNLOAD = 'https://ipfs.test.rumsan.net/ipfs'; +process.env.REACT_APP_DEFAULT_AGENCY_API = 'https://testAgency-nft.rahat.io/api/v1';