Quellcode durchsuchen

Moved API saga to separate file

Fela Maslen vor 7 Jahren
Ursprung
Commit
142a40d8c1
4 geänderte Dateien mit 78 neuen und 66 gelöschten Zeilen
  1. 25 0
      src/sagas/api.js
  2. 1 22
      src/sagas/crud.js
  3. 48 0
      test/sagas/api.spec.js
  4. 4 44
      test/sagas/crud.spec.js

+ 25 - 0
src/sagas/api.js

@@ -0,0 +1,25 @@
+import { call } from 'redux-saga/effects';
+import axios from 'axios';
+
+export function *apiRequest(method, route, params = [], data = null) {
+    let path = `/api1/${route}`;
+    if (params.length) {
+        path = `${path}/${params.join('/')}`;
+    }
+
+    const args = [[axios, method], path];
+
+    if (['post', 'put'].includes(method)) {
+        args.push(data);
+    }
+
+    try {
+        const response = yield call(...args);
+
+        return { response, err: null };
+
+    } catch (err) {
+        return { response: null, err };
+    }
+}
+

+ 1 - 22
src/sagas/crud.js

@@ -1,5 +1,4 @@
 import { takeEvery, call, put } from 'redux-saga/effects';
-import axios from 'axios';
 
 import {
     DOC_CREATED,
@@ -15,27 +14,7 @@ import {
     docDeleteResponded
 } from 'actions/crud';
 
-export function *apiRequest(method, route, params = [], data = null) {
-    let path = `/api1/${route}`;
-    if (params.length) {
-        path = `${path}/${params.join('/')}`;
-    }
-
-    const args = [[axios, method], path];
-
-    if (['post', 'put'].includes(method)) {
-        args.push(data);
-    }
-
-    try {
-        const response = yield call(...args);
-
-        return { response, err: null };
-
-    } catch (err) {
-        return { response: null, err };
-    }
-}
+import { apiRequest } from 'sagas/api';
 
 export function *createDoc({ route, pendingId, fields }) {
     const { response, err } = yield call(apiRequest, 'post', route, [], fields);

+ 48 - 0
test/sagas/api.spec.js

@@ -0,0 +1,48 @@
+/* eslint-disable prefer-reflect */
+import { testSaga } from 'redux-saga-test-plan';
+import axios from 'axios';
+
+import {
+    apiRequest
+} from 'sagas/api';
+
+describe('API sagas', () => {
+    describe('apiRequest', () => {
+        const route = 'employees';
+        const fields = {
+            name: 'John Doe',
+            email: 'john.doe@mubaloo.com'
+        };
+
+        it('should make an API POST request', () => {
+            const response = { isResponse: true };
+
+            testSaga(apiRequest, 'post', route, [], fields)
+                .next()
+                .call([axios, 'post'], '/api1/employees', fields)
+                .next(response)
+                .returns({ response, err: null });
+        });
+
+        it('should handle errors', () => {
+            const err = new Error('something bad happened');
+
+            testSaga(apiRequest, 'post', route, [], fields)
+                .next()
+                .call([axios, 'post'], '/api1/employees', fields)
+                .throw(err)
+                .returns({ response: null, err });
+        });
+
+        it('should make an API GET request', () => {
+            const response = { isResponse: true };
+
+            testSaga(apiRequest, 'get', route, ['a0b'])
+                .next()
+                .call([axios, 'get'], '/api1/employees/a0b')
+                .next(response)
+                .returns({ response, err: null });
+        });
+    });
+});
+

+ 4 - 44
test/sagas/crud.spec.js

@@ -1,20 +1,18 @@
 /* eslint-disable prefer-reflect */
 import { testSaga } from 'redux-saga-test-plan';
-import axios from 'axios';
 
 import {
-    apiRequest,
     createDoc,
-    createListener,
     readDoc,
-    readListener,
     updateDoc,
-    updateListener,
     deleteDoc,
-    deleteListener,
     crudSaga
 } from 'sagas/crud';
 
+import {
+    apiRequest
+} from 'sagas/api';
+
 import {
     DOC_CREATED,
     DOC_READ,
@@ -34,44 +32,6 @@ import {
 } from 'actions/crud';
 
 describe('crudSaga', () => {
-    describe('apiRequest', () => {
-        const route = 'employees';
-        const fields = {
-            name: 'John Doe',
-            email: 'john.doe@mubaloo.com'
-        };
-
-        it('should make an API POST request', () => {
-            const response = { isResponse: true };
-
-            testSaga(apiRequest, 'post', route, [], fields)
-                .next()
-                .call([axios, 'post'], '/api1/employees', fields)
-                .next(response)
-                .returns({ response, err: null });
-        });
-
-        it('should handle errors', () => {
-            const err = new Error('something bad happened');
-
-            testSaga(apiRequest, 'post', route, [], fields)
-                .next()
-                .call([axios, 'post'], '/api1/employees', fields)
-                .throw(err)
-                .returns({ response: null, err });
-        });
-
-        it('should make an API GET request', () => {
-            const response = { isResponse: true };
-
-            testSaga(apiRequest, 'get', route, ['a0b'])
-                .next()
-                .call([axios, 'get'], '/api1/employees/a0b')
-                .next(response)
-                .returns({ response, err: null });
-        });
-    });
-
     describe('createDoc', () => {
         const route = 'employees';
         const pendingId = '<pendingId>';