Skip to content

Commit

Permalink
Replace fetch-mock with nock (#340)
Browse files Browse the repository at this point in the history
`fetch-mock` has been replaced with `nock`. Both libraries do basically
the same thing. I chose to use `nock` because the types shipped with
`fetch-mock` were incorrect and they were giving me trouble. But `nock`
also has a nicer API, and it's what we use for mocking `fetch` in most
of our other libraries.
  • Loading branch information
Gudahtt authored Feb 12, 2021
1 parent 06fd4ee commit aaf3bcc
Show file tree
Hide file tree
Showing 7 changed files with 228 additions and 399 deletions.
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@
},
"devDependencies": {
"@metamask/eslint-config": "^4.1.0",
"@types/fetch-mock": "^7.3.1",
"@types/jest": "^22.2.3",
"@types/node": "^10.1.4",
"@types/sinon": "^4.3.3",
Expand All @@ -72,10 +71,10 @@
"eslint-plugin-jest": "^23.6.0",
"eslint-plugin-node": "^11.1.0",
"ethjs-provider-http": "^0.1.6",
"fetch-mock": "^9.10.7",
"jest": "^26.4.2",
"jest-environment-jsdom": "^25.0.0",
"lint-staged": "^6.1.0",
"nock": "^13.0.7",
"prettier": "^2.1.1",
"sinon": "^7.4.1",
"ts-jest": "^26.3.0",
Expand Down
137 changes: 50 additions & 87 deletions tests/AssetsController.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createSandbox } from 'sinon';
import { getOnce } from 'fetch-mock';
import * as nock from 'nock';
import AssetsController from '../src/assets/AssetsController';
import ComposableController from '../src/ComposableController';
import PreferencesController from '../src/user/PreferencesController';
Expand All @@ -10,7 +10,8 @@ const HttpProvider = require('ethjs-provider-http');

const KUDOSADDRESS = '0x2aea4add166ebf38b63d09a75de1a7b94aa24163';
const MAINNET_PROVIDER = new HttpProvider('https://mainnet.infura.io/v3/341eacb578dd44a1a049cbc5f6fd4035');
const OPEN_SEA_API = 'https://api.opensea.io/api/v1/';
const OPEN_SEA_HOST = 'https://api.opensea.io';
const OPEN_SEA_PATH = '/api/v1';

describe('AssetsController', () => {
let assetsController: AssetsController;
Expand All @@ -27,94 +28,56 @@ describe('AssetsController', () => {

new ComposableController([assetsController, assetsContract, network, preferences]);

getOnce(
`${OPEN_SEA_API}asset_contract/0xfoO`,
() => ({
body: JSON.stringify({
description: 'Description',
image_url: 'url',
name: 'Name',
symbol: 'FOO',
total_supply: 0,
}),
}),
{ overwriteRoutes: true, method: 'GET' },
);
getOnce(
`${OPEN_SEA_API}asset_contract/0xFOu`,
() => ({
body: JSON.stringify({
description: 'Description',
image_url: 'url',
name: 'Name',
symbol: 'FOU',
total_supply: 10,
}),
}),
{ overwriteRoutes: true, method: 'GET' },
);
getOnce(
`${OPEN_SEA_API}asset/0xfoO/1`,
() => ({
body: JSON.stringify({
description: 'Description',
image_original_url: 'url',
name: 'Name',
}),
}),
{ overwriteRoutes: true, method: 'GET' },
);
getOnce(
`${OPEN_SEA_API}asset/0x2aEa4Add166EBf38b63d09a75dE1a7b94Aa24163/1203`,
() => ({
body: JSON.stringify({
description: 'Kudos Description',
image_original_url: 'Kudos url',
name: 'Kudos Name',
}),
}),
{ overwriteRoutes: true, method: 'GET' },
);
getOnce(
'https://ipfs.gitcoin.co:443/api/v0/cat/QmPmt6EAaioN78ECnW5oCL8v2YvVSpoBjLCjrXhhsAvoov',
() => ({
body: JSON.stringify({
image: 'Kudos Image',
name: 'Kudos Name',
}),
}),
{ overwriteRoutes: true, method: 'GET' },
);
getOnce(
`${OPEN_SEA_API}asset/0x6EbeAf8e8E946F0716E6533A6f2cefc83f60e8Ab/798958393`,
() => ({
throws: new TypeError('Failed to fetch'),
}),
{ overwriteRoutes: true, method: 'GET' },
);
getOnce(
`${OPEN_SEA_API}asset_contract/0x6EbeAf8e8E946F0716E6533A6f2cefc83f60e8Ab`,
() => ({
throws: new TypeError('Failed to fetch'),
}),
{ overwriteRoutes: true, method: 'GET' },
);
getOnce(
`${OPEN_SEA_API}asset_contract/0x2aEa4Add166EBf38b63d09a75dE1a7b94Aa24163`,
() => ({
body: JSON.stringify({
description: 'Kudos Description',
image_url: 'Kudos url',
name: 'Kudos',
symbol: 'KDO',
total_supply: 10,
}),
}),
{ overwriteRoutes: true, method: 'GET' },
);
nock(OPEN_SEA_HOST)
.get(`${OPEN_SEA_PATH}/asset_contract/0xfoO`)
.reply(200, {
description: 'Description',
image_url: 'url',
name: 'Name',
symbol: 'FOO',
total_supply: 0,
})
.get(`${OPEN_SEA_PATH}/asset_contract/0xFOu`)
.reply(200, {
description: 'Description',
image_url: 'url',
name: 'Name',
symbol: 'FOU',
total_supply: 10,
})
.get(`${OPEN_SEA_PATH}/asset/0xfoO/1`)
.reply(200, {
description: 'Description',
image_original_url: 'url',
name: 'Name',
})
.get(`${OPEN_SEA_PATH}/asset/0x2aEa4Add166EBf38b63d09a75dE1a7b94Aa24163/1203`)
.reply(200, {
description: 'Kudos Description',
image_original_url: 'Kudos url',
name: 'Kudos Name',
})
.get(`${OPEN_SEA_PATH}/asset/0x6EbeAf8e8E946F0716E6533A6f2cefc83f60e8Ab/798958393`)
.replyWithError(new TypeError('Failed to fetch'))
.get(`${OPEN_SEA_PATH}/asset_contract/0x6EbeAf8e8E946F0716E6533A6f2cefc83f60e8Ab`)
.replyWithError(new TypeError('Failed to fetch'))
.get(`${OPEN_SEA_PATH}/asset_contract/0x2aEa4Add166EBf38b63d09a75dE1a7b94Aa24163`)
.reply(200, {
description: 'Kudos Description',
image_url: 'Kudos url',
name: 'Kudos',
symbol: 'KDO',
total_supply: 10,
});

nock('https://ipfs.gitcoin.co:443').get('/api/v0/cat/QmPmt6EAaioN78ECnW5oCL8v2YvVSpoBjLCjrXhhsAvoov').reply(200, {
image: 'Kudos Image',
name: 'Kudos Name',
});
});

afterEach(() => {
nock.cleanAll();
sandbox.reset();
});

Expand Down
Loading

0 comments on commit aaf3bcc

Please sign in to comment.