Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate to React Testing Library #97

Merged
merged 4 commits into from
Jun 1, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 52 additions & 21 deletions src/components/Elements.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -139,17 +139,35 @@ describe('Elements', () => {
});

test('allows a transition from null to a valid Stripe object', () => {
let stripeProp: any = null;
const wrapper = ({children}: any) => (
<Elements stripe={stripeProp}>{children}</Elements>
const TestComponent = () => {
Copy link
Contributor

@dweedon-stripe dweedon-stripe Jun 1, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This actually seems overly round-about. The way you had it before is fine once I get my head around it. Especially if the old way is the blessed way of testing.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool, I'll leave it the original way then

const elements = useElements();

if (!elements) {
return null;
}

if (elements === mockElements) {
return <>expected</>;
}

throw new Error(`unexpected useElements value: ${elements}`);
};

const {container, rerender} = render(
<Elements stripe={null}>
<TestComponent />
</Elements>
);

const {result, rerender} = renderHook(() => useElements(), {wrapper});
expect(result.current).toBe(null);
expect(container).toBeEmpty();

stripeProp = mockStripe;
rerender();
expect(result.current).toBe(mockElements);
rerender(
<Elements stripe={mockStripe}>
<TestComponent />
</Elements>
);

expect(container).toHaveTextContent('expected');
});

test('works with a Promise resolving to a valid Stripe object', async () => {
Expand All @@ -169,24 +187,37 @@ describe('Elements', () => {
});

test('allows a transition from null to a valid Promise', async () => {
let stripeProp: any = null;
const wrapper = ({children}: any) => (
<Elements stripe={stripeProp}>{children}</Elements>
);
const TestComponent = () => {
const elements = useElements();

const {result, rerender, waitForNextUpdate} = renderHook(
() => useElements(),
{wrapper}
if (!elements) {
return null;
}

if (elements === mockElements) {
return <>expected</>;
}

throw new Error(`unexpected useElements value: ${elements}`);
};

const {container, rerender, findByText} = render(
<Elements stripe={null}>
<TestComponent />
</Elements>
);
expect(result.current).toBe(null);

stripeProp = mockStripePromise;
rerender();
expect(result.current).toBe(null);
expect(container).toBeEmpty();

await waitForNextUpdate();
rerender(
<Elements stripe={mockStripePromise}>
<TestComponent />
</Elements>
);

expect(result.current).toBe(mockElements);
expect(container).toBeEmpty();

await findByText('expected');
});

test('does not set context if Promise resolves after Elements is unmounted', async () => {
Expand Down