Skip to content

Commit

Permalink
Set up unit tests (#2)
Browse files Browse the repository at this point in the history
* Set up unit tests
* Add junit reporter
* Add test to CI
* Ignore test helpers from coverage
  • Loading branch information
adamjmcgrath committed May 1, 2020
1 parent da7fbae commit da68bd7
Show file tree
Hide file tree
Showing 9 changed files with 6,342 additions and 654 deletions.
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?$'
};

0 comments on commit da68bd7

Please sign in to comment.