Welcome to mirror list, hosted at ThFree Co, Russian Federation.

assert_props.js « __helpers__ « frontend « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 9935719580a4a8909d5b54bfc2de740a3df73365 (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
import { mount } from '@vue/test-utils';
import { ErrorWithStack } from 'jest-util';

function installConsoleHandler(method) {
  const originalHandler = global.console[method];

  global.console[method] = function throwableHandler(...args) {
    if (args[0]?.includes('Invalid prop') || args[0]?.includes('Missing required prop')) {
      throw new ErrorWithStack(
        `Unexpected call of console.${method}() with:\n\n${args.join(', ')}`,
        this[method],
      );
    }

    originalHandler.apply(this, args);
  };

  return function restore() {
    global.console[method] = originalHandler;
  };
}

export function assertProps(Component, props, extraMountArgs = {}) {
  const [restoreError, restoreWarn] = [
    installConsoleHandler('error'),
    installConsoleHandler('warn'),
  ];
  const ComponentWithoutRenderFn = {
    ...Component,
    render() {
      return '';
    },
  };

  try {
    mount(ComponentWithoutRenderFn, { propsData: props, ...extraMountArgs });
  } finally {
    restoreError();
    restoreWarn();
  }
}