gitlab-org--gitlab-foss/spec/frontend/helpers/fixtures.js
2019-07-19 02:27:14 -05:00

37 lines
1 KiB
JavaScript

import fs from 'fs';
import path from 'path';
import { ErrorWithStack } from 'jest-util';
export function getFixture(relativePath) {
const basePath = relativePath.startsWith('static/')
? global.staticFixturesBasePath
: global.fixturesBasePath;
const absolutePath = path.join(basePath, relativePath);
if (!fs.existsSync(absolutePath)) {
throw new ErrorWithStack(
`Fixture file ${relativePath} does not exist.
Did you run bin/rake frontend: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);
};