Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hipstersmoothie committed Feb 22, 2020
1 parent dc807fd commit b7ac738
Showing 1 changed file with 117 additions and 0 deletions.
117 changes: 117 additions & 0 deletions packages/core/src/__tests__/remote.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
import Auto from '../auto';
import { dummyLog } from '../utils/logger';

const defaultRemote = '[email protected]'
const defaults = {
owner: 'foo',
repo: 'bar',
token: 'XXXX'
};

const reposGet = jest.fn();

jest.mock('@octokit/rest', () => {
const Octokit = class MockOctokit {
static plugin = () => Octokit;

authenticate = () => undefined;

repos = {
get: reposGet
};

hook = {
error: () => undefined
};
};

return { Octokit };
});

const execSpy = jest.fn();
// @ts-ignore
jest.mock('../utils/exec-promise.ts', () => (...args) => execSpy(...args));


describe('remote parsing', () => {
test('should fall back to origin when no git', async () => {
const auto = new Auto(defaults);
auto.logger = dummyLog();

execSpy.mockReturnValue(Promise.resolve())

// @ts-ignore
expect(await auto.getRemote()).toBe('origin');
});


test('should fall back to configured remote when no git', async () => {
const auto = new Auto(defaults);
auto.logger = dummyLog();

execSpy.mockReturnValue(Promise.resolve(defaultRemote))

// @ts-ignore
expect(await auto.getRemote()).toBe(defaultRemote);
});

test('should fall back to configured remote', async () => {
const auto = new Auto(defaults);
auto.logger = dummyLog();

execSpy.mockReturnValue(Promise.resolve(defaultRemote))
auto.git = {
getProject: () => {},
verifyAuth: () => false
} as any

// @ts-ignore
expect(await auto.getRemote()).toBe(defaultRemote);
});

test('use html_url when authed', async () => {
const html_url = 'https://my.repo'
const auto = new Auto(defaults);
auto.logger = dummyLog();

execSpy.mockReturnValue(Promise.resolve(defaultRemote))
auto.git = {
getProject: () => ({ html_url }),
verifyAuth: () => true
} as any

// @ts-ignore
expect(await auto.getRemote()).toBe(html_url);
});

test('use fall back to default when not authed', async () => {
const html_url = 'https://my.repo'
const auto = new Auto(defaults);
auto.logger = dummyLog();

execSpy.mockReturnValue(Promise.resolve(defaultRemote))
auto.git = {
getProject: () => ({ html_url }),
verifyAuth: () => false
} as any

// @ts-ignore
expect(await auto.getRemote()).toBe(defaultRemote);
});

test('add token to url if html doesn\'t auth', async () => {
const html_url = 'https://my.repo'
const auto = new Auto(defaults);
process.env.GH_TOKEN = 'XXXX'
auto.logger = dummyLog();

execSpy.mockReturnValue(Promise.resolve(defaultRemote))
auto.git = {
getProject: () => ({ html_url }),
verifyAuth: (url: string) => url !== html_url
} as any

// @ts-ignore
expect(await auto.getRemote()).toBe("https://[email protected]/");
});
});

0 comments on commit b7ac738

Please sign in to comment.