Skip to content

Commit

Permalink
[WIP]
Browse files Browse the repository at this point in the history
  • Loading branch information
avocadowastaken committed Jan 4, 2019
1 parent eb314d0 commit 2425cfa
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 3 deletions.
24 changes: 21 additions & 3 deletions src/__tests__/getMarkupFromTree-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import * as React from 'react';
import { renderToString } from 'react-dom/server';

import { ApolloProvider } from '../ApolloContext';
import { TestErrorBoundary } from '../__testutils__/TestErrorBoundary';
import createClient from '../__testutils__/createClient';
import { getMarkupFromTree } from '../getMarkupFromTree';
import { QueryHookOptions, useQuery } from '../useQuery';
Expand Down Expand Up @@ -82,9 +83,11 @@ interface UserWrapperProps extends QueryHookOptions<{}> {

function UserDetailsWrapper({ client, ...props }: UserWrapperProps) {
return (
<ApolloProvider client={client}>
<UserDetails {...props} />
</ApolloProvider>
<TestErrorBoundary>
<ApolloProvider client={client}>
<UserDetails {...props} />
</ApolloProvider>
</TestErrorBoundary>
);
}

Expand Down Expand Up @@ -351,6 +354,21 @@ it('should handle errors thrown by queries without suspense', async () => {
);
});

it('should handle errors thrown by queries with suspense', async () => {
const client = createMockClient(linkReturningError);
const tree = <UserDetailsWrapper client={client} suspend />;

await expect(
getMarkupFromTree({ tree, renderFunction: renderToString })
).rejects.toMatchInlineSnapshot(
`[Error: Network error: Simulating network error]`
);

expect(renderToString(tree)).toMatchInlineSnapshot(
`"No Current User (failed)"`
);
});

it('should correctly skip queries with suspense', async () => {
const client = createMockClient();

Expand Down
31 changes: 31 additions & 0 deletions src/__testutils__/TestErrorBoundary.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import React, { Component, ReactNode } from 'react';

interface Props {
readonly children: ReactNode;
}

interface State {
readonly error?: Error;
}

export class TestErrorBoundary extends Component<Props, State> {
public static getDerivedStateFromError(error: Error): State {
return { error };
}

public constructor(props: Readonly<Props>) {
super(props);

this.state = {};
}

public render(): React.ReactNode {
const { error } = this.state;

if (error) {
return <>TestErrorBoundary: {error.message}</>;
}

return this.props.children;
}
}

0 comments on commit 2425cfa

Please sign in to comment.