Support authentication

This commit is contained in:
Chi Vinh Le
2018-08-09 16:14:35 +02:00
parent ff62dc4ecc
commit d140e2449f
22 changed files with 357 additions and 15 deletions
@@ -0,0 +1,34 @@
import { commitLocalUpdate, Environment, RecordSource } from "relay-runtime";
import { timeout } from "talk-common/utils";
import { LOCAL_ID } from "talk-framework/lib/relay";
import { createRelayEnvironment } from "talk-framework/testHelpers";
import { commit } from "./SetAuthTokenMutation";
let environment: Environment;
const source: RecordSource = new RecordSource();
beforeAll(() => {
environment = createRelayEnvironment({
source,
});
});
it("Sets auth token", async () => {
const authToken = "auth token";
commit(environment, { authToken });
expect(source.get(LOCAL_ID)!.authToken).toEqual(authToken);
});
it("Should call gc", async () => {
commitLocalUpdate(environment, store => {
store.create("should-disappear", "tmp");
});
const authToken = null;
expect(source.get("should-disappear")).not.toBeUndefined();
commit(environment, { authToken });
await timeout();
expect(source.get(LOCAL_ID)!.authToken).toEqual(authToken);
expect(source.get("should-disappear")).toBeUndefined();
});
@@ -0,0 +1,34 @@
import { commitLocalUpdate, Environment } from "relay-runtime";
import { createMutationContainer } from "talk-framework/lib/relay";
import { LOCAL_ID } from "talk-framework/lib/relay/withLocalStateContainer";
export interface SetAuthTokenInput {
authToken: string | null;
}
export type SetAuthTokenMutation = (input: SetAuthTokenInput) => Promise<void>;
export async function commit(
environment: Environment,
input: SetAuthTokenInput
) {
return commitLocalUpdate(environment, store => {
const record = store.get(LOCAL_ID)!;
record.setValue(input.authToken, "authToken");
// Force gc to trigger.
environment
.retain({
dataID: "tmp",
node: { selections: [] },
variables: {},
})
.dispose();
});
}
export const withSetAuthTokenMutation = createMutationContainer(
"setCommentID",
commit
);
@@ -0,0 +1,5 @@
export {
withSetAuthTokenMutation,
SetAuthTokenMutation,
SetAuthTokenInput,
} from "./SetAuthTokenMutation";