/* global describe: true, test:true, expect:true */
import { initialState, friendActivities } from '~/reducers/friendActivities';
import {
	GET_COMMENTS_SUCCESS,
	DELETE_ACTIVITY_PHOTO,
	DELETE_ACTIVITY_PHOTO_ERROR,
	DELETE_ACTIVITY_PHOTO_SUCCESS,
} from '~/store/actionTypes';
import Factory from 'helper/factories/api';

describe('friendActivities reducer', () => {
	test('CASE 1: should return initial state', () => {
		expect(friendActivities(undefined, { type: 'Default' }))
			.toEqual(initialState);
	});

	test('CASE 2a: should return with comments when comment is related to the friend and actionType is GET_COMMENTS_SUCCESS', () => {
		const friend = { username: 'friend@mynixplay.com' };
		const originalState = {
			friend,
			nextTimestamp: 0,
			activities: [],
		};
		const comments = Factory.buildList('Comment', 2, { friend: friend.username });
		const payload = { comments };
		const newState = friendActivities(originalState, { type: GET_COMMENTS_SUCCESS, payload });
		expect(newState.mediaViewer.comments)
			.toHaveLength(2);
	});

	test('CASE 2b: should return without comments when comment is not related to the friend and actionType is GET_COMMENTS_SUCCESS', () => {
		const friend = { username: 'friend@mynixplay.com' };
		const originalState = {
			friend,
			nextTimestamp: 0,
			activities: [],
		};
		const comments = Factory.buildList('Comment', 2, { friend: 'other@mynixplay.com' });
		const payload = { comments };
		const newState = friendActivities(originalState, { type: GET_COMMENTS_SUCCESS, payload });
		expect(newState.mediaViewer.comments)
			.toHaveLength(0);
	});

	test('CASE 3: friendActivities should update meta loading state when type is DELETE_ACTIVITY_PHOTO', (s3Key = '', albumId = '', timestamp = '', friend = '') => {
		const existingState = { ...initialState, meta: { loading: true, error: '' } };
		const stateResult = friendActivities(existingState, {
			type: DELETE_ACTIVITY_PHOTO,
			payload: {
				s3Key,
				albumId,
				timestamp,
				friend,
			},
		});
		expect(stateResult).toEqual(existingState);
	});

	test('CASE 4: friendActivities should update meta loading state when type is DELETE_ACTIVITY_PHOTO_ERROR', () => {
		const existingState = { ...initialState, actions: { submitting: false, error: '' } };
		const stateResult = friendActivities(existingState, {
			type: DELETE_ACTIVITY_PHOTO_ERROR,
			payload: {
				error: '',
			},
		});
		expect(stateResult).toEqual(existingState);
	});

	test('CASE 5: friendActivities should update meta loading state when type is DELETE_ACTIVITY_PHOTO_SUCCESS', (s3Key = '', albumId = '', timestamp = '', friend = '') => {
		const existingState = { ...initialState, meta: { loading: false, error: '' } };
		const stateResult = friendActivities(existingState, {
			type: DELETE_ACTIVITY_PHOTO_SUCCESS,
			payload: {
				s3Key,
				albumId,
				timestamp,
				friend,
			},
		});
		expect(stateResult).toEqual(existingState);
	});
});
