« get me outta code hell

_support.js « snapshots « test - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/test/snapshots/_support.js
blob: b51f28477dbbf482150dea31149316aa675d56f1 (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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import {quickEvaluate} from '../../src/content-function.js';
import {quickLoadContentDependencies} from '../../src/content/dependencies/index.js';

import chroma from 'chroma-js';
import * as html from '../../src/util/html.js';
import urlSpec from '../../src/url-spec.js';
import {getColors} from '../../src/util/colors.js';
import {generateURLs} from '../../src/util/urls.js';

export function testContentFunctions(t, message, fn) {
  const urls = generateURLs(urlSpec);

  t.test(message, async t => {
    const loadedContentDependencies = await quickLoadContentDependencies();

    const evaluate = ({
      from = 'localized.home',
      contentDependencies = {},
      extraDependencies = {},
      ...opts
    }) => {
      const {to} = urls.from(from);

      try {
        return quickEvaluate({
          ...opts,
          contentDependencies: {
            ...contentDependencies,
            ...loadedContentDependencies,
          },
          extraDependencies: {
            html,
            to,
            urls,
            appendIndexHTML: false,
            getColors: c => getColors(c, {chroma}),
            ...extraDependencies,
          },
        });
      } catch (error) {
        if (error instanceof AggregateError) {
          error = new Error(`AggregateError: ${error.message}\n${error.errors.map(err => `** ${err}`).join('\n')}`);
        }
        throw error;
      }
    };

    evaluate.snapshot = (opts, fn) => {
      const result = (fn ? fn(evaluate(opts)) : evaluate(opts));
      t.matchSnapshot(result.toString(), 'output');
    };

    return fn(t, evaluate);
  });
}