summaryrefslogtreecommitdiff
path: root/spec/frontend/helpers/fixtures.js
blob: f0351aa31c6575ea7a1c5b4397f2d47666a4ddf9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import fs from 'fs';
import path from 'path';

import { ErrorWithStack } from 'jest-util';

const fixturesBasePath = path.join(process.cwd(), 'spec', 'javascripts', 'fixtures');

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?`,
      getFixture,
    );
  }

  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);
};