mirror of
https://github.com/featurebasedb/featurebase.git
synced 2026-09-10 15:01:03 +00:00
UI - added test files
This commit is contained in:
parent
9bf56188f7
commit
0ad2bffd33
2 changed files with 107 additions and 0 deletions
12
lattice/src/services/__mocks__/eventServices.tsx
Normal file
12
lattice/src/services/__mocks__/eventServices.tsx
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
const pilosa = {
|
||||
get: {
|
||||
auth() {
|
||||
return new Promise((resolve, reject) => {});
|
||||
},
|
||||
userinfo() {
|
||||
return new Promise((resolve, reject) => {});
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
module.exports.pilosa = pilosa;
|
||||
95
lattice/src/services/useAuth.test.tsx
Normal file
95
lattice/src/services/useAuth.test.tsx
Normal 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);
|
||||
});
|
||||
Loading…
Add table
Reference in a new issue