Fix tests

This commit is contained in:
Chi Vinh Le
2018-09-06 00:06:58 +02:00
parent 31bc5fa913
commit c96112fc8c
39 changed files with 1095 additions and 262 deletions
@@ -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;