<commit_before>//
//  ObservableStoreMiddlewareTests.swift
//  ReSwiftRx
//
//  Created by <NAME> on 25/11/16.
//  Copyright © 2015 <NAME>. All rights reserved.
//

import XCTest
import ReSwiftRx

// swiftlint:disable function_body_length
class StoreMiddlewareTests: XCTestCase {

    /**
     it can decorate dispatch function
     */
    func testDecorateDispatch() {
        let store = Store(reducer: testValueStringReducer,
            stateType: TestStringAppState.self,
            observable: ObservableProperty(TestStringAppState()),
            middleware: Middleware(firstMiddleware, secondMiddleware))

        let subscriber = TestStoreSubscriber<TestStringAppState>()
        store.observable.subscribe(subscriber.subscription)

        let action = SetValueStringAction("OK")
        store.dispatch(action)

        XCTAssertEqual(store.observable.value.testValue, "OK First Middleware Second Middleware")
    }

    /**
     it can dispatch actions
     */
    func testCanDispatch() {
        let store = Store(reducer: testValueStringReducer,
            stateType: TestStringAppState.self,
            observable: ObservableProperty(TestStringAppState()),
            middleware: Middleware(firstMiddleware, secondMiddleware, dispatchingMiddleware))

        let subscriber = TestStoreSubscriber<TestStringAppState>()
        store.observable.subscribe(subscriber.subscription)

        let action = SetValueAction(10)
        store.dispatch(action)

        XCTAssertEqual(store.observable.value.testValue, "10 First Middleware Second Middleware")
    }

    /**
     it middleware can access the store's state
     */
    func testMiddlewareCanAccessState() {
        let property = ObservableProperty(TestStringAppState(testValue: "OK"))
        let store = Store(reducer: testValueStringReducer,
                                    stateType: TestStringAppState.self,
                                    observable: property,
                                    middleware: stateAccessingMiddleware)

        store.dispatch(SetValueStringAction("Action That Won't Go Through"))

        XCTAssertEqual(store.observable.value.testValue, "Not OK")
    }<commit_msg>Add new Middleware test<commit_after>//
//  ObservableStoreMiddlewareTests.swift
//  ReSwiftRx
//
//  Created by <NAME> on 25/11/16.
//  Copyright © 2015 <NAME>. All rights reserved.
//

import XCTest
import ReSwiftRx

// swiftlint:disable function_body_length
class StoreMiddlewareTests: XCTestCase {

    /**
     it can decorate dispatch function
     */
    func testDecorateDispatch() {
        let store = Store(reducer: testValueStringReducer,
            stateType: TestStringAppState.self,
            observable: ObservableProperty(TestStringAppState()),
            middleware: Middleware(firstMiddleware, secondMiddleware))

        let subscriber = TestStoreSubscriber<TestStringAppState>()
        store.observable.subscribe(subscriber.subscription)

        let action = SetValueStringAction("OK")
        store.dispatch(action)

        XCTAssertEqual(store.observable.value.testValue, "OK First Middleware Second Middleware")
    }

    /**
     it can dispatch actions
     */
    func testCanDispatch() {
        let store = Store(reducer: testValueStringReducer,
            stateType: TestStringAppState.self,
            observable: ObservableProperty(TestStringAppState()),
            middleware: Middleware(firstMiddleware, secondMiddleware, dispatchingMiddleware))

        let subscriber = TestStoreSubscriber<TestStringAppState>()
        store.observable.subscribe(subscriber.subscription)

        let action = SetValueAction(10)
        store.dispatch(action)

        XCTAssertEqual(store.observable.value.testValue, "10 First Middleware Second Middleware")
    }

    /**
     it middleware can access the store's state
     */
    func testMiddlewareCanAccessState() {
        let property = ObservableProperty(TestStringAppState(testValue: "OK"))
        let store = Store(reducer: testValueStringReducer,
                                    stateType: TestStringAppState.self,
                                    observable: property,
                                    middleware: stateAccessingMiddleware)

        store.dispatch(SetValueStringAction("Action That Won't Go Through"))

        XCTAssertEqual(store.observable.value.testValue, "Not OK")
    }

    func testMiddlewareSkipsReducersWhenPassedNil() {
        let filteringMiddleware = Middleware<TestStringAppState> { _, _, _ in nil }.map { _, _, action in
            XCTFail()
            return action
        }

        let property = ObservableProperty(TestStringAppState(testValue: "OK"))
        let store = Store(reducer: testValueStringReducer,
                          stateType: TestStringAppState.self,
                          observable: property,
                          middleware: filteringMiddleware)

        store.dispatch(SetValueStringAction("Action That Won't Go Through"))
    }
{'fraction_non_alphanumeric': 0.08231524133787302, 'fraction_numerical': 0.005607850991387943, 'mean_word_length': 2.626724763979666, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '821471', 'n_tokens_mistral': 1301, 'n_tokens_neox': 1249, 'n_words': 273}