From b6efc25db0a18deb98221d6e98549e23a06fd7c3 Mon Sep 17 00:00:00 2001 From: Alex Date: Thu, 2 Dec 2021 15:59:17 -0600 Subject: [PATCH] tests fixed --- src/assets/CollectiblesController.test.ts | 2 +- src/util.test.ts | 34 +++++++++++++---------- src/util.ts | 20 ++++++------- 3 files changed, 29 insertions(+), 27 deletions(-) diff --git a/src/assets/CollectiblesController.test.ts b/src/assets/CollectiblesController.test.ts index 8bd04136425..7a5179df00f 100644 --- a/src/assets/CollectiblesController.test.ts +++ b/src/assets/CollectiblesController.test.ts @@ -37,7 +37,7 @@ const DEPRESSIONIST_CID_V1 = const DEPRESSIONIST_CLOUDFLARE_IPFS_SUBDOMAIN_PATH = getFormattedIpfsUrl( CLOUDFARE_PATH, `ipfs://${DEPRESSIONIST_CID_V1}`, - true + true, ); describe('CollectiblesController', () => { diff --git a/src/util.test.ts b/src/util.test.ts index 434b8b68f49..90f499c3486 100644 --- a/src/util.test.ts +++ b/src/util.test.ts @@ -1075,14 +1075,22 @@ describe('util', () => { describe('getFormattedIpfsUrl', () => { it('should return a correctly formatted subdomained ipfs url when passed ipfsGateway without protocol prefix, no path and subdomainSupported argument set to true', () => { - expect(util.getFormattedIpfsUrl(IFPS_GATEWAY, `${DEFAULT_IPFS_URL_FORMAT}${IPFS_CID_V1}`, true)).toStrictEqual( - `https://${IPFS_CID_V1}.ipfs.${IFPS_GATEWAY}`, - ); + expect( + util.getFormattedIpfsUrl( + IFPS_GATEWAY, + `${DEFAULT_IPFS_URL_FORMAT}${IPFS_CID_V1}`, + true, + ), + ).toStrictEqual(`https://${IPFS_CID_V1}.ipfs.${IFPS_GATEWAY}`); }); - it('should return a correctly formatted subdomained ipfs url when passed ipfsGateway with protocol prefix, a cidv0 no path and subdomainSupported argument set to true', () => { + it('should return a correctly formatted subdomained ipfs url when passed ipfsGateway with protocol prefix, a cidv0 no path and subdomainSupported argument set to true', () => { expect( - util.getFormattedIpfsUrl(`https://${IFPS_GATEWAY}`, `${DEFAULT_IPFS_URL_FORMAT}${IPFS_CID_V0}`, true), + util.getFormattedIpfsUrl( + `https://${IFPS_GATEWAY}`, + `${DEFAULT_IPFS_URL_FORMAT}${IPFS_CID_V0}`, + true, + ), ).toStrictEqual(`https://${IPFS_CID_V1}.ipfs.${IFPS_GATEWAY}`); }); @@ -1124,7 +1132,7 @@ describe('util', () => { `${DEFAULT_IPFS_URL_FORMAT}${IPFS_CID_V0}/test`, ), ).toStrictEqual(`${IPFS_CID_V0}/test`); - }) + }); it('should return content identifier string from default ipfs url format if no path preset', () => { expect( @@ -1132,7 +1140,7 @@ describe('util', () => { `${DEFAULT_IPFS_URL_FORMAT}${IPFS_CID_V0}`, ), ).toStrictEqual(IPFS_CID_V0); - }) + }); it('should return content identifier string from alternate ipfs url format', () => { expect( @@ -1140,21 +1148,19 @@ describe('util', () => { `${ALTERNATIVE_IPFS_URL_FORMAT}${IPFS_CID_V0}`, ), ).toStrictEqual(IPFS_CID_V0); - }) + }); it('should throw error if passed a non ipfs url', () => { expect(() => util.removeIpfsProtocolPrefix(SOME_API)).toThrow( 'this method should not be used with non ipfs urls', ); }); - }) + }); describe('getIpfsCIDv1AndPath', () => { it('should return content identifier from default ipfs url format', () => { expect( - util.getIpfsCIDv1AndPath( - `${DEFAULT_IPFS_URL_FORMAT}${IPFS_CID_V0}`, - ), + util.getIpfsCIDv1AndPath(`${DEFAULT_IPFS_URL_FORMAT}${IPFS_CID_V0}`), ).toStrictEqual({ cid: IPFS_CID_V1, path: '' }); }); @@ -1168,9 +1174,7 @@ describe('util', () => { it('should return unchanged content identifier if already v1', () => { expect( - util.getIpfsCIDv1AndPath( - `${DEFAULT_IPFS_URL_FORMAT}${IPFS_CID_V1}`, - ), + util.getIpfsCIDv1AndPath(`${DEFAULT_IPFS_URL_FORMAT}${IPFS_CID_V1}`), ).toStrictEqual({ cid: IPFS_CID_V1, path: '' }); }); diff --git a/src/util.ts b/src/util.ts index 87b9bbfe955..234a4b2e230 100644 --- a/src/util.ts +++ b/src/util.ts @@ -770,21 +770,20 @@ export function validateMinimumIncrease(proposed: string, min: string) { } /** - * removes ipfs protocol prefix from ipfs-url + * Removes ipfs protocol prefix from ipfs-url. * * @param ipfsUrl - ipfs url * @returns Ipfs content identifier and (possibly) path in a string - * @throws Will throw if the url passed is not ipfs. + * @throws will throw if the url passed is not ipfs. */ export function removeIpfsProtocolPrefix(ipfsUrl: string) { if (ipfsUrl.startsWith('ipfs://ipfs/')) { return ipfsUrl.replace('ipfs://ipfs/', ''); } else if (ipfsUrl.startsWith('ipfs://')) { return ipfsUrl.replace('ipfs://', ''); - } else { - // this method should not be used with non-ipfs urls (i.e. startsWith('ipfs://') === true) - throw new Error('this method should not be used with non ipfs urls'); } + // this method should not be used with non-ipfs urls (i.e. startsWith('ipfs://') === true) + throw new Error('this method should not be used with non ipfs urls'); } /** @@ -842,11 +841,10 @@ export function getFormattedIpfsUrl( const gatewayHost = new URL(addUrlProtocolPrefix(ipfsGateway))?.host; const { cid, path } = getIpfsCIDv1AndPath(ipfsUrl); return `https://${cid}.ipfs.${gatewayHost}${path ?? ''}`; - } else { - const cidAndPath = removeIpfsProtocolPrefix(ipfsUrl); - const gateway = ipfsGateway.endsWith('/ipfs/') - ? ipfsGateway - : `${ipfsGateway}/ipfs/`; - return `${gateway}${cidAndPath}`; } + const cidAndPath = removeIpfsProtocolPrefix(ipfsUrl); + const gateway = ipfsGateway.endsWith('/ipfs/') + ? ipfsGateway + : `${ipfsGateway}/ipfs/`; + return `${gateway}${cidAndPath}`; }