import ParticipantsDataProvider from 'bitbucket/internal/bbui/data-provider/participants';
import MockParticipantsDataProvider from 'mocks/data-provider/participants';

QUnit.module('bitbucket-participants-data-provider');

QUnit.test('mock returns users', function(assert) {
    const itemsPerPage = 10;
    const pages = 2;
    const mock = new MockParticipantsDataProvider({
        nextUrl: 'meh',
        itemsPerPage,
        pages,
    });

    function assertUsers(pageIndex, users) {
        assert.strictEqual(users.length, itemsPerPage);
        // not going to do a full validation - too brittle
        assert.strictEqual(users[0].name, 'USER' + (pageIndex * itemsPerPage + 1));
    }

    assert.expect(pages * 2);
    for(let page = 0; page < pages && !mock.reachedEnd; page++) {
        mock.fetchNext().then(assertUsers.bind(null, page));
        mock._requestPromise.resolve();
    }
});

QUnit.test('SPI expects user output', function(assert) {
    class TestedUserDataProvider extends ParticipantsDataProvider {
        constructor(...args) {
            super(...args);
        }
        _transform() {
            return [{}]; // not a user
        }
    }

    let mock;
    try {
        mock = new TestedUserDataProvider({});
    } catch (e) {
        console.log(e.stack);
        throw e;
    }
    assert.throws(function() {
        mock.transform([]);
    }, 'Transform should throw on non-user data.');
});
