Add helpers for HTML fixtures to Jest
This commit is contained in:
parent
346f7027e2
commit
9c2237569d
|
@ -1,5 +1,3 @@
|
|||
/* 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);
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue