2019-03-11 09:47:36 -04:00
|
|
|
import fs from 'fs';
|
|
|
|
import path from 'path';
|
|
|
|
|
|
|
|
import { ErrorWithStack } from 'jest-util';
|
|
|
|
|
2019-03-27 09:50:11 -04:00
|
|
|
export function getFixture(relativePath) {
|
2019-07-17 18:47:33 -04:00
|
|
|
const basePath = relativePath.startsWith('static/')
|
|
|
|
? global.staticFixturesBasePath
|
|
|
|
: global.fixturesBasePath;
|
|
|
|
const absolutePath = path.join(basePath, relativePath);
|
2019-03-11 09:47:36 -04:00
|
|
|
if (!fs.existsSync(absolutePath)) {
|
|
|
|
throw new ErrorWithStack(
|
|
|
|
`Fixture file ${relativePath} does not exist.
|
|
|
|
|
2019-07-17 18:17:37 -04:00
|
|
|
Did you run bin/rake frontend:fixtures?`,
|
2019-03-27 09:50:11 -04:00
|
|
|
getFixture,
|
2019-03-11 09:47:36 -04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-03-27 09:50:11 -04:00
|
|
|
return fs.readFileSync(absolutePath, 'utf8');
|
2019-03-11 09:47:36 -04:00
|
|
|
}
|
2019-03-27 09:50:11 -04:00
|
|
|
|
2021-10-14 17:12:54 -04:00
|
|
|
/**
|
|
|
|
* @deprecated Use `import` to load a JSON fixture instead.
|
|
|
|
* See https://docs.gitlab.com/ee/development/testing_guide/frontend_testing.html#use-fixtures,
|
|
|
|
* https://gitlab.com/gitlab-org/gitlab/-/issues/339346.
|
|
|
|
*/
|
2020-12-23 16:10:24 -05:00
|
|
|
export const getJSONFixture = (relativePath) => JSON.parse(getFixture(relativePath));
|
2019-03-27 09:50:11 -04:00
|
|
|
|
|
|
|
export const resetHTMLFixture = () => {
|
2020-05-19 05:08:12 -04:00
|
|
|
document.head.innerHTML = '';
|
|
|
|
document.body.innerHTML = '';
|
2019-03-27 09:50:11 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
export const setHTMLFixture = (htmlContent, resetHook = afterEach) => {
|
2020-05-19 05:08:12 -04:00
|
|
|
document.body.innerHTML = htmlContent;
|
2019-03-27 09:50:11 -04:00
|
|
|
resetHook(resetHTMLFixture);
|
|
|
|
};
|
|
|
|
|
|
|
|
export const loadHTMLFixture = (relativePath, resetHook = afterEach) => {
|
|
|
|
const fileContent = getFixture(relativePath);
|
|
|
|
setHTMLFixture(fileContent, resetHook);
|
|
|
|
};
|