Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to test a race #8

Open
hdsenevi opened this issue Jan 16, 2018 · 1 comment
Open

How to test a race #8

hdsenevi opened this issue Jan 16, 2018 · 1 comment

Comments

@hdsenevi
Copy link

hdsenevi commented Jan 16, 2018

Hi, first of all thank you for taking the time to create and release this package. Saved a lot of time for me.

Question
How to test a race in redux-saga

Saga code

// Render a simple custom alert view with OK and CANCEL buttons
yield put(SettingsActions.alertView.showAlertView());

// Wait for user to interact with one of the buttons before saga continues
const { alertAction } = yield race({
    alertAction: take(SettingsActionTypes.alertView.ON_ACTION_TRIGGERED),
});

// User interacted with either OK or CANCEL so take appropriate action (save relevant prefs)
switch (alertAction.payload) {
    case SettingsFieldTypes.alertView.type.CONFIRM:
        yield put(RealmActions.saveUser(**with OK setting**));
        break;
    default:
        yield put(RealmActions.saveUser(**with CANCEL setting**));
        break;
}

Saga test

// Test if an alert is getting trigger
it('should call an AlertView', (result) => {
    expect(result).toEqual(put(SettingsActions.alertView.showAlertView()));
});

// Test if a race is getting called?
it('should wait for AlertView action triggered', (result) => {
    expect(result).toEqual(race({
        alertAction: take(SettingsActionTypes.alertView.ON_ACTION_TRIGGERED),
    }));
});

// Up to here it's fine, but after this point how do I test the saga?
// In actuality, we wait for user to tap on OK, or CANCEL before continuing with the saga
// But how do I simulate that
@hdsenevi
Copy link
Author

hdsenevi commented Jan 16, 2018

Sorry, partly answering my own question. After posting the question I realised that I could do the following

it('should call an AlertView', (result) => {
    expect(result).toEqual(put(SettingsActions.alertView.showAlertView()));
});

it('should wait for AlertView action triggered', (result) => {
    const alertAction = race({
        alertAction: take(SettingsActionTypes.alertView.ON_ACTION_TRIGGERED),
    });
    expect(result).toEqual(alertAction);
    return ({ alertAction });
});

it('should save user preferences', (result) => {
    expect(result).toEqual(put(RealmActions.saveUser()));
});

Now the problem is, I'm only testing the default case of the switch statement.

Question
How to test the 'case SettingsFieldTypes.alertView.type.CONFIRM' in the switch

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant