import React from 'react';
import TestUtils from 'react-addons-test-utils';
import { findDOMNode } from 'react-dom';

import Summary from 'bitbucket/internal/bbui/inbox/components/summary';

import mockModels from 'mocks/models';

QUnit.module('bitbucket-inbox-summary', {
    beforeEach: function() {
        this.render = props => {
            this.component = TestUtils.renderIntoDocument(<table><tbody><Summary {...props} /></tbody></table>);
            this.el = findDOMNode(this.component);
        };
    },
});

QUnit.test('it renders one Summary component', function(assert) {
    this.render({ pullRequest: mockModels.pullRequest() });
    assert.equal(this.el.querySelectorAll('td.summary').length, 1, 'expected 1 summary table cell');
});

QUnit.test('it renders the pull request title', function(assert) {
    const PR_TITLE = 'The perfect PR';
    this.render({ pullRequest: mockModels.pullRequest({
        title: PR_TITLE,
    }) });
    assert.equal(this.el.querySelector('a.pull-request-title').textContent, PR_TITLE);
});

QUnit.test('it renders the correct project & repo names', function(assert) {
    const PROJECT_NAME = 'An adequate project name';
    const REPO_NAME = 'A reasonable Repo name';
    const pullRequest = {
        to_ref: {
            repository: {
                name: REPO_NAME,
                project: {
                    name: PROJECT_NAME,
                },
            },
        },
    };
    this.render({ pullRequest: pullRequest });
    assert.equal(this.el.querySelector('.project-name').textContent, PROJECT_NAME);
    assert.equal(this.el.querySelector('.repo-name').textContent, REPO_NAME);
});

