Skip to content

Commit 637b040

Browse files
increase test coverage
1 parent 4cf3c13 commit 637b040

File tree

6 files changed

+77
-6
lines changed

6 files changed

+77
-6
lines changed

jest.config.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ module.exports = {
77
},
88
coverageThreshold: {
99
global: {
10-
branches: 17,
11-
functions: 28,
12-
lines: 33,
13-
statements: 33
10+
branches: 20,
11+
functions: 42,
12+
lines: 47,
13+
statements: 47
1414
}
1515
}
1616
};

package-lock.json

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
},
2727
"homepage": "https://github.com/saucelabs/docusaurus-code-references#readme",
2828
"devDependencies": {
29+
"@types/jest": "^27.0.0",
2930
"@types/node": "^14.11.8",
3031
"@types/react": "^16.9.52",
3132
"@types/recursive-readdir": "^2.2.0",

src/theme/ReferenceCodeBlock/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ async function fetchCode ({ url, fromLine, toLine }: GitHubReference, fetchResul
7979
})
8080
}
8181

82-
function codeReducer (prevState: any, { type, value }: DispatchMessage) {
82+
export function codeReducer (prevState: any, { type, value }: DispatchMessage) {
8383
switch (type) {
8484
case 'reset': {
8585
return initialFetchResultState;

tests/ReferenceCodeBlock.test.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,38 @@
11
import { timeStamp } from 'console'
2-
import { parseReference } from '../src/theme/ReferenceCodeBlock/index'
2+
import { parseReference, codeReducer } from '../src/theme/ReferenceCodeBlock/index'
33

44
test('should parse GitHub reference properly', () => {
55
expect(parseReference('https://github.com/saucelabs/docusaurus-theme-github-codeblock/blob/main/src/theme/ReferenceCodeBlock/index.tsx'))
66
.toMatchSnapshot()
77
expect(parseReference('https://github.com/saucelabs/docusaurus-theme-github-codeblock/blob/main/src/theme/ReferenceCodeBlock/index.tsx#L105-L108'))
88
.toMatchSnapshot()
99
})
10+
11+
test('codeReducer', () => {
12+
const prevState = { foo: 'bar' }
13+
expect(codeReducer(prevState, { type: 'reset', value: '' })).toMatchSnapshot()
14+
expect(codeReducer(prevState, { type: 'loading', value: '' })).toMatchSnapshot()
15+
expect(codeReducer(prevState, { type: 'loaded', value: 'foobar' })).toMatchSnapshot()
16+
expect(codeReducer(prevState, { type: 'error', value: 'ups' })).toMatchSnapshot()
17+
// @ts-expect-error
18+
expect(codeReducer(prevState, { type: 'unknown', value: '' })).toEqual(prevState)
19+
})
20+
21+
// export function codeReducer (prevState: any, { type, value }: DispatchMessage) {
22+
// switch (type) {
23+
// case 'reset': {
24+
// return initialFetchResultState;
25+
// }
26+
// case 'loading': {
27+
// return {...prevState, loading: true};
28+
// }
29+
// case 'loaded': {
30+
// return {...prevState, code: value, loading: false};
31+
// }
32+
// case 'error': {
33+
// return {...prevState, error: value, loading: false};
34+
// }
35+
// default:
36+
// return prevState;
37+
// }
38+
// }

tests/__snapshots__/ReferenceCodeBlock.test.ts.snap

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,36 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`codeReducer 1`] = `
4+
Object {
5+
"code": "loading...",
6+
"error": null,
7+
"loading": null,
8+
}
9+
`;
10+
11+
exports[`codeReducer 2`] = `
12+
Object {
13+
"foo": "bar",
14+
"loading": true,
15+
}
16+
`;
17+
18+
exports[`codeReducer 3`] = `
19+
Object {
20+
"code": "foobar",
21+
"foo": "bar",
22+
"loading": false,
23+
}
24+
`;
25+
26+
exports[`codeReducer 4`] = `
27+
Object {
28+
"error": "ups",
29+
"foo": "bar",
30+
"loading": false,
31+
}
32+
`;
33+
334
exports[`should parse GitHub reference properly 1`] = `
435
Object {
536
"fromLine": 0,

0 commit comments

Comments
 (0)