gitlab-org--gitlab-foss/spec/frontend/__helpers__/fixtures.js

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

35 lines
858 B
JavaScript
Raw Normal View History

2019-03-11 13:47:36 +00:00
import fs from 'fs';
import path from 'path';
import { ErrorWithStack } from 'jest-util';
2019-03-27 13:50:11 +00:00
export function getFixture(relativePath) {
2019-07-17 22:47:33 +00:00
const basePath = relativePath.startsWith('static/')
? global.staticFixturesBasePath
: global.fixturesBasePath;
const absolutePath = path.join(basePath, 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 frontend: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 resetHTMLFixture = () => {
document.head.innerHTML = '';
document.body.innerHTML = '';
2019-03-27 13:50:11 +00:00
};
export const setHTMLFixture = (htmlContent) => {
document.body.innerHTML = htmlContent;
2019-03-27 13:50:11 +00:00
};
export const loadHTMLFixture = (relativePath) => {
setHTMLFixture(getFixture(relativePath));
2019-03-27 13:50:11 +00:00
};