mirror of
https://github.com/wassname/talk.git
synced 2026-07-21 12:51:03 +08:00
Fix tests
This commit is contained in:
@@ -1,18 +1,12 @@
|
||||
import React from "react";
|
||||
import { hoistStatics, wrapDisplayName } from "recompose";
|
||||
import * as React from "react";
|
||||
import {
|
||||
hoistStatics,
|
||||
InferableComponentEnhancer,
|
||||
wrapDisplayName,
|
||||
} from "recompose";
|
||||
|
||||
import { Omit } from "talk-ui/types";
|
||||
import { TalkContext, TalkContextConsumer } from "./TalkContext";
|
||||
|
||||
// Injects props and removes them from the prop requirements.
|
||||
// Will not pass through the injected props if they are passed in during
|
||||
// render. Also adds new prop requirements from TNeedsProps.
|
||||
type InferableComponentEnhancerWithProps<TInjectedProps> = <
|
||||
P extends TInjectedProps
|
||||
>(
|
||||
component: React.ComponentType<P>
|
||||
) => React.ComponentType<Omit<P, keyof TInjectedProps>>;
|
||||
|
||||
/**
|
||||
* withContext is a HOC wrapper around `TalkContextConsumer`.
|
||||
* `propsCallback` must be provided which accepts the `TalkContext`
|
||||
@@ -20,7 +14,7 @@ type InferableComponentEnhancerWithProps<TInjectedProps> = <
|
||||
*/
|
||||
function withContext<T>(
|
||||
propsCallback: (context: TalkContext) => T
|
||||
): InferableComponentEnhancerWithProps<T> {
|
||||
): InferableComponentEnhancer<T> {
|
||||
return hoistStatics<T>(
|
||||
<U extends T>(WrappedComponent: React.ComponentType<U>) => {
|
||||
const Component: React.StatelessComponent<any> = props => (
|
||||
@@ -33,7 +27,7 @@ function withContext<T>(
|
||||
Component.displayName = wrapDisplayName(WrappedComponent, "withContext");
|
||||
return Component;
|
||||
}
|
||||
) as any;
|
||||
);
|
||||
}
|
||||
|
||||
export default withContext;
|
||||
|
||||
@@ -5,6 +5,31 @@ import path from "path";
|
||||
// These locale prefixes are always loaded.
|
||||
const commonPrefixes = ["common", "framework"];
|
||||
|
||||
function decorateErrorWhenMissing(bundle: FluentBundle) {
|
||||
const originalHasMessage = bundle.hasMessage;
|
||||
const originalGetMessage = bundle.getMessage;
|
||||
const missing: string[] = [];
|
||||
bundle.hasMessage = (id: string) => {
|
||||
const result = originalHasMessage.apply(bundle, [id]);
|
||||
if (!result) {
|
||||
const msg = `${bundle.locales} translation for key "${id}" not found`;
|
||||
// tslint:disable-next-line:no-console
|
||||
console.error(msg);
|
||||
missing.push(id);
|
||||
}
|
||||
// Even if it is missing, we say it is available and later return a descriptive error
|
||||
// string as the translation.
|
||||
return true;
|
||||
};
|
||||
bundle.getMessage = (id: string) => {
|
||||
if (missing.indexOf(id) !== -1) {
|
||||
return `Missing translation "${id}"`;
|
||||
}
|
||||
return originalGetMessage.apply(bundle, [id]);
|
||||
};
|
||||
return bundle;
|
||||
}
|
||||
|
||||
function createFluentBundle(
|
||||
target: string,
|
||||
pathToLocale: string
|
||||
@@ -15,13 +40,11 @@ function createFluentBundle(
|
||||
files.forEach(f => {
|
||||
prefixes.forEach(prefix => {
|
||||
if (f.startsWith(prefix)) {
|
||||
bundle.addMessages(
|
||||
fs.readFileSync(path.resolve(pathToLocale, f)).toString()
|
||||
);
|
||||
bundle.addMessages(require(path.resolve(pathToLocale, f)));
|
||||
}
|
||||
});
|
||||
});
|
||||
return bundle;
|
||||
return decorateErrorWhenMissing(bundle);
|
||||
}
|
||||
|
||||
export default createFluentBundle;
|
||||
|
||||
Reference in New Issue
Block a user