import React from 'react';
import {shallow} from 'enzyme';

import ReviewsView from '../../lib/views/reviews-view';

describe('ReviewsView', function() {
  let atomEnv;

  beforeEach(function() {
    atomEnv = global.buildAtomEnvironment();
  });

  afterEach(function() {
    atomEnv.destroy();
  });

  function aggregatePullRequest(pullRequest, override = {}) {
    const reviewThreads = pullRequest.reviewThreads.edges.map(e => e.node);
    const summaries = pullRequest.reviews.edges.map(e => e.node);
    const commentThreads = reviewThreads.map(thread => {
      return {
        thread,
        comments: thread.comments.edges.map(e => e.node),
      };
    });
    return {summaries, commentThreads, errors: [], loading: false, ...override};
  }

  function buildApp(override = {}) {
    const props = {
      repository: {pullRequest: {}},
      ...override,
    };

    return <ReviewsView {...props} />;
  }

  it('renders a PullRequestsReviewsContainer', function() {
    const wrapper = shallow(buildApp());
    assert.lengthOf(wrapper.find('ForwardRef(Relay(PullRequestReviewsController))'), 1);
  });

  it('does not render empty summary or comment sections', function() {
    const wrapper = shallow(buildApp())
      .find('ForwardRef(Relay(PullRequestReviewsController))')
      .renderProp('children')({reviews: [], commentThreads: []});
    assert.lengthOf(wrapper.find('.github-Reviews-section.summaries'), 0);
    assert.lengthOf(wrapper.find('.github-Reviews-section.comments'), 0);
  });

  it('renders summary and comment sections', function() {
    const {reviews, commentThreads} = mocks;
    const wrapper = shallow(buildApp())
      .find('ForwardRef(Relay(PullRequestReviewsController))')
      .renderProp('children')({reviews, commentThreads});
    assert.lengthOf(wrapper.find('.github-Reviews-section.summaries'), 1);
    assert.lengthOf(wrapper.find('.github-Reviews-section.comments'), 1);
  });


});

@@ -53,16 +53,66 @@
 zyme';%0A%0A
+import %7BpullRequestBuilder%7D from '../builder/pr';%0A
 import R
@@ -346,16 +346,128 @@
 %0A  %7D);%0A%0A
+  // TODO: probably need to either refactor or fix this after the pullRequestBuilder implementation has settled%0A
   functi
@@ -1005,16 +1005,76 @@
 t: %7B%7D%7D,%0A
+      checkoutOp: %7B%0A        isEnabled: () =%3E true,%0A      %7D,%0A
       ..
@@ -1156,20 +1156,22 @@
 s a 
-PullRequests
+BareAggregated
 Revi
@@ -1286,35 +1286,38 @@
 rdRef(Relay(
-PullRequest
+BareAggregated
 ReviewsContr
@@ -1311,28 +1311,27 @@
 dReviewsCont
-roll
+ain
 er))'), 1);%0A
@@ -1347,31 +1347,57 @@
 it('
-does not 
+registers atom commands');%0A%0A  it('
 render
+s
  empty s
 umma
@@ -1396,49 +1396,168 @@
 ty s
-ummary or comment sections', function() %7B
+tate if there is no review', function() %7B%0A    const pr = pullRequestBuilder().build();%0A    const %7Berrors, summaries, commentThreads%7D = aggregatePullRequest(pr);
 %0A   
@@ -1619,35 +1619,38 @@
 rdRef(Relay(
-PullRequest
+BareAggregated
 ReviewsContr
@@ -1640,36 +1640,35 @@
 gatedReviewsCont
-roll
+ain
 er))')%0A      .re
@@ -1693,19 +1693,25 @@
 ')(%7B
-reviews: %5B%5D
+errors, summaries
 , co
@@ -1722,20 +1722,16 @@
 tThreads
-: %5B%5D
 %7D);%0A    
@@ -1875,16 +1875,84 @@
 '), 0);%0A
+    assert.lengthOf(wrapper.find('.github-Reviews-emptyState'), 1);%0A
   %7D);%0A%0A 
@@ -2022,41 +2022,265 @@
 nst 
-%7Breviews, commentThreads%7D = mocks
+pr = pullRequestBuilder()%0A      .addReview(r =%3E r.id(0))%0A      .addReviewThread(t =%3E t.addComment(c =%3E c.id(1)))%0A      .addReviewThread(t =%3E t.addComment(c =%3E c.id(2)))%0A      .build();%0A    const %7Berrors, summaries, commentThreads%7D = aggregatePullRequest(pr)
 ;%0A  
@@ -2347,27 +2347,30 @@
 f(Relay(
-PullRequest
+BareAggregated
 ReviewsC
@@ -2376,12 +2376,11 @@
 Cont
-roll
+ain
 er))
@@ -2413,22 +2413,32 @@
 dren')(%7B
-rev
+errors, summar
 ie
-w
 s, comme
@@ -2595,24 +2595,150 @@
 ents'), 1);%0A
+    assert.lengthOf(wrapper.find('.github-ReviewSummary'), 1);%0A    assert.lengthOf(wrapper.find('details.github-Review'), 2);%0A
   %7D);%0A%0A%0A%7D);%0A

fix up the existing reviews-view tests