Skip to content

Instantly share code, notes, and snippets.

@mraible
Created November 24, 2020 15:21
Show Gist options
  • Save mraible/e304df9cdcc93ed3e42eb8b903391d79 to your computer and use it in GitHub Desktop.
Save mraible/e304df9cdcc93ed3e42eb8b903391d79 to your computer and use it in GitHub Desktop.
App.test.js from CRA with Okta React 4.0.0
import React from 'react';
import ReactDOM from 'react-dom';
import { act } from 'react-dom/test-utils';
import App from './App';
jest.mock('@okta/okta-auth-js', () => {
return {
OktaAuth: jest.fn(() => {
return {
userAgent: 'okta/okta-auth-js',
options: {},
authStateManager: {
getAuthState: jest.fn().mockImplementation(() =>
({
isPending: true,
isAuthenticated: false,
idToken: null,
accessToken: null,
refreshToken: null
})),
subscribe: jest.fn(),
updateAuthState: jest.fn(),
},
isLoginRedirect: jest.fn().mockImplementation(() => false),
}
})
}
});
/*jest.mock('@okta/okta-react', () => {
return {
withOktaAuth: jest.fn(),
Security: ({ children }) => (<>{children}</>)
}
});*/
let container;
beforeEach(() => {
container = document.createElement('div');
document.body.appendChild(container);
});
afterEach(() => {
document.body.removeChild(container);
container = null;
});
test('renders learn react link', async () => {
await act(async () => {
ReactDOM.render(<App/>, container);
});
const linkElement = container.querySelector('a');
expect(linkElement.textContent).toBe('Learn React');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment