Skip to content

Commit

Permalink
chore(clerk-react): Additional tests with vitest (#4772)
Browse files Browse the repository at this point in the history
  • Loading branch information
jacekradko authored Dec 13, 2024
1 parent a02173d commit e7476f0
Show file tree
Hide file tree
Showing 28 changed files with 317 additions and 100 deletions.
2 changes: 2 additions & 0 deletions .changeset/breezy-foxes-lay.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
---
2 changes: 1 addition & 1 deletion packages/react/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports = {
root: true,
extends: ['@clerk/custom/browser', '@clerk/custom/typescript', '@clerk/custom/jest', '@clerk/custom/react'],
extends: ['@clerk/custom/browser', '@clerk/custom/typescript', '@clerk/custom/react'],
};
24 changes: 0 additions & 24 deletions packages/react/jest.config.js

This file was deleted.

4 changes: 1 addition & 3 deletions packages/react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,7 @@
"lint:attw": "attw --pack .",
"lint:publint": "publint",
"publish:local": "pnpm yalc push --replace --sig",
"test": "jest",
"test:cache:clear": "jest --clearCache --useStderr",
"test:ci": "jest --maxWorkers=70%"
"test": "vitest"
},
"dependencies": {
"@clerk/shared": "workspace:^",
Expand Down
11 changes: 6 additions & 5 deletions packages/react/src/__tests__/isomorphicClerk.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import type { Resources, UnsubscribeCallback } from '@clerk/types';
import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest';

import { IsomorphicClerk } from '../isomorphicClerk';

describe('isomorphicClerk', () => {
beforeAll(() => {
jest.useFakeTimers();
vi.useFakeTimers();
});

afterAll(() => {
jest.useRealTimers();
vi.useRealTimers();
});

it('instantiates a IsomorphicClerk instance', () => {
Expand All @@ -35,10 +36,10 @@ describe('isomorphicClerk', () => {
void isomorphicClerk.__unstable__updateProps({ appearance: { baseTheme: 'green' } });
expect(propsHistory).toEqual([]);

jest.spyOn(isomorphicClerk, 'loaded', 'get').mockReturnValue(true);
vi.spyOn(isomorphicClerk, 'loaded', 'get').mockReturnValue(true);
isomorphicClerk.emitLoaded();
void isomorphicClerk.__unstable__updateProps({ appearance: { baseTheme: 'white' } });
await jest.runAllTimersAsync();
await vi.runAllTimersAsync();

expect(propsHistory).toEqual([
{ appearance: { baseTheme: 'dark' } },
Expand Down Expand Up @@ -75,7 +76,7 @@ describe('isomorphicClerk', () => {
// Unsubscribe one listener before ClerkJS is loaded
unsubscribe1();

jest.spyOn(isomorphicClerk, 'loaded', 'get').mockReturnValue(true);
vi.spyOn(isomorphicClerk, 'loaded', 'get').mockReturnValue(true);
isomorphicClerk.emitLoaded();
const unsubscribe3 = isomorphicClerk.addListener(payload => listenerCallHistory.push(payload));

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { expectTypeOf } from 'expect-type';
import type React from 'react';
import { describe, test } from 'vitest';

import type { OrganizationProfile } from '..';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { expectTypeOf } from 'expect-type';
import type React from 'react';
import { describe, test } from 'vitest';

import type { OrganizationSwitcher } from '..';

Expand Down
1 change: 1 addition & 0 deletions packages/react/src/components/__tests__/SignIn.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { expectTypeOf } from 'expect-type';
import type React from 'react';
import { describe, test } from 'vitest';

import type { SignIn } from '..';

Expand Down
16 changes: 9 additions & 7 deletions packages/react/src/components/__tests__/SignInButton.test.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { userEvent } from '@testing-library/user-event';
import React from 'react';
import { afterAll, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest';

import { SignInButton } from '../SignInButton';

const mockRedirectToSignIn = jest.fn();
const mockRedirectToSignIn = vi.fn();
const originalError = console.error;

const mockClerk = {
redirectToSignIn: mockRedirectToSignIn,
} as any;

jest.mock('../withClerk', () => {
vi.mock('../withClerk', () => {
return {
withClerk: (Component: any) => (props: any) => {
return (
Expand All @@ -28,7 +29,7 @@ const url = 'https://www.clerk.com';

describe('<SignInButton/>', () => {
beforeAll(() => {
console.error = jest.fn();
console.error = vi.fn();
});

afterAll(() => {
Expand Down Expand Up @@ -68,7 +69,7 @@ describe('<SignInButton/>', () => {
});

it('renders passed button and calls both click handlers', async () => {
const handler = jest.fn();
const handler = vi.fn();

render(
<SignInButton>
Expand All @@ -90,10 +91,11 @@ describe('<SignInButton/>', () => {

it('uses text passed as children', async () => {
render(<SignInButton>text</SignInButton>);
screen.getByText('text');

await screen.findByText('text');
});

it('throws if multiple children provided', async () => {
it('throws if multiple children provided', () => {
expect(() => {
render(
<SignInButton>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import { render, screen, waitFor } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { userEvent } from '@testing-library/user-event';
import React from 'react';
import { afterAll, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest';

import { SignInWithMetamaskButton } from '../SignInWithMetamaskButton';

const mockAuthenticatewithMetamask = jest.fn();
const mockAuthenticatewithMetamask = vi.fn();
const originalError = console.error;

const mockClerk = {
authenticateWithMetamask: mockAuthenticatewithMetamask,
} as any;

jest.mock('../withClerk', () => {
vi.mock('../withClerk', () => {
return {
withClerk: (Component: any) => (props: any) => (
<Component
Expand All @@ -24,7 +25,7 @@ jest.mock('../withClerk', () => {

describe('<SignInWithMetamaskButton/>', () => {
beforeAll(() => {
console.error = jest.fn();
console.error = vi.fn();
});

afterAll(() => {
Expand All @@ -38,7 +39,8 @@ describe('<SignInWithMetamaskButton/>', () => {
it('calls clerk.authenticateWithMetamask when clicked', async () => {
render(<SignInWithMetamaskButton />);
const btn = screen.getByText('Sign in with Metamask');
userEvent.click(btn);
await userEvent.click(btn);

await waitFor(() => {
expect(mockAuthenticatewithMetamask).toHaveBeenCalled();
});
Expand All @@ -53,8 +55,8 @@ describe('<SignInWithMetamaskButton/>', () => {
expect(() => {
render(
<SignInWithMetamaskButton>
<button>1</button>
<button>2</button>
<button type='button'>1</button>
<button type='button'>2</button>
</SignInWithMetamaskButton>,
);
}).toThrow();
Expand Down
24 changes: 13 additions & 11 deletions packages/react/src/components/__tests__/SignOutButton.test.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import { render, screen, waitFor } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { userEvent } from '@testing-library/user-event';
import React from 'react';
import { afterAll, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest';

import { SignOutButton } from '../SignOutButton';

const mockSignOut = jest.fn();
const mockSignOut = vi.fn();
const originalError = console.error;

const mockClerk = {
signOut: mockSignOut,
} as any;

jest.mock('../withClerk', () => {
vi.mock('../withClerk', () => {
return {
withClerk: (Component: any) => (props: any) => {
return (
Expand All @@ -28,7 +29,7 @@ const url = 'https://www.clerk.com';

describe('<SignOutButton />', () => {
beforeAll(() => {
console.error = jest.fn();
console.error = vi.fn();
});

afterAll(() => {
Expand All @@ -42,7 +43,7 @@ describe('<SignOutButton />', () => {
it('calls clerk.signOutOne when clicked', async () => {
render(<SignOutButton />);
const btn = screen.getByText('Sign out');
userEvent.click(btn);
await userEvent.click(btn);
await waitFor(() => {
expect(mockSignOut).toHaveBeenCalled();
});
Expand All @@ -51,7 +52,7 @@ describe('<SignOutButton />', () => {
it('handles redirectUrl prop', async () => {
render(<SignOutButton redirectUrl={url} />);
const btn = screen.getByText('Sign out');
userEvent.click(btn);
await userEvent.click(btn);
await waitFor(() => {
expect(mockSignOut).toHaveBeenCalledWith({ redirectUrl: url });
});
Expand All @@ -60,23 +61,24 @@ describe('<SignOutButton />', () => {
it('handles signOutOptions prop', async () => {
render(<SignOutButton signOutOptions={{ redirectUrl: url, sessionId: 'sess_1yDceUR8SIKtQ0gIOO8fNsW7nhe' }} />);
const btn = screen.getByText('Sign out');
userEvent.click(btn);
await userEvent.click(btn);
await waitFor(() => {
expect(mockSignOut).toHaveBeenCalledWith({ redirectUrl: url, sessionId: 'sess_1yDceUR8SIKtQ0gIOO8fNsW7nhe' });
});
});

it('uses text passed as children', async () => {
render(<SignOutButton>text</SignOutButton>);
screen.getByText('text');

await screen.findByText('text');
});

it('throws if multiple children provided', async () => {
it('throws if multiple children provided', () => {
expect(() => {
render(
<SignOutButton>
<button>1</button>
<button>2</button>
<button type='button'>1</button>
<button type='button'>2</button>
</SignOutButton>,
);
}).toThrow();
Expand Down
1 change: 1 addition & 0 deletions packages/react/src/components/__tests__/SignUp.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { expectTypeOf } from 'expect-type';
import type React from 'react';
import { describe, test } from 'vitest';

import type { SignUp } from '..';

Expand Down
34 changes: 22 additions & 12 deletions packages/react/src/components/__tests__/SignUpButton.test.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import { render, screen, waitFor } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { userEvent } from '@testing-library/user-event';
import React from 'react';
import { afterAll, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest';

import { SignUpButton } from '../SignUpButton';

const mockRedirectToSignUp = jest.fn();
const mockRedirectToSignUp = vi.fn();
const originalError = console.error;

const mockClerk = {
redirectToSignUp: mockRedirectToSignUp,
} as any;

jest.mock('../withClerk', () => {
vi.mock('../withClerk', () => {
return {
withClerk: (Component: any) => (props: any) => {
return (
Expand All @@ -28,7 +29,7 @@ const url = 'https://www.clerk.com';

describe('<SignUpButton/>', () => {
beforeAll(() => {
console.error = jest.fn();
console.error = vi.fn();
});

afterAll(() => {
Expand All @@ -42,7 +43,8 @@ describe('<SignUpButton/>', () => {
it('calls clerk.redirectToSignUp when clicked', async () => {
render(<SignUpButton />);
const btn = screen.getByText('Sign up');
userEvent.click(btn);

await userEvent.click(btn);
await waitFor(() => {
expect(mockRedirectToSignUp).toHaveBeenCalled();
});
Expand All @@ -51,7 +53,8 @@ describe('<SignUpButton/>', () => {
it('handles forceRedirectUrl prop', async () => {
render(<SignUpButton forceRedirectUrl={url} />);
const btn = screen.getByText('Sign up');
userEvent.click(btn);

await userEvent.click(btn);
await waitFor(() => {
expect(mockRedirectToSignUp).toHaveBeenCalledWith({ forceRedirectUrl: url, signUpForceRedirectUrl: url });
});
Expand All @@ -60,7 +63,8 @@ describe('<SignUpButton/>', () => {
it('handles fallbackRedirectUrl prop', async () => {
render(<SignUpButton fallbackRedirectUrl={url} />);
const btn = screen.getByText('Sign up');
userEvent.click(btn);

await userEvent.click(btn);
await waitFor(() => {
expect(mockRedirectToSignUp).toHaveBeenCalledWith({
fallbackRedirectUrl: url,
Expand All @@ -70,14 +74,20 @@ describe('<SignUpButton/>', () => {
});

it('renders passed button and calls both click handlers', async () => {
const handler = jest.fn();
const handler = vi.fn();
render(
<SignUpButton>
<button onClick={handler}>custom button</button>
<button
onClick={handler}
type='button'
>
custom button
</button>
</SignUpButton>,
);
const btn = screen.getByText('custom button');
userEvent.click(btn);

await userEvent.click(btn);
await waitFor(() => {
expect(handler).toHaveBeenCalled();
expect(mockRedirectToSignUp).toHaveBeenCalled();
Expand All @@ -93,8 +103,8 @@ describe('<SignUpButton/>', () => {
expect(() => {
render(
<SignUpButton>
<button>1</button>
<button>2</button>
<button type='button'>1</button>
<button type='button'>2</button>
</SignUpButton>,
);
}).toThrow();
Expand Down
Loading

0 comments on commit e7476f0

Please sign in to comment.