Skip to content

Commit e31a7f6

Browse files
author
antialias
committed
fixup! squash! test: unit tests for the cli (#459) (#469)
1 parent a13e63b commit e31a7f6

2 files changed

Lines changed: 44 additions & 36 deletions

File tree

lib/render-html.js

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,29 @@
1-
const tryResolve = (...args) => {
1+
import { join } from 'path';
2+
3+
const tryRequire = (...args) => {
24
try {
3-
return require.resolve(...args);
5+
return require(...args);
46
} catch (err) {
57
return false;
68
}
79
};
810

911
export default async ({ resume, themePath }) => {
1012
const cwd = process.cwd();
11-
let path;
13+
let theme;
1214
if (themePath[0] === '.') {
13-
path = tryResolve(path.join(cwd, themePath), { paths: [cwd] });
14-
throw new Error(
15-
`Theme ${themePath} could not be resolved relative to ${cwd}`,
16-
);
17-
}
18-
if (!path) {
19-
path = tryResolve(themePath, { paths: [cwd] });
15+
theme = tryRequire(join(cwd, themePath));
16+
} else {
17+
theme = tryRequire(themePath);
2018
}
21-
if (!path && /^[a-z0-9]/i.test(path)) {
22-
path = tryResolve(`jsonresume-theme-${themePath}`, { paths: [cwd] });
19+
if (!theme && /^[a-z0-9-]+$/i.test(themePath)) {
20+
theme = tryRequire(`jsonresume-theme-${themePath}`);
2321
}
24-
if (!path) {
22+
if (!theme) {
2523
throw new Error(
26-
`theme path ${themePath} could not be resolved from current working directory`,
24+
`theme path ${themePath} could not be resolved. cwd is ${cwd}`,
2725
);
2826
}
29-
const theme = require(path);
3027
if (typeof theme?.render !== 'function') {
3128
throw new Error('theme.render is not a function');
3229
}

lib/render-html.test.js

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,27 @@
11
import renderHTML from './render-html';
22

3-
describe('renderHTML', () => {
4-
beforeAll(() => {
5-
const originalRequireResolve = require.resolve;
6-
const mockThemePath = 'mock/path/to/jsonresume-theme-even';
7-
require.resolve = (...args) => {
8-
if (args[0] === 'jsonresume-theme-even') {
9-
return mockThemePath;
10-
}
11-
if (args[0] === 'jsonresume-theme-even') {
12-
return mockThemePath;
13-
}
14-
return originalRequireResolve.apply(require, ...args);
3+
jest.mock(
4+
'jsonresume-theme-even',
5+
() => {
6+
return {
7+
render: () => 'hello from theme even',
158
};
16-
require.cache[mockThemePath] = {
17-
render: () => 'here-is-your-mocked-theme',
9+
},
10+
{ virtual: true },
11+
);
12+
jest.mock(
13+
'/the/mocked/cwd/some-local-theme',
14+
() => {
15+
return {
16+
render: () => 'hello from the local mocked theme',
1817
};
18+
},
19+
{ virtual: true },
20+
);
21+
describe('renderHTML', () => {
22+
beforeAll(() => {
23+
const localPath = '/the/mocked/cwd/';
24+
process.cwd = jest.fn().mockReturnValue(localPath);
1925
});
2026
const resume = {
2127
basics: {
@@ -31,17 +37,22 @@ describe('renderHTML', () => {
3137
).rejects.toBeTruthy();
3238
});
3339

34-
describe('should render html when theme is availlable', () => {
35-
it('with long theme name', async () => {
40+
describe('when theme is availlable', () => {
41+
it('should resolve from cwd when themePath starts with a period', async () => {
42+
expect(
43+
await renderHTML({ resume, themePath: './some-local-theme' }),
44+
).toMatchInlineSnapshot(`"hello from the local mocked theme"`);
45+
});
46+
it('should render html with long theme name', async () => {
3647
expect(
3748
await renderHTML({ resume, themePath: 'jsonresume-theme-even' }),
38-
).toStartWith('<!doctype html>');
49+
).toMatchInlineSnapshot(`"hello from theme even"`);
3950
});
4051

41-
it('with short theme name', async () => {
42-
expect(await renderHTML({ resume, themePath: 'even' })).toStartWith(
43-
'<!doctype html>',
44-
);
52+
it('should render html with short theme name', async () => {
53+
expect(
54+
await renderHTML({ resume, themePath: 'even' }),
55+
).toMatchInlineSnapshot(`"hello from theme even"`);
4556
});
4657
});
4758
});

0 commit comments

Comments
 (0)