Skip to content

Commit

Permalink
test(lib): adds test to github and npm
Browse files Browse the repository at this point in the history
  • Loading branch information
sharvit committed Jul 28, 2019
1 parent aae3549 commit d33e55b
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 1 deletion.
12 changes: 12 additions & 0 deletions __mocks__/@octokit/rest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export const authenticatedUserToken = 'some-token';
export const createAuthorization = jest
.fn()
.mockResolvedValue({ data: { token: authenticatedUserToken } });
export const createRepoForAuthenticatedUser = jest.fn();

const mock = jest.fn().mockImplementation(() => ({
repos: { createForAuthenticatedUser: createRepoForAuthenticatedUser },
oauthAuthorizations: { createAuthorization },
}));

export default mock;
5 changes: 5 additions & 0 deletions __mocks__/npm-profile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const loginCouch = jest.fn();

const mock = { loginCouch };

export default mock;
56 changes: 56 additions & 0 deletions src/app/lib/github.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import Octokit, {
createRepoForAuthenticatedUser,
createAuthorization,
authenticatedUserToken,
} from '@octokit/rest';
import Github from './github';

describe('Github', () => {
let github;
const username = 'some-username';
const password = 'some-password';
const on2fa = jest.fn();

beforeEach(() => {
Octokit.mockClear();
createRepoForAuthenticatedUser.mockClear();
createAuthorization.mockClear();

github = new Github(username, password, on2fa);
});

it('should create a new Github instance', () => {
expect(Octokit).toBeCalledWith({
auth: {
username,
password,
on2fa,
},
});
});

it('should create-repository', () => {
const name = 'some-name';
const description = 'some-description';

github.createRepository({ name, description });

expect(createRepoForAuthenticatedUser).toBeCalledWith({
name,
description,
});
});

it('should create-createToken', async () => {
const note = 'some-note';
const scopes = 'some-scopes';

const token = await github.createToken(note, scopes);

expect(token).toBe(authenticatedUserToken);
expect(createAuthorization).toBeCalledWith({
note,
scopes,
});
});
});
2 changes: 1 addition & 1 deletion src/app/lib/npm.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const npmProfile = require('npm-profile');
import npmProfile from 'npm-profile';

export const login = ({ username, password }) =>
npmProfile.loginCouch(username, password, {
Expand Down
15 changes: 15 additions & 0 deletions src/app/lib/npm.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import npmProfile from 'npm-profile';
import { login } from './npm';

describe('npm', () => {
it('should login', () => {
const username = 'some-username';
const password = 'some-password';

login({ username, password });

expect(npmProfile.loginCouch).toBeCalledWith(username, password, {
registry: 'https://registry.npmjs.org',
});
});
});

0 comments on commit d33e55b

Please sign in to comment.