Skip to content

Commit da68bd7

Browse files
authored
Set up unit tests (auth0#2)
* Set up unit tests * Add junit reporter * Add test to CI * Ignore test helpers from coverage
1 parent da7fbae commit da68bd7

File tree

9 files changed

+6342
-654
lines changed

9 files changed

+6342
-654
lines changed

.circleci/config.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ jobs:
1616
- ~/.npm
1717
- ~/.cache
1818
- run: npm run build
19+
- run: npm test
20+
- store_test_results:
21+
path: test-results
22+
- store_artifacts:
23+
path: coverage
1924

2025
workflows:
2126
Build and Test:

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,3 +104,4 @@ dist
104104
.tern-port
105105

106106
.idea
107+
test-results

__tests__/auth0-provider.test.tsx

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { useContext } from 'react';
2+
import { Auth0Context } from '../src/auth0-provider';
3+
import { renderHook } from '@testing-library/react-hooks';
4+
import { Auth0Client } from '@auth0/auth0-spa-js';
5+
import { createWrapper } from './helpers';
6+
7+
jest.mock('@auth0/auth0-spa-js');
8+
9+
describe('Auth0Provider', () => {
10+
it('should provide an instance of the Auth0Client', () => {
11+
const wrapper = createWrapper();
12+
const {
13+
result: { current },
14+
} = renderHook(() => useContext(Auth0Context), { wrapper });
15+
expect(current).toBeInstanceOf(Auth0Client);
16+
});
17+
18+
it('should configure an instance of the Auth0Client', () => {
19+
const opts = {
20+
client_id: 'foo',
21+
domain: 'bar',
22+
};
23+
const wrapper = createWrapper(opts);
24+
renderHook(() => useContext(Auth0Context), {
25+
wrapper,
26+
});
27+
expect(Auth0Client).toHaveBeenCalledWith(opts);
28+
});
29+
});

__tests__/helpers.tsx

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { Auth0ClientOptions } from '@auth0/auth0-spa-js';
2+
import React, { PropsWithChildren } from 'react';
3+
import Auth0Provider from '../src/auth0-provider';
4+
5+
export const createWrapper = ({
6+
client_id = '__test_client_id__',
7+
domain = '__test_domain__',
8+
}: Partial<Auth0ClientOptions> = {}) => ({
9+
children,
10+
}: PropsWithChildren<{}>) => (
11+
<Auth0Provider domain={domain} client_id={client_id}>
12+
{children}
13+
</Auth0Provider>
14+
);

__tests__/use-auth0.test.tsx

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import useAuth0 from '../src/use-auth0';
2+
import { renderHook } from '@testing-library/react-hooks';
3+
import { Auth0Client } from '@auth0/auth0-spa-js';
4+
import { createWrapper } from './helpers';
5+
6+
jest.mock('@auth0/auth0-spa-js');
7+
8+
describe('useAuth0', () => {
9+
it('should provide an instance of the Auth0Client', () => {
10+
const wrapper = createWrapper();
11+
const {
12+
result: { current },
13+
} = renderHook(useAuth0, { wrapper });
14+
expect(current).toBeInstanceOf(Auth0Client);
15+
});
16+
});

jest.config.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module.exports = {
2+
clearMocks: true,
3+
coveragePathIgnorePatterns: ['/__tests__/'],
4+
coverageReporters: ['lcov', 'text', 'text-summary'],
5+
preset: 'ts-jest',
6+
reporters: [
7+
'default',
8+
['jest-junit', { outputDirectory: 'test-results/jest' }]
9+
],
10+
testRegex: '/__tests__/.+test\.tsx?$'
11+
};

0 commit comments

Comments
 (0)