Permalink
Browse files

fix: correct query parameter in AJAX call

The correct query parameter to get a file from GitHub API is `ref` but
we've been using `branch` this whole time. This has worked so far
because all the tests have been performed on a file
(`https://github.com/huy-nguyen/squarify/blob/d7074c2/.babelrc`) that
has only one version in git history. Providing a `branch` parameter
instead of a `ref` parameter causes GitHub to assume no `ref` has been
specified and as such to return the latest version of that file on the
`master` branch, which coincidentally is the correct content of the
tested file (which ever only has one version).

This commit fixes that error and also updates all test files so that
they don't use the special case of files with only one version in git
history.
  • Loading branch information...
huy-nguyen committed Apr 19, 2018
1 parent 1a4b954 commit fba0ec77c3d1201d6f0dbff7374b086f06f7b9b8
@@ -3,10 +3,11 @@ This is a test of the plugin
Should include language and range in code block:
```javascript
{
"targets": {
"node": "current",
},
"spec": true
import baseIteratee from './_baseIteratee.js';
import baseSum from './_baseSum.js';
function sumBy(array, iteratee) {
return (array && array.length)
? baseSum(array, baseIteratee(iteratee, 2))
: 0;
}
```
@@ -2,4 +2,4 @@ This is a test of the plugin
Should include language and range in code block:
GITHUB-EMBED https://github.com/huy-nguyen/squarify/blob/d7074c2/.babelrc javascript 1,5-8,14 GITHUB-EMBED
GITHUB-EMBED https://github.com/lodash/lodash/blob/2900cfd/sumBy.js javascript 1,2,27-31 GITHUB-EMBED
@@ -3,54 +3,93 @@ This is a test of the plugin
Should not include language or range in code block:
```
{
"presets": [
[
"env", {
"targets": {
"node": "current",
},
"spec": true
}
]
],
"plugins": [
]
import baseIteratee from './_baseIteratee.js';
import baseSum from './_baseSum.js';
/**
* This method is like `_.sum` except that it accepts `iteratee` which is
* invoked for each element in `array` to generate the value to be summed.
* The iteratee is invoked with one argument: (value).
*
* @static
* @memberOf _
* @since 4.0.0
* @category Math
* @param {Array} array The array to iterate over.
* @param {Function} [iteratee=_.identity] The iteratee invoked per element.
* @returns {number} Returns the sum.
* @example
*
* var objects = [{ 'n': 4 }, { 'n': 2 }, { 'n': 8 }, { 'n': 6 }];
*
* _.sumBy(objects, function(o) { return o.n; });
* // => 20
*
* // The `_.property` iteratee shorthand.
* _.sumBy(objects, 'n');
* // => 20
*/
function sumBy(array, iteratee) {
return (array && array.length)
? baseSum(array, baseIteratee(iteratee, 2))
: 0;
}
export default sumBy;
```
Should include language but not range in code block:
```javascript
{
"presets": [
[
"env", {
"targets": {
"node": "current",
},
"spec": true
}
]
],
"plugins": [
]
import baseIteratee from './_baseIteratee.js';
import baseSum from './_baseSum.js';
/**
* This method is like `_.sum` except that it accepts `iteratee` which is
* invoked for each element in `array` to generate the value to be summed.
* The iteratee is invoked with one argument: (value).
*
* @static
* @memberOf _
* @since 4.0.0
* @category Math
* @param {Array} array The array to iterate over.
* @param {Function} [iteratee=_.identity] The iteratee invoked per element.
* @returns {number} Returns the sum.
* @example
*
* var objects = [{ 'n': 4 }, { 'n': 2 }, { 'n': 8 }, { 'n': 6 }];
*
* _.sumBy(objects, function(o) { return o.n; });
* // => 20
*
* // The `_.property` iteratee shorthand.
* _.sumBy(objects, 'n');
* // => 20
*/
function sumBy(array, iteratee) {
return (array && array.length)
? baseSum(array, baseIteratee(iteratee, 2))
: 0;
}
export default sumBy;
```
Should include language and range in code block:
```javascript
{
import baseIteratee from './_baseIteratee.js';
import baseSum from './_baseSum.js';
// ...
"targets": {
"node": "current",
},
"spec": true
// ...
function sumBy(array, iteratee) {
return (array && array.length)
? baseSum(array, baseIteratee(iteratee, 2))
: 0;
}
// ...
```
Should not transform this:
@@ -2,15 +2,15 @@ This is a test of the plugin
Should not include language or range in code block:
GITHUB-EMBED https://github.com/huy-nguyen/squarify/blob/d7074c2/.babelrc GITHUB-EMBED
GITHUB-EMBED https://github.com/lodash/lodash/blob/2900cfd/sumBy.js GITHUB-EMBED
Should include language but not range in code block:
GITHUB-EMBED https://github.com/huy-nguyen/squarify/blob/d7074c2/.babelrc javascript GITHUB-EMBED
GITHUB-EMBED https://github.com/lodash/lodash/blob/2900cfd/sumBy.js javascript GITHUB-EMBED
Should include language and range in code block:
GITHUB-EMBED https://github.com/huy-nguyen/squarify/blob/d7074c2/.babelrc javascript 1,5-8,14 GITHUB-EMBED
GITHUB-EMBED https://github.com/lodash/lodash/blob/2900cfd/sumBy.js javascript 1,2,27-31 GITHUB-EMBED
Should not transform this:
@@ -3,54 +3,93 @@ This is a test of the plugin
Should not include language or range in code block:
```
{
"presets": [
[
"env", {
"targets": {
"node": "current",
},
"spec": true
}
]
],
"plugins": [
]
import baseIteratee from './_baseIteratee.js';
import baseSum from './_baseSum.js';
/**
* This method is like `_.sum` except that it accepts `iteratee` which is
* invoked for each element in `array` to generate the value to be summed.
* The iteratee is invoked with one argument: (value).
*
* @static
* @memberOf _
* @since 4.0.0
* @category Math
* @param {Array} array The array to iterate over.
* @param {Function} [iteratee=_.identity] The iteratee invoked per element.
* @returns {number} Returns the sum.
* @example
*
* var objects = [{ 'n': 4 }, { 'n': 2 }, { 'n': 8 }, { 'n': 6 }];
*
* _.sumBy(objects, function(o) { return o.n; });
* // => 20
*
* // The `_.property` iteratee shorthand.
* _.sumBy(objects, 'n');
* // => 20
*/
function sumBy(array, iteratee) {
return (array && array.length)
? baseSum(array, baseIteratee(iteratee, 2))
: 0;
}
export default sumBy;
```
Should include language but not range in code block:
```javascript
{
"presets": [
[
"env", {
"targets": {
"node": "current",
},
"spec": true
}
]
],
"plugins": [
]
import baseIteratee from './_baseIteratee.js';
import baseSum from './_baseSum.js';
/**
* This method is like `_.sum` except that it accepts `iteratee` which is
* invoked for each element in `array` to generate the value to be summed.
* The iteratee is invoked with one argument: (value).
*
* @static
* @memberOf _
* @since 4.0.0
* @category Math
* @param {Array} array The array to iterate over.
* @param {Function} [iteratee=_.identity] The iteratee invoked per element.
* @returns {number} Returns the sum.
* @example
*
* var objects = [{ 'n': 4 }, { 'n': 2 }, { 'n': 8 }, { 'n': 6 }];
*
* _.sumBy(objects, function(o) { return o.n; });
* // => 20
*
* // The `_.property` iteratee shorthand.
* _.sumBy(objects, 'n');
* // => 20
*/
function sumBy(array, iteratee) {
return (array && array.length)
? baseSum(array, baseIteratee(iteratee, 2))
: 0;
}
export default sumBy;
```
Should include language and range in code block:
```javascript
{
import baseIteratee from './_baseIteratee.js';
import baseSum from './_baseSum.js';
// ...
"targets": {
"node": "current",
},
"spec": true
// ...
function sumBy(array, iteratee) {
return (array && array.length)
? baseSum(array, baseIteratee(iteratee, 2))
: 0;
}
// ...
```
Should not transform this:
@@ -2,15 +2,15 @@ This is a test of the plugin
Should not include language or range in code block:
GITHUB-EMBED https://github.com/huy-nguyen/squarify/blob/d7074c2/.babelrc GITHUB-EMBED
GITHUB-EMBED https://github.com/lodash/lodash/blob/2900cfd/sumBy.js GITHUB-EMBED
Should include language but not range in code block:
GITHUB-EMBED https://github.com/huy-nguyen/squarify/blob/d7074c2/.babelrc javascript GITHUB-EMBED
GITHUB-EMBED https://github.com/lodash/lodash/blob/2900cfd/sumBy.js javascript GITHUB-EMBED
Should include language and range in code block:
GITHUB-EMBED https://github.com/huy-nguyen/squarify/blob/d7074c2/.babelrc javascript 1,5-8,14 GITHUB-EMBED
GITHUB-EMBED https://github.com/lodash/lodash/blob/2900cfd/sumBy.js javascript 1,2,27-31 GITHUB-EMBED
Should not transform this:
@@ -7,11 +7,11 @@ const token = process.env.GITHUB_TOKEN;
test('Should succeed with valid GitHub URL', async () => {
const actual = await fetchGithubFile(
'https://github.com/huy-nguyen/squarify/blob/d7074c2/.babelrc',
'https://github.com/lodash/lodash/blob/2900cfd/sumBy.js',
token,
fetch,
);
expect(actual.startsWith('{\n "presets": [')).toBe(true);
expect(actual.startsWith('import baseIteratee from \'./_baseIteratee.js\';')).toBe(true);
});
test('Should throw if called with invalid GitHub URL', async () => {
@@ -27,7 +27,7 @@ test('Should throw when given URL points to non-existent GitHub content', async
expect.assertions(1);
try {
await fetchGithubFile(
'https://github.com/huy-nguyen/squarify/blob/d7074c2/someFile',
'https://github.com/lodash/lodash/blob/2900cfd/someFile.js',
token,
fetch
);
@@ -40,7 +40,7 @@ test('Should throw when given URL points to a GitHub directory', async () => {
expect.assertions(1);
try {
await fetchGithubFile(
'https://github.com/huy-nguyen/squarify/blob/d7074c2/src',
'https://github.com/lodash/lodash/tree/2900cfd/.github',
token,
fetch
);
@@ -11,7 +11,7 @@ test('Should handle failed AJAX request to valid GitHub URL', async () => {
fetch.mockRejectedValue(new Error('Network error'));
try {
await fetchGithubFile(
'https://github.com/huy-nguyen/squarify/blob/d7074c2/.babelrc', token, fetch
'https://github.com/lodash/lodash/blob/2900cfd/sumBy.js', token, fetch
);
} catch (e) {
expect(e.message).toMatch(/Network error/);
@@ -10,7 +10,7 @@ export const fetchGithubFile =
if (parseResult !== null) {
// If the provided URL is a valid GitHub URL:
const {branch, path, repo, user} = parseResult;
const fetchUrl = `https://api.github.com/repos/${user}/${repo}/contents/${path}?branch=${branch}`;
const fetchUrl = `https://api.github.com/repos/${user}/${repo}/contents/${path}?ref=${branch}`;
let response: Response;
try {

0 comments on commit fba0ec7

Please sign in to comment.