Skip to content

Commit

Permalink
chore: add TypeScript use test
Browse files Browse the repository at this point in the history
  • Loading branch information
rvagg committed Apr 10, 2021
1 parent 5528bcd commit f1eea96
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 5 deletions.
11 changes: 6 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,18 @@
"types": "./types/index.d.ts",
"type": "module",
"scripts": {
"lint": "standard",
"lint": "standard *.js test/*.js",
"build": "npm run build:js && npm run build:types",
"build:js": "npm_config_yes=true ipjs build --tests --main && npm run build:copy",
"build:copy": "cp -a tsconfig.json index.js test/ dist/",
"build:copy": "cp -a tsconfig.json index.js dist/ && mkdir -p dist/test && cp test/*.js dist/test/",
"build:types": "npm run build:copy && cd dist && tsc --build",
"check": "tsc --build --noErrorTruncation",
"publish": "npm_config_yes=true ipjs publish",
"test:cjs": "npm run build && mocha dist/cjs/node-test/test-*.js",
"test:cjs": "npm run build && mocha dist/cjs/node-test/test-*.js && npm run test:cjs:browser",
"test:node": "hundreds mocha test/test-*.js",
"test:browser": "polendina --cleanup dist/cjs/node-test/test-*.js",
"test": "npm run lint && npm run test:node && npm run test:cjs && npm run test:browser",
"test:cjs:browser": "polendina --cleanup dist/cjs/node-test/test-*.js",
"test:ts": "npm run build:types && npm run test --prefix test/ts-use",
"test": "npm run lint && npm run test:node && npm run test:cjs && npm run test:ts",
"coverage": "c8 --reporter=html mocha test/test-*.js && npx st -d coverage -p 8080"
},
"exports": {
Expand Down
3 changes: 3 additions & 0 deletions test/ts-use/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
src/main.js
tsconfig.tsbuildinfo
11 changes: 11 additions & 0 deletions test/ts-use/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "ts-use",
"private": true,
"dependencies": {
"@ipld/dag-cbor": "file:../../dist/",
"multiformats": "file:../../node_modules/multiformats"
},
"scripts": {
"test": "npm install && npx -p typescript tsc && node src/main.js"
}
}
51 changes: 51 additions & 0 deletions test/ts-use/src/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { deepStrictEqual } from 'assert'

import { BlockEncoder, BlockDecoder, BlockCodec, CodecFeature } from 'multiformats/codecs/interface'
import * as dagCbor from '@ipld/dag-cbor'

const main = () => {
// make sure we have a full CodecFeature
useCodecFeature(dagCbor)
}

function useCodecFeature (codec: CodecFeature<0x71, any>) {
// use only as a BlockEncoder
const { encoder } = codec
useEncoder(encoder)

// use only as a BlockDecoder
const { decoder } = codec
useDecoder(decoder)

// use as a full BlockCodec which does both BlockEncoder & BlockDecoder
useBlockCodec(codec)

// use BlockEncoder & BlockDecoder as a full BlockCodec
// (demonstrates and tests BlockEncoder & BlockDecoder composability)
useBlockCodec(Object.assign({}, encoder, decoder))
}

function useEncoder<Codec extends number> (encoder: BlockEncoder<Codec, string>) {
deepStrictEqual(encoder.code, 0x71)
deepStrictEqual(encoder.name, 'dag-cbor')
deepStrictEqual(Array.from(encoder.encode('blip')), [100, 98, 108, 105, 112])
console.log('[TS] ✓ { encoder: BlockEncoder }')
}

function useDecoder<Codec extends number> (decoder: BlockDecoder<Codec, Uint8Array>) {
deepStrictEqual(decoder.code, 0x71)
deepStrictEqual(decoder.decode(Uint8Array.from([100, 98, 108, 105, 112])), 'blip')
console.log('[TS] ✓ { decoder: BlockDecoder }')
}

function useBlockCodec<Codec extends number> (blockCodec: BlockCodec<Codec, string>) {
deepStrictEqual(blockCodec.code, 0x71)
deepStrictEqual(blockCodec.name, 'dag-cbor')
deepStrictEqual(Array.from(blockCodec.encode('blip')), [100, 98, 108, 105, 112])
deepStrictEqual(blockCodec.decode(Uint8Array.from([100, 98, 108, 105, 112])), 'blip')
console.log('[TS] ✓ {}:BlockCodec')
}

main()

export default main
9 changes: 9 additions & 0 deletions test/ts-use/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"compilerOptions": {
"strict": true,
"moduleResolution": "node",
"noImplicitAny": true,
"skipLibCheck": true,
"incremental": true
}
}

0 comments on commit f1eea96

Please sign in to comment.