Skip to content

Commit

Permalink
chore(deps): update dependency @types/jest to v26 (#51)
Browse files Browse the repository at this point in the history
* chore(deps): update dependency @types/jest to v26

* test(fix): fix jest types & formatting

Co-authored-by: Renovate Bot <bot@renovateapp.com>
Co-authored-by: Mircea Nistor <mirceanis@gmail.com>
  • Loading branch information
3 people authored Jul 21, 2020
1 parent eafefd1 commit 0e4ef57
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 26 deletions.
3 changes: 2 additions & 1 deletion .prettierrc
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
"tabWidth": 2,
"printWidth": 80,
"singleQuote": true,
"semi": false
"semi": false,
"trailingComma": "none"
}
112 changes: 108 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"test": "jest",
"test-with-coverage": "jest --coverage && codecov",
"dev": "tsc --watch",
"format": "prettier --write \"src/**/*.ts\" \"src/**/*.js\"",
"format": "prettier --write \"src/**/*.ts\"",
"lint": "tslint -p tsconfig.json",
"prepare": "npm run build",
"prepublishOnly": "npm test && npm run lint",
Expand All @@ -27,7 +27,7 @@
"@commitlint/config-conventional": "9.1.1",
"@semantic-release/changelog": "5.0.1",
"@semantic-release/git": "9.0.0",
"@types/jest": "23.3.10",
"@types/jest": "26.0.5",
"codecov": "3.7.1",
"jest": "26.1.0",
"prettier": "2.0.5",
Expand Down
34 changes: 18 additions & 16 deletions src/__tests__/resolver.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import { Resolver, DIDResolver, DIDDocument } from 'did-resolver'
import { getResolver } from '../resolver'
import fetch from 'cross-fetch'
jest.mock('cross-fetch')
const mockedFetch = fetch as jest.Mock<typeof fetch>
import { mocked } from 'ts-jest/utils'
const mockedFetch = mocked(fetch, true)

describe('web did resolver', () => {
const did: string = 'did:web:example.com'
Expand All @@ -27,7 +28,9 @@ describe('web did resolver', () => {
]
}

const validResponseLong: DIDDocument = JSON.parse(JSON.stringify(validResponse).replace(did, didLong))
const validResponseLong: DIDDocument = JSON.parse(
JSON.stringify(validResponse).replace(did, didLong)
)
const noContextResponse: object = {
id: validResponse.id,
publicKey: validResponse.publicKey,
Expand Down Expand Up @@ -55,48 +58,47 @@ describe('web did resolver', () => {

it('resolves document', () => {
mockedFetch.mockResolvedValueOnce({
json: () => validResponse
})
json: () => Promise.resolve(validResponse)
} as Response)
return expect(didResolver.resolve(did)).resolves.toEqual(validResponse)
})

it('resolves document with long did', () => {
mockedFetch.mockResolvedValueOnce({
json: () => validResponseLong
})
return expect(didResolver.resolve(didLong)).resolves.toEqual(validResponseLong)
json: () => Promise.resolve(validResponseLong)
} as Response)
return expect(didResolver.resolve(didLong)).resolves.toEqual(
validResponseLong
)
})


it('fails if the did is not a valid https url', () => {
mockedFetch.mockRejectedValueOnce({ status: 404 })
return expect(didResolver.resolve(did)).rejects.toThrow()
})

it('fails if the did document is not valid json', () => {
mockedFetch.mockResolvedValueOnce({
json: () => {
throw new Error('unable to parse json')
}
})
json: () => Promise.reject(new Error('unable to parse json'))
} as Response)
return expect(didResolver.resolve(did)).rejects.toThrowError(
/unable to parse json/
)
})

it('fails if the did document id does not match', () => {
mockedFetch.mockResolvedValueOnce({
json: () => wrongIdResponse
})
json: () => Promise.resolve(wrongIdResponse)
} as Response)
return expect(didResolver.resolve(did)).rejects.toThrowError(
'DID document id does not match requested did'
)
})

it('fails if the did document has no public keys', () => {
mockedFetch.mockResolvedValueOnce({
json: () => noPublicKeyResponse
})
json: () => Promise.resolve(noPublicKeyResponse)
} as Response)
return expect(didResolver.resolve(did)).rejects.toThrowError(
'DID document has no public keys'
)
Expand Down
5 changes: 2 additions & 3 deletions src/resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,9 @@ export function getResolver() {
did: string,
parsed: ParsedDID
): Promise<DIDDocument | null> {

let path = parsed.id + DOC_PATH
const id = parsed.id.split(':');
if (id.length > 1) path = id.join('/') + '/did.json';
const id = parsed.id.split(':')
if (id.length > 1) path = id.join('/') + '/did.json'
const url: string = `https://${path}`

let data: any = null
Expand Down

0 comments on commit 0e4ef57

Please sign in to comment.