Skip to content

Clean up sagas and exception handling#39

Open
manbearshark wants to merge 6 commits intomasterfrom
env-and-api-fixes
Open

Clean up sagas and exception handling#39
manbearshark wants to merge 6 commits intomasterfrom
env-and-api-fixes

Conversation

@manbearshark
Copy link
Copy Markdown

@manbearshark manbearshark commented May 14, 2017

There were some issues with how sagas handled exceptions - this has been moved to the API code. Also I added a common generator function the sagas can use to handle todo actions, which reduces code duplication. Also added some tests for the async API stuff and the new saga generator functions.


This change is Reviewable

Comment thread common/api/todos.js
return ({ result });
} catch (error) {
return ({ error });
}
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Return either a result or error key depending on the API call outcome.,

describe('callApi', () => {
it('handles 400 error with an exception', async () => {
const url = 'http://fakeurl';
fetch.mockResponseOnce(JSON.stringify([{description: "add a todo"}]), {status: 400});
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added fetch-mock to test the API stuff.

Comment thread common/sagas/index.js
yield put(todosActions.addTodoSuccess(normalizeObjectToMap(response)));
} catch (e) {
yield put(todosActions.addTodoError());
export function* raceCallApi({
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generator used to handle all apiCall cases in the sagas.


const action = todosActions.toggleTodo(payload);
const generator = sagas.toggleTodo(action);
fetch.mockResponseOnce(JSON.stringify([{description: "add a todo"}]), delay);
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pretty cool that fetch-mock can handle testing timeout use cases.

Comment thread common/test/setupJest.js
@@ -0,0 +1 @@
global.fetch = require('jest-fetch-mock');
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is required to test using fetch-mock.

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

Successfully merging this pull request may close these issues.

1 participant