All files / modules zero-state.js

48% Statements 12/25
60% Branches 6/10
28.57% Functions 2/7
45.45% Lines 10/22

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 861x         1x         1x                   66x 66x 1x     65x                   2x                             1x                                           1x                          
import { globalAppRegistryEmit } from 'mongodb-redux-common/app-registry';
 
/**
 * Zero state changed action.
 */
export const IS_ZERO_STATE_CHANGED = 'validation/namespace/IS_ZERO_STATE_CHANGED';
 
/**
 * The initial state.
 */
export const INITIAL_STATE = true;
 
/**
 * Reducer function for handle state changes to namespace.
 *
 * @param {String} state - The namespace state.
 * @param {Object} action - The action.
 *
 * @returns {String} The new state.
 */
export default function reducer(state = INITIAL_STATE, action) {
  if (action.type === IS_ZERO_STATE_CHANGED) {
    return action.isZeroState;
  }
 
  return state;
}
 
/**
 * Action creator for zero state changed events.
 *
 * @param {Boolean} isZeroState - Is zero state.
 *
 * @returns {Object} The zero state changed action.
 */
export const zeroStateChanged = (isZeroState) => ({
  type: IS_ZERO_STATE_CHANGED,
  isZeroState
});
 
/**
 * Send metrics.
 *
 * @param {Function} dispatch - Dispatch.
 * @param {Object} dataService - Data service.
 * @param {Object} namespace - Namespace.
 * @param {String} registryEvent - Registry event.
 *
 * @returns {Function} The function.
 */
const sendMetrics = (dispatch, dataService, namespace, registryEvent) => dataService
  .database(namespace.database, {}, (errorDB, res) => {
    let collectionSize = 0;
 
    if (!errorDB) {
      const collection = res.collections.find((coll) => (
        coll.name === namespace.collection
      ));
 
      collectionSize = collection.document_count;
    }
 
    return dispatch(globalAppRegistryEmit(registryEvent, { collectionSize } ));
  });
 
/**
* Change zero state.
*
* @param {Boolean} isZeroState - Is zero state.
*
* @returns {Function} The function.
*/
export const changeZeroState = (isZeroState) => {
  return (dispatch, getState) => {
    const state = getState();
    const dataService = state.dataService.dataService;
    const namespace = state.namespace;
 
    if (isZeroState === false) {
      sendMetrics(dispatch, dataService, namespace, 'schema-validation-rules-added');
    }
 
    return dispatch(zeroStateChanged(isZeroState));
  };
};