Skip to content

Commit

Permalink
refactor: start adding test suites
Browse files Browse the repository at this point in the history
  • Loading branch information
animify committed Feb 11, 2020
1 parent 9a6e5be commit 0364073
Show file tree
Hide file tree
Showing 9 changed files with 3,282 additions and 53 deletions.
4 changes: 4 additions & 0 deletions .babelrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
plugins: ['@babel/plugin-transform-runtime'],
presets: ['@babel/preset-env', '@babel/preset-typescript', '@babel/preset-react'],
};
38 changes: 27 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,24 @@
"build": "sh ./build.sh",
"bundle": "rollup -c",
"prepublishOnly": "yarn build",
"lint": "tslint -p tsconfig.json"
"lint": "tslint -p tsconfig.json",
"test": "jest"
},
"repository": {
"type": "git",
"url": "git+https://github.com/animify/dribbblejs.git"
"dependencies": {
"whatwg-fetch": "^3.0.0"
},
"devDependencies": {
"@babel/core": "^7.8.4",
"@babel/plugin-transform-runtime": "^7.8.3",
"@babel/preset-env": "^7.8.4",
"@babel/preset-react": "^7.8.3",
"@babel/preset-typescript": "^7.8.3",
"@types/jest": "^25.1.2",
"@types/react": "^16.8.17",
"changelog.md": "^1.1.0",
"husky": "^2.2.0",
"jest": "^25.1.0",
"jest-fetch-mock": "^3.0.1",
"lint-staged": "^8.1.6",
"rollup": "^1.11.3",
"rollup-plugin-babel": "^4.3.2",
Expand All @@ -32,12 +40,20 @@
"tslint": "^5.16.0",
"typescript": "^3.4.5"
},
"publishConfig": {
"access": "public",
"registry": "https://registry.npmjs.org/"
},
"files": [
"dist",
"index.d.ts",
"README.md",
"LICENSE"
],
"repository": {
"type": "git",
"url": "git+https://github.com/animify/dribbblejs.git"
},
"husky": {
"hooks": {
"pre-commit": "lint-staged"
Expand All @@ -46,11 +62,11 @@
"lint-staged": {
"*.ts": "yarn run lint"
},
"publishConfig": {
"access": "public",
"registry": "https://registry.npmjs.org/"
},
"dependencies": {
"whatwg-fetch": "^3.0.0"
"jest": {
"automock": false,
"verbose": true,
"setupFiles": [
"./setupTests.js"
]
}
}
}
1 change: 1 addition & 0 deletions setupTests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require('jest-fetch-mock').enableMocks();
24 changes: 13 additions & 11 deletions src/Request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,19 @@ export default class Request {

public static async fetch(opts: FetchOptions) {
try {
const response = await fetch(Request.buildURL(opts.url), {
const requestOptions: RequestInit = {
method: opts.method,
body: JSON.stringify(opts.body),
headers: {
};

if (Request.authToken) {
requestOptions.headers = {
...requestOptions.headers,
Authorization: `Bearer ${Request.authToken}`,
},
});
};
}

const response = await fetch(Request.buildURL(opts.url), requestOptions);

Request.checkStatus(response);

Expand All @@ -27,19 +33,15 @@ export default class Request {
}
}

public static get authToken() {
if (!Request._authToken) {
throw new Error('Auth token is not defined.');
}

return Request._authToken;
public static get authToken(): string {
return Request._authToken || '';
}

public static set authToken(token: string) {
Request._authToken = token;
}

private static buildURL(url: string) {
public static buildURL(url: string) {
return `https://api.dribbble.com/v2/${url}`.replace(/([^:]\/)\/+/g, '$1');
}

Expand Down
17 changes: 17 additions & 0 deletions src/__tests__/Dribbble.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Dribbble } from '..';
import 'jest-fetch-mock';

describe('dribbble', () => {
beforeEach(() => {
fetchMock.resetMocks();
});

test('fetch project data', async () => {
const mockData = { name: 'Stefan Mansson' };
fetchMock.mockResponseOnce(JSON.stringify(mockData), { status: 200 });

const dribbble = new Dribbble({ authToken: '1234' });

expect(await dribbble.user.get()).toEqual(mockData);
});
});
22 changes: 22 additions & 0 deletions src/__tests__/Request.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import Request from '../Request';
import 'jest-fetch-mock';

describe('Request', () => {
beforeEach(() => {
fetchMock.resetMocks();
});

test('builds and fetches from url', async () => {
fetchMock.mockResponseOnce(JSON.stringify({}), { status: 200 });

await Request.fetch({ url: '/user/projects', method: 'GET' });

expect(fetchMock.mock.calls[0][0]).toEqual(Request.buildURL('/user/projects'));
});

test('fetch throws with error status', async () => {
fetchMock.mockResponseOnce(JSON.stringify({}), { status: 400 });

await expect(Request.fetch({ url: '/user/projects', method: 'GET' })).rejects.toThrow();
});
});
6 changes: 2 additions & 4 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
{
"compilerOptions": {
"lib": ["dom", "es2015", "es2016", "es2017"],
"outDir": "build",
"lib": ["dom", "es2015", "es2016", "es2017", "esnext"],
"module": "esnext",
"target": "es5",
"jsx": "react",
"target": "es6",
"sourceMap": false,
"allowJs": false,
"noUnusedLocals": true,
Expand Down
1 change: 1 addition & 0 deletions tslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"rules": {
"quotemark": [true, "single"],
"array-type": [true, "array"],
"arrow-parens": [true, "ban-single-arg-parens"],
"no-namespace": [true, "allow-declarations"],
"no-trailing-whitespace": false,
"no-bitwise": false,
Expand Down
Loading

0 comments on commit 0364073

Please sign in to comment.