提交 9c223756 编写于 作者: W Winnie Hellmann

Add helpers for HTML fixtures to Jest

上级 346f7027
/* eslint-disable import/prefer-default-export, global-require, import/no-dynamic-require */
import fs from 'fs';
import path from 'path';
......@@ -7,16 +5,32 @@ import { ErrorWithStack } from 'jest-util';
const fixturesBasePath = path.join(process.cwd(), 'spec', 'javascripts', 'fixtures');
export function getJSONFixture(relativePath, ee = false) {
const absolutePath = path.join(fixturesBasePath, ee ? 'ee' : '', relativePath);
export function getFixture(relativePath) {
const absolutePath = path.join(fixturesBasePath, relativePath);
if (!fs.existsSync(absolutePath)) {
throw new ErrorWithStack(
`Fixture file ${relativePath} does not exist.
Did you run bin/rake karma:fixtures?`,
getJSONFixture,
getFixture,
);
}
return require(absolutePath);
return fs.readFileSync(absolutePath, 'utf8');
}
export const getJSONFixture = relativePath => JSON.parse(getFixture(relativePath));
export const resetHTMLFixture = () => {
document.body.textContent = '';
};
export const setHTMLFixture = (htmlContent, resetHook = afterEach) => {
document.body.outerHTML = htmlContent;
resetHook(resetHTMLFixture);
};
export const loadHTMLFixture = (relativePath, resetHook = afterEach) => {
const fileContent = getFixture(relativePath);
setHTMLFixture(fileContent, resetHook);
};
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册