@open-pioneer/test-utils 1.0.2 → 1.1.1

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/CHANGELOG.md CHANGED
@@ -1,5 +1,21 @@
1
1
  # @open-pioneer/test-utils
2
2
 
3
+ ## 1.1.1
4
+
5
+ ### Patch Changes
6
+
7
+ - b617e3b: Fix typo in readme
8
+
9
+ ## 1.1.0
10
+
11
+ ### Minor Changes
12
+
13
+ - 2d5a94a: Provide `createIntl` from `@open-pioneer/test-utils/vanilla` to make creation of a `PackageIntl` instance easier.
14
+
15
+ ### Patch Changes
16
+
17
+ - @open-pioneer/runtime-react-support@1.0.0
18
+
3
19
  ## 1.0.2
4
20
 
5
21
  ### Patch Changes
package/README.md CHANGED
@@ -112,6 +112,20 @@ it("creates a new service instance with the defined references", async () => {
112
112
  });
113
113
  ```
114
114
 
115
+ ## Utilities for Vanilla JS
116
+
117
+ The function `createIntl` from `@open-pioneer/test-utils/vanilla` can be used to make the creation of an `intl` object easier from vanilla JavaScript:
118
+
119
+ ```js
120
+ import { createIntl } from "@open-pioneer/test-utils/vanilla";
121
+
122
+ // In your test:
123
+ const intl = createIntl(/* optional arguments such as messages or locale */);
124
+ intl.formatMessage({
125
+ /* ... */
126
+ });
127
+ ```
128
+
115
129
  ## License
116
130
 
117
131
  Apache-2.0 (see `LICENSE` file)
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "type": "module",
3
3
  "name": "@open-pioneer/test-utils",
4
- "version": "1.0.2",
4
+ "version": "1.1.1",
5
5
  "license": "Apache-2.0",
6
6
  "peerDependencies": {
7
7
  "@testing-library/react": "^14.1.2",
@@ -14,6 +14,10 @@
14
14
  },
15
15
  "exports": {
16
16
  "./package.json": "./package.json",
17
+ "./vanilla": {
18
+ "import": "./vanilla.js",
19
+ "types": "./vanilla.d.ts"
20
+ },
17
21
  "./web-components": {
18
22
  "import": "./web-components.js",
19
23
  "types": "./web-components.d.ts"
package/react.js CHANGED
@@ -1,9 +1,8 @@
1
1
  import { jsx } from 'react/jsx-runtime';
2
- import { createIntlCache, createIntl } from '@formatjs/intl';
3
2
  import { CustomChakraProvider } from '@open-pioneer/chakra-integration';
4
3
  import { PackageContext } from '@open-pioneer/runtime-react-support';
5
4
  import { useMemo } from 'react';
6
- import { INTL_ERROR_HANDLER } from './utils.js';
5
+ import { createIntl } from './vanilla.js';
7
6
 
8
7
  const PackageContextProvider = (props) => {
9
8
  const { children, ...rest } = props;
@@ -62,15 +61,10 @@ function createPackageContextMethods(options) {
62
61
  getIntl(packageName) {
63
62
  const initIntl = () => {
64
63
  const packageMessages = messages[packageName];
65
- const cache = createIntlCache();
66
- return createIntl(
67
- {
68
- locale,
69
- messages: packageMessages,
70
- onError: INTL_ERROR_HANDLER
71
- },
72
- cache
73
- );
64
+ return createIntl({
65
+ locale,
66
+ messages: packageMessages
67
+ });
74
68
  };
75
69
  return cachedIntl[packageName] ??= initIntl();
76
70
  }
package/react.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"react.js","sources":["react.tsx"],"sourcesContent":["// SPDX-FileCopyrightText: 2023 Open Pioneer project (https://github.com/open-pioneer)\n// SPDX-License-Identifier: Apache-2.0\nimport { createIntl, createIntlCache, IntlShape } from \"@formatjs/intl\";\nimport { CustomChakraProvider } from \"@open-pioneer/chakra-integration\";\nimport type { Service } from \"@open-pioneer/runtime\";\nimport {\n PackageContext as InternalPackageContext,\n PackageContextMethods\n} from \"@open-pioneer/runtime-react-support\";\nimport { FC, ReactNode, useMemo } from \"react\";\nimport { INTL_ERROR_HANDLER } from \"./utils\";\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport type AnyService = Service<any>;\n\nexport interface PackageContextProviderProps {\n /** Interface implementations, keyed by interface name. */\n services?: {\n [interfaceName: string]: AnyService;\n };\n\n /** Interface implementations, keyed by interface name and then by qualifier. */\n qualifiedServices?: {\n [interfaceName: string]: {\n [qualifier: string]: AnyService;\n };\n };\n\n /** Package properties (keyed by package name). */\n properties?: {\n [packageName: string]: Record<string, unknown>;\n };\n\n /**\n * The locale for i18n messages and formatting.\n *\n * @default \"en\"\n */\n locale?: string;\n\n /**\n * I18n messages for packages\n */\n messages?: {\n /**\n * I18n messages as (messageId, message) entries.\n *\n * @default {}\n */\n [packageName: string]: Record<string, string>;\n };\n\n /** Children to render */\n children?: ReactNode;\n}\n\n/**\n * Injects services and properties into the component tree.\n * React components using pioneer hooks like `useService` and `useProperties`\n * will receive the mocked properties here instead.\n * Currently also wrapped with the CustomChakraProvider to support chakra-ui elements\n */\nexport const PackageContextProvider: FC<PackageContextProviderProps> = (props) => {\n const { children, ...rest } = props;\n const contextMethods = useMemo(() => createPackageContextMethods(rest), [rest]);\n return (\n <CustomChakraProvider container={document.body}>\n <InternalPackageContext.Provider value={contextMethods}>\n {children}\n </InternalPackageContext.Provider>\n </CustomChakraProvider>\n );\n};\n\nfunction createPackageContextMethods(\n options: Omit<PackageContextProviderProps, \"children\">\n): PackageContextMethods {\n const services = options?.services ?? {};\n const qualifiedServices = options?.qualifiedServices ?? {};\n const properties = options?.properties ?? {};\n const locale = options?.locale ?? \"en\";\n const messages = options?.messages ?? {};\n const cachedIntl: Record<string, IntlShape> = {};\n return {\n getService(packageName, interfaceName, options) {\n if (!options.qualifier) {\n const service = services[interfaceName];\n if (service) {\n return service;\n }\n const qualified = qualifiedServices[interfaceName];\n if (qualified) {\n for (const value of Object.values(qualified)) {\n return value; // Return any implementation\n }\n }\n throw new Error(\n `Interface name not bound for testing: '${interfaceName}'. Update the configuration of PackageContextProvider.`\n );\n }\n\n const qualifiedService = qualifiedServices[interfaceName]?.[options.qualifier];\n if (!qualifiedService) {\n throw new Error(\n `Interface name not bound for testing: '${interfaceName}' (qualifier '${options.qualifier}'). Update the configuration of PackageContextProvider.`\n );\n }\n return qualifiedService;\n },\n getServices(packageName, interfaceName) {\n const unqualified = services[interfaceName];\n const results = Object.values(qualifiedServices[interfaceName] ?? {});\n if (unqualified) {\n results.push(unqualified);\n }\n return results;\n },\n getProperties(packageName) {\n const packageProperties = properties[packageName];\n if (!packageProperties) {\n throw new Error(\n `No properties for package '${packageName}' bound for testing. Update the configuration of PackageContextProvider.`\n );\n }\n return packageProperties;\n },\n getIntl(packageName) {\n const initIntl = () => {\n const packageMessages = messages[packageName];\n const cache = createIntlCache();\n return createIntl(\n {\n locale,\n messages: packageMessages,\n onError: INTL_ERROR_HANDLER\n },\n cache\n );\n };\n return (cachedIntl[packageName] ??= initIntl());\n }\n };\n}\n"],"names":["InternalPackageContext","options"],"mappings":";;;;;;;AA8Da,MAAA,sBAAA,GAA0D,CAAC,KAAU,KAAA;AAC9E,EAAA,MAAM,EAAE,QAAA,EAAU,GAAG,IAAA,EAAS,GAAA,KAAA,CAAA;AAC9B,EAAM,MAAA,cAAA,GAAiB,QAAQ,MAAM,2BAAA,CAA4B,IAAI,CAAG,EAAA,CAAC,IAAI,CAAC,CAAA,CAAA;AAC9E,EAAA,uBACK,GAAA,CAAA,oBAAA,EAAA,EAAqB,SAAW,EAAA,QAAA,CAAS,IACtC,EAAA,QAAA,kBAAA,GAAA,CAACA,cAAuB,CAAA,QAAA,EAAvB,EAAgC,KAAA,EAAO,cACnC,EAAA,QAAA,EACL,CACJ,EAAA,CAAA,CAAA;AAER,EAAA;AAEA,SAAS,4BACL,OACqB,EAAA;AACrB,EAAM,MAAA,QAAA,GAAW,OAAS,EAAA,QAAA,IAAY,EAAC,CAAA;AACvC,EAAM,MAAA,iBAAA,GAAoB,OAAS,EAAA,iBAAA,IAAqB,EAAC,CAAA;AACzD,EAAM,MAAA,UAAA,GAAa,OAAS,EAAA,UAAA,IAAc,EAAC,CAAA;AAC3C,EAAM,MAAA,MAAA,GAAS,SAAS,MAAU,IAAA,IAAA,CAAA;AAClC,EAAM,MAAA,QAAA,GAAW,OAAS,EAAA,QAAA,IAAY,EAAC,CAAA;AACvC,EAAA,MAAM,aAAwC,EAAC,CAAA;AAC/C,EAAO,OAAA;A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ACvB,MAAM,MAAA,iBAAA,GAAoB,WAAW,WAAW,CAAA,CAAA;AAChD,MAAA,IAAI,CAAC,iBAAmB,EAAA;AACpB,QAAA,MAAM,IAAI,KAAA;AAAA,UACN,8BAA8B,WAAW,CAAA,wEAAA,CAAA;AAAA,SAC7C,CAAA;AAAA,OACJ;AACA,MAAO,OAAA,iBAAA,CAAA;AAAA,KACX;AAAA,IACA,QAAQ,WAAa,EAAA;AACjB,MAAA,MAAM,WAAW,MAAM;AACnB,QAAM,MAAA,eAAA,GAAkB,SAAS,WAAW,CAAA,CAAA;AAC5C,QAAA,MAAM,QAAQ,eAAgB,EAAA,CAAA;AAC9B,QAAO,OAAA,UAAA;AAAA,UACH;AAAA,YACI,MAAA;AAAA,YACA,QAAU,EAAA,eAAA;AAAA,YACV,OAAS,EAAA,kBAAA;AAAA,WACb;AAAA,UACA,KAAA;AAAA,SACJ,CAAA;AAAA,OACJ,CAAA;AACA,MAAQ,OAAA,UAAA,CAAW,WAAW,CAAA,KAAM,QAAS,EAAA,CAAA;AAAA,KACjD;AAAA,GACJ,CAAA;AACJ;;;;"}
1
+ {"version":3,"file":"react.js","sources":["react.tsx"],"sourcesContent":["// SPDX-FileCopyrightText: 2023 Open Pioneer project (https://github.com/open-pioneer)\n// SPDX-License-Identifier: Apache-2.0\nimport { CustomChakraProvider } from \"@open-pioneer/chakra-integration\";\nimport type { PackageIntl, Service } from \"@open-pioneer/runtime\";\nimport {\n PackageContext as InternalPackageContext,\n PackageContextMethods\n} from \"@open-pioneer/runtime-react-support\";\nimport { FC, ReactNode, useMemo } from \"react\";\nimport { createIntl } from \"./vanilla\";\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport type AnyService = Service<any>;\n\nexport interface PackageContextProviderProps {\n /** Interface implementations, keyed by interface name. */\n services?: {\n [interfaceName: string]: AnyService;\n };\n\n /** Interface implementations, keyed by interface name and then by qualifier. */\n qualifiedServices?: {\n [interfaceName: string]: {\n [qualifier: string]: AnyService;\n };\n };\n\n /** Package properties (keyed by package name). */\n properties?: {\n [packageName: string]: Record<string, unknown>;\n };\n\n /**\n * The locale for i18n messages and formatting.\n *\n * @default \"en\"\n */\n locale?: string;\n\n /**\n * I18n messages for packages\n */\n messages?: {\n /**\n * I18n messages as (messageId, message) entries.\n *\n * @default {}\n */\n [packageName: string]: Record<string, string>;\n };\n\n /** Children to render */\n children?: ReactNode;\n}\n\n/**\n * Injects services and properties into the component tree.\n * React components using pioneer hooks like `useService` and `useProperties`\n * will receive the mocked properties here instead.\n * Currently also wrapped with the CustomChakraProvider to support chakra-ui elements\n */\nexport const PackageContextProvider: FC<PackageContextProviderProps> = (props) => {\n const { children, ...rest } = props;\n const contextMethods = useMemo(() => createPackageContextMethods(rest), [rest]);\n return (\n <CustomChakraProvider container={document.body}>\n <InternalPackageContext.Provider value={contextMethods}>\n {children}\n </InternalPackageContext.Provider>\n </CustomChakraProvider>\n );\n};\n\nfunction createPackageContextMethods(\n options: Omit<PackageContextProviderProps, \"children\">\n): PackageContextMethods {\n const services = options?.services ?? {};\n const qualifiedServices = options?.qualifiedServices ?? {};\n const properties = options?.properties ?? {};\n const locale = options?.locale ?? \"en\";\n const messages = options?.messages ?? {};\n const cachedIntl: Record<string, PackageIntl> = {};\n return {\n getService(packageName, interfaceName, options) {\n if (!options.qualifier) {\n const service = services[interfaceName];\n if (service) {\n return service;\n }\n const qualified = qualifiedServices[interfaceName];\n if (qualified) {\n for (const value of Object.values(qualified)) {\n return value; // Return any implementation\n }\n }\n throw new Error(\n `Interface name not bound for testing: '${interfaceName}'. Update the configuration of PackageContextProvider.`\n );\n }\n\n const qualifiedService = qualifiedServices[interfaceName]?.[options.qualifier];\n if (!qualifiedService) {\n throw new Error(\n `Interface name not bound for testing: '${interfaceName}' (qualifier '${options.qualifier}'). Update the configuration of PackageContextProvider.`\n );\n }\n return qualifiedService;\n },\n getServices(packageName, interfaceName) {\n const unqualified = services[interfaceName];\n const results = Object.values(qualifiedServices[interfaceName] ?? {});\n if (unqualified) {\n results.push(unqualified);\n }\n return results;\n },\n getProperties(packageName) {\n const packageProperties = properties[packageName];\n if (!packageProperties) {\n throw new Error(\n `No properties for package '${packageName}' bound for testing. Update the configuration of PackageContextProvider.`\n );\n }\n return packageProperties;\n },\n getIntl(packageName) {\n const initIntl = () => {\n const packageMessages = messages[packageName];\n return createIntl({\n locale,\n messages: packageMessages\n });\n };\n return (cachedIntl[packageName] ??= initIntl());\n }\n };\n}\n"],"names":["InternalPackageContext","options"],"mappings":";;;;;;AA6Da,MAAA,sBAAA,GAA0D,CAAC,KAAU,KAAA;AAC9E,EAAA,MAAM,EAAE,QAAA,EAAU,GAAG,IAAA,EAAS,GAAA,KAAA,CAAA;AAC9B,EAAM,MAAA,cAAA,GAAiB,QAAQ,MAAM,2BAAA,CAA4B,IAAI,CAAG,EAAA,CAAC,IAAI,CAAC,CAAA,CAAA;AAC9E,EAAA,uBACK,GAAA,CAAA,oBAAA,EAAA,EAAqB,SAAW,EAAA,QAAA,CAAS,IACtC,EAAA,QAAA,kBAAA,GAAA,CAACA,cAAuB,CAAA,QAAA,EAAvB,EAAgC,KAAA,EAAO,cACnC,EAAA,QAAA,EACL,CACJ,EAAA,CAAA,CAAA;AAER,EAAA;AAEA,SAAS,4BACL,OACqB,EAAA;AACrB,EAAM,MAAA,QAAA,GAAW,OAAS,EAAA,QAAA,IAAY,EAAC,CAAA;AACvC,EAAM,MAAA,iBAAA,GAAoB,OAAS,EAAA,iBAAA,IAAqB,EAAC,CAAA;AACzD,EAAM,MAAA,UAAA,GAAa,OAAS,EAAA,UAAA,IAAc,EAAC,CAAA;AAC3C,EAAM,MAAA,MAAA,GAAS,SAAS,MAAU,IAAA,IAAA,CAAA;AAClC,EAAM,MAAA,QAAA,GAAW,OAAS,EAAA,QAAA,IAAY,EAAC,CAAA;AACvC,EAAA,MAAM,aAA0C,EAAC,CAAA;AACjD,EAAO,OAAA;AAAA,IACH,UAAA,CAAW,WAAa,EAAA,aAAA,EAAeC,QAAS,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A;AAChD,MAAA,IAAI,CAAC,iBAAmB,EAAA;AACpB,QAAA,MAAM,IAAI,KAAA;AAAA,UACN,8BAA8B,WAAW,CAAA,wEAAA,CAAA;AAAA,SAC7C,CAAA;AAAA,OACJ;AACA,MAAO,OAAA,iBAAA,CAAA;AAAA,KACX;AAAA,IACA,QAAQ,WAAa,EAAA;AACjB,MAAA,MAAM,WAAW,MAAM;AACnB,QAAM,MAAA,eAAA,GAAkB,SAAS,WAAW,CAAA,CAAA;AAC5C,QAAA,OAAO,UAAW,CAAA;AAAA,UACd,MAAA;AAAA,UACA,QAAU,EAAA,eAAA;AAAA,SACb,CAAA,CAAA;AAAA,OACL,CAAA;AACA,MAAQ,OAAA,UAAA,CAAW,WAAW,CAAA,KAAM,QAAS,EAAA,CAAA;AAAA,KACjD;AAAA,GACJ,CAAA;AACJ;;;;"}
package/services.js CHANGED
@@ -1,5 +1,4 @@
1
- import { createIntlCache, createIntl } from '@formatjs/intl';
2
- import { INTL_ERROR_HANDLER } from './utils.js';
1
+ import { createIntl } from './vanilla.js';
3
2
 
4
3
  async function createService(clazz, options) {
5
4
  const references = options?.references ?? {};
@@ -9,19 +8,7 @@ async function createService(clazz, options) {
9
8
  referenceMeta(referenceName, reference)
10
9
  ])
11
10
  );
12
- const locale = options?.locale ?? "en";
13
- const defaultMessageLocale = options?.defaultMessageLocale ?? "en";
14
- const messages = options?.messages ?? {};
15
- const cache = createIntlCache();
16
- const intl = createIntl(
17
- {
18
- locale,
19
- defaultLocale: defaultMessageLocale,
20
- messages,
21
- onError: INTL_ERROR_HANDLER
22
- },
23
- cache
24
- );
11
+ const intl = createIntl(options);
25
12
  return new clazz({
26
13
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
27
14
  references,
package/services.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"services.js","sources":["services.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2023 Open Pioneer project (https://github.com/open-pioneer)\n// SPDX-License-Identifier: Apache-2.0\nimport { ReferenceMeta, Service, ServiceConstructor } from \"@open-pioneer/runtime\";\nimport { createIntl, createIntlCache } from \"@formatjs/intl\";\nimport { INTL_ERROR_HANDLER } from \"./utils\";\n\n/**\n * Options for the {@link createService} function.\n */\nexport interface CreateServiceOptions<References> {\n /**\n * References provided to the service constructor.\n *\n * @default {}\n */\n references?: PartialServiceReferences<References>;\n\n /**\n * Properties provided to the service constructor.\n *\n * @default {}\n */\n properties?: Record<string, unknown>;\n\n /**\n * The locale for i18n messages and formatting.\n *\n * @default \"en\"\n */\n locale?: string;\n\n /**\n * The locale for embedded default messages.\n *\n * See also https://formatjs.io/docs/intl#message-descriptor\n *\n * @default \"en\"\n */\n defaultMessageLocale?: string;\n\n /**\n * I18n messages as (messageId, message) entries.\n *\n * @default {}\n */\n messages?: Record<string, string>;\n}\n\ntype PartialServiceReferences<References> = {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n [referenceName in keyof References]?: References[referenceName] extends any[]\n ? Partial<References[referenceName][number]>[]\n : Partial<References[referenceName]>;\n};\n\n/**\n * Creates a new service instance of `clazz`.\n *\n * The options passed into the constructor of `clazz` are taking from the `options` parameter.\n *\n * This function allows the user to define mock- or test objects as service references.\n *\n * Most type-checking features are disabled for convenience, but by default this function's types\n * enforce that a value provided for a reference must match the partial interface of the actually required type.\n *\n * For example, if a service depends on `{ ctx: ApplicationContext }`, this function ensures\n * that the test value for `ctx` implements `Partial<ApplicationContext>`.\n * This can of course be disabled by casting the value explicitly.\n *\n * @param clazz a service class\n * @param options options passed to the service constructor\n * @returns a new instance of the given service class\n */\nexport async function createService<References extends {}, Interface extends {}>(\n clazz: ServiceConstructor<References, Interface>,\n options?: CreateServiceOptions<References>\n): Promise<Service<Interface>> {\n const references = options?.references ?? {};\n const referencesMeta = Object.fromEntries(\n Object.entries(references).map(([referenceName, reference]) => [\n referenceName,\n referenceMeta(referenceName, reference)\n ])\n );\n\n const locale = options?.locale ?? \"en\";\n const defaultMessageLocale = options?.defaultMessageLocale ?? \"en\";\n const messages = options?.messages ?? {};\n const cache = createIntlCache();\n const intl = createIntl(\n {\n locale,\n defaultLocale: defaultMessageLocale,\n messages,\n onError: INTL_ERROR_HANDLER\n },\n cache\n );\n\n return new clazz({\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n references: references as any,\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n referencesMeta: referencesMeta as any,\n properties: options?.properties ?? {},\n intl\n });\n}\n\nfunction referenceMeta(referenceName: string, reference: unknown): ReferenceMeta | ReferenceMeta[] {\n if (Array.isArray(reference)) {\n return reference.map<ReferenceMeta>((_, index) => ({\n serviceId: `test-utils::${referenceName}-${index}`\n }));\n }\n return {\n serviceId: `test-utils::${referenceName}`\n };\n}\n"],"names":[],"mappings":";;;AAyEsB,eAAA,aAAA,CAClB,OACA,OAC2B,EAAA;AAC3B,EAAM,MAAA,UAAA,GAAa,OAAS,EAAA,UAAA,IAAc,EAAC,CAAA;AAC3C,EAAA,MAAM,iBAAiB,MAAO,CAAA,WAAA;AAAA,IAC1B,MAAA,CAAO,QAAQ,UAAU,CAAA,CAAE,IAAI,CAAC,CAAC,aAAe,EAAA,SAAS,CAAM,KAAA;AAAA,MAC3D,aAAA;AAAA,MACA,aAAA,CAAc,eAAe,SAAS,CAAA;AAAA,KACzC,CAAA;AAAA,GACL,CAAA;AAEA,EAAM,MAAA,MAAA,GAAS,SAAS,MAAU,IAAA,IAAA,CAAA;AAClC,EAAM,MAAA,oBAAA,GAAuB,SAAS,oBAAwB,IAAA,IAAA,CAAA;AAC9D,EAAM,MAAA,QAAA,GAAW,OAAS,EAAA,QAAA,IAAY,EAAC,CAAA;AACvC,EAAA,MAAM,QAAQ,eAAgB,EAAA,CAAA;AAC9B,EAAA,MAAM,IAAO,GAAA,UAAA;AAAA,IACT;AAAA,MACI,MAAA;AAAA,MACA,aAAe,EAAA,oBAAA;AAAA,MACf,QAAA;AAAA,MACA,OAAS,EAAA,kBAAA;AAAA,KACb;AAAA,IACA,KAAA;AAAA,GACJ,CAAA;AAEA,EAAA,OAAO,IAAI,KAAM,CAAA;AAAA;AAAA,IAEb,UAAA;AAAA;AAAA,IAEA,cAAA;AAAA,IACA,UAAA,EAAY,OAAS,EAAA,UAAA,IAAc,EAAC;AAAA,IACpC,IAAA;AAAA,GACH,CAAA,CAAA;AACL,CAAA;AAEA,SAAS,aAAA,CAAc,eAAuB,SAAqD,EAAA;AAC/F,EAAI,IAAA,KAAA,CAAM,OAAQ,CAAA,SAAS,CAAG,EAAA;AAC1B,IAAA,OAAO,SAAU,CAAA,GAAA,CAAmB,CAAC,CAAA,EAAG,KAAW,MAAA;AAAA,MAC/C,SAAW,EAAA,CAAA,YAAA,EAAe,aAAa,CAAA,CAAA,EAAI,KAAK,CAAA,CAAA;AAAA,KAClD,CAAA,CAAA,CAAA;AAAA,GACN;AACA,EAAO,OAAA;AAAA,IACH,SAAA,EAAW,eAAe,aAAa,CAAA,CAAA;AAAA,GAC3C,CAAA;AACJ;;;;"}
1
+ {"version":3,"file":"services.js","sources":["services.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2023 Open Pioneer project (https://github.com/open-pioneer)\n// SPDX-License-Identifier: Apache-2.0\nimport { ReferenceMeta, Service, ServiceConstructor } from \"@open-pioneer/runtime\";\nimport { createIntl } from \"./vanilla\";\n\n/**\n * Options for the {@link createService} function.\n */\nexport interface CreateServiceOptions<References> {\n /**\n * References provided to the service constructor.\n *\n * @default {}\n */\n references?: PartialServiceReferences<References>;\n\n /**\n * Properties provided to the service constructor.\n *\n * @default {}\n */\n properties?: Record<string, unknown>;\n\n /**\n * The locale for i18n messages and formatting.\n *\n * @default \"en\"\n */\n locale?: string;\n\n /**\n * The locale for embedded default messages.\n *\n * See also https://formatjs.io/docs/intl#message-descriptor\n *\n * @default \"en\"\n */\n defaultMessageLocale?: string;\n\n /**\n * I18n messages as (messageId, message) entries.\n *\n * @default {}\n */\n messages?: Record<string, string>;\n}\n\ntype PartialServiceReferences<References> = {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n [referenceName in keyof References]?: References[referenceName] extends any[]\n ? Partial<References[referenceName][number]>[]\n : Partial<References[referenceName]>;\n};\n\n/**\n * Creates a new service instance of `clazz`.\n *\n * The options passed into the constructor of `clazz` are taking from the `options` parameter.\n *\n * This function allows the user to define mock- or test objects as service references.\n *\n * Most type-checking features are disabled for convenience, but by default this function's types\n * enforce that a value provided for a reference must match the partial interface of the actually required type.\n *\n * For example, if a service depends on `{ ctx: ApplicationContext }`, this function ensures\n * that the test value for `ctx` implements `Partial<ApplicationContext>`.\n * This can of course be disabled by casting the value explicitly.\n *\n * @param clazz a service class\n * @param options options passed to the service constructor\n * @returns a new instance of the given service class\n */\nexport async function createService<References extends {}, Interface extends {}>(\n clazz: ServiceConstructor<References, Interface>,\n options?: CreateServiceOptions<References>\n): Promise<Service<Interface>> {\n const references = options?.references ?? {};\n const referencesMeta = Object.fromEntries(\n Object.entries(references).map(([referenceName, reference]) => [\n referenceName,\n referenceMeta(referenceName, reference)\n ])\n );\n\n const intl = createIntl(options);\n return new clazz({\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n references: references as any,\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n referencesMeta: referencesMeta as any,\n properties: options?.properties ?? {},\n intl\n });\n}\n\nfunction referenceMeta(referenceName: string, reference: unknown): ReferenceMeta | ReferenceMeta[] {\n if (Array.isArray(reference)) {\n return reference.map<ReferenceMeta>((_, index) => ({\n serviceId: `test-utils::${referenceName}-${index}`\n }));\n }\n return {\n serviceId: `test-utils::${referenceName}`\n };\n}\n"],"names":[],"mappings":";;AAwEsB,eAAA,aAAA,CAClB,OACA,OAC2B,EAAA;AAC3B,EAAM,MAAA,UAAA,GAAa,OAAS,EAAA,UAAA,IAAc,EAAC,CAAA;AAC3C,EAAA,MAAM,iBAAiB,MAAO,CAAA,WAAA;AAAA,IAC1B,MAAA,CAAO,QAAQ,UAAU,CAAA,CAAE,IAAI,CAAC,CAAC,aAAe,EAAA,SAAS,CAAM,KAAA;AAAA,MAC3D,aAAA;AAAA,MACA,aAAA,CAAc,eAAe,SAAS,CAAA;AAAA,KACzC,CAAA;AAAA,GACL,CAAA;AAEA,EAAM,MAAA,IAAA,GAAO,WAAW,OAAO,CAAA,CAAA;AAC/B,EAAA,OAAO,IAAI,KAAM,CAAA;AAAA;AAAA,IAEb,UAAA;AAAA;AAAA,IAEA,cAAA;AAAA,IACA,UAAA,EAAY,OAAS,EAAA,UAAA,IAAc,EAAC;AAAA,IACpC,IAAA;AAAA,GACH,CAAA,CAAA;AACL,CAAA;AAEA,SAAS,aAAA,CAAc,eAAuB,SAAqD,EAAA;AAC/F,EAAI,IAAA,KAAA,CAAM,OAAQ,CAAA,SAAS,CAAG,EAAA;AAC1B,IAAA,OAAO,SAAU,CAAA,GAAA,CAAmB,CAAC,CAAA,EAAG,KAAW,MAAA;AAAA,MAC/C,SAAW,EAAA,CAAA,YAAA,EAAe,aAAa,CAAA,CAAA,EAAI,KAAK,CAAA,CAAA;AAAA,KAClD,CAAA,CAAA,CAAA;AAAA,GACN;AACA,EAAO,OAAA;AAAA,IACH,SAAA,EAAW,eAAe,aAAa,CAAA,CAAA;AAAA,GAC3C,CAAA;AACJ;;;;"}
package/vanilla.d.ts ADDED
@@ -0,0 +1,36 @@
1
+ import { PackageIntl } from "@open-pioneer/runtime";
2
+ /** Options for `createIntl`. */
3
+ export interface I18nOptions {
4
+ /**
5
+ * The locale for i18n messages and formatting.
6
+ *
7
+ * @default "en"
8
+ */
9
+ locale?: string;
10
+ /**
11
+ * The locale for embedded default messages (e.g. `defaultMessage` property in `intl.formatMessage(...)`).
12
+ * It is usually not necessary to specify this option.
13
+ *
14
+ * See also https://formatjs.io/docs/intl#message-descriptor
15
+ *
16
+ * @default "en"
17
+ */
18
+ defaultMessageLocale?: string;
19
+ /**
20
+ * I18n messages as (messageId, message) entries.
21
+ *
22
+ * @default {}
23
+ */
24
+ messages?: Record<string, string>;
25
+ }
26
+ /**
27
+ * Creates an `intl` instance that can be used for testing.
28
+ *
29
+ * Other than the default implementation provided by `@formatjs/intl`, this
30
+ * `intl` object will not warn if a message is not defined.
31
+ * Instead, it will simply render the fallback message.
32
+ * This behavior makes testing easier.
33
+ *
34
+ * Note that messages can still be defined by using the `messages` parameter.
35
+ */
36
+ export declare function createIntl(options?: I18nOptions): PackageIntl;
package/vanilla.js ADDED
@@ -0,0 +1,27 @@
1
+ import { createIntlCache, createIntl as createIntl$1, IntlErrorCode } from '@formatjs/intl';
2
+
3
+ function createIntl(options) {
4
+ const messages = options?.messages ?? {};
5
+ const locale = options?.locale ?? "en";
6
+ const defaultMessageLocale = options?.defaultMessageLocale ?? "en";
7
+ const cache = createIntlCache();
8
+ const intl = createIntl$1(
9
+ {
10
+ locale,
11
+ defaultLocale: defaultMessageLocale,
12
+ messages,
13
+ onError: INTL_ERROR_HANDLER
14
+ },
15
+ cache
16
+ );
17
+ return intl;
18
+ }
19
+ const INTL_ERROR_HANDLER = (err) => {
20
+ if (err.code === IntlErrorCode.MISSING_TRANSLATION) {
21
+ return;
22
+ }
23
+ console.error(err);
24
+ };
25
+
26
+ export { createIntl };
27
+ //# sourceMappingURL=vanilla.js.map
package/vanilla.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"vanilla.js","sources":["vanilla.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2023 Open Pioneer project (https://github.com/open-pioneer)\n// SPDX-License-Identifier: Apache-2.0\nimport {\n createIntlCache,\n createIntl as createFormatJsIntl,\n IntlErrorCode,\n OnErrorFn\n} from \"@formatjs/intl\";\nimport { PackageIntl } from \"@open-pioneer/runtime\";\n\n/** Options for `createIntl`. */\nexport interface I18nOptions {\n /**\n * The locale for i18n messages and formatting.\n *\n * @default \"en\"\n */\n locale?: string;\n\n /**\n * The locale for embedded default messages (e.g. `defaultMessage` property in `intl.formatMessage(...)`).\n * It is usually not necessary to specify this option.\n *\n * See also https://formatjs.io/docs/intl#message-descriptor\n *\n * @default \"en\"\n */\n defaultMessageLocale?: string;\n\n /**\n * I18n messages as (messageId, message) entries.\n *\n * @default {}\n */\n messages?: Record<string, string>;\n}\n\n/**\n * Creates an `intl` instance that can be used for testing.\n *\n * Other than the default implementation provided by `@formatjs/intl`, this\n * `intl` object will not warn if a message is not defined.\n * Instead, it will simply render the fallback message.\n * This behavior makes testing easier.\n *\n * Note that messages can still be defined by using the `messages` parameter.\n */\nexport function createIntl(options?: I18nOptions): PackageIntl {\n const messages = options?.messages ?? {};\n const locale = options?.locale ?? \"en\";\n const defaultMessageLocale = options?.defaultMessageLocale ?? \"en\";\n const cache = createIntlCache();\n const intl = createFormatJsIntl(\n {\n locale,\n defaultLocale: defaultMessageLocale,\n messages,\n onError: INTL_ERROR_HANDLER\n },\n cache\n );\n return intl;\n}\n\n/** Hides missing translation errors during tests */\nconst INTL_ERROR_HANDLER: OnErrorFn = (err) => {\n if (err.code === IntlErrorCode.MISSING_TRANSLATION) {\n return;\n }\n\n console.error(err);\n};\n"],"names":["createFormatJsIntl"],"mappings":";;AA+CO,SAAS,WAAW,OAAoC,EAAA;AAC3D,EAAM,MAAA,QAAA,GAAW,OAAS,EAAA,QAAA,IAAY,EAAC,CAAA;AACvC,EAAM,MAAA,MAAA,GAAS,SAAS,MAAU,IAAA,IAAA,CAAA;AAClC,EAAM,MAAA,oBAAA,GAAuB,SAAS,oBAAwB,IAAA,IAAA,CAAA;AAC9D,EAAA,MAAM,QAAQ,eAAgB,EAAA,CAAA;AAC9B,EAAA,MAAM,IAAO,GAAAA,YAAA;AAAA,IACT;AAAA,MACI,MAAA;AAAA,MACA,aAAe,EAAA,oBAAA;AAAA,MACf,QAAA;AAAA,MACA,OAAS,EAAA,kBAAA;AAAA,KACb;AAAA,IACA,KAAA;AAAA,GACJ,CAAA;AACA,EAAO,OAAA,IAAA,CAAA;AACX,CAAA;AAGA,MAAM,kBAAA,GAAgC,CAAC,GAAQ,KAAA;AAC3C,EAAI,IAAA,GAAA,CAAI,IAAS,KAAA,aAAA,CAAc,mBAAqB,EAAA;AAChD,IAAA,OAAA;AAAA,GACJ;AAEA,EAAA,OAAA,CAAQ,MAAM,GAAG,CAAA,CAAA;AACrB,CAAA;;;;"}
package/utils.d.ts DELETED
@@ -1,3 +0,0 @@
1
- import { OnErrorFn } from "@formatjs/intl";
2
- /** Hides missing translation errors during tests */
3
- export declare const INTL_ERROR_HANDLER: OnErrorFn;
package/utils.js DELETED
@@ -1,11 +0,0 @@
1
- import { IntlErrorCode } from '@formatjs/intl';
2
-
3
- const INTL_ERROR_HANDLER = (err) => {
4
- if (err.code === IntlErrorCode.MISSING_TRANSLATION) {
5
- return;
6
- }
7
- console.error(err);
8
- };
9
-
10
- export { INTL_ERROR_HANDLER };
11
- //# sourceMappingURL=utils.js.map
package/utils.js.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"file":"utils.js","sources":["utils.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2023 Open Pioneer project (https://github.com/open-pioneer)\n// SPDX-License-Identifier: Apache-2.0\nimport { IntlErrorCode, OnErrorFn } from \"@formatjs/intl\";\n\n/** Hides missing translation errors during tests */\nexport const INTL_ERROR_HANDLER: OnErrorFn = (err) => {\n if (err.code === IntlErrorCode.MISSING_TRANSLATION) {\n return;\n }\n\n console.error(err);\n};\n"],"names":[],"mappings":";;AAKa,MAAA,kBAAA,GAAgC,CAAC,GAAQ,KAAA;AAClD,EAAI,IAAA,GAAA,CAAI,IAAS,KAAA,aAAA,CAAc,mBAAqB,EAAA;AAChD,IAAA,OAAA;AAAA,GACJ;AAEA,EAAA,OAAA,CAAQ,MAAM,GAAG,CAAA,CAAA;AACrB;;;;"}