summaryrefslogtreecommitdiff
path: root/spec
diff options
context:
space:
mode:
authorWinnie Hellmann <winnie@gitlab.com>2019-03-27 14:50:11 +0100
committerWinnie Hellmann <winnie@gitlab.com>2019-03-29 07:33:30 +0100
commit9c2237569d9ff9dcca8182de794506b5500ef024 (patch)
tree8ff44a6196d27703e401285b6e2f972e5bf7c91a /spec
parent346f7027e2285b3ff1280197045c4db541f4a700 (diff)
downloadgitlab-ce-9c2237569d9ff9dcca8182de794506b5500ef024.tar.gz
Add helpers for HTML fixtures to Jest
Diffstat (limited to 'spec')
-rw-r--r--spec/frontend/helpers/fixtures.js26
1 files changed, 20 insertions, 6 deletions
diff --git a/spec/frontend/helpers/fixtures.js b/spec/frontend/helpers/fixtures.js
index de9058d7832..f0351aa31c6 100644
--- a/spec/frontend/helpers/fixtures.js
+++ b/spec/frontend/helpers/fixtures.js
@@ -1,5 +1,3 @@
-/* eslint-disable import/prefer-default-export, global-require, import/no-dynamic-require */
-
import fs from 'fs';
import path from 'path';
@@ -7,16 +5,32 @@ import { ErrorWithStack } from 'jest-util';
const fixturesBasePath = path.join(process.cwd(), 'spec', 'javascripts', 'fixtures');
-export function getJSONFixture(relativePath, ee = false) {
- const absolutePath = path.join(fixturesBasePath, ee ? 'ee' : '', relativePath);
+export function getFixture(relativePath) {
+ const absolutePath = path.join(fixturesBasePath, relativePath);
if (!fs.existsSync(absolutePath)) {
throw new ErrorWithStack(
`Fixture file ${relativePath} does not exist.
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);
+};