debian-mirror-gitlab/spec/frontend/__helpers__/fixtures.js
2022-07-16 19:58:13 +02:00

34 lines
858 B
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 resetHTMLFixture = () => {
document.head.innerHTML = '';
document.body.innerHTML = '';
};
export const setHTMLFixture = (htmlContent) => {
document.body.innerHTML = htmlContent;
};
export const loadHTMLFixture = (relativePath) => {
setHTMLFixture(getFixture(relativePath));
};