/* global AJS:false */
AJS.test.require([
    'com.atlassian.bitbucket.server.bitbucket-web:bitbucket-ref-data-provider',
    'com.atlassian.bitbucket.server.bitbucket-it-javascript:js-test-utils'
], function() {
    'use strict';

    var testContext = require('test-context');

    var RefDataProvider = require('bitbucket/internal/impl/data-provider/ref');

    var projKey = 'PROJECT_1';
    var repoSlug = 'rep_1';

    function repo() {
        return {
            id: "string",
            name: "string",
            is_forkable: true,
            is_public: true,
            scm_id: 'git',
            slug: repoSlug,
            project: { key: projKey }
        };
    }

    QUnit.module('ref-data-provider', {
        setup: function() {
            this._context = testContext.context();
            this._context.loginAsAdmin();
            this._context.branch(projKey, repoSlug, {
                name: 'ref-data-provider-test',
                startPoint: '0a943a29376f2336b78312d99e65da17048951db'
            });
        },
        teardown: function() {
            this._context.destroy();
        }
    });

    QUnit.test('DataProvider grabs real data', function(assert) {
        var rdp = new RefDataProvider({
            filter: {
                repository: repo(),
                type: 'branch',
                term: 'ref-data-provider-test'
            }
        });

        return rdp.fetchNext().then(function(branches) {
            assert.strictEqual(branches.length, 1, 'Returns filtered branch list');
            assert.strictEqual(branches[0].display_id, 'ref-data-provider-test');
            assert.strictEqual(rdp.reachedEnd, true, 'Only one page');
        });
    });

    QUnit.test('DataProvider filters by type', function(assert) {
        var rdp = new RefDataProvider({
            filter: {
                repository: repo(),
                type: 'tag',
                term: 'ref-data-provider-test'
            }
        });

        return rdp.fetchNext().then(function(branches) {
            assert.strictEqual(branches.length, 0, 'Returns filtered branch list');
            assert.strictEqual(rdp.reachedEnd, true, 'Only one page');
        });
    });
});
