2019-03-11 13:47:36 +00:00
|
|
|
import fs from 'fs';
|
|
|
|
import path from 'path';
|
|
|
|
|
|
|
|
import { ErrorWithStack } from 'jest-util';
|
|
|
|
|
|
|
|
const fixturesBasePath = path.join(process.cwd(), 'spec', 'javascripts', 'fixtures');
|
|
|
|
|
2019-03-27 13:50:11 +00:00
|
|
|
export function getFixture(relativePath) {
|
|
|
|
const absolutePath = path.join(fixturesBasePath, relativePath);
|
2019-03-11 13:47:36 +00:00
|
|
|
if (!fs.existsSync(absolutePath)) {
|
|
|
|
throw new ErrorWithStack(
|
|
|
|
`Fixture file ${relativePath} does not exist.
|
|
|
|
|
|
|
|
Did you run bin/rake karma:fixtures?`,
|
2019-03-27 13:50:11 +00:00
|
|
|
getFixture,
|
2019-03-11 13:47:36 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-03-27 13:50:11 +00:00
|
|
|
return fs.readFileSync(absolutePath, 'utf8');
|
2019-03-11 13:47:36 +00:00
|
|
|
}
|
2019-03-27 13:50:11 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
};
|