Just curious what would be the most optimal strategy in this case:
- Testing Logout functionality (Header with Logout Button)
When that button is clicked, I
dispatch(Logout), which changes the state of my redux reducer:#action-creator.jsreturn { type: LOGOUT };#reducers.jscase LOGOUT: return { ...state, loggedin: false }
In my Header I have basically the following:
const user_state = useSelector(state => state.user)
...
{ user_state.loggedin ? <button>Logout</button> : <button>Login</button> }
So in my tests, among other things, I want to test if the UI changes when the Logout button is clicked:
test('Can logout', () => {
let store = mockStore({
user: {
loggedin: true // simulate logged in state to display "Logout"
}
});
const { getByText } = render(<Provider store={store}><Header/>.
</Provider>);
fireEvent.click(getByText('Logout'))
// ????
});
Ideally, I want to test expect(getByText("Login")).toBeTruthy(); but, obviously, my mocked state is not going to change... Should I initialize the test with the real store and then somehow update it so when fireEvent is fired, the state actually updates and the Login button is rendered.