-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement a utility for PackageIntl creation during tests (#34)
- Loading branch information
Showing
8 changed files
with
143 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@open-pioneer/test-utils": minor | ||
--- | ||
|
||
Provide `createIntl` from `@open-pioneer/test-utils/vanilla` to make creation of a `PackageIntl` instance easier. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// SPDX-FileCopyrightText: 2023 Open Pioneer project (https://github.com/open-pioneer) | ||
// SPDX-License-Identifier: Apache-2.0 | ||
import { expect, it, afterEach, vi } from "vitest"; | ||
import { createIntl } from "./vanilla"; | ||
|
||
afterEach(() => { | ||
vi.restoreAllMocks(); | ||
}); | ||
|
||
it("creates an intl object", () => { | ||
const intl = createIntl(); | ||
expect(intl.locale).toBe("en"); | ||
}); | ||
|
||
it("creates an intl object with custom locale", () => { | ||
const intl = createIntl({ | ||
locale: "de" | ||
}); | ||
expect(intl.locale).toBe("de"); | ||
}); | ||
|
||
it("creates an intl object with custom messages", () => { | ||
const intl = createIntl({ | ||
messages: { | ||
"foo.bar": "baz" | ||
} | ||
}); | ||
const message = intl.formatMessage({ id: "foo.bar" }); | ||
expect(message).toBe("baz"); | ||
}); | ||
|
||
it("renders the message id and does not warn if a message is not defined", () => { | ||
const spy = vi.spyOn(console, "error").mockImplementation(() => undefined); | ||
|
||
const intl = createIntl({ | ||
messages: {} | ||
}); | ||
const message = intl.formatMessage({ id: "foo.bar" }); | ||
expect(message).toBe("foo.bar"); | ||
expect(spy).not.toHaveBeenCalled(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// SPDX-FileCopyrightText: 2023 Open Pioneer project (https://github.com/open-pioneer) | ||
// SPDX-License-Identifier: Apache-2.0 | ||
import { | ||
createIntlCache, | ||
createIntl as createFormatJsIntl, | ||
IntlErrorCode, | ||
OnErrorFn | ||
} from "@formatjs/intl"; | ||
import { PackageIntl } from "@open-pioneer/runtime"; | ||
|
||
/** Options for `createIntl`. */ | ||
export interface I18nOptions { | ||
/** | ||
* The locale for i18n messages and formatting. | ||
* | ||
* @default "en" | ||
*/ | ||
locale?: string; | ||
|
||
/** | ||
* The locale for embedded default messages (e.g. `defaultMessage` property in `intl.formatMessage(...)`). | ||
* It is usually not necessary to specify this option. | ||
* | ||
* See also https://formatjs.io/docs/intl#message-descriptor | ||
* | ||
* @default "en" | ||
*/ | ||
defaultMessageLocale?: string; | ||
|
||
/** | ||
* I18n messages as (messageId, message) entries. | ||
* | ||
* @default {} | ||
*/ | ||
messages?: Record<string, string>; | ||
} | ||
|
||
/** | ||
* Creates an `intl` instance that can be used for testing. | ||
* | ||
* Other than the default implementation provided by `@formatjs/intl`, this | ||
* `intl` object will not warn if a message is not defined. | ||
* Instead, it will simply render the fallback message. | ||
* This behavior makes testing easier. | ||
* | ||
* Note that messages can still be defined by using the `messages` parameter. | ||
*/ | ||
export function createIntl(options?: I18nOptions): PackageIntl { | ||
const messages = options?.messages ?? {}; | ||
const locale = options?.locale ?? "en"; | ||
const defaultMessageLocale = options?.defaultMessageLocale ?? "en"; | ||
const cache = createIntlCache(); | ||
const intl = createFormatJsIntl( | ||
{ | ||
locale, | ||
defaultLocale: defaultMessageLocale, | ||
messages, | ||
onError: INTL_ERROR_HANDLER | ||
}, | ||
cache | ||
); | ||
return intl; | ||
} | ||
|
||
/** Hides missing translation errors during tests */ | ||
const INTL_ERROR_HANDLER: OnErrorFn = (err) => { | ||
if (err.code === IntlErrorCode.MISSING_TRANSLATION) { | ||
return; | ||
} | ||
|
||
console.error(err); | ||
}; |