[CORL-331] Better tests with types (#2270)

* feat: suspending, banning, now propogation

* feat: new mutation api with hooks support

* feat: better types in tests and refactor

* fix: lint
This commit is contained in:
Kiwi
2019-04-23 19:46:14 +00:00
committed by Wyatt Johnson
parent dbbc1af42e
commit 5150cdf60e
136 changed files with 2546 additions and 2465 deletions
@@ -26,10 +26,10 @@ import { PostMessageService } from "../postMessage";
import SendPymReady from "./SendPymReady";
import { TalkContext, TalkContextProvider } from "./TalkContext";
export type InitLocalState = ((
export type InitLocalState = (
environment: Environment,
context: TalkContext
) => void | Promise<void>);
) => void | Promise<void>;
interface CreateContextArguments {
/** Locales that the user accepts, usually `navigator.languages`. */
@@ -104,7 +104,7 @@ function createRelayEnvironment() {
return { environment, tokenGetter };
}
function createRestAPI(tokenGetter: (() => string)) {
function createRestAPI(tokenGetter: () => string) {
return new RestClient("/api", tokenGetter);
}
@@ -5,11 +5,7 @@ import React from "react";
interface Props {
form: FormApi;
rootKey?: string;
children: (
params: {
onInitValues: (data: any) => void;
}
) => React.ReactNode;
children: (params: { onInitValues: (data: any) => void }) => React.ReactNode;
}
/**
@@ -7,11 +7,9 @@ import { AddSubmitHook, SubmitHook, SubmitHookContextProvider } from "./";
interface Props {
onExecute: (data: any, form: FormApi) => Promise<void>;
children: (
params: {
onSubmit: (settings: any, form: FormApi) => void;
}
) => React.ReactNode;
children: (params: {
onSubmit: (settings: any, form: FormApi) => void;
}) => React.ReactNode;
}
/**
@@ -3,7 +3,7 @@ export interface BundledLocales {
}
export interface LoadableLocales {
[locale: string]: (() => Promise<string>);
[locale: string]: () => Promise<string>;
}
/**
@@ -15,6 +15,8 @@ import { TalkContext, withContext } from "../bootstrap";
* and the signature (input: I) => Promise<R>. Calling
* this will call the specified `commit` callback with
* the Relay `environment` provided by the context.
*
* @deprecated
*/
function createMutationContainer<T extends string, I, R>(
propName: T,
@@ -9,8 +9,8 @@ type RecordSourceProxy<T> = T extends object
readonly [P in keyof T]?: T[P] extends Array<infer U>
? ReadonlyArray<RecordSourceProxy<U>>
: T[P] extends ReadonlyArray<infer V>
? ReadonlyArray<RecordSourceProxy<V>>
: RecordSourceProxy<T[P]>
? ReadonlyArray<RecordSourceProxy<V>>
: RecordSourceProxy<T[P]>
}
: T;
@@ -35,14 +35,18 @@ export type MutationProp<
> = T extends Mutation<any, infer I, infer R>
? Parameters<T["commit"]>[1] extends undefined
? () => R
: keyof Parameters<T["commit"]>[1] extends never ? () => R : (input: I) => R
: keyof Parameters<T["commit"]>[1] extends never
? () => R
: (input: I) => R
: never;
type RemoveClientMutationID<T> = T extends Promise<infer U>
? Promise<
U extends { clientMutationId: any } ? Omit<U, "clientMutationId"> : U
>
: T extends { clientMutationId: any } ? Omit<T, "clientMutationId"> : T;
: T extends { clientMutationId: any }
? Omit<T, "clientMutationId">
: T;
export function createMutation<N extends string, I, R>(
name: N,
@@ -10,21 +10,18 @@ export default function useLoadMore(
count: number
): [() => void, boolean] {
const [isLoadingMore, setIsLoadingMore] = useState(false);
const loadMore = useCallback(
() => {
if (!relay.hasMore() || relay.isLoading()) {
return;
const loadMore = useCallback(() => {
if (!relay.hasMore() || relay.isLoading()) {
return;
}
setIsLoadingMore(true);
relay.loadMore(count, error => {
setIsLoadingMore(false);
if (error) {
// tslint:disable-next-line:no-console
console.error(error);
}
setIsLoadingMore(true);
relay.loadMore(count, error => {
setIsLoadingMore(false);
if (error) {
// tslint:disable-next-line:no-console
console.error(error);
}
});
},
[relay]
);
});
}, [relay]);
return [loadMore, isLoadingMore];
}
@@ -15,34 +15,31 @@ export default function useRefetch<V = Variables>(
): [() => void, boolean] {
const [manualRefetchCount, setManualRefetchCount] = useState(0);
const [refetching, setRefetching] = useState(false);
useEffectAfterMount(
() => {
setRefetching(true);
const disposable = relay.refetchConnection(
10,
error => {
setRefetching(false);
if (error) {
// tslint:disable-next-line:no-console
console.error(error);
}
},
variables
);
return () => {
if (disposable) {
disposable.dispose();
useEffectAfterMount(() => {
setRefetching(true);
const disposable = relay.refetchConnection(
10,
error => {
setRefetching(false);
if (error) {
// tslint:disable-next-line:no-console
console.error(error);
}
};
},
[
relay,
manualRefetchCount,
...Object.keys(variables).reduce<any[]>((a, k) => {
a.push((variables as any)[k]);
return a;
}, []),
]
);
},
variables
);
return () => {
if (disposable) {
disposable.dispose();
}
};
}, [
relay,
manualRefetchCount,
...Object.keys(variables).reduce<any[]>((a, k) => {
a.push((variables as any)[k]);
return a;
}, []),
]);
return [() => setManualRefetchCount(manualRefetchCount + 1), refetching];
}
@@ -1,7 +1,7 @@
import createPymStorage from "./PymStorage";
class PymStub {
public listeners: Record<string, ((msg: string) => void)> = {};
public listeners: Record<string, (msg: string) => void> = {};
public messages: Array<{ key: string; value: string }> = [];
public type: string;
@@ -14,7 +14,7 @@ class PymStorage implements PromisifiedStorage {
/** A Map of requestID => {resolve, reject} */
private requests: Record<
string,
{ resolve: ((v: any) => void); reject: ((v: any) => void) }
{ resolve: (v: any) => void; reject: (v: any) => void }
> = {};
/** Requests method with parameters over pym. */