Skip to content

Instantly share code, notes, and snippets.

@estrattonbailey
Last active March 10, 2020 23:23
Show Gist options
  • Save estrattonbailey/4135b8cfd996d85c7f7536677c90bbea to your computer and use it in GitHub Desktop.
Save estrattonbailey/4135b8cfd996d85c7f7536677c90bbea to your computer and use it in GitHub Desktop.
Observer
/* eslint-disable */
import * as React from 'react';
import { act } from 'react-dom/test-utils';
import { mount } from 'enzyme';
import { Provider } from 'react-redux';
import store from '@services/store';
import { AUTH_HYDRATE, AUTH_SET_IS_LOGGED_IN } from '@services/reducers/auth/types';
import { UserRole } from '@api/auth/types';
import { AuthObserver } from '@components/AuthObserver';
jest.useFakeTimers();
jest.mock('@api/auth', () => ({
isLoggedIn() {
return false;
}
}));
const hydrateAction = {
type: AUTH_HYDRATE,
payload: {
id: 0,
email: '',
username: '',
firstName: '',
role: UserRole.VERIFIER,
token: '',
intercomToken: '',
companyInfo: {},
usesTwoFactorAuth: false,
needsTwoFactorVerification: false,
twoFactorAuthDevices: [],
isVerifier: true,
isEmployer: false,
isHRResponder: false,
isEmployerType: false,
isEmployee: false,
}
};
describe('@components/AuthObserver', () => {
it('idle timeout', async done => {
let wrapper: any;
act(() => {
wrapper = mount(
<Provider store={store}>
<AuthObserver />
</Provider>
);
});
const initial = store.getState();
expect(initial.auth).toEqual(null);
store.dispatch(hydrateAction);
store.dispatch({
type: AUTH_SET_IS_LOGGED_IN,
payload: {
isLoggedIn: true,
},
});
const loggedIn = store.getState();
// @ts-ignore
expect(loggedIn.auth.id).toEqual(0);
wrapper.update();
act(() => {
jest.advanceTimersByTime(5 * 60 * 1000);
});
const loggedOut = store.getState();
expect(loggedOut.auth).toEqual(null);
done();
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment