Last active
March 23, 2020 13:46
-
-
Save lucassus/730139425cdbdeb94f7eb27488ab0f7c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const fetchUser = createAsyncThunk<Cleaner>( | |
"authentication/fetchUser", | |
async () => { | |
const cleaner = await authenticationApi.fetchUser(); | |
return cleaner; | |
} | |
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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