Commit 9c223756 authored by Winnie Hellmann's avatar Winnie Hellmann

Add helpers for HTML fixtures to Jest

parent 346f7027
/* eslint-disable import/prefer-default-export, global-require, import/no-dynamic-require */
import fs from 'fs'; import fs from 'fs';
import path from 'path'; import path from 'path';
...@@ -7,16 +5,32 @@ import { ErrorWithStack } from 'jest-util'; ...@@ -7,16 +5,32 @@ import { ErrorWithStack } from 'jest-util';
const fixturesBasePath = path.join(process.cwd(), 'spec', 'javascripts', 'fixtures'); const fixturesBasePath = path.join(process.cwd(), 'spec', 'javascripts', 'fixtures');
export function getJSONFixture(relativePath, ee = false) { export function getFixture(relativePath) {
const absolutePath = path.join(fixturesBasePath, ee ? 'ee' : '', relativePath); const absolutePath = path.join(fixturesBasePath, relativePath);
if (!fs.existsSync(absolutePath)) { if (!fs.existsSync(absolutePath)) {
throw new ErrorWithStack( throw new ErrorWithStack(
`Fixture file ${relativePath} does not exist. `Fixture file ${relativePath} does not exist.
Did you run bin/rake karma:fixtures?`, Did you run bin/rake karma:fixtures?`,
getJSONFixture, getFixture,
); );
} }
return require(absolutePath); 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);
};
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment