<commit_before>/**
 * Copyright 2017, IOOF Holdings Limited.
 * All rights reserved.
 *
 * This source code is licensed under the BSD-style license found in the
 * LICENSE file in the root directory of this source tree.
 */

import configureStore from 'redux-mock-store'
import { mapTo } from 'rxjs/operator/mapTo'
import { namespacedAction, globalAction } from 'redux-subspace'
import { subspaced, createEpicMiddleware } from '../../src'

describe('subspaced Tests', () => {

    it('should throw error when not using subspace createEpicMiddleware', () => {
        const epic = actions$ => actions$.ofType('PING')
            ::mapTo({ type: 'PONG' })

        expect(
            subspaced(state => state)(epic)
        ).to.throw('Subspace epic couldn\'t find the store. Make sure you\'ve used createEpicMiddleware from redux-subspace-observable')
    })

    it('should get substate for epic', () => {
        
        const state = {
            subState: {
                value: "expected"
            },
            value: "wrong"
        }

        const epic = (actions$, store) => actions$.ofType('TEST')
            ::mapTo({ type: 'VERIFY', value: store.getState().value })

        const subspacedEpic = subspaced(state => state.subState)(epic)

        const mockStore = configureStore([createEpicMiddleware(subspacedEpic)])(state)

        mockStore.dispatch({ type: 'TEST' })

        expect(mockStore.getActions()).to.deep.equal([
            { type: "TEST" },
            { type: "VERIFY", value: "expected" }
        ])
    })

    it('should namespace actions for epic', () => {
        
        const state = {
            subState: {
                value: "expected"
            },
            value: "wrong"
        }

        const epic = (actions$, store) => actions$.ofType('TEST')
            ::mapTo({ type: 'VERIFY', value: store.getState().value })

        const subspacedEpic = subspaced(state => state.subState, "test")(epic)

        const mockStore = configureStore([createEpicMiddleware(subspacedEpic)])(state)

        mockStore.dispatch(namespacedAction('test')({ type: 'TEST' }))

        expect(mockStore.getActions()).to.deep.equal([
            { type: "test/TEST" },
            { type: "test/VERIFY", value: "expected" }
        ])
    })

    it('should use namespace for substate for epic', () => {
        
        const state = {
            subState: {
                value: "expected"
            },
            value: "wrong"
        }

        const epic = (actions$, store) => actions$.ofType('TEST')
            ::mapTo({ type: 'VERIFY', value: store.getState().value })

        const subspacedEpic = subspaced("subState")(epic)

        const mockStore = configureStore([createEpicMiddleware(subspacedEpic)])(state)

        mockStore.dispatch(namespacedAction('subState')({ type: 'TEST' }))

        expect(mockStore.getActions()).to.deep.equal([
            { type: "subState/TEST" },
            { type: "subState/VERIFY", value: "expected" }
        ])
    })

    it('should accept global actions for saga', () => {
        
        const state = {
            subState: {
                value: "expected"
            },
            value: "wrong"
        }

        const epic = (actions$, store) => actions$.ofType('TEST')
            ::mapTo({ type: 'VERIFY', value: store.getState().value })

        const subspacedEpic = subspaced(state => state.subState, "test")(epic)

        const mockStore = configureStore([createEpicMiddleware(subspacedEpic)])(state)

        mockStore.dispatch(globalAction({ type: 'TEST' }))

        expect(mockStore.getActions()).to.deep.equal([
            { type: "TEST", globalAction: true },
            { type: "test/VERIFY", value: "expected" }
        ])
    })

    it('should not namespace global actions for saga', () => {
        
        const state = {
            subState: {
                value: "expected"
            },
            value: "wrong"
        }

        const epic = (actions$, store) => actions$.ofType('TEST')
            ::mapTo(globalAction({ type: 'VERIFY', value: store.getState().value }))

        const subspacedEpic = subspaced(state => state.subState, "test")(epic)

        const mockStore = configureStore([createEpicMiddleware(subspacedEpic)])(state)

        mockStore.dispatch(namespacedAction('test')({ type: 'TEST' }))

        expect(mockStore.getActions()).to.deep.equal([
            { type: "test/TEST" },
            { type: "VERIFY", value: "expected", globalAction: true }
        ])
    })
    
    it('should pass on dependencies to epic', () => {
        
        const state = {
            subState: {
                value: "wrong"
            },
            value: "wrong"<commit_msg>fixed typo in test names<commit_after>/**
 * Copyright 2017, IOOF Holdings Limited.
 * All rights reserved.
 *
 * This source code is licensed under the BSD-style license found in the
 * LICENSE file in the root directory of this source tree.
 */

import configureStore from 'redux-mock-store'
import { mapTo } from 'rxjs/operator/mapTo'
import { namespacedAction, globalAction } from 'redux-subspace'
import { subspaced, createEpicMiddleware } from '../../src'

describe('subspaced Tests', () => {

    it('should throw error when not using subspace createEpicMiddleware', () => {
        const epic = actions$ => actions$.ofType('PING')
            ::mapTo({ type: 'PONG' })

        expect(
            subspaced(state => state)(epic)
        ).to.throw('Subspace epic couldn\'t find the store. Make sure you\'ve used createEpicMiddleware from redux-subspace-observable')
    })

    it('should get substate for epic', () => {
        
        const state = {
            subState: {
                value: "expected"
            },
            value: "wrong"
        }

        const epic = (actions$, store) => actions$.ofType('TEST')
            ::mapTo({ type: 'VERIFY', value: store.getState().value })

        const subspacedEpic = subspaced(state => state.subState)(epic)

        const mockStore = configureStore([createEpicMiddleware(subspacedEpic)])(state)

        mockStore.dispatch({ type: 'TEST' })

        expect(mockStore.getActions()).to.deep.equal([
            { type: "TEST" },
            { type: "VERIFY", value: "expected" }
        ])
    })

    it('should namespace actions for epic', () => {
        
        const state = {
            subState: {
                value: "expected"
            },
            value: "wrong"
        }

        const epic = (actions$, store) => actions$.ofType('TEST')
            ::mapTo({ type: 'VERIFY', value: store.getState().value })

        const subspacedEpic = subspaced(state => state.subState, "test")(epic)

        const mockStore = configureStore([createEpicMiddleware(subspacedEpic)])(state)

        mockStore.dispatch(namespacedAction('test')({ type: 'TEST' }))

        expect(mockStore.getActions()).to.deep.equal([
            { type: "test/TEST" },
            { type: "test/VERIFY", value: "expected" }
        ])
    })

    it('should use namespace for substate for epic', () => {
        
        const state = {
            subState: {
                value: "expected"
            },
            value: "wrong"
        }

        const epic = (actions$, store) => actions$.ofType('TEST')
            ::mapTo({ type: 'VERIFY', value: store.getState().value })

        const subspacedEpic = subspaced("subState")(epic)

        const mockStore = configureStore([createEpicMiddleware(subspacedEpic)])(state)

        mockStore.dispatch(namespacedAction('subState')({ type: 'TEST' }))

        expect(mockStore.getActions()).to.deep.equal([
            { type: "subState/TEST" },
            { type: "subState/VERIFY", value: "expected" }
        ])
    })

    it('should accept global actions for epic', () => {
        
        const state = {
            subState: {
                value: "expected"
            },
            value: "wrong"
        }

        const epic = (actions$, store) => actions$.ofType('TEST')
            ::mapTo({ type: 'VERIFY', value: store.getState().value })

        const subspacedEpic = subspaced(state => state.subState, "test")(epic)

        const mockStore = configureStore([createEpicMiddleware(subspacedEpic)])(state)

        mockStore.dispatch(globalAction({ type: 'TEST' }))

        expect(mockStore.getActions()).to.deep.equal([
            { type: "TEST", globalAction: true },
            { type: "test/VERIFY", value: "expected" }
        ])
    })

    it('should not namespace global actions for epic', () => {
        
        const state = {
            subState: {
                value: "expected"
            },
            value: "wrong"
        }

        const epic = (actions$, store) => actions$.ofType('TEST')
            ::mapTo(globalAction({ type: 'VERIFY', value: store.getState().value }))

        const subspacedEpic = subspaced(state => state.subState, "test")(epic)

        const mockStore = configureStore([createEpicMiddleware(subspacedEpic)])(state)

        mockStore.dispatch(namespacedAction('test')({ type: 'TEST' }))

        expect(mockStore.getActions()).to.deep.equal([
            { type: "test/TEST" },
            { type: "VERIFY", value: "expected", globalAction: true }
        ])
    })
    
    it('should pass on dependencies to epic', () => {
        
        const state = {
            subState: {
                value: "wrong"
            },
            value: "wrong"
{'fraction_non_alphanumeric': 0.14434508153603368, 'fraction_numerical': 0.0008416622830089426, 'mean_word_length': 2.1055210715452466, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 41, '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': '2370774', 'n_tokens_mistral': 2754, 'n_tokens_neox': 2653, 'n_words': 605}