mirror of
https://github.com/wassname/talk.git
synced 2026-09-09 11:38:08 +08:00
Get view from query + tests
This commit is contained in:
@@ -14,7 +14,7 @@ export interface AppProps {
|
||||
const App: StatelessComponent<AppProps> = props => {
|
||||
return (
|
||||
<Flex justifyContent="center" className={styles.root}>
|
||||
Hello World
|
||||
Show view {props.view}
|
||||
</Flex>
|
||||
);
|
||||
};
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
||||
|
||||
exports[`init local state 1`] = `
|
||||
"{
|
||||
\\"client:root\\": {
|
||||
\\"__id\\": \\"client:root\\",
|
||||
\\"__typename\\": \\"__Root\\",
|
||||
\\"local\\": {
|
||||
\\"__ref\\": \\"client:root.local\\"
|
||||
}
|
||||
},
|
||||
\\"client:root.local\\": {
|
||||
\\"__id\\": \\"client:root.local\\",
|
||||
\\"__typename\\": \\"Local\\",
|
||||
\\"view\\": \\"SIGN_IN\\"
|
||||
}
|
||||
}"
|
||||
`;
|
||||
@@ -0,0 +1,36 @@
|
||||
import { Environment, RecordSource } from "relay-runtime";
|
||||
|
||||
import { LOCAL_ID } from "talk-framework/lib/relay";
|
||||
import { createRelayEnvironment } from "talk-framework/testHelpers";
|
||||
|
||||
import initLocalState from "./initLocalState";
|
||||
|
||||
let environment: Environment;
|
||||
let source: RecordSource;
|
||||
|
||||
beforeEach(() => {
|
||||
source = new RecordSource();
|
||||
environment = createRelayEnvironment({
|
||||
source,
|
||||
initLocalState: false,
|
||||
});
|
||||
});
|
||||
|
||||
it("init local state", () => {
|
||||
initLocalState(environment);
|
||||
expect(JSON.stringify(source.toJSON(), null, 2)).toMatchSnapshot();
|
||||
});
|
||||
|
||||
it("set view from query", () => {
|
||||
const view = "SIGN_UP";
|
||||
const previousLocation = location.toString();
|
||||
const previousState = window.history.state;
|
||||
window.history.replaceState(
|
||||
previousState,
|
||||
document.title,
|
||||
`http://localhost/?view=${view}`
|
||||
);
|
||||
initLocalState(environment);
|
||||
expect(source.get(LOCAL_ID)!.view).toBe(view);
|
||||
window.history.replaceState(previousState, document.title, previousLocation);
|
||||
});
|
||||
@@ -1,3 +1,4 @@
|
||||
import qs from "query-string";
|
||||
import { commitLocalUpdate, Environment } from "relay-runtime";
|
||||
|
||||
import {
|
||||
@@ -16,8 +17,11 @@ export default async function initLocalState(environment: Environment) {
|
||||
// Create the Local Record which is the Root for the client states.
|
||||
const localRecord = createAndRetain(environment, s, LOCAL_ID, LOCAL_TYPE);
|
||||
|
||||
// Parse query params
|
||||
const query = qs.parse(location.search);
|
||||
|
||||
// Set default view.
|
||||
localRecord.setValue("SIGN_IN", "view");
|
||||
localRecord.setValue(query.view || "SIGN_IN", "view");
|
||||
|
||||
root.setLinkedRecord(localRecord, "local");
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user