/* global describe: true, test:true, expect:true */
/* eslint-disable max-len */
import { upload as uploadReducer, initialState } from '~/reducers/upload';
import {
	UPLOAD_ITEM_STARTED,
	UPLOAD_SUCCESS,
	UPLOAD_ITEMS,
	UPLOAD_ITEMS_STATE_CHANGED,
	UPLOAD_ITEMS_ERROR,
} from '~/store/actionTypes';
import {
	UPLOAD_STATUS_COMPLETED,
	UPLOAD_STATUS_UPLOADING,
	UPLOAD_STATUS_ERROR,
} from '~/lib/uploaders/constants';

describe('Upload reducer', () => {
	test('CASE 1: should return initial state', () => {
		expect(uploadReducer(undefined, { type: 'Default' }))
			.toEqual(initialState);
	});
	test('CASE 2: should return new state with items when type is UPLOAD_ITEMS', () => {
		const items = [{ uri: '1.jpg' }, { uri: '2.jpg' }];

		const stateResult = uploadReducer(undefined, { type: UPLOAD_ITEMS, payload: { items } });
		expect(stateResult).toHaveProperty('items');
		expect(stateResult.items).toHaveLength(items.length);
		stateResult.items.forEach(item => {
			expect(item).toHaveProperty('meta');
		});
	});
	test('CASE 3: should return state with loading when type is UPLOAD_ITEM_STARTED', () => {
		const expectedState = { ...initialState, meta: { ...initialState.meta, loading: true } };
		expect(uploadReducer(initialState, { type: UPLOAD_ITEM_STARTED })).toEqual(expectedState);
	});
	test('CASE 4: should return empty items with meta property when type is UPLOAD_SUCCESS', () => {
		const expectedState = {
			items: [],
			meta: {
				error: '',
				status: UPLOAD_STATUS_COMPLETED,
				loading: false,
				elapsed: 0,
			},
			receivers: [],
		};
		expect(uploadReducer(initialState, { type: UPLOAD_SUCCESS })).toEqual(expectedState);
	});

	test('CASE 5: should return items with meta property when type is UPLOAD_ITEMS_STATE_CHANGED', () => {
		// add pending items
		const items = [{ fullFilePath: '1.jpg' }, { fullFilePath: '2.jpg' }];
		const item = { fullFilePath: '1.jpg', progress: 1 };
		const pendingItemState = uploadReducer(initialState, { type: UPLOAD_ITEMS, payload: { items } });

		const stateResult = uploadReducer(
			pendingItemState,
			{
				type: UPLOAD_ITEMS_STATE_CHANGED,
				payload: {
					item,
					elapsed: 1,
				},
			},
		);
		const expectedState = {
			items: [{
				fullFilePath: '1.jpg',
				meta: {
					progress: 1,
					error: '',
					status: UPLOAD_STATUS_UPLOADING,
					loading: true,
				},
			}, {
				fullFilePath: '2.jpg',
				meta: {
					progress: 0.0,
					error: '',
					status: UPLOAD_STATUS_UPLOADING,
					loading: true,
				},
			}],
			meta: {
				error: '',
				status: UPLOAD_STATUS_UPLOADING,
				loading: true,
				elapsed: 1,
			},
		};
		stateResult.items.forEach(i => {
			expect(i).toHaveProperty('meta');
		});
		expect(stateResult).toEqual(expectedState);
	});

	test('CASE 6: should return correct state when type is UPLOAD_ITEMS_ERROR', () => {
		const items = [{ fullFilePath: '1.jpg' }, { fullFilePath: '2.jpg' }];
		const item = { fullFilePath: '1.jpg', error: 'error' };
		const pendingItemState = uploadReducer(initialState, { type: UPLOAD_ITEMS, payload: { items } });
		const stateResult = uploadReducer(
			pendingItemState,
			{
				type: UPLOAD_ITEMS_ERROR,
				payload: {
					item,
					error: item.error,
				},
			},
		);
		const expectedState = {
			items: [],
			meta: {
				error: 'error',
				status: UPLOAD_STATUS_ERROR,
				loading: false,
				elapsed: 0,
			},
		};
		expect(stateResult).toEqual(expectedState);
	});

	// test('CASE 6: should return state with album loading to TRUE when type is GET_ALBUM_CONTENT', () => {
	// 	const albums = [{ id: 1 }, { id: 2 }];
	// 	const payload = {
	// 		albumId: 1,
	// 	};
	// 	const originalState = { ...initialState, albums, meta: { error: '' } };
	// 	const stateResult = uploadReducer({ ...originalState, albums, meta: { error: '' } }, {
	// 		type: GET_ALBUM_CONTENT,
	// 		payload,
	// 	});
	// 	const expectedState = {
	// 		albums: [{
	// 			id: 1,
	// 			meta: { error: '', loading: true },
	// 		},
	// 		{ id: 2 }],
	// 		meta: { error: '' },
	// 	};
	// 	expect(stateResult).toEqual(expectedState);
	// });

	// test('CASE 7: should return state with album content when type is GET_ALBUM_CONTENT_SUCCESS', () => {
	// 	const albums = [{ id: 1 }, { id: 2 }];
	// 	const content = {
	// 		name: 'album name',
	// 		photos: [
	// 			{ id: 4 },
	// 			{ id: 5 },
	// 		],
	// 	};
	// 	const payload = {
	// 		albumId: 1,
	// 		content,
	// 	};
	// 	const originalState = { ...initialState, albums, meta: { error: '' } };
	// 	const stateResult = uploadReducer({ ...originalState, albums, meta: { error: '' } }, {
	// 		type: GET_ALBUM_CONTENT_SUCCESS,
	// 		payload,
	// 	});
	// 	const expectedState = {
	// 		albums: [{
	// 			id: 1,
	// 			content,
	// 			meta: { error: '', loading: false },
	// 		},
	// 		{ id: 2 }],
	// 		meta: { error: '' },
	// 	};
	// 	expect(stateResult).toEqual(expectedState);
	// });

	// test('CASE 8: should return state with album error when type is GET_ALBUM_CONTENT_ERROR', () => {
	// 	const albums = [{ id: 1 }, { id: 2 }];
	// 	const albumErr = new Error('getAlbumsContentError');
	// 	const payload = {
	// 		albumId: 1,
	// 		error: albumErr,
	// 	};
	// 	const originalState = { ...initialState, albums, meta: { error: '' } };
	// 	const stateResult = uploadReducer({ ...originalState, albums, meta: { error: '' } }, {
	// 		type: GET_ALBUM_CONTENT_ERROR,
	// 		payload,
	// 	});
	// 	const expectedState = {
	// 		albums: [{
	// 			id: 1,
	// 			meta: { error: albumErr, loading: false },
	// 		},
	// 		{ id: 2 }],
	// 		meta: { error: '' },
	// 	};
	// 	expect(stateResult).toEqual(expectedState);
	// });

	// test('CASE 9: should return state with album error when type is CREATE_ALBUM_SUCCESS', () => {
	// 	const albums = [{ id: 1 }, { id: 2 }];
	// 	const payload = {
	// 		album: { id: 3 },
	// 	};
	// 	const originalState = { ...initialState, albums, meta: { error: '' } };
	// 	const stateResult = uploadReducer({ ...originalState, albums, meta: { error: '' } }, {
	// 		type: CREATE_ALBUM_SUCCESS,
	// 		payload,
	// 	});
	// 	const expectedState = {
	// 		albums: [
	// 			{ id: 1 },
	// 			{ id: 2 },
	// 			{ id: 3, meta: { error: '', loading: false } },
	// 		],
	// 		meta: { error: '', loading: false },
	// 	};
	// 	expect(stateResult).toEqual(expectedState);
	// });

	// test('CASE 10: should empty the state on LOGOUT_SUCCESS', () => {
	// 	const existingState = [{ id: 1 }, { id: 2 }];
	// 	expect(uploadReducer(existingState, { type: LOGOUT_SUCCESS }))
	// 		.toEqual(initialState);
	// });
});
