package io.github.reactivemvp.statecontainer;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import io.github.reactivemvp.statecontainer.action.UpdateCounterAction;
import io.github.reactivemvp.statecontainer.reducer.UpdateCounterReducer;
import io.github.reactivemvp.statecontainer.store.CounterStore;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

public class StoreTest {
    private CounterStore mStore;
    private Throwable mError;
    private int mLocalCount;
    private Disposable mDisposable;

    @Before
    public void before() {
        mStore = new CounterStore();
        mStore.init(
                new CounterState.Builder().build(),
                new UpdateCounterReducer());

        mDisposable = mStore.getObservable()
                .subscribe(new Consumer<StateChangedEventArgs<CounterState>>() {
                    @Override
                    public void accept(@NonNull StateChangedEventArgs<CounterState> args) throws Exception {
                        mLocalCount = args.getState().getCount();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(@NonNull Throwable throwable) throws Exception {
                        mError = throwable;
                    }
                });

        mLocalCount = mStore.getState().getCount();
    }

    @Test
    public void testInc() {
        final int originalCount = mStore.getState().getCount();
        mStore.dispatch(new UpdateCounterAction(UpdateCounterAction.Operation.Increase));
        Assert.assertEquals(originalCount + 1, mStore.getState().getCount());
    }

    @Test
    public void testDec() {
        final int originalCount = mStore.getState().getCount();
        mStore.dispatch(new UpdateCounterAction(UpdateCounterAction.Operation.Decrease));
        Assert.assertEquals(originalCount - 1, mStore.getState().getCount());
    }


    @After
    public void after() {
        Assert.assertEquals(mLocalCount, mStore.getState().getCount());
        Assert.assertNull("mError is not null", mError);
        Assert.assertNotNull("mDisposable is null", mDisposable);
        Assert.assertFalse(mDisposable.isDisposed());
        mDisposable.dispose();
    }

}

@@ -1479,32 +1479,254 @@
 Count();%0A    %7D%0A%0A
+    @Test%0A    public void testNullAction() %7B%0A        final int originalCount = mStore.getState().getCount();%0A        mStore.dispatch(null);%0A        Assert.assertEquals(originalCount, mStore.getState().getCount());%0A    %7D%0A%0A%0A
     @Test%0A    pu

add testNullAction test