import {
    LOAD_REPOSITORIES_SUCCESS,
    CLEAR_REPOSITORIES
} from 'bitbucket/internal/feature/dashboard/actions';
import pagingReducer from 'bitbucket/internal/feature/dashboard/reducers/paging';
import { SEARCH }from 'bitbucket/internal/feature/dashboard/repository-type';

QUnit.module('dashboard-paging-reducer');

QUnit.test('it removes paging information on CLEAR_REPOSITORIES', function (assert) {
    const newState = pagingReducer({
        [SEARCH]: []
    }, {
        type: CLEAR_REPOSITORIES,
        meta: {
            repoType: SEARCH
        }
    });
    assert.equal(newState[SEARCH], undefined);
});

QUnit.test('it replaces paging information on LOAD_REPOSITORIES_SUCCESS with different query', function (assert) {
    const callback = () => {};
    const newState = pagingReducer({
        'search': {
            query: 'bar',
            repositories: [1]
        }
    }, {
        type: LOAD_REPOSITORIES_SUCCESS,
        payload: {
            other: 1,
            values: [{ id: 2 }]
        },
        meta: {
            repoType: SEARCH,
            query: 'baz',
            nextSearch: callback
        }
    });

    assert.equal(newState.search.query, 'baz');
    assert.deepEqual(newState.search.repositories, [2]);
    assert.deepEqual(newState.search.lastPageMeta, { other: 1 });
    assert.equal(newState.search.loadMoreCallback, callback);
});

QUnit.test('it appends to paging information on LOAD_REPOSITORIES_SUCCESS with same query', function (assert) {
    const newState = pagingReducer({
        'search': {
            query: 'bar',
            repositories: [1]
        }
    }, {
        type: LOAD_REPOSITORIES_SUCCESS,
        payload: {
            other: 1,
            values: [{ id: 2 }]
        },
        meta: {
            repoType: SEARCH,
            query: 'bar',
        }
    });

    assert.deepEqual(newState.search.repositories, [1, 2]);
    assert.deepEqual(newState.search.lastPageMeta, { other: 1 });
});