debian-mirror-gitlab/spec/frontend/helpers/fixtures.js

35 lines
927 B
JavaScript
Raw Normal View History

2019-07-07 11:18:12 +05:30
import fs from 'fs';
import path from 'path';
import { ErrorWithStack } from 'jest-util';
export function getFixture(relativePath) {
2019-07-31 22:56:46 +05:30
const absolutePath = path.join(global.fixturesBasePath, relativePath);
2019-07-07 11:18:12 +05:30
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);
};