Skip to content

Commit cc1c34d

Browse files
olizillaagentofuser
authored andcommitted
feat: add example ava test
License: MIT Signed-off-by: Oli Evans <oli@tableflip.io>
1 parent b4c181f commit cc1c34d

File tree

5 files changed

+2878
-1720
lines changed

5 files changed

+2878
-1720
lines changed

index.js

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,9 @@ const _ = require('lodash')
1919
const fp = require('lodash/fp')
2020
const neatFrame = require('neat-frame')
2121
const { stripIndent } = require('common-tags')
22+
const httpGatewayUrl = require('./src/gateway')
2223

2324
// # Pure functions
24-
function httpGatewayUrl(hash, gatewayProvider = 'ipfs') {
25-
const gateways = {
26-
ipfs: 'https://ipfs.io',
27-
infura: 'https://ipfs.infura.io',
28-
pinata: 'https://gateway.pinata.cloud',
29-
}
30-
const origin = gateways[gatewayProvider] || gateways['ipfs']
31-
return `${origin}/ipfs/${hash}`
32-
}
3325

3426
function formatError(e) {
3527
const prettierJson = obj =>

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
"@semantic-release/npm": "^5.1.7",
6565
"@semantic-release/release-notes-generator": "^7.1.7",
6666
"all-contributors-cli": "^6.4.0",
67+
"ava": "^1.4.1",
6768
"eslint": "^6.0.0-alpha.0",
6869
"eslint-config-prettier": "^4.2.0",
6970
"eslint-plugin-import": "^2.17.2",
@@ -76,7 +77,7 @@
7677
},
7778
"scripts": {
7879
"format": "npx prettier --write ./**/*.{js,json,md,mdx,html,css}",
79-
"test": "echo \"Error: no test specified\" && exit 1",
80+
"test": "ava",
8081
"commit": "npx sgc",
8182
"commit:retry": "npx sgc --retry",
8283
"semantic-release": "./semantic-release-dry-run.bash",

src/gateway.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
function getGatewayUrl(cid, gatewayProvider = 'ipfs') {
2+
if (!cid) {
3+
throw new Error('getGatewayUrl expects to be called with a valud IPFS CID')
4+
}
5+
const gateways = {
6+
ipfs: 'https://ipfs.io',
7+
infura: 'https://ipfs.infura.io',
8+
pinata: 'https://gateway.pinata.cloud',
9+
}
10+
const origin = gateways[gatewayProvider] || gateways['ipfs']
11+
return `${origin}/ipfs/${cid}`
12+
}
13+
14+
module.exports = getGatewayUrl

test/gateway.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
const test = require('ava')
2+
const httpGatewayUrl = require('../src/gateway')
3+
4+
test('get http gateway url for a cid', t => {
5+
const expected = 'https://ipfs.io/ipfs/fakecid'
6+
const actual = httpGatewayUrl('fakecid')
7+
t.is(actual, expected)
8+
})
9+
10+
test('get http gateway url for a cid on infura', t => {
11+
const expected = 'https://ipfs.infura.io/ipfs/fakecid'
12+
const actual = httpGatewayUrl('fakecid', 'infura')
13+
t.is(actual, expected)
14+
})
15+
16+
test('get http gateway url for a cid on pinata', t => {
17+
const expected = 'https://gateway.pinata.cloud/ipfs/fakecid'
18+
const actual = httpGatewayUrl('fakecid', 'pinata')
19+
t.is(actual, expected)
20+
})
21+
22+
test('throw if no cid', t => {
23+
t.throws(() => {
24+
httpGatewayUrl()
25+
})
26+
})

0 commit comments

Comments
 (0)