<commit_before>import jsc from 'jsverify'
import update from 'immutability-helper'

import * as generators from './generators'
import * as utils from './utils'
import {
  actionsIncludingCrud,
  configureCrudReducer,
  crudReducer,
  defaultExtractors,
} from './crud'

const actions = actionsIncludingCrud('test')
const myCrudReducer = configureCrudReducer({
  error: action => action.payload.errors,
  index: action => action.payload.data,
  meta: action => action.payload.meta_data,
  single: action => action.payload,
})

describe('utils/crud', () => {
  describe('the default extractors', () => {
    ['index', 'error', 'single'].forEach(extractor => (
      describe(extractor, () => {
        test('returns the full action payload', () => {
          expect(defaultExtractors[extractor]({ payload: 'Hello' })).toEqual('Hello')
        })
      })
    ))

    describe('meta', () => {
      test('returns an empty object', () => {
        expect(defaultExtractors.meta({ payload: 'No please' })).toEqual({})
      })
    })
  })

  describe('the default crudReducer', () => {
    test('is a function', () => {
      expect(typeof crudReducer).toBe('function')
    })

    test('uses the full payload for all extractors except meta', () => {
      const expectedDefaultReducer = configureCrudReducer({
        index: action => action.payload,
        meta: () => {},
        error: action => action.payload,
        single: action => action.payload,
      })
      Object.values(actions)
        .filter(action => typeof action === 'function')
        .forEach(action => (
          jsc.assertForall(generators.crudReducerState, (state) => {
            const before = JSON.stringify(state)
            const expected = JSON.stringify(expectedDefaultReducer, action(), actions)
            const result = JSON.stringify(myCrudReducer, action(), actions)
            const after = JSON.stringify(state)
            return before === after && result === expected
          })
        )
      )
    })
  })

  describe('configureCrudReducer with example extractors', () => {
    test('is a function', () => {
      expect(typeof configureCrudReducer).toBe('function')
    })

    test('produces a function', () => {
      expect(typeof myCrudReducer).toBe('function')
    })

    describe('on empty action', () => {
      test('does not react', () => {
        jsc.assertForall(generators.crudReducerState, (state) => {
          const before = JSON.stringify(state)
          const result = JSON.stringify(myCrudReducer(state, {}, actions))
          const after = JSON.stringify(state)
          return before === result && result === after
        })
      })
    })

    describe('on CREATE', () => {
      test('just sets the create loading to true', () => {
        jsc.assertForall(generators.crudReducerState, jsc.dict(jsc.json), (state, payload) => {
          const before = JSON.stringify(state)
          const expected = JSON.stringify(update(state, { loading: { create: { $set: true } } }))
          const result = JSON.stringify(myCrudReducer(state, actions.create(payload), actions))
          const after = JSON.stringify(state)
          return before === after && result === expected
        })
      })
    })

    describe('on CREATE_SUCCESS', () => {
      test('sets the create loading to false, reset changes and sets single', () => {
        jsc.assertForall(generators.crudReducerState, jsc.dict(jsc.json), (state, payload) => {
          const before = JSON.stringify(state)
          const result = myCrudReducer(state, utils.action(actions.CREATE_SUCCESS, payload), actions)
          const after = JSON.stringify(state)
          return before === after &&
            result.loading.create === false &&
            Object.keys(result.changes).length === 0 &&
            JSON.stringify(payload) === JSON.stringify(result.single)
        })
      })
    })

    describe('on CREATE_FAILURE', () => {
      test('sets the loading to false and the keeps the errors', () => {
        jsc.assertForall(
          generators.crudReducerState,
          generators.errorResponse,
          (state, payload) => {
            const before = JSON.stringify(state)
            const result = myCrudReducer(state, utils.action(actions.CREATE_FAILURE, payload), actions)
            const after = JSON.stringify(state)
            return before === after &&
              result.loading.create === false &&
              result.errors.length === payload.errors.length
          }
        )
      })
    })

    describe('on DELETE', () => {
      test('sets the delete loading to true', () => {
        jsc.assertForall(
          generators.crudReducerState,
          generators.uuid,
          (state, payload) => {
            const before = JSON.stringify(state)
            const result = myCrudReducer(state, actions.delete(payload), actions)
            const after = JSON.stringify(state)
            return before === after &&
              result.loading.delete === true
          }
        )
      })
    })

    describe('on DELETE_FAILURE', () => {
      test('sets the delete loading to false', () => {
        jsc.assertForall(
          generators.crudReducerState,
          generators.uuid,
          (state, payload) => {
            const before = JSON.stringify(state)
            const result = myCrudReducer(state, utils.action(actions.DELETE_FAILURE, payload), actions)
            const after = JSON.stringify(state)
            return before === after &&
              result.loading.delete === false
          }
        )
      })
    })
<commit_msg>✅ Test delete success case<commit_after>import jsc from 'jsverify'
import update from 'immutability-helper'

import * as generators from './generators'
import * as utils from './utils'
import {
  actionsIncludingCrud,
  configureCrudReducer,
  crudReducer,
  defaultExtractors,
} from './crud'

const actions = actionsIncludingCrud('test')
const myCrudReducer = configureCrudReducer({
  error: action => action.payload.errors,
  index: action => action.payload.data,
  meta: action => action.payload.meta_data,
  single: action => action.payload,
})

describe('utils/crud', () => {
  describe('the default extractors', () => {
    ['index', 'error', 'single'].forEach(extractor => (
      describe(extractor, () => {
        test('returns the full action payload', () => {
          expect(defaultExtractors[extractor]({ payload: 'Hello' })).toEqual('Hello')
        })
      })
    ))

    describe('meta', () => {
      test('returns an empty object', () => {
        expect(defaultExtractors.meta({ payload: 'No please' })).toEqual({})
      })
    })
  })

  describe('the default crudReducer', () => {
    test('is a function', () => {
      expect(typeof crudReducer).toBe('function')
    })

    test('uses the full payload for all extractors except meta', () => {
      const expectedDefaultReducer = configureCrudReducer({
        index: action => action.payload,
        meta: () => {},
        error: action => action.payload,
        single: action => action.payload,
      })
      Object.values(actions)
        .filter(action => typeof action === 'function')
        .forEach(action => (
          jsc.assertForall(generators.crudReducerState, (state) => {
            const before = JSON.stringify(state)
            const expected = JSON.stringify(expectedDefaultReducer, action(), actions)
            const result = JSON.stringify(myCrudReducer, action(), actions)
            const after = JSON.stringify(state)
            return before === after && result === expected
          })
        )
      )
    })
  })

  describe('configureCrudReducer with example extractors', () => {
    test('is a function', () => {
      expect(typeof configureCrudReducer).toBe('function')
    })

    test('produces a function', () => {
      expect(typeof myCrudReducer).toBe('function')
    })

    describe('on empty action', () => {
      test('does not react', () => {
        jsc.assertForall(generators.crudReducerState, (state) => {
          const before = JSON.stringify(state)
          const result = JSON.stringify(myCrudReducer(state, {}, actions))
          const after = JSON.stringify(state)
          return before === result && result === after
        })
      })
    })

    describe('on CREATE', () => {
      test('just sets the create loading to true', () => {
        jsc.assertForall(generators.crudReducerState, jsc.dict(jsc.json), (state, payload) => {
          const before = JSON.stringify(state)
          const expected = JSON.stringify(update(state, { loading: { create: { $set: true } } }))
          const result = JSON.stringify(myCrudReducer(state, actions.create(payload), actions))
          const after = JSON.stringify(state)
          return before === after && result === expected
        })
      })
    })

    describe('on CREATE_SUCCESS', () => {
      test('sets the create loading to false, reset changes and sets single', () => {
        jsc.assertForall(generators.crudReducerState, jsc.dict(jsc.json), (state, payload) => {
          const before = JSON.stringify(state)
          const result = myCrudReducer(state, utils.action(actions.CREATE_SUCCESS, payload), actions)
          const after = JSON.stringify(state)
          return before === after &&
            result.loading.create === false &&
            Object.keys(result.changes).length === 0 &&
            JSON.stringify(payload) === JSON.stringify(result.single)
        })
      })
    })

    describe('on CREATE_FAILURE', () => {
      test('sets the loading to false and the keeps the errors', () => {
        jsc.assertForall(
          generators.crudReducerState,
          generators.errorResponse,
          (state, payload) => {
            const before = JSON.stringify(state)
            const result = myCrudReducer(state, utils.action(actions.CREATE_FAILURE, payload), actions)
            const after = JSON.stringify(state)
            return before === after &&
              result.loading.create === false &&
              result.errors.length === payload.errors.length
          }
        )
      })
    })

    describe('on DELETE', () => {
      test('sets the delete loading to true', () => {
        jsc.assertForall(
          generators.crudReducerState,
          generators.uuid,
          (state, payload) => {
            const before = JSON.stringify(state)
            const result = myCrudReducer(state, actions.delete(payload), actions)
            const after = JSON.stringify(state)
            return before === after &&
              result.loading.delete === true
          }
        )
      })
    })

    describe('on DELETE_SUCCESS', () => {
      test('sets the delete loading to false', () => {
        jsc.assertForall(
          generators.crudReducerState,
          generators.uuid,
          (state, payload) => {
            const before = JSON.stringify(state)
            const result = myCrudReducer(state, utils.action(actions.DELETE_SUCCESS, payload), actions)
            const after = JSON.stringify(state)
            return before === after &&
              result.loading.delete === false
          }
        )
      })
    })

    describe('on DELETE_FAILURE', () => {
      test('sets the delete loading to false', () => {
        jsc.assertForall(
          generators.crudReducerState,
          generators.uuid,
          (state, payload) => {
            const before = JSON.stringify(state)
            const result = myCrudReducer(state, utils.action(actions.DELETE_FAILURE, payload), actions)
            const after = JSON.stringify(state)
            return before === after &&
              result.loading.delete === false
          }
        )
      })
    })

{'fraction_non_alphanumeric': 0.1380454197499362, 'fraction_numerical': 0.00017011142298205324, 'mean_word_length': 2.3431902189365936, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 90, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6839817', 'n_tokens_mistral': 3244, 'n_tokens_neox': 3179, 'n_words': 704}