@tramvai/test-helpers 2.26.2 → 2.28.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/index.es.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import { parse } from 'node-html-parser';
2
+ import prettier from 'prettier';
2
3
  import supertest from 'supertest';
3
4
  import { walkOfModules, getModuleParameters } from '@tramvai/core';
4
5
  import { createMockDi } from '@tramvai/test-mocks';
@@ -8,7 +9,8 @@ const parseHtml = (html, parserOptions = { blockTextElements: { script: false, s
8
9
  if (!html) {
9
10
  return null;
10
11
  }
11
- const parsed = parse(html, parserOptions);
12
+ const prettyHtml = prettier.format(html, { parser: 'html', printWidth: 120 });
13
+ const parsed = parse(prettyHtml, parserOptions);
12
14
  return {
13
15
  parsed,
14
16
  body: (_a = parsed.querySelector('body')) === null || _a === void 0 ? void 0 : _a.innerHTML,
@@ -17,9 +19,13 @@ const parseHtml = (html, parserOptions = { blockTextElements: { script: false, s
17
19
  };
18
20
  };
19
21
 
20
- const renderFactory = (request) => async (path, { method = 'get', parserOptions, } = {}) => {
22
+ const renderFactory = (request, { replaceDynamicStrings = {}, } = {}) => async (path, { method = 'get', parserOptions, } = {}) => {
21
23
  const response = await request(path, { method });
22
- const parsed = parseHtml(response.text, parserOptions);
24
+ let { text } = response;
25
+ for (const key in replaceDynamicStrings) {
26
+ text = text.replace(new RegExp(key, 'g'), replaceDynamicStrings[key]);
27
+ }
28
+ const parsed = parseHtml(text, parserOptions);
23
29
  if (!parsed) {
24
30
  throw new Error(`Cannot parse response
25
31
  got response:
@@ -29,12 +35,12 @@ const renderFactory = (request) => async (path, { method = 'get', parserOptions,
29
35
  return {
30
36
  ...parsed,
31
37
  get initialState() {
32
- var _a;
38
+ var _a, _b, _c;
33
39
  const parsedWithScripts = (_a = parseHtml(response.text, {})) === null || _a === void 0 ? void 0 : _a.parsed;
34
40
  if (!parsedWithScripts) {
35
41
  return;
36
42
  }
37
- return JSON.parse(parsedWithScripts.querySelector('#__TRAMVAI_STATE__').textContent);
43
+ return JSON.parse((_c = (_b = parsedWithScripts.querySelector('#__TRAMVAI_STATE__')) === null || _b === void 0 ? void 0 : _b.textContent) !== null && _c !== void 0 ? _c : 'null');
38
44
  },
39
45
  };
40
46
  };
package/lib/index.js CHANGED
@@ -3,12 +3,14 @@
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var nodeHtmlParser = require('node-html-parser');
6
+ var prettier = require('prettier');
6
7
  var supertest = require('supertest');
7
8
  var core = require('@tramvai/core');
8
9
  var testMocks = require('@tramvai/test-mocks');
9
10
 
10
11
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
11
12
 
13
+ var prettier__default = /*#__PURE__*/_interopDefaultLegacy(prettier);
12
14
  var supertest__default = /*#__PURE__*/_interopDefaultLegacy(supertest);
13
15
 
14
16
  const parseHtml = (html, parserOptions = { blockTextElements: { script: false, style: false } }) => {
@@ -16,7 +18,8 @@ const parseHtml = (html, parserOptions = { blockTextElements: { script: false, s
16
18
  if (!html) {
17
19
  return null;
18
20
  }
19
- const parsed = nodeHtmlParser.parse(html, parserOptions);
21
+ const prettyHtml = prettier__default["default"].format(html, { parser: 'html', printWidth: 120 });
22
+ const parsed = nodeHtmlParser.parse(prettyHtml, parserOptions);
20
23
  return {
21
24
  parsed,
22
25
  body: (_a = parsed.querySelector('body')) === null || _a === void 0 ? void 0 : _a.innerHTML,
@@ -25,9 +28,13 @@ const parseHtml = (html, parserOptions = { blockTextElements: { script: false, s
25
28
  };
26
29
  };
27
30
 
28
- const renderFactory = (request) => async (path, { method = 'get', parserOptions, } = {}) => {
31
+ const renderFactory = (request, { replaceDynamicStrings = {}, } = {}) => async (path, { method = 'get', parserOptions, } = {}) => {
29
32
  const response = await request(path, { method });
30
- const parsed = parseHtml(response.text, parserOptions);
33
+ let { text } = response;
34
+ for (const key in replaceDynamicStrings) {
35
+ text = text.replace(new RegExp(key, 'g'), replaceDynamicStrings[key]);
36
+ }
37
+ const parsed = parseHtml(text, parserOptions);
31
38
  if (!parsed) {
32
39
  throw new Error(`Cannot parse response
33
40
  got response:
@@ -37,12 +44,12 @@ const renderFactory = (request) => async (path, { method = 'get', parserOptions,
37
44
  return {
38
45
  ...parsed,
39
46
  get initialState() {
40
- var _a;
47
+ var _a, _b, _c;
41
48
  const parsedWithScripts = (_a = parseHtml(response.text, {})) === null || _a === void 0 ? void 0 : _a.parsed;
42
49
  if (!parsedWithScripts) {
43
50
  return;
44
51
  }
45
- return JSON.parse(parsedWithScripts.querySelector('#__TRAMVAI_STATE__').textContent);
52
+ return JSON.parse((_c = (_b = parsedWithScripts.querySelector('#__TRAMVAI_STATE__')) === null || _b === void 0 ? void 0 : _b.textContent) !== null && _c !== void 0 ? _c : 'null');
46
53
  },
47
54
  };
48
55
  };
package/lib/render.d.ts CHANGED
@@ -1,6 +1,8 @@
1
1
  import type { ParseOptions } from './parseHtml';
2
2
  import type { requestFactory } from './request';
3
- export declare const renderFactory: (request: ReturnType<typeof requestFactory>) => (path: string, { method, parserOptions, }?: {
3
+ export declare const renderFactory: (request: ReturnType<typeof requestFactory>, { replaceDynamicStrings, }?: {
4
+ replaceDynamicStrings?: Record<string, string> | undefined;
5
+ }) => (path: string, { method, parserOptions, }?: {
4
6
  method?: "get" | "post" | "put" | undefined;
5
7
  parserOptions?: Partial<import("node-html-parser").Options> | undefined;
6
8
  }) => Promise<{
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tramvai/test-helpers",
3
- "version": "2.26.2",
3
+ "version": "2.28.0",
4
4
  "description": "",
5
5
  "main": "lib/index.js",
6
6
  "typings": "lib/index.d.ts",
@@ -18,12 +18,13 @@
18
18
  "build-for-publish": "true"
19
19
  },
20
20
  "dependencies": {
21
- "@tramvai/core": "2.26.2",
22
- "@tramvai/test-mocks": "2.26.2",
21
+ "@tramvai/core": "2.28.0",
22
+ "@tramvai/test-mocks": "2.28.0",
23
23
  "@types/express": "^4.17.1",
24
24
  "@types/supertest": "^2.0.11",
25
25
  "fastify": "^3.29.0",
26
26
  "node-html-parser": "^3.0.4",
27
+ "prettier": "^2.0.0",
27
28
  "supertest": "^6.1.3"
28
29
  },
29
30
  "peerDependencies": {