Skip to content

Commit

Permalink
Corrected usage of ReactDOMTestUtils.act, fixes broken tests in lates…
Browse files Browse the repository at this point in the history
…t versions of testing-library/jest
  • Loading branch information
jamesdh committed Feb 28, 2023
1 parent 1dbb4aa commit 2747985
Showing 1 changed file with 58 additions and 50 deletions.
108 changes: 58 additions & 50 deletions src/__tests__/AuthContext.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,27 +24,27 @@ jest.mock('oidc-client-ts', () => {

describe('AuthContext', () => {
it('should check for user and redirect', async () => {
const u = {
getUser: jest.fn(),
signinRedirect: jest.fn(),
events,
} as any;
const onBeforeSignIn = jest.fn();
await act(async () => {
const u = {
getUser: jest.fn(),
signinRedirect: jest.fn(),
events,
} as any;
const onBeforeSignIn = jest.fn();
render(<AuthProvider userManager={u} onBeforeSignIn={onBeforeSignIn} />);
await waitFor(() => expect(u.getUser).toHaveBeenCalled());
await waitFor(() => expect(onBeforeSignIn).toHaveBeenCalled());
await waitFor(() => expect(u.signinRedirect).toHaveBeenCalled());
});
expect(u.getUser).toHaveBeenCalled();
expect(onBeforeSignIn).toHaveBeenCalled();
expect(u.signinRedirect).toHaveBeenCalled();
});

it('should redirect when asked', async () => {
const u = {
getUser: jest.fn(),
signinRedirect: jest.fn(),
events,
} as any;
await act(async () => {
const u = {
getUser: jest.fn(),
signinRedirect: jest.fn(),
events,
} as any;
render(
<AuthProvider userManager={u} autoSignIn={false}>
<AuthContext.Consumer>
Expand All @@ -55,18 +55,19 @@ describe('AuthContext', () => {
</AuthContext.Consumer>
</AuthProvider>,
);
await waitFor(() => expect(u.getUser).toHaveBeenCalled());
await waitFor(() => expect(u.signinRedirect).toHaveBeenCalled());
});
expect(u.getUser).toHaveBeenCalled();
expect(u.signinRedirect).toHaveBeenCalled();
});

it('should open Popup when asked', async () => {
const u = {
getUser: jest.fn(),
signinPopupCallback: jest.fn(),
signinPopup: jest.fn(),
events,
} as any;
await act(async () => {
const u = {
getUser: jest.fn(),
signinPopupCallback: jest.fn(),
signinPopup: jest.fn(),
events,
} as any;
render(
<AuthProvider userManager={u} autoSignIn={false}>
<AuthContext.Consumer>
Expand All @@ -77,20 +78,22 @@ describe('AuthContext', () => {
</AuthContext.Consumer>
</AuthProvider>,
);
await waitFor(() => expect(u.signinPopupCallback).toHaveBeenCalled());
await waitFor(() => expect(u.signinPopup).toHaveBeenCalled());
});
expect(u.signinPopupCallback).toHaveBeenCalled();
expect(u.signinPopup).toHaveBeenCalled();
});

it('should not redirect when asked', async () => {
const u = {
getUser: jest.fn(),
events,
} as any;
await act(async () => {
const u = {
getUser: jest.fn(),
events,
} as any;
render(<AuthProvider userManager={u} autoSignIn={false}></AuthProvider>);
await waitFor(() => expect(u.getUser).toHaveBeenCalled());
});
expect(u.getUser).toHaveBeenCalled();
});

it('should generate a UserManager', async () => {
render(
<AuthProvider
Expand All @@ -99,8 +102,9 @@ describe('AuthContext', () => {
redirectUri="http://127.0.0.1"
/>,
);
await waitFor(() => expect(UserManager).toHaveBeenCalled());
expect(UserManager).toHaveBeenCalled();
});

it('should use post-logout redirect URI when given', async () => {
render(
<AuthProvider
Expand All @@ -110,10 +114,11 @@ describe('AuthContext', () => {
postLogoutRedirectUri="https://localhost"
/>,
);
await waitFor(() => expect(UserManager).toHaveBeenLastCalledWith(
expect(UserManager).toHaveBeenLastCalledWith(
expect.objectContaining({ post_logout_redirect_uri: 'https://localhost'})
));
);
});

it('should fall back to redirectUri when post-logout redirect URI is not given', async () => {
render(
<AuthProvider
Expand All @@ -122,10 +127,11 @@ describe('AuthContext', () => {
redirectUri="http://127.0.0.1"
/>,
);
await waitFor(() => expect(UserManager).toHaveBeenLastCalledWith(
expect(UserManager).toHaveBeenLastCalledWith(
expect.objectContaining({ post_logout_redirect_uri: 'http://127.0.0.1'})
));
);
});

it('should use silent redirect URI when given', async () => {
render(
<AuthProvider
Expand All @@ -135,10 +141,11 @@ describe('AuthContext', () => {
silentRedirectUri="https://localhost"
/>,
);
await waitFor(() => expect(UserManager).toHaveBeenLastCalledWith(
expect(UserManager).toHaveBeenLastCalledWith(
expect.objectContaining({ silent_redirect_uri: 'https://localhost'})
));
);
});

it('should fall back to redirectUri when silent redirect URI is not given', async () => {
render(
<AuthProvider
Expand All @@ -147,21 +154,22 @@ describe('AuthContext', () => {
redirectUri="http://127.0.0.1"
/>,
);
await waitFor(() => expect(UserManager).toHaveBeenLastCalledWith(
expect(UserManager).toHaveBeenLastCalledWith(
expect.objectContaining({ silent_redirect_uri: 'http://127.0.0.1'})
));
);
});

it('should get userData', async () => {
const userManager = {
getUser: async () => ({
access_token: 'token',
}),
signinCallback: jest.fn(),
events,
} as any;
let result: any;
await act(async () => {
const userManager = {
getUser: async () => ({
access_token: 'token',
}),
signinCallback: jest.fn(),
events,
} as any;
const { getByText } = render(
result = render(
<AuthProvider userManager={userManager}>
<AuthContext.Consumer>
{(value) =>
Expand All @@ -172,10 +180,8 @@ describe('AuthContext', () => {
</AuthContext.Consumer>
</AuthProvider>,
);
await waitFor(() =>
expect(getByText(/^Received:/).textContent).toBe('Received: token'),
);
});
expect(result.getByText(/^Received:/).textContent).toBe('Received: token');
});

it('should refresh userData when new data is available', async () => {
Expand Down Expand Up @@ -255,6 +261,7 @@ describe('AuthContext', () => {
await waitFor(() => expect(onSignOut).toHaveBeenCalled());
await waitFor(() => expect(userManager.removeUser).toHaveBeenCalled());
});

it('should end session and logout the user when signoutRedirect is true', async () => {
const userManager = {
getUser: async () => ({
Expand All @@ -281,6 +288,7 @@ describe('AuthContext', () => {
await waitFor(() => expect(onSignOut).toHaveBeenCalled());
await waitFor(() => expect(userManager.signoutRedirect).toHaveBeenCalled());
});

it('should end session and logout the user when signoutRedirect is an object', async () => {
const userManager = {
getUser: async () => ({
Expand Down Expand Up @@ -313,4 +321,4 @@ describe('AuthContext', () => {
}),
);
});
});
});

0 comments on commit 2747985

Please sign in to comment.