@papillonarts/setup 0.39.0 → 0.41.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.
@@ -1,3 +1,4 @@
1
1
  export { getJestSetup } from './config';
2
2
  export { runJestSetup } from './setup';
3
+ export { renderSnapshot, renderSnapshotAsync, createMockStore, waitFor } from './utility';
3
4
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/jest/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,UAAU,CAAA;AACvC,OAAO,EAAE,YAAY,EAAE,MAAM,SAAS,CAAA"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/jest/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,UAAU,CAAA;AACvC,OAAO,EAAE,YAAY,EAAE,MAAM,SAAS,CAAA;AACtC,OAAO,EAAE,cAAc,EAAE,mBAAmB,EAAE,eAAe,EAAE,OAAO,EAAE,MAAM,WAAW,CAAA"}
@@ -3,17 +3,42 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
+ Object.defineProperty(exports, "createMockStore", {
7
+ enumerable: true,
8
+ get: function get() {
9
+ return _utility.createMockStore;
10
+ }
11
+ });
6
12
  Object.defineProperty(exports, "getJestSetup", {
7
13
  enumerable: true,
8
14
  get: function get() {
9
15
  return _config.getJestSetup;
10
16
  }
11
17
  });
18
+ Object.defineProperty(exports, "renderSnapshot", {
19
+ enumerable: true,
20
+ get: function get() {
21
+ return _utility.renderSnapshot;
22
+ }
23
+ });
24
+ Object.defineProperty(exports, "renderSnapshotAsync", {
25
+ enumerable: true,
26
+ get: function get() {
27
+ return _utility.renderSnapshotAsync;
28
+ }
29
+ });
12
30
  Object.defineProperty(exports, "runJestSetup", {
13
31
  enumerable: true,
14
32
  get: function get() {
15
33
  return _setup.runJestSetup;
16
34
  }
17
35
  });
36
+ Object.defineProperty(exports, "waitFor", {
37
+ enumerable: true,
38
+ get: function get() {
39
+ return _utility.waitFor;
40
+ }
41
+ });
18
42
  var _config = require("./config");
19
- var _setup = require("./setup");
43
+ var _setup = require("./setup");
44
+ var _utility = require("./utility");
@@ -1 +1 @@
1
- {"version":3,"file":"setup.d.ts","sourceRoot":"","sources":["../../src/jest/setup.ts"],"names":[],"mappings":"AAGA,OAAO,uBAAuB,CAAA;AAG9B,wBAAgB,YAAY,SAS3B"}
1
+ {"version":3,"file":"setup.d.ts","sourceRoot":"","sources":["../../src/jest/setup.ts"],"names":[],"mappings":"AAGA,OAAO,uBAAuB,CAAA;AAG9B,wBAAgB,YAAY,SAmG3B"}
@@ -11,6 +11,88 @@ var _snapshotDiff = require("snapshot-diff");
11
11
  // https://github.com/jest-community/snapshot-diff
12
12
 
13
13
  function runJestSetup() {
14
+ /**
15
+ * Normalizes Date.prototype methods to output deterministic ISO-like UTC strings.
16
+ * This prevents snapshot churn across different environments/timezones.
17
+ *
18
+ * Format: yyyy-MM-ddTHH:mm:ssZ (24-hour, explicit UTC designator)
19
+ * Timezone: UTC (set via process.env.TZ)
20
+ *
21
+ * @example
22
+ * new Date('2020-07-05T19:04:15Z').toLocaleString() // "2020-07-05T19:04:15Z"
23
+ */
24
+
25
+ // Force deterministic date/time output in tests (avoids locale/timezone snapshot churn)
26
+ process.env.TZ = 'UTC';
27
+
28
+ /**
29
+ * Pads a number with leading zero if less than 10.
30
+ * @param {number} n - Number to pad
31
+ * @returns {string} Padded string
32
+ */
33
+ function pad2(n) {
34
+ return n < 10 ? "0".concat(n) : "".concat(n);
35
+ }
36
+
37
+ /**
38
+ * Formats a date-time as "yyyy-MM-ddTHH:mm:ssZ" in UTC.
39
+ * @param {Date} date - Date to format
40
+ * @returns {string} Formatted ISO-like date-time string
41
+ */
42
+ function formatAsISODateTime(date) {
43
+ var d = pad2(date.getUTCDate());
44
+ var m = pad2(date.getUTCMonth() + 1);
45
+ var y = date.getUTCFullYear();
46
+ var hh = pad2(date.getUTCHours());
47
+ var mm = pad2(date.getUTCMinutes());
48
+ var ss = pad2(date.getUTCSeconds());
49
+ return "".concat(y, "-").concat(m, "-").concat(d, "T").concat(hh, ":").concat(mm, ":").concat(ss, "Z");
50
+ }
51
+
52
+ /**
53
+ * Formats a date as "yyyy-MM-dd" in UTC.
54
+ * @param {Date} date - Date to format
55
+ * @returns {string} Formatted ISO-like date string
56
+ */
57
+ function formatAsISODate(date) {
58
+ var d = pad2(date.getUTCDate());
59
+ var m = pad2(date.getUTCMonth() + 1);
60
+ var y = date.getUTCFullYear();
61
+ return "".concat(y, "-").concat(m, "-").concat(d);
62
+ }
63
+
64
+ /**
65
+ * Formats a time as "HH:mm:ssZ" in UTC.
66
+ * @param {Date} date - Date to format
67
+ * @returns {string} Formatted ISO-like time string
68
+ */
69
+ function formatAsISOTime(date) {
70
+ var hh = pad2(date.getUTCHours());
71
+ var mm = pad2(date.getUTCMinutes());
72
+ var ss = pad2(date.getUTCSeconds());
73
+ return "".concat(hh, ":").concat(mm, ":").concat(ss, "Z");
74
+ }
75
+ var originalToLocaleString = Date.prototype.toLocaleString;
76
+ var originalToLocaleDateString = Date.prototype.toLocaleDateString;
77
+ var originalToLocaleTimeString = Date.prototype.toLocaleTimeString;
78
+ Date.prototype.toLocaleString = function toLocaleStringPatched(locale, options) {
79
+ if (!locale && !options) {
80
+ return formatAsISODateTime(this);
81
+ }
82
+ return originalToLocaleString.call(this, locale, options);
83
+ };
84
+ Date.prototype.toLocaleDateString = function toLocaleDateStringPatched(locale, options) {
85
+ if (!locale && !options) {
86
+ return formatAsISODate(this);
87
+ }
88
+ return originalToLocaleDateString.call(this, locale, options);
89
+ };
90
+ Date.prototype.toLocaleTimeString = function toLocaleTimeStringPatched(locale, options) {
91
+ if (!locale && !options) {
92
+ return formatAsISOTime(this);
93
+ }
94
+ return originalToLocaleTimeString.call(this, locale, options);
95
+ };
14
96
  global.act = _react.act;
15
97
  global.cleanup = _react.cleanup;
16
98
  global.fireEvent = _react.fireEvent;
@@ -0,0 +1,73 @@
1
+ /**
2
+ * Shared test utilities for reducing boilerplate in test files.
3
+ *
4
+ * These utilities wrap common testing patterns used across the codebase,
5
+ * particularly for snapshot testing with React components.
6
+ */
7
+ /**
8
+ * Renders a component and matches it against a snapshot.
9
+ *
10
+ * @param component - JSX element to render
11
+ * @returns Snapshot matcher result
12
+ *
13
+ * @example
14
+ * ```typescript
15
+ * import { renderSnapshot } from '../__tests__/test-utils'
16
+ * import { defaults } from './Component.feature'
17
+ *
18
+ * test('must match defaults()', () => {
19
+ * renderSnapshot(defaults())
20
+ * })
21
+ * ```
22
+ */
23
+ import type { ReactElement } from 'react';
24
+ export declare const renderSnapshot: (component: ReactElement) => void;
25
+ /**
26
+ * Renders an async component and matches it against a snapshot.
27
+ * Wraps the render in React's act() to ensure all updates are flushed.
28
+ *
29
+ * @param component - JSX element to render (can be a Promise)
30
+ * @returns Promise that resolves to snapshot matcher result
31
+ *
32
+ * @example
33
+ * ```typescript
34
+ * import { renderSnapshotAsync } from '../__tests__/test-utils'
35
+ * import { async } from './Component.feature'
36
+ *
37
+ * test('must match async()', async () => {
38
+ * await renderSnapshotAsync(async())
39
+ * })
40
+ * ```
41
+ */
42
+ export declare const renderSnapshotAsync: (component: ReactElement | Promise<ReactElement>) => Promise<void>;
43
+ /**
44
+ * Creates a mock store with default configuration.
45
+ * Useful for testing Redux-connected components.
46
+ *
47
+ * @param initialState - Optional initial state for the store
48
+ * @returns Mocked Redux store
49
+ *
50
+ * @example
51
+ * ```typescript
52
+ * import { createMockStore } from '../__tests__/test-utils'
53
+ *
54
+ * const store = createMockStore({ context: { cars: [] } })
55
+ * ```
56
+ */
57
+ export declare const createMockStore: (initialState?: {}) => {};
58
+ /**
59
+ * Waits for a condition to be true with timeout.
60
+ * Useful for testing async state updates.
61
+ *
62
+ * @param condition - Function that returns true when condition is met
63
+ * @param timeout - Maximum time to wait in milliseconds (default: 1000)
64
+ * @param interval - Check interval in milliseconds (default: 50)
65
+ * @returns Promise that resolves when condition is true
66
+ *
67
+ * @example
68
+ * ```typescript
69
+ * await waitFor(() => element.textContent === 'Loaded', 2000)
70
+ * ```
71
+ */
72
+ export declare const waitFor: (condition: () => boolean, timeout?: number, interval?: number) => Promise<void>;
73
+ //# sourceMappingURL=utility.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utility.d.ts","sourceRoot":"","sources":["../../src/jest/utility.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH;;;;;;;;;;;;;;;GAeG;AACH,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,OAAO,CAAA;AAEzC,eAAO,MAAM,cAAc,GAAI,WAAW,YAAY,SAA6D,CAAA;AAEnH;;;;;;;;;;;;;;;;GAgBG;AACH,eAAO,MAAM,mBAAmB,GAAU,WAAW,YAAY,GAAG,OAAO,CAAC,YAAY,CAAC,kBACW,CAAA;AAEpG;;;;;;;;;;;;;GAaG;AACH,eAAO,MAAM,eAAe,GAAI,iBAAiB,OAIhD,CAAA;AAED;;;;;;;;;;;;;GAaG;AACH,eAAO,MAAM,OAAO,GAAU,WAAW,MAAM,OAAO,EAAE,gBAAc,EAAE,iBAAa,KAAG,OAAO,CAAC,IAAI,CASnG,CAAA"}
@@ -0,0 +1,169 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+ Object.defineProperty(exports, "__esModule", {
5
+ value: true
6
+ });
7
+ exports.waitFor = exports.renderSnapshotAsync = exports.renderSnapshot = exports.createMockStore = void 0;
8
+ var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
9
+ var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
10
+ /**
11
+ * Shared test utilities for reducing boilerplate in test files.
12
+ *
13
+ * These utilities wrap common testing patterns used across the codebase,
14
+ * particularly for snapshot testing with React components.
15
+ */
16
+
17
+ /**
18
+ * Renders a component and matches it against a snapshot.
19
+ *
20
+ * @param component - JSX element to render
21
+ * @returns Snapshot matcher result
22
+ *
23
+ * @example
24
+ * ```typescript
25
+ * import { renderSnapshot } from '../__tests__/test-utils'
26
+ * import { defaults } from './Component.feature'
27
+ *
28
+ * test('must match defaults()', () => {
29
+ * renderSnapshot(defaults())
30
+ * })
31
+ * ```
32
+ */
33
+
34
+ var renderSnapshot = exports.renderSnapshot = function renderSnapshot(component) {
35
+ return expect(global.renderToJSON(component)).toMatchSnapshot();
36
+ };
37
+
38
+ /**
39
+ * Renders an async component and matches it against a snapshot.
40
+ * Wraps the render in React's act() to ensure all updates are flushed.
41
+ *
42
+ * @param component - JSX element to render (can be a Promise)
43
+ * @returns Promise that resolves to snapshot matcher result
44
+ *
45
+ * @example
46
+ * ```typescript
47
+ * import { renderSnapshotAsync } from '../__tests__/test-utils'
48
+ * import { async } from './Component.feature'
49
+ *
50
+ * test('must match async()', async () => {
51
+ * await renderSnapshotAsync(async())
52
+ * })
53
+ * ```
54
+ */
55
+ var renderSnapshotAsync = exports.renderSnapshotAsync = /*#__PURE__*/function () {
56
+ var _ref = (0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator["default"].mark(function _callee2(component) {
57
+ var _t2;
58
+ return _regenerator["default"].wrap(function (_context2) {
59
+ while (1) switch (_context2.prev = _context2.next) {
60
+ case 0:
61
+ _t2 = expect;
62
+ _context2.next = 1;
63
+ return global.act(/*#__PURE__*/(0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator["default"].mark(function _callee() {
64
+ var _t;
65
+ return _regenerator["default"].wrap(function (_context) {
66
+ while (1) switch (_context.prev = _context.next) {
67
+ case 0:
68
+ _t = global;
69
+ _context.next = 1;
70
+ return component;
71
+ case 1:
72
+ return _context.abrupt("return", _t.renderToJSON.call(_t, _context.sent));
73
+ case 2:
74
+ case "end":
75
+ return _context.stop();
76
+ }
77
+ }, _callee);
78
+ })));
79
+ case 1:
80
+ _context2.next = 2;
81
+ return _t2(_context2.sent).toMatchSnapshot();
82
+ case 2:
83
+ return _context2.abrupt("return", _context2.sent);
84
+ case 3:
85
+ case "end":
86
+ return _context2.stop();
87
+ }
88
+ }, _callee2);
89
+ }));
90
+ return function renderSnapshotAsync(_x) {
91
+ return _ref.apply(this, arguments);
92
+ };
93
+ }();
94
+
95
+ /**
96
+ * Creates a mock store with default configuration.
97
+ * Useful for testing Redux-connected components.
98
+ *
99
+ * @param initialState - Optional initial state for the store
100
+ * @returns Mocked Redux store
101
+ *
102
+ * @example
103
+ * ```typescript
104
+ * import { createMockStore } from '../__tests__/test-utils'
105
+ *
106
+ * const store = createMockStore({ context: { cars: [] } })
107
+ * ```
108
+ */
109
+ var createMockStore = exports.createMockStore = function createMockStore() {
110
+ var initialState = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
111
+ // This is a placeholder - implement based on your store configuration
112
+ // You may need to import from redux-mock-store or similar
113
+ return initialState;
114
+ };
115
+
116
+ /**
117
+ * Waits for a condition to be true with timeout.
118
+ * Useful for testing async state updates.
119
+ *
120
+ * @param condition - Function that returns true when condition is met
121
+ * @param timeout - Maximum time to wait in milliseconds (default: 1000)
122
+ * @param interval - Check interval in milliseconds (default: 50)
123
+ * @returns Promise that resolves when condition is true
124
+ *
125
+ * @example
126
+ * ```typescript
127
+ * await waitFor(() => element.textContent === 'Loaded', 2000)
128
+ * ```
129
+ */
130
+ var waitFor = exports.waitFor = /*#__PURE__*/function () {
131
+ var _ref3 = (0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator["default"].mark(function _callee3(condition) {
132
+ var timeout,
133
+ interval,
134
+ startTime,
135
+ _args3 = arguments;
136
+ return _regenerator["default"].wrap(function (_context3) {
137
+ while (1) switch (_context3.prev = _context3.next) {
138
+ case 0:
139
+ timeout = _args3.length > 1 && _args3[1] !== undefined ? _args3[1] : 1000;
140
+ interval = _args3.length > 2 && _args3[2] !== undefined ? _args3[2] : 50;
141
+ startTime = Date.now();
142
+ case 1:
143
+ if (condition()) {
144
+ _context3.next = 4;
145
+ break;
146
+ }
147
+ if (!(Date.now() - startTime > timeout)) {
148
+ _context3.next = 2;
149
+ break;
150
+ }
151
+ throw new Error("Timeout waiting for condition after ".concat(timeout, "ms"));
152
+ case 2:
153
+ _context3.next = 3;
154
+ return new Promise(function (resolve) {
155
+ return setTimeout(resolve, interval);
156
+ });
157
+ case 3:
158
+ _context3.next = 1;
159
+ break;
160
+ case 4:
161
+ case "end":
162
+ return _context3.stop();
163
+ }
164
+ }, _callee3);
165
+ }));
166
+ return function waitFor(_x2) {
167
+ return _ref3.apply(this, arguments);
168
+ };
169
+ }();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@papillonarts/setup",
3
- "version": "0.39.0",
3
+ "version": "0.41.0",
4
4
  "description": "Papillon Arts Setup",
5
5
  "homepage": "https://github.com/papillonarts/papillonarts/tree/master/packages/setup",
6
6
  "repository": {
@@ -37,5 +37,5 @@
37
37
  "build-acceptance": "npm run build",
38
38
  "build-release": "npm run build"
39
39
  },
40
- "gitHead": "b8f07f71414d7133bc1cfa967ecf30ebca383ef7"
40
+ "gitHead": "38632df415e9a45e174ea18880ce31b17f8ae2e5"
41
41
  }