UI - added test files

This commit is contained in:
Hoang Pham 2021-12-28 10:56:31 -06:00
parent 9bf56188f7
commit 0ad2bffd33
2 changed files with 107 additions and 0 deletions

View file

@ -0,0 +1,12 @@
const pilosa = {
get: {
auth() {
return new Promise((resolve, reject) => {});
},
userinfo() {
return new Promise((resolve, reject) => {});
},
},
};
module.exports.pilosa = pilosa;

View file

@ -0,0 +1,95 @@
import { AxiosResponse } from 'axios';
import { act } from 'react-dom/test-utils';
import ReactDOM from 'react-dom';
import { ProvideAuth, useAuth } from 'services/useAuth';
import { pilosa } from './eventServices';
jest.mock('./eventServices');
const AUTHENTICATED = 'Authenticated';
const NOTAUTHED = 'Not Authed';
const AUTHOFF = 'Auth off';
function TestUseAuthComponent() {
const auth = useAuth();
if (auth.isAuthOn === true && auth.isAuthenticated === true) {
return <div>{AUTHENTICATED}</div>;
} else if (auth.isAuthOn === true && auth.isAuthenticated === false) {
return <div>{NOTAUTHED}</div>;
} else {
return <div>{AUTHOFF}</div>;
}
}
beforeEach(() => {
jest.clearAllMocks();
});
test('useAuth - expect authenticated', async () => {
const mockResponse: AxiosResponse<any> = {
status: 200,
data: 'OK',
statusText: '',
headers: {},
config: {},
};
const root = document.createElement('root');
await act(async () => {
jest.spyOn(pilosa.get, 'auth').mockResolvedValueOnce(mockResponse);
ReactDOM.render(
<ProvideAuth>
<TestUseAuthComponent />
</ProvideAuth>,
root
);
});
expect(pilosa.get.auth).toHaveBeenCalledTimes(1);
expect(root.innerHTML).toContain(AUTHENTICATED);
});
test('test useAuth - expect not authed', async () => {
const mockResponse: AxiosResponse<any> = {
status: 200,
data: '',
statusText: '',
headers: {},
config: {},
};
const root = document.createElement('root');
await act(async () => {
jest.spyOn(pilosa.get, 'auth').mockResolvedValueOnce(mockResponse);
ReactDOM.render(
<ProvideAuth>
<TestUseAuthComponent />
</ProvideAuth>,
root
);
});
expect(pilosa.get.auth).toHaveBeenCalledTimes(1);
expect(root.innerHTML).toContain(NOTAUTHED);
});
test('test useAuth - expect auth off', async () => {
const mockResponse: AxiosResponse<any> = {
status: 204,
data: '',
statusText: '',
headers: {},
config: {},
};
const root = document.createElement('root');
await act(async () => {
jest.spyOn(pilosa.get, 'auth').mockResolvedValueOnce(mockResponse);
ReactDOM.render(
<ProvideAuth>
<TestUseAuthComponent />
</ProvideAuth>,
root
);
});
expect(pilosa.get.auth).toHaveBeenCalledTimes(1);
expect(root.innerHTML).toContain(AUTHOFF);
});