@open-pioneer/test-utils 0.1.4 → 0.1.5

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,11 @@
1
1
  # @open-pioneer/test-utils
2
2
 
3
+ ## 0.1.5
4
+
5
+ ### Patch Changes
6
+
7
+ - 5e08907: Don't show MISSING_TRANSLATION errors when testing components or services.
8
+
3
9
  ## 0.1.4
4
10
 
5
11
  ### Patch Changes
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "type": "module",
3
3
  "name": "@open-pioneer/test-utils",
4
- "version": "0.1.4",
4
+ "version": "0.1.5",
5
5
  "license": "Apache-2.0",
6
6
  "dependencies": {
7
7
  "@testing-library/react": "^14.0.0",
package/react.js CHANGED
@@ -3,6 +3,7 @@ import { createIntlCache, createIntl } from '@formatjs/intl';
3
3
  import { CustomChakraProvider } from '@open-pioneer/chakra-integration';
4
4
  import { PackageContext } from '@open-pioneer/runtime-react-support';
5
5
  import { useMemo } from 'react';
6
+ import { INTL_ERROR_HANDLER } from './utils.js';
6
7
 
7
8
  const PackageContextProvider = (props) => {
8
9
  const { children, ...rest } = props;
@@ -65,7 +66,8 @@ function createPackageContextMethods(options) {
65
66
  return createIntl(
66
67
  {
67
68
  locale,
68
- messages: packageMessages
69
+ messages: packageMessages,
70
+ onError: INTL_ERROR_HANDLER
69
71
  },
70
72
  cache
71
73
  );
package/react.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"react.js","sources":["react.tsx"],"sourcesContent":["// SPDX-FileCopyrightText: con terra GmbH and contributors\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\";\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 },\n cache\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,OA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AO,kBAAkB,aAAa,CAAA,IAAK,EAAE,CAAA,CAAA;AACpE,MAAA,IAAI,WAAa,EAAA;AACb,QAAA,OAAA,CAAQ,KAAK,WAAW,CAAA,CAAA;AAAA,OAC5B;AACA,MAAO,OAAA,OAAA,CAAA;AAAA,KACX;AAAA,IACA,cAAc,WAAa,EAAA;AACvB,MAAM,MAAA,iBAAA,GAAoB,WAAW,WAAW,CAAA,CAAA;AAChD,MAAA,IAAI,CAAC,iBAAmB,EAAA;AACpB,QAAA,MAAM,IAAI,KAAA;AAAA,UACN,CAA8B,2BAAA,EAAA,WAAA,CAAA,wEAAA,CAAA;AAAA,SAClC,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,WACd;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: con terra GmbH and contributors\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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,kBAAkB,aAAa,CAAA,IAAK,EAAE,CAAA,CAAA;AACpE,MAAA,IAAI,WAAa,EAAA;AACb,QAAA,OAAA,CAAQ,KAAK,WAAW,CAAA,CAAA;AAAA,OAC5B;AACA,MAAO,OAAA,OAAA,CAAA;AAAA,KACX;AAAA,IACA,cAAc,WAAa,EAAA;AACvB,MAAM,MAAA,iBAAA,GAAoB,WAAW,WAAW,CAAA,CAAA;AAChD,MAAA,IAAI,CAAC,iBAAmB,EAAA;AACpB,QAAA,MAAM,IAAI,KAAA;AAAA,UACN,CAA8B,2BAAA,EAAA,WAAA,CAAA,wEAAA,CAAA;AAAA,SAClC,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;;;;"}
package/services.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import { createIntlCache, createIntl } from '@formatjs/intl';
2
+ import { INTL_ERROR_HANDLER } from './utils.js';
2
3
 
3
4
  async function createService(clazz, options) {
4
5
  const references = options?.references ?? {};
@@ -16,7 +17,8 @@ async function createService(clazz, options) {
16
17
  {
17
18
  locale,
18
19
  defaultLocale: defaultMessageLocale,
19
- messages
20
+ messages,
21
+ onError: INTL_ERROR_HANDLER
20
22
  },
21
23
  cache
22
24
  );
package/services.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"services.js","sources":["services.ts"],"sourcesContent":["// SPDX-FileCopyrightText: con terra GmbH and contributors\n// SPDX-License-Identifier: Apache-2.0\nimport { ReferenceMeta, Service, ServiceConstructor } from \"@open-pioneer/runtime\";\nimport { createIntl, createIntlCache } from \"@formatjs/intl\";\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 },\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":";;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,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,KACJ;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,SAAA,EAAW,eAAe,aAAiB,CAAA,CAAA,EAAA,KAAA,CAAA,CAAA;AAAA,KAC7C,CAAA,CAAA,CAAA;AAAA,GACN;AACA,EAAO,OAAA;AAAA,IACH,WAAW,CAAe,YAAA,EAAA,aAAA,CAAA,CAAA;AAAA,GAC9B,CAAA;AACJ;;;;"}
1
+ {"version":3,"file":"services.js","sources":["services.ts"],"sourcesContent":["// SPDX-FileCopyrightText: con terra GmbH and contributors\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,aAA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A;AAAA,GACN;AACA,EAAO,OAAA;AAAA,IACH,WAAW,CAAe,YAAA,EAAA,aAAA,CAAA,CAAA;AAAA,GAC9B,CAAA;AACJ;;;;"}
package/utils.d.ts ADDED
@@ -0,0 +1,3 @@
1
+ import { OnErrorFn } from "@formatjs/intl";
2
+ /** Hides missing translation errors during tests */
3
+ export declare const INTL_ERROR_HANDLER: OnErrorFn;
package/utils.js ADDED
@@ -0,0 +1,11 @@
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 ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.js","sources":["utils.ts"],"sourcesContent":["// SPDX-FileCopyrightText: con terra GmbH and contributors\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;;;;"}