2020-01-18 02:11:17 +00:00
|
|
|
import {getURL, getLatestVersion} from '../src/get-latest-version';
|
|
|
|
import nock from 'nock';
|
|
|
|
import {FetchError} from 'node-fetch';
|
|
|
|
import {Tool} from '../src/constants';
|
|
|
|
import jsonTestBrew from './data/brew.json';
|
|
|
|
import jsonTestGithub from './data/github.json';
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
jest.resetModules();
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
nock.cleanAll();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('getURL()', () => {
|
|
|
|
test('return expected URL', () => {
|
|
|
|
const urlBrewExpected = `https://formulae.brew.sh/api/formula/${Tool.Repo}.json`;
|
|
|
|
const urlBrew: string = getURL(Tool.Org, Tool.Repo, 'brew');
|
|
|
|
expect(urlBrew).toMatch(urlBrewExpected);
|
|
|
|
|
|
|
|
const urlGithubExpected = `https://api.github.com/repos/${Tool.Org}/${Tool.Repo}/releases/latest`;
|
|
|
|
const urlGithub: string = getURL(Tool.Org, Tool.Repo, 'github');
|
|
|
|
expect(urlGithub).toMatch(urlGithubExpected);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('getLatestVersion()', () => {
|
|
|
|
test('return latest version via brew', async () => {
|
2020-06-21 20:40:06 +00:00
|
|
|
nock('https://formulae.brew.sh').get(`/api/formula/${Tool.Repo}.json`).reply(200, jsonTestBrew);
|
2020-01-18 02:11:17 +00:00
|
|
|
|
2020-06-21 20:40:06 +00:00
|
|
|
const versionLatest: string = await getLatestVersion(Tool.Org, Tool.Repo, 'brew');
|
2020-01-18 02:11:17 +00:00
|
|
|
expect(versionLatest).toMatch(Tool.TestVersionLatest);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('return latest version via GitHub', async () => {
|
|
|
|
nock('https://api.github.com')
|
|
|
|
.get(`/repos/${Tool.Org}/${Tool.Repo}/releases/latest`)
|
|
|
|
.reply(200, jsonTestGithub);
|
|
|
|
|
2020-06-21 20:40:06 +00:00
|
|
|
const versionLatest: string = await getLatestVersion(Tool.Org, Tool.Repo, 'github');
|
2020-01-18 02:11:17 +00:00
|
|
|
expect(versionLatest).toMatch(Tool.TestVersionLatest);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('return exception 404', async () => {
|
2020-06-21 20:40:06 +00:00
|
|
|
nock('https://formulae.brew.sh').get(`/api/formula/${Tool.Repo}.json`).reply(404);
|
2020-01-18 02:11:17 +00:00
|
|
|
|
2020-06-21 20:40:06 +00:00
|
|
|
await expect(getLatestVersion(Tool.Org, Tool.Repo, 'brew')).rejects.toThrowError(FetchError);
|
2020-01-18 02:11:17 +00:00
|
|
|
});
|
|
|
|
});
|