fbac16b2de
(cherry picked from commit 2917a28a885922a03f2026ddbb2680bc2b6e5d50)
34 lines
927 B
JavaScript
34 lines
927 B
JavaScript
import fs from 'fs';
|
|
import path from 'path';
|
|
|
|
import { ErrorWithStack } from 'jest-util';
|
|
|
|
export function getFixture(relativePath) {
|
|
const absolutePath = path.join(global.fixturesBasePath, relativePath);
|
|
if (!fs.existsSync(absolutePath)) {
|
|
throw new ErrorWithStack(
|
|
`Fixture file ${relativePath} does not exist.
|
|
|
|
Did you run bin/rake karma:fixtures?`,
|
|
getFixture,
|
|
);
|
|
}
|
|
|
|
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);
|
|
};
|