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

Set up unit tests #2

Merged
merged 4 commits into from
May 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ jobs:
- ~/.npm
- ~/.cache
- run: npm run build
- run: npm test
- store_test_results:
path: test-results
- store_artifacts:
path: coverage

workflows:
Build and Test:
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,4 @@ dist
.tern-port

.idea
test-results
29 changes: 29 additions & 0 deletions __tests__/auth0-provider.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { useContext } from 'react';
import { Auth0Context } from '../src/auth0-provider';
import { renderHook } from '@testing-library/react-hooks';
import { Auth0Client } from '@auth0/auth0-spa-js';
import { createWrapper } from './helpers';

jest.mock('@auth0/auth0-spa-js');

describe('Auth0Provider', () => {
it('should provide an instance of the Auth0Client', () => {
const wrapper = createWrapper();
const {
result: { current },
} = renderHook(() => useContext(Auth0Context), { wrapper });
expect(current).toBeInstanceOf(Auth0Client);
});

it('should configure an instance of the Auth0Client', () => {
const opts = {
client_id: 'foo',
domain: 'bar',
};
const wrapper = createWrapper(opts);
renderHook(() => useContext(Auth0Context), {
wrapper,
});
expect(Auth0Client).toHaveBeenCalledWith(opts);
});
});
14 changes: 14 additions & 0 deletions __tests__/helpers.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Auth0ClientOptions } from '@auth0/auth0-spa-js';
import React, { PropsWithChildren } from 'react';
import Auth0Provider from '../src/auth0-provider';

export const createWrapper = ({
client_id = '__test_client_id__',
domain = '__test_domain__',
}: Partial<Auth0ClientOptions> = {}) => ({
children,
}: PropsWithChildren<{}>) => (
<Auth0Provider domain={domain} client_id={client_id}>
{children}
</Auth0Provider>
);
16 changes: 16 additions & 0 deletions __tests__/use-auth0.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import useAuth0 from '../src/use-auth0';
import { renderHook } from '@testing-library/react-hooks';
import { Auth0Client } from '@auth0/auth0-spa-js';
import { createWrapper } from './helpers';

jest.mock('@auth0/auth0-spa-js');

describe('useAuth0', () => {
it('should provide an instance of the Auth0Client', () => {
const wrapper = createWrapper();
const {
result: { current },
} = renderHook(useAuth0, { wrapper });
expect(current).toBeInstanceOf(Auth0Client);
});
});
11 changes: 11 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module.exports = {
clearMocks: true,
coveragePathIgnorePatterns: ['/__tests__/'],
coverageReporters: ['lcov', 'text', 'text-summary'],
preset: 'ts-jest',
reporters: [
'default',
['jest-junit', { outputDirectory: 'test-results/jest' }]
],
testRegex: '/__tests__/.+test\.tsx?$'
};