@salesforce/pwa-kit-react-sdk 3.0.0-preview.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.
Files changed (53) hide show
  1. package/CHANGELOG.md +86 -0
  2. package/LICENSE +14 -0
  3. package/README.md +37 -0
  4. package/package.json +82 -0
  5. package/scripts/file-utils.js +80 -0
  6. package/scripts/file-utils.test.js +189 -0
  7. package/scripts/setup-jsdom.js +20 -0
  8. package/scripts/version.js +22 -0
  9. package/ssr/browser/main.js +122 -0
  10. package/ssr/browser/main.test.js +54 -0
  11. package/ssr/server/react-rendering.js +405 -0
  12. package/ssr/server/react-rendering.test.js +708 -0
  13. package/ssr/universal/compatibility.js +31 -0
  14. package/ssr/universal/components/_app/index.js +35 -0
  15. package/ssr/universal/components/_app/index.test.js +20 -0
  16. package/ssr/universal/components/_app-config/index.js +88 -0
  17. package/ssr/universal/components/_app-config/index.test.js +21 -0
  18. package/ssr/universal/components/_document/index.js +93 -0
  19. package/ssr/universal/components/_document/index.test.js +58 -0
  20. package/ssr/universal/components/_error/index.js +56 -0
  21. package/ssr/universal/components/_error/index.test.js +28 -0
  22. package/ssr/universal/components/app-error-boundary/index.js +115 -0
  23. package/ssr/universal/components/app-error-boundary/index.test.js +109 -0
  24. package/ssr/universal/components/fetch-strategy/index.js +42 -0
  25. package/ssr/universal/components/route-component/index.js +409 -0
  26. package/ssr/universal/components/route-component/index.test.js +375 -0
  27. package/ssr/universal/components/switch/index.js +63 -0
  28. package/ssr/universal/components/throw-404/index.js +37 -0
  29. package/ssr/universal/components/throw-404/index.test.js +26 -0
  30. package/ssr/universal/components/with-correlation-id/index.js +36 -0
  31. package/ssr/universal/components/with-legacy-get-props/index.js +86 -0
  32. package/ssr/universal/components/with-legacy-get-props/index.test.js +35 -0
  33. package/ssr/universal/components/with-react-query/index.js +103 -0
  34. package/ssr/universal/components/with-react-query/index.test.js +44 -0
  35. package/ssr/universal/contexts/index.js +71 -0
  36. package/ssr/universal/contexts/index.test.js +101 -0
  37. package/ssr/universal/errors.js +34 -0
  38. package/ssr/universal/errors.test.js +20 -0
  39. package/ssr/universal/events.js +40 -0
  40. package/ssr/universal/events.test.js +39 -0
  41. package/ssr/universal/hooks/index.js +52 -0
  42. package/ssr/universal/routes.js +16 -0
  43. package/ssr/universal/utils.client.test.js +46 -0
  44. package/ssr/universal/utils.js +60 -0
  45. package/ssr/universal/utils.server.test.js +24 -0
  46. package/utils/assets.js +120 -0
  47. package/utils/assets.test.js +106 -0
  48. package/utils/url.js +39 -0
  49. package/utils/url.test.js +47 -0
  50. package/utils/uuidv4.client.js +21 -0
  51. package/utils/uuidv4.client.test.js +27 -0
  52. package/utils/warnings.js +81 -0
  53. package/utils/warnings.test.js +48 -0
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+
3
+ var utils = _interopRequireWildcard(require("./utils"));
4
+ var _ssrShared = require("@salesforce/pwa-kit-runtime/utils/ssr-shared");
5
+ function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
6
+ function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
7
+ /**
8
+ * @jest-environment node
9
+ */
10
+ /*
11
+ * Copyright (c) 2022, Salesforce, Inc.
12
+ * All rights reserved.
13
+ * SPDX-License-Identifier: BSD-3-Clause
14
+ * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
15
+ */
16
+ // Jest requires the @jest-environment comment at the start of file, which
17
+ // conflicts with the eslint header rule.
18
+ /* eslint-disable header/header */
19
+
20
+ describe('getProxyConfigs (server-side)', () => {
21
+ test('should return the currently used proxy configs', () => {
22
+ expect(utils.getProxyConfigs()).toEqual(_ssrShared.proxyConfigs);
23
+ });
24
+ });
@@ -0,0 +1,120 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.loadScript = void 0;
7
+ function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
8
+ /*
9
+ * Copyright (c) 2021, salesforce.com, inc.
10
+ * All rights reserved.
11
+ * SPDX-License-Identifier: BSD-3-Clause
12
+ * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
13
+ */
14
+
15
+ /**
16
+ * loadScriptCounter is used as a key to the window.Mobify object handlers. We
17
+ * Want this outside the loadScript function so that it can be updated every
18
+ * loadScript is called with a new script to be embedded to the head tag.
19
+ * @private
20
+ */
21
+ let loadScriptCounter = 0;
22
+
23
+ /**
24
+ * Writes script tag to the document, head tag
25
+ * @function
26
+ * @param {string} id - The id for script
27
+ * @param {string} src - The path to script
28
+ * @param {boolean} isAsync=true - Writes an asynchronous function
29
+ * @param {boolean} docwrite=false - Writes a string of text to a document
30
+ * @param {function} onload - The onload callback function
31
+ * @param {boolean} onerror - Rejects the function
32
+ * @example
33
+ * import {loadScript} from '@salesforce/pwa-kit-react-sdk/utils/utils'
34
+ *
35
+ * loadScript({
36
+ * id: 'loadScriptTest1',
37
+ * src: 'loadScriptTest1src'
38
+ * })
39
+ */
40
+ const loadScript = ({
41
+ id,
42
+ src,
43
+ isAsync = true,
44
+ docwrite = false,
45
+ onload,
46
+ onerror
47
+ }) => {
48
+ const hasTriedLoadScript = ({
49
+ id,
50
+ src,
51
+ method
52
+ }) => {
53
+ const idQuery = id ? `[id="${id}"]` : '';
54
+ return document.querySelectorAll(`script${idQuery}` + `[src="${src}"]` + `[data-load-method="${method}"]`).length > 0;
55
+ };
56
+ const loadMethod = docwrite ? 'document.write()' : 'DOM';
57
+ if (hasTriedLoadScript({
58
+ id,
59
+ src,
60
+ method: loadMethod
61
+ })) {
62
+ console.warn(`[mobify.progressive] loadScript() already called for this script. Ignoring call. (method='${loadMethod}' id='${id}' src='${src}')`);
63
+ return;
64
+ }
65
+ if (onload && typeof onload !== 'function') {
66
+ throw new Error(`loadScript()'s 'onload' parameter must be a function but was passed a ${typeof onload}!`);
67
+ }
68
+ if (onerror && typeof onerror !== 'function') {
69
+ throw new Error(`loadScript()'s 'onerror' parameter must be a function but was passed a ${typeof onerror}!`);
70
+ }
71
+
72
+ // TODO: Check for navigator.connection. Need Android for this.
73
+ /* istanbul ignore next */
74
+ if (docwrite && document.readyState === 'loading') {
75
+ window.Mobify = window.Mobify || {};
76
+ let onLoadString = '';
77
+ let onErrorString = '';
78
+ if (typeof onload === 'function') {
79
+ window.Mobify.scriptOnLoads = _extends({}, window.Mobify.scriptOnLoads, {
80
+ [loadScriptCounter]: onload
81
+ });
82
+ // Space prefix is important for valid rendered HTML
83
+ onLoadString = ` onload="window.Mobify.scriptOnLoads['${loadScriptCounter}'] && window.Mobify.scriptOnLoads['${loadScriptCounter}']()"`;
84
+ }
85
+ if (typeof onerror === 'function') {
86
+ window.Mobify.scriptOnErrors = _extends({}, window.Mobify.scriptOnErrors, {
87
+ [loadScriptCounter]: onerror
88
+ });
89
+ // Space prefix is important for valid rendered HTML
90
+ onErrorString = ` onerror="window.Mobify.scriptOnErrors['${loadScriptCounter}'] && window.Mobify.scriptOnErrors['${loadScriptCounter}']()"`;
91
+ }
92
+ document.write(`<script id='${id}' src='${src}' data-load-method='${loadMethod}' charset='utf-8'${onLoadString}${onErrorString}></script>`);
93
+ loadScriptCounter++;
94
+ } else {
95
+ const script = document.createElement('script');
96
+
97
+ // Setting UTF-8 as our encoding ensures that certain strings (i.e.
98
+ // Japanese text) are not improperly converted to something else. We
99
+ // do this on the vendor scripts also just in case any libs we
100
+ // import have localized strings in them.
101
+ script.charset = 'utf-8';
102
+ script.async = isAsync;
103
+ if (id) {
104
+ script.id = id;
105
+ }
106
+ script.src = src;
107
+ script.dataset.loadMethod = loadMethod;
108
+
109
+ /* istanbul ignore next */
110
+ if (typeof onload === 'function') {
111
+ script.onload = onload;
112
+ }
113
+ /* istanbul ignore next */
114
+ if (typeof onerror === 'function') {
115
+ script.onerror = onerror;
116
+ }
117
+ document.getElementsByTagName('head')[0].appendChild(script);
118
+ }
119
+ };
120
+ exports.loadScript = loadScript;
@@ -0,0 +1,106 @@
1
+ "use strict";
2
+
3
+ var _assets = require("./assets");
4
+ var _sinon = _interopRequireDefault(require("sinon"));
5
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
6
+ /*
7
+ * Copyright (c) 2021, salesforce.com, inc.
8
+ * All rights reserved.
9
+ * SPDX-License-Identifier: BSD-3-Clause
10
+ * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
11
+ */
12
+
13
+ const sandbox = _sinon.default.createSandbox();
14
+
15
+ // Silence the error messages, they're on purpose here
16
+ let consoleError;
17
+ beforeEach(() => {
18
+ consoleError = console.error;
19
+ console.error = jest.fn();
20
+ });
21
+ afterEach(() => {
22
+ console.error = consoleError;
23
+ sandbox.restore();
24
+ });
25
+ describe('loadScript', () => {
26
+ let fakeScriptElement;
27
+ const headElement = document.getElementsByTagName('head')[0];
28
+ const originalReadyState = document.readyState;
29
+ beforeEach(() => {
30
+ fakeScriptElement = {
31
+ id: null,
32
+ src: null,
33
+ async: null,
34
+ charset: null,
35
+ onload: null,
36
+ onerror: null,
37
+ dataset: {}
38
+ };
39
+
40
+ // Mock out createElement and body.appendChild so that we don't have
41
+ // to fetch an actual script
42
+ sandbox.stub(document, 'createElement').returns(fakeScriptElement);
43
+ sandbox.stub(headElement, 'appendChild');
44
+ sandbox.stub(document, 'write');
45
+ Object.defineProperty(document, 'readyState', {
46
+ value: 'loading',
47
+ writable: true
48
+ });
49
+
50
+ // If you need to actually see the logging from these modules,
51
+ // change these `.stub()` calls to `.spy()`. Tests won't be affected.
52
+ sandbox.stub(console, 'log');
53
+ sandbox.stub(console, 'warn');
54
+ });
55
+ afterAll(() => {
56
+ sandbox.restore();
57
+ document.readyState = originalReadyState;
58
+ });
59
+ test('loadScript defaults to using the DOM to load the script', () => {
60
+ (0, _assets.loadScript)({
61
+ id: 'loadScriptTest1',
62
+ src: 'loadScriptTest1src'
63
+ });
64
+ expect(document.createElement.called).toBe(true);
65
+ expect(headElement.appendChild.called).toBe(true);
66
+ const element = headElement.appendChild.getCall(0).args[0];
67
+ expect(element.id).toBe('loadScriptTest1');
68
+ expect(element.src).toBe('loadScriptTest1src');
69
+ });
70
+ test('loadScript uses document.write when the flag is enabled', () => {
71
+ (0, _assets.loadScript)({
72
+ id: 'loadScriptTest1',
73
+ src: 'loadScriptTest1src',
74
+ docwrite: true
75
+ });
76
+ expect(document.write.called).toBe(true);
77
+ });
78
+ test('loadScript aborts if it finds the requested script has already been attempted', () => {
79
+ sandbox.stub(document, 'querySelectorAll').returns([1]);
80
+ (0, _assets.loadScript)({
81
+ id: 'loadScriptTest1',
82
+ src: 'loadScriptTest1src'
83
+ });
84
+
85
+ // Shouldn't have tried either method of inserting the script
86
+ expect(document.createElement.called).toBe(false);
87
+ expect(headElement.appendChild.called).toBe(false);
88
+ expect(document.write.called).toBe(false);
89
+ });
90
+ test('loadScript throws if `onload` is provided but is not a function', () => {
91
+ expect(() => {
92
+ (0, _assets.loadScript)({
93
+ src: 'loadScriptTest2src',
94
+ onload: 'onload expects this to be a function'
95
+ });
96
+ }).toThrow();
97
+ });
98
+ test('loadScript throws if `onerror` is provided but is not a function', () => {
99
+ expect(() => {
100
+ (0, _assets.loadScript)({
101
+ src: 'loadScriptTest2src',
102
+ onerror: 'onerror expects this to be a function'
103
+ });
104
+ }).toThrow();
105
+ });
106
+ });
package/utils/url.js ADDED
@@ -0,0 +1,39 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.getAppOrigin = void 0;
7
+ /*
8
+ * Copyright (c) 2021, salesforce.com, inc.
9
+ * All rights reserved.
10
+ * SPDX-License-Identifier: BSD-3-Clause
11
+ * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
12
+ */
13
+
14
+ /**
15
+ * Returns the application's origin.
16
+ *
17
+ * NOTE: This utility can only be used server-side after your application has been
18
+ * initialized using the `_createApp` method (This happens in your /app/ssr.js file).
19
+ *
20
+ * @function
21
+ * @returns {string} Returns the ORIGIN under which we are serving the page.
22
+ * @example
23
+ * import {getAppOrigin} from '@salesforce/pwa-kit-react-sdk/utils/url'
24
+ *
25
+ * const url = `${getAppOrigin()}/path`
26
+ */
27
+ const getAppOrigin = () => {
28
+ if (typeof window !== 'undefined') {
29
+ return window.location.origin;
30
+ }
31
+ const {
32
+ APP_ORIGIN
33
+ } = process.env;
34
+ if (!APP_ORIGIN) {
35
+ throw new Error(`Application is not initialized. Please ensure '_createApp' has been invoked before using this method.`);
36
+ }
37
+ return process.env.APP_ORIGIN;
38
+ };
39
+ exports.getAppOrigin = getAppOrigin;
@@ -0,0 +1,47 @@
1
+ "use strict";
2
+
3
+ var _url = require("./url");
4
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
5
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
6
+ function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
7
+ function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return typeof key === "symbol" ? key : String(key); }
8
+ function _toPrimitive(input, hint) { if (typeof input !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (typeof res !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); } /*
9
+ * Copyright (c) 2021, salesforce.com, inc.
10
+ * All rights reserved.
11
+ * SPDX-License-Identifier: BSD-3-Clause
12
+ * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
13
+ */
14
+ describe('getAppOrigin', () => {
15
+ const OLD_ENV = process.env;
16
+ const OLD_WINDOW = global.window;
17
+ const TEST_ORIGIN = 'https://www.example.com';
18
+ beforeEach(() => {
19
+ jest.resetModules();
20
+ process.env = _objectSpread({}, OLD_ENV);
21
+ });
22
+ afterEach(() => {
23
+ process.env = OLD_ENV;
24
+ global.window = OLD_WINDOW;
25
+ });
26
+ test('returns `process.env.APP_ORIGIN` when on server', () => {
27
+ // Simulate being on the server by deleting the window.
28
+ delete global.window;
29
+
30
+ // Simulate starting the app server by simply setting the `APP_ORIGIN`
31
+ process.env.APP_ORIGIN = TEST_ORIGIN;
32
+ expect((0, _url.getAppOrigin)()).toBe(TEST_ORIGIN);
33
+ });
34
+ test('returns `window.location.origin` when on client', () => {
35
+ expect((0, _url.getAppOrigin)()).toBe('http://localhost');
36
+ });
37
+ test('throws error when APP_ORIGIN is not defined on server.', () => {
38
+ // Simulate being on the server by deleting the window.
39
+ delete global.window;
40
+
41
+ // Simulate app server not being initialized.
42
+ process.env.APP_ORIGIN = undefined;
43
+ expect(() => {
44
+ (0, _url.getAppOrigin)();
45
+ }).toThrow();
46
+ });
47
+ });
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.uuidv4 = uuidv4;
7
+ /*
8
+ * Copyright (c) 2022, salesforce.com, inc.
9
+ * All rights reserved.
10
+ * SPDX-License-Identifier: BSD-3-Clause
11
+ * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
12
+ */
13
+
14
+ /**
15
+ * Simple implementation to create a uuid using crypto
16
+ * See: https://stackoverflow.com/questions/105034/how-do-i-create-a-guid-uuid
17
+ * @returns {*}
18
+ */
19
+ function uuidv4() {
20
+ return ([1e7] + -1e3 + -4e3 + -8e3 + -1e11).replace(/[018]/g, c => (c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16));
21
+ }
@@ -0,0 +1,27 @@
1
+ "use strict";
2
+
3
+ var _uuidv = require("./uuidv4.client");
4
+ /*
5
+ * Copyright (c) 2022, salesforce.com, inc.
6
+ * All rights reserved.
7
+ * SPDX-License-Identifier: BSD-3-Clause
8
+ * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
9
+ */
10
+
11
+ let originalCrypto;
12
+ describe('uuidv4', () => {
13
+ beforeEach(() => {
14
+ originalCrypto = global.crypto;
15
+ });
16
+ afterEach(() => {
17
+ global.crypto = originalCrypto;
18
+ });
19
+ test('returns correct format', () => {
20
+ global.crypto = {
21
+ // we mock the module because crypto.getRandomValues
22
+ // is not available on node v14 (came out in node v15)
23
+ getRandomValues: () => [123]
24
+ };
25
+ expect((0, _uuidv.uuidv4)()).toBe('abbbbbbb-abbb-4bbb-bbbb-abbbbbbbbbbb');
26
+ });
27
+ });
@@ -0,0 +1,81 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.experimental = exports.deprecated = void 0;
7
+ /*
8
+ * Copyright (c) 2021, salesforce.com, inc.
9
+ * All rights reserved.
10
+ * SPDX-License-Identifier: BSD-3-Clause
11
+ * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
12
+ */
13
+ /**
14
+ * @module progressive-web-sdk/utils/warnings
15
+ * @private
16
+ */
17
+
18
+ let displayed = {};
19
+
20
+ /**
21
+ *
22
+ * Prints a warning to the console that a deprecated function is being used.
23
+ * This allows you to say when the function will be removed and what alternate
24
+ * function should be used. Example: "This function will be removed in version
25
+ * 1.2.3. Please use alternateFunction() instead."
26
+ *
27
+ * @function
28
+ * @param {String} message A message to follow the initial deprecation warning
29
+ */
30
+ const deprecated = message => {
31
+ warn('deprecated', message);
32
+ };
33
+
34
+ /**
35
+ * Prints a warning to the console that an experimental function is being used.
36
+ *
37
+ * @function
38
+ * @param {String} message A message to follow the initial experimental warning
39
+ */
40
+ exports.deprecated = deprecated;
41
+ const experimental = message => {
42
+ warn('experimental', message);
43
+ };
44
+
45
+ /**
46
+ * Displays a warning once per hard navigation.
47
+ * Does not display a warning when in production mode.
48
+ * @private
49
+ */
50
+ exports.experimental = experimental;
51
+ const shouldDisplay = name => {
52
+ if (process.env.NODE_ENV === 'production' || displayed[name]) {
53
+ return false;
54
+ } else {
55
+ displayed[name] = true;
56
+ return true;
57
+ }
58
+ };
59
+
60
+ /**
61
+ * Logs a warning to the console
62
+ * @param type Warning type
63
+ * @param message A message to follow the initial warning
64
+ */
65
+ const warn = (type, message) => {
66
+ let functionName = '';
67
+
68
+ // Get caller name by reading an Error stack trace instead of getting
69
+ // function property `caller` because it is not allowed in `strict` mode.
70
+ try {
71
+ throw new Error();
72
+ } catch (e) {
73
+ functionName = e.stack.split('\n')[3].split(' ')[5];
74
+ }
75
+ const messages = {
76
+ experimental: `You are currently using an experimental function: [${functionName}] This function may change at any time.`,
77
+ deprecated: `You are currently using an deprecated function: [${functionName}].`
78
+ };
79
+ if (!shouldDisplay(functionName)) return;
80
+ console.warn(`[PWA Kit API WARNING]: ${messages[type]} ${message ? message : ''}`);
81
+ };
@@ -0,0 +1,48 @@
1
+ "use strict";
2
+
3
+ var _warnings = require("./warnings");
4
+ /*
5
+ * Copyright (c) 2021, salesforce.com, inc.
6
+ * All rights reserved.
7
+ * SPDX-License-Identifier: BSD-3-Clause
8
+ * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
9
+ */
10
+
11
+ describe('warnings', () => {
12
+ let original;
13
+ beforeEach(() => {
14
+ original = console.warn;
15
+ console.warn = jest.fn();
16
+ });
17
+ afterEach(() => {
18
+ console.warn = original;
19
+ });
20
+ test('deprecated', () => {
21
+ const testFunction1 = () => {
22
+ (0, _warnings.deprecated)('msg');
23
+ };
24
+ testFunction1();
25
+ expect(console.warn.mock.calls[0][0]).toBe(`[PWA Kit API WARNING]: You are currently using an deprecated function: [testFunction1]. msg`);
26
+ testFunction1();
27
+ expect(console.warn).toHaveBeenCalledTimes(1);
28
+ const testFunction2 = () => {
29
+ (0, _warnings.deprecated)();
30
+ };
31
+ testFunction2();
32
+ expect(console.warn.mock.calls[1][0]).toBe(`[PWA Kit API WARNING]: You are currently using an deprecated function: [testFunction2]. `);
33
+ });
34
+ test('experimental', () => {
35
+ const testFunction3 = () => {
36
+ (0, _warnings.experimental)('msg');
37
+ };
38
+ testFunction3();
39
+ expect(console.warn.mock.calls[0][0]).toBe(`[PWA Kit API WARNING]: You are currently using an experimental function: [testFunction3] This function may change at any time. msg`);
40
+ testFunction3();
41
+ expect(console.warn).toHaveBeenCalledTimes(1);
42
+ const testFunction4 = () => {
43
+ (0, _warnings.experimental)();
44
+ };
45
+ testFunction4();
46
+ expect(console.warn.mock.calls[1][0]).toBe(`[PWA Kit API WARNING]: You are currently using an experimental function: [testFunction4] This function may change at any time. `);
47
+ });
48
+ });