resumefy
Version:
A simple toolkit to bring your JSON Resume to life
63 lines (62 loc) • 2.46 kB
JavaScript
import { describe, it, expect, vi, afterEach } from 'vitest';
import * as utils from '../render/utils.js';
import * as writeFile from '../init.js';
import { log } from './log.js';
import { init } from './init.js';
vi.mock('ansicolor', () => ({
underline: vi.fn((text) => text),
}));
vi.mock('../init', () => ({
init: vi.fn(),
}));
vi.mock('./log', () => ({
log: {
warn: vi.fn(),
success: vi.fn(),
},
}));
describe('init', () => {
const filename = 'test-resume.json';
const options = { theme: 'jsonresume-theme-even' };
const loadThemeSpy = vi.spyOn(utils, 'loadTheme');
const writeFileSpy = vi.spyOn(writeFile, 'init');
const successLog = 'Created file test-resume.json 🚀';
afterEach(() => {
vi.clearAllMocks();
});
it('should load the theme if specified', async () => {
await init(filename, options);
expect(utils.loadTheme).toHaveBeenCalledWith(options.theme);
});
describe('when loading theme fails', () => {
it('should log a warning with error message', async () => {
const error = new Error('Theme load error');
loadThemeSpy.mockRejectedValue(error);
await init(filename, options);
expect(log.warn).toHaveBeenCalledWith(error.message);
});
it('should log a warning with rejected value', async () => {
const error = 'Theme load error';
loadThemeSpy.mockRejectedValue(error);
await init(filename, options);
expect(log.warn).toHaveBeenCalledWith(error);
});
});
it('should write the file with the specified theme', async () => {
await init(filename, options);
expect(writeFileSpy).toHaveBeenCalledWith(filename, options.theme);
expect(log.success).toHaveBeenCalledWith(successLog);
});
it('should write the file without a theme if none is specified', async () => {
const noThemeOptions = {};
await init(filename, noThemeOptions);
expect(writeFileSpy).toHaveBeenCalledWith(filename, undefined);
expect(log.success).toHaveBeenCalledWith(successLog);
});
it('should throw an error if the theme write fails', async () => {
const error = new Error('Write error');
writeFileSpy.mockRejectedValue(error);
await expect(() => init(filename, options)).rejects.toThrow(error);
expect(log.success).not.toHaveBeenCalled();
});
});