Skip to content
This repository has been archived by the owner on Aug 10, 2021. It is now read-only.

Commit

Permalink
fix: correct query parameter in AJAX call
Browse files Browse the repository at this point in the history
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 fba0ec7
Show file tree
Hide file tree
Showing 9 changed files with 161 additions and 82 deletions.
11 changes: 6 additions & 5 deletions src/__fixtures__/no-ellipsis-comments/expected.md
Expand Up @@ -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 changes: 1 addition & 1 deletion src/__fixtures__/no-ellipsis-comments/input.md
Expand Up @@ -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
103 changes: 71 additions & 32 deletions src/__fixtures__/simple-example/expected.md
Expand Up @@ -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:
Expand Down
6 changes: 3 additions & 3 deletions src/__fixtures__/simple-example/input.md
Expand Up @@ -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:

Expand Down
103 changes: 71 additions & 32 deletions src/__fixtures__/with-cache/expected.md
Expand Up @@ -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:
Expand Down
6 changes: 3 additions & 3 deletions src/__fixtures__/with-cache/input.md
Expand Up @@ -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:

Expand Down
8 changes: 4 additions & 4 deletions src/__tests__/fetchGithubContent.js
Expand Up @@ -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 () => {
Expand All @@ -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
);
Expand All @@ -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
);
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/fetchGithubContentFail.js
Expand Up @@ -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/);
Expand Down
2 changes: 1 addition & 1 deletion src/fetchGithubContent.ts
Expand Up @@ -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 {
Expand Down

0 comments on commit fba0ec7

Please sign in to comment.