diff --git a/__mocks__/@octokit/rest.js b/__mocks__/@octokit/rest.js new file mode 100644 index 0000000..575c814 --- /dev/null +++ b/__mocks__/@octokit/rest.js @@ -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; diff --git a/__mocks__/npm-profile.js b/__mocks__/npm-profile.js new file mode 100644 index 0000000..5a7d227 --- /dev/null +++ b/__mocks__/npm-profile.js @@ -0,0 +1,5 @@ +export const loginCouch = jest.fn(); + +const mock = { loginCouch }; + +export default mock; diff --git a/src/app/lib/github.test.js b/src/app/lib/github.test.js new file mode 100644 index 0000000..b16e199 --- /dev/null +++ b/src/app/lib/github.test.js @@ -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, + }); + }); +}); diff --git a/src/app/lib/npm.js b/src/app/lib/npm.js index 35f6d38..52b8b1d 100644 --- a/src/app/lib/npm.js +++ b/src/app/lib/npm.js @@ -1,4 +1,4 @@ -const npmProfile = require('npm-profile'); +import npmProfile from 'npm-profile'; export const login = ({ username, password }) => npmProfile.loginCouch(username, password, { diff --git a/src/app/lib/npm.test.js b/src/app/lib/npm.test.js new file mode 100644 index 0000000..3d1a7db --- /dev/null +++ b/src/app/lib/npm.test.js @@ -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', + }); + }); +});