Skip to content

Instantly share code, notes, and snippets.

@lucassus
Last active March 23, 2020 13:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lucassus/730139425cdbdeb94f7eb27488ab0f7c to your computer and use it in GitHub Desktop.
Save lucassus/730139425cdbdeb94f7eb27488ab0f7c to your computer and use it in GitHub Desktop.
export const fetchUser = createAsyncThunk<Cleaner>(
"authentication/fetchUser",
async () => {
const cleaner = await authenticationApi.fetchUser();
return cleaner;
}
);
export const getAuthenticationToken = (state: AppState) =>
state.authentication.token;
export const isAuthenticated = createSelector(
getAuthenticationToken,
token => token !== null
);
export const isPending = (state: AppState) => state.authentication.pending;
export const getCurrentUser = (state: AppState) => state.authentication.user;
it(`handles .fetchUser`, async () => {
// Given
const store = configureStore({ reducer });
jest
.spyOn(authenticationApi, "fetchUser")
.mockReturnValue(Promise.resolve(cleaner));
// When
await store.dispatch(fetchUser());
// Then
expect(isPending(store.getState()).toBe(true);
// When the fetching is completed
await wait(() => expect(isPending(store.getState()).toBe(false));
// Then
expect(getCurrentUser(store.getState())).toBe(cleaner);
});
it(`handles .logout`, async () => {
// Given
const store = configureStore({
reducer,
preloadedState: {
authentication: {
token: "asdf",
user: cleaner
}
}
});
// When
await store.dispatch(logout());
const state = store.getState();
// Then
expect(getAuthenticationToken(state)).toEqual(null);
expect(getCurrentUser(state)).toEqual(null);
expect(isAuthenticated(state)).toBe(false);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment