Skip to content

Commit

Permalink
Remove itAsync from getDataFromTree tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jerelmiller committed Dec 6, 2024
1 parent 3b83ece commit 723acee
Showing 1 changed file with 70 additions and 83 deletions.
153 changes: 70 additions & 83 deletions src/react/hoc/__tests__/ssr/getDataFromTree.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { DocumentNode } from "graphql";
import { ApolloClient } from "../../../../core";
import { ApolloProvider } from "../../../context";
import { InMemoryCache as Cache } from "../../../../cache";
import { itAsync, mockSingleLink } from "../../../../testing";
import { mockSingleLink } from "../../../../testing";
import { Query } from "../../../components";
import { getDataFromTree, getMarkupFromTree } from "../../../ssr";
import { graphql } from "../../graphql";
Expand Down Expand Up @@ -543,86 +543,78 @@ describe("SSR", () => {
});
});

itAsync(
"should allow for setting state in a component",
(resolve, reject) => {
const query = gql`
query user($id: ID) {
currentUser(id: $id) {
firstName
}
it("should allow for setting state in a component", async () => {
const query = gql`
query user($id: ID) {
currentUser(id: $id) {
firstName
}
`;
const resultData = { currentUser: { firstName: "James" } };
const variables = { id: "1" };
const link = mockSingleLink({
request: { query, variables },
result: { data: resultData },
});

const cache = new Cache({ addTypename: false });
const apolloClient = new ApolloClient({
link,
cache,
});

interface Props {
id: string;
}
interface Data {
currentUser: {
firstName: string;
`;
const resultData = { currentUser: { firstName: "James" } };
const variables = { id: "1" };
const link = mockSingleLink({
request: { query, variables },
result: { data: resultData },
});

const cache = new Cache({ addTypename: false });
const apolloClient = new ApolloClient({
link,
cache,
});

interface Props {
id: string;
}
interface Data {
currentUser: {
firstName: string;
};
}
interface Variables {
id: string;
}

class Element extends React.Component<
ChildProps<Props, Data, Variables>,
{ thing: number }
> {
state = { thing: 1 };

static getDerivedStateFromProps() {
return {
thing: 2,
};
}
interface Variables {
id: string;
}

class Element extends React.Component<
ChildProps<Props, Data, Variables>,
{ thing: number }
> {
state = { thing: 1 };
render() {
const { data } = this.props;
expect(this.state.thing).toBe(2);
return (
<div>
{!data || data.loading || !data.currentUser ?
"loading"
: data.currentUser.firstName}
</div>
);
}
}

static getDerivedStateFromProps() {
return {
thing: 2,
};
}
const ElementWithData = graphql<Props, Data, Variables>(query)(Element);

render() {
const { data } = this.props;
expect(this.state.thing).toBe(2);
return (
<div>
{!data || data.loading || !data.currentUser ?
"loading"
: data.currentUser.firstName}
</div>
);
}
}
const app = (
<ApolloProvider client={apolloClient}>
<ElementWithData id={"1"} />
</ApolloProvider>
);

const ElementWithData = graphql<Props, Data, Variables>(query)(Element);
await getDataFromTree(app);

const app = (
<ApolloProvider client={apolloClient}>
<ElementWithData id={"1"} />
</ApolloProvider>
);

getDataFromTree(app)
.then(() => {
const initialState = cache.extract();
expect(initialState).toBeTruthy();
expect(
initialState.ROOT_QUERY!['currentUser({"id":"1"})']
).toBeTruthy();
resolve();
})
.catch(console.error);
}
);
const initialState = cache.extract();
expect(initialState).toBeTruthy();
expect(initialState.ROOT_QUERY!['currentUser({"id":"1"})']).toBeTruthy();
});

it("should correctly initialize an empty state to null", () => {
class Element extends React.Component<any, any> {
Expand Down Expand Up @@ -651,7 +643,7 @@ describe("SSR", () => {
return getDataFromTree(<Element />);
});

itAsync("should allow prepping state from props", (resolve, reject) => {
it("should allow prepping state from props", async () => {
const query = gql`
query user($id: ID) {
currentUser(id: $id) {
Expand Down Expand Up @@ -730,16 +722,11 @@ describe("SSR", () => {
</ApolloProvider>
);

getDataFromTree(app)
.then(() => {
const initialState = apolloClient.cache.extract();
expect(initialState).toBeTruthy();
expect(
initialState.ROOT_QUERY!['currentUser({"id":"1"})']
).toBeTruthy();
resolve();
})
.catch(console.error);
await getDataFromTree(app);

const initialState = apolloClient.cache.extract();
expect(initialState).toBeTruthy();
expect(initialState.ROOT_QUERY!['currentUser({"id":"1"})']).toBeTruthy();
});

it("shouldn't run queries if ssr is turned to off", () => {
Expand Down

0 comments on commit 723acee

Please sign in to comment.