import * as clickActionCreators from '../actions/clickActionCreators';
import * as syncActionCreators from '../actions/syncActionCreators';
import {
	DEFAULT_APP_STATE,
	DEFAULT_CLICK_TRACK,
	app,
	clickTracking,
} from './platform';

describe('app reducer', () => {
	beforeEach(function() {
		this.MOCK_STATE = { foo: 'bar' };
	});
	it('returns default state for empty action', () => {
		expect(app(undefined, {})).toEqual(DEFAULT_APP_STATE);
	});
	it('re-sets app state on logout API_REQUEST', function() {
		const logoutRequest = syncActionCreators.apiRequest([], { logout: true });
		expect(app(this.MOCK_STATE, logoutRequest)).toEqual(DEFAULT_APP_STATE);
	});
	it('assembles success responses into single state tree', () => {
		const API_SUCCESS = {
			type: 'API_SUCCESS',
			payload: {
				responses: [{ foo: 'bar'}, { bar: 'baz' }, { baz: 'foo' }],
			},
		};
		expect(app(undefined, API_SUCCESS)).toEqual({
			foo: 'bar',
			bar: 'baz',
			baz: 'foo',
			isFetching: false,
		});
	});
	it('populates an `error` key on API_ERROR', () => {
		const API_ERROR = {
			type: 'API_ERROR',
			payload: new Error('this is the worst'),
		};
		const errorState = app(undefined, API_ERROR);
		expect(errorState.error).toBe(API_ERROR.payload);
	});
	it('sets isFetching:true on API_REQUEST', () => {
		const API_REQUEST = {
			type: 'API_REQUEST',
			payload: [],
			meta: {},
		};
		const appState = app(undefined, API_REQUEST);
		expect(appState.isFetching).toBe(true);
	});
	it('sets isFetching:false on API_SUCCESS', () => {
		const API_SUCCESS = {
			type: 'API_SUCCESS',
			payload: { queries: [], responses: [] },
		};
		const appState = app(undefined, API_SUCCESS);
		expect(appState.isFetching).toBe(false);
	});
	it('does not change isFetching on CACHE_SUCCESS', () => {
		const CACHE_SUCCESS = {
			type: 'CACHE_SUCCESS',
			payload: { queries: [], responses: [] },
		};
		const appState = app({ isFetching: true }, CACHE_SUCCESS);
		expect(appState.isFetching).toBe(true);
	});
});

describe('clickTracking reducer', () => {
	it('appends a click action to state.clicks', () => {
		const initialState = { history: [{ bar: 'baz' }] };
		const click = { foo: 'bar' };
		const action = clickActionCreators.click(click);
		expect(clickTracking(initialState, action).history.length).toBe(2);
		expect(clickTracking(initialState, action).history[1]).toEqual(click);
	});
	it('clears click data on clear clicks', () => {
		const initialState = { history: [{ bar: 'baz' }] };
		const action = clickActionCreators.clearClick();
		expect(clickTracking(initialState, action)).toBe(DEFAULT_CLICK_TRACK);
	});
	it('returns unmodified state for non-click actions', () => {
		const initialState = { history: [{ bar: 'baz' }] };
		expect(clickTracking(initialState, { type: 'FOO' })).toBe(initialState);
	});
});


@@ -1863,32 +1863,83 @@
 ses: %5B%5D %7D,%0A%09%09%7D;%0A
+%09%09%5Btrue, false, 'monkey'%5D.forEach(isFetching =%3E %7B%0A%09
 %09%09const appState
@@ -1957,22 +1957,16 @@
 Fetching
-: true
  %7D, CACH
@@ -1971,32 +1971,33 @@
 CHE_SUCCESS);%0A%09%09
+%09
 expect(appState.
@@ -2005,36 +2005,48 @@
 sFetching).toBe(
-true
+isFetching);%0A%09%09%7D
 );%0A%09%7D);%0A%7D);%0A%0Ades

test more isFetching states