Skip to content

How to test a race #8

Open
Open
@hdsenevi

Description

@hdsenevi

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions