Skip to content

Commit

Permalink
fixup! squash! test: unit tests for the cli (#459) (#469)
Browse files Browse the repository at this point in the history
  • Loading branch information
antialias committed Jan 15, 2021
1 parent a13e63b commit e31a7f6
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 36 deletions.
27 changes: 12 additions & 15 deletions lib/render-html.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,29 @@
const tryResolve = (...args) => {
import { join } from 'path';

const tryRequire = (...args) => {
try {
return require.resolve(...args);
return require(...args);
} catch (err) {
return false;
}
};

export default async ({ resume, themePath }) => {
const cwd = process.cwd();
let path;
let theme;
if (themePath[0] === '.') {
path = tryResolve(path.join(cwd, themePath), { paths: [cwd] });
throw new Error(
`Theme ${themePath} could not be resolved relative to ${cwd}`,
);
}
if (!path) {
path = tryResolve(themePath, { paths: [cwd] });
theme = tryRequire(join(cwd, themePath));
} else {
theme = tryRequire(themePath);
}
if (!path && /^[a-z0-9]/i.test(path)) {
path = tryResolve(`jsonresume-theme-${themePath}`, { paths: [cwd] });
if (!theme && /^[a-z0-9-]+$/i.test(themePath)) {
theme = tryRequire(`jsonresume-theme-${themePath}`);
}
if (!path) {
if (!theme) {
throw new Error(
`theme path ${themePath} could not be resolved from current working directory`,
`theme path ${themePath} could not be resolved. cwd is ${cwd}`,
);
}
const theme = require(path);
if (typeof theme?.render !== 'function') {
throw new Error('theme.render is not a function');
}
Expand Down
53 changes: 32 additions & 21 deletions lib/render-html.test.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
import renderHTML from './render-html';

describe('renderHTML', () => {
beforeAll(() => {
const originalRequireResolve = require.resolve;
const mockThemePath = 'mock/path/to/jsonresume-theme-even';
require.resolve = (...args) => {
if (args[0] === 'jsonresume-theme-even') {
return mockThemePath;
}
if (args[0] === 'jsonresume-theme-even') {
return mockThemePath;
}
return originalRequireResolve.apply(require, ...args);
jest.mock(
'jsonresume-theme-even',
() => {
return {
render: () => 'hello from theme even',
};
require.cache[mockThemePath] = {
render: () => 'here-is-your-mocked-theme',
},
{ virtual: true },
);
jest.mock(
'/the/mocked/cwd/some-local-theme',
() => {
return {
render: () => 'hello from the local mocked theme',
};
},
{ virtual: true },
);
describe('renderHTML', () => {
beforeAll(() => {
const localPath = '/the/mocked/cwd/';
process.cwd = jest.fn().mockReturnValue(localPath);
});
const resume = {
basics: {
Expand All @@ -31,17 +37,22 @@ describe('renderHTML', () => {
).rejects.toBeTruthy();
});

describe('should render html when theme is availlable', () => {
it('with long theme name', async () => {
describe('when theme is availlable', () => {
it('should resolve from cwd when themePath starts with a period', async () => {
expect(
await renderHTML({ resume, themePath: './some-local-theme' }),
).toMatchInlineSnapshot(`"hello from the local mocked theme"`);
});
it('should render html with long theme name', async () => {
expect(
await renderHTML({ resume, themePath: 'jsonresume-theme-even' }),
).toStartWith('<!doctype html>');
).toMatchInlineSnapshot(`"hello from theme even"`);
});

it('with short theme name', async () => {
expect(await renderHTML({ resume, themePath: 'even' })).toStartWith(
'<!doctype html>',
);
it('should render html with short theme name', async () => {
expect(
await renderHTML({ resume, themePath: 'even' }),
).toMatchInlineSnapshot(`"hello from theme even"`);
});
});
});

0 comments on commit e31a7f6

Please sign in to comment.