Fix tests and strong-type locales (#57)
* Add new event type * Add state handling for locale change * Add strongly typed language codes * fix tests
This commit is contained in:
parent
cc18c3deac
commit
d4670b048c
8 changed files with 77 additions and 7 deletions
|
@ -133,6 +133,7 @@ describe("useAppBridge hook", () => {
|
||||||
path: "",
|
path: "",
|
||||||
ready: false,
|
ready: false,
|
||||||
theme: "light",
|
theme: "light",
|
||||||
|
locale: "en",
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
import { LocaleCode } from "../locales";
|
||||||
import { ThemeType } from "./events";
|
import { ThemeType } from "./events";
|
||||||
|
|
||||||
export type AppBridgeState = {
|
export type AppBridgeState = {
|
||||||
|
@ -7,11 +8,11 @@ export type AppBridgeState = {
|
||||||
domain: string;
|
domain: string;
|
||||||
path: string;
|
path: string;
|
||||||
theme: ThemeType;
|
theme: ThemeType;
|
||||||
locale: string;
|
locale: LocaleCode;
|
||||||
};
|
};
|
||||||
|
|
||||||
type Options = {
|
type Options = {
|
||||||
initialLocale?: string;
|
initialLocale?: LocaleCode;
|
||||||
};
|
};
|
||||||
|
|
||||||
export class AppBridgeStateContainer {
|
export class AppBridgeStateContainer {
|
||||||
|
|
|
@ -1,6 +1,10 @@
|
||||||
import { fireEvent } from "@testing-library/dom";
|
import { fireEvent } from "@testing-library/dom";
|
||||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||||
|
|
||||||
|
import { LocaleCode } from "../locales";
|
||||||
|
// eslint-disable-next-line
|
||||||
|
import { actions, AppBridge, DispatchResponseEvent, HandshakeEvent, ThemeEvent } from ".";
|
||||||
|
|
||||||
// mock document.referrer
|
// mock document.referrer
|
||||||
const origin = "http://example.com";
|
const origin = "http://example.com";
|
||||||
const domain = "saleor.domain.host";
|
const domain = "saleor.domain.host";
|
||||||
|
@ -17,9 +21,6 @@ Object.defineProperty(window, "location", {
|
||||||
writable: true,
|
writable: true,
|
||||||
});
|
});
|
||||||
|
|
||||||
// eslint-disable-next-line
|
|
||||||
import { actions, DispatchResponseEvent, HandshakeEvent, AppBridge, ThemeEvent } from ".";
|
|
||||||
|
|
||||||
const handshakeEvent: HandshakeEvent = {
|
const handshakeEvent: HandshakeEvent = {
|
||||||
payload: {
|
payload: {
|
||||||
token: "mock-token",
|
token: "mock-token",
|
||||||
|
@ -211,4 +212,12 @@ describe("AppBridge", () => {
|
||||||
|
|
||||||
expect(appBridge.getState().domain).toEqual("https://foo.bar");
|
expect(appBridge.getState().domain).toEqual("https://foo.bar");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it.each<LocaleCode>(["pl", "en", "it"])("sets initial locale \"%s\" from constructor", (locale) => {
|
||||||
|
const instance = new AppBridge({
|
||||||
|
initialLocale: locale,
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(instance.getState().locale).toBe(locale);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
import debugPkg from "debug";
|
import debugPkg from "debug";
|
||||||
|
|
||||||
|
import { LocaleCode } from "../locales";
|
||||||
import { Actions } from "./actions";
|
import { Actions } from "./actions";
|
||||||
import { AppBridgeState, AppBridgeStateContainer } from "./app-bridge-state";
|
import { AppBridgeState, AppBridgeStateContainer } from "./app-bridge-state";
|
||||||
import { SSR } from "./constants";
|
import { SSR } from "./constants";
|
||||||
|
@ -67,6 +68,7 @@ const createEmptySubscribeMap = (): SubscribeMap => ({
|
||||||
|
|
||||||
export type AppBridgeOptions = {
|
export type AppBridgeOptions = {
|
||||||
targetDomain?: string;
|
targetDomain?: string;
|
||||||
|
initialLocale?: LocaleCode;
|
||||||
};
|
};
|
||||||
|
|
||||||
const getDefaultOptions = (): AppBridgeOptions => ({
|
const getDefaultOptions = (): AppBridgeOptions => ({
|
||||||
|
@ -74,7 +76,7 @@ const getDefaultOptions = (): AppBridgeOptions => ({
|
||||||
});
|
});
|
||||||
|
|
||||||
export class AppBridge {
|
export class AppBridge {
|
||||||
private state = new AppBridgeStateContainer();
|
private state: AppBridgeStateContainer;
|
||||||
|
|
||||||
private refererOrigin = document.referrer ? new URL(document.referrer).origin : undefined;
|
private refererOrigin = document.referrer ? new URL(document.referrer).origin : undefined;
|
||||||
|
|
||||||
|
@ -91,6 +93,10 @@ export class AppBridge {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.state = new AppBridgeStateContainer({
|
||||||
|
initialLocale: options.initialLocale,
|
||||||
|
});
|
||||||
|
|
||||||
this.combinedOptions = {
|
this.combinedOptions = {
|
||||||
...this.combinedOptions,
|
...this.combinedOptions,
|
||||||
...options,
|
...options,
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
import { LocaleCode } from "../locales";
|
||||||
import { Values } from "./helpers";
|
import { Values } from "./helpers";
|
||||||
|
|
||||||
export type Version = 1;
|
export type Version = 1;
|
||||||
|
@ -51,7 +52,7 @@ export type ThemeEvent = Event<
|
||||||
export type LocaleChangedEvent = Event<
|
export type LocaleChangedEvent = Event<
|
||||||
"localeChanged",
|
"localeChanged",
|
||||||
{
|
{
|
||||||
locale: string;
|
locale: LocaleCode;
|
||||||
}
|
}
|
||||||
>;
|
>;
|
||||||
|
|
||||||
|
|
|
@ -2,3 +2,5 @@ export const SALEOR_DOMAIN_HEADER = "saleor-domain";
|
||||||
export const SALEOR_EVENT_HEADER = "saleor-event";
|
export const SALEOR_EVENT_HEADER = "saleor-event";
|
||||||
export const SALEOR_SIGNATURE_HEADER = "saleor-signature";
|
export const SALEOR_SIGNATURE_HEADER = "saleor-signature";
|
||||||
export const SALEOR_AUTHORIZATION_BEARER_HEADER = "authorization-bearer";
|
export const SALEOR_AUTHORIZATION_BEARER_HEADER = "authorization-bearer";
|
||||||
|
|
||||||
|
export * from "./locales";
|
||||||
|
|
|
@ -6,6 +6,9 @@ const capitalize = (value: string) => value.charAt(0).toUpperCase() + value.slic
|
||||||
|
|
||||||
const dropFileExtension = (filename: string) => path.parse(filename).name;
|
const dropFileExtension = (filename: string) => path.parse(filename).name;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @deprecated remove magic
|
||||||
|
*/
|
||||||
export const inferWebhooks = async (
|
export const inferWebhooks = async (
|
||||||
baseURL: string,
|
baseURL: string,
|
||||||
webhooksPath: string,
|
webhooksPath: string,
|
||||||
|
|
47
src/locales.ts
Normal file
47
src/locales.ts
Normal file
|
@ -0,0 +1,47 @@
|
||||||
|
/**
|
||||||
|
* Available locales in Saleor Dashboard.
|
||||||
|
* TODO: Extract to shared package between Dashboard and sdk, so this stay in sync?
|
||||||
|
*/
|
||||||
|
export type LocaleCode =
|
||||||
|
| "ar"
|
||||||
|
| "az"
|
||||||
|
| "bg"
|
||||||
|
| "bn"
|
||||||
|
| "ca"
|
||||||
|
| "cs"
|
||||||
|
| "da"
|
||||||
|
| "de"
|
||||||
|
| "el"
|
||||||
|
| "en"
|
||||||
|
| "es"
|
||||||
|
| "es-CO"
|
||||||
|
| "et"
|
||||||
|
| "fa"
|
||||||
|
| "fr"
|
||||||
|
| "hi"
|
||||||
|
| "hu"
|
||||||
|
| "hy"
|
||||||
|
| "id"
|
||||||
|
| "is"
|
||||||
|
| "it"
|
||||||
|
| "ja"
|
||||||
|
| "ko"
|
||||||
|
| "mn"
|
||||||
|
| "nb"
|
||||||
|
| "nl"
|
||||||
|
| "pl"
|
||||||
|
| "pt"
|
||||||
|
| "pt-BR"
|
||||||
|
| "ro"
|
||||||
|
| "ru"
|
||||||
|
| "sk"
|
||||||
|
| "sl"
|
||||||
|
| "sq"
|
||||||
|
| "sr"
|
||||||
|
| "sv"
|
||||||
|
| "th"
|
||||||
|
| "tr"
|
||||||
|
| "uk"
|
||||||
|
| "vi"
|
||||||
|
| "zh-Hans"
|
||||||
|
| "zh-Hant";
|
Loading…
Reference in a new issue