 THIRD_PAGE  = '1.3'
 
 PAGE = require '../../api/pages/0e58aa87-2e09-40a7-8bf3-269b2fa16509.json'
+
+
+
+ECO_ID = '3'
+ECO_TOC  = require '../../api/ecosystems/3/readings.json'
+ECO_PAGES = {}
+ECO_PAGES['334f8b61-30eb-4475-8e05-5260a4866b4b@4.68'] = require '../../api/pages/334f8b61-30eb-4475-8e05-5260a4866b4b@4.68.json'
+ECO_PAGES['d419f72d-3349-4449-ab34-c705409df4aa@5'] = require '../../api/pages/d419f72d-3349-4449-ab34-c705409df4aa@5.json'
+ECO_PAGES['17f6ff53-2d92-4669-acdd-9a958ea7fd0a@12'] = require '../../api/pages/17f6ff53-2d92-4669-acdd-9a958ea7fd0a@12.json'
+ECO_FIRST_PAGE = '1'
+ECO_SECOND_PAGE = '1.1'
+ECO_SECOND_PAGE_ID = 'd419f72d-3349-4449-ab34-c705409df4aa@5'
+
+TOCS =
+  '1': TOC,
+  '3': ECO_TOC
 
 
 renderBook = (section, ecosystemId) ->

      .to.match(/1\.1/)

  it 'renders page html', ->
    expect(@state.div.querySelector('.page').textContent)
      .to.equal('A bunch of html')

  it 'sets current page to active on menu', ->
    page = ReferenceBookActions.loaded(TOC, ECOSYSTEM_ID)
    console.log page
    expect(@state.div.querySelector('.page').textContent)
      .to.equal('A bunch of html')


    selection = @state.div.querySelector(".toc [data-section='#{FIRST_PAGE}'] a")
    expect(selection).not.to.be.null
    expect(_.toArray(selection.classList)).to.include('active')


describe 'Reference Book Component for a non-default ecosystem', ->

  beforeEach ->
    CourseActions.loaded(COURSE, COURSE_ID)
    ReferenceBookActions.loaded(ECO_TOC, ECO_ID)
    _.each(ECO_PAGES, (page, cnx_id) ->
      ReferenceBookPageActions.loaded(page, cnx_id)
    )

    renderBook(ECO_FIRST_PAGE, ECO_ID).then (state) =>
      @state = state

  it 'renders the section title on the navbar',  ->
    expect(@state.div.querySelector('.section-title').textContent)
      .to.match(/1/)

  it 'sets the menu item to be active based on the current page', ->
    selection = @state.div.querySelector(".toc [data-section='#{ECO_FIRST_PAGE}'] a")
    expect(selection).not.to.be.null
    expect(_.toArray(selection.classList)).to.include('active')

  it 'navigates forward and back between pages, maintaining ecosystem id', ->
    prevControl = @state.div.querySelector('.page-wrapper > .prev')
    expect(prevControl).to.not.exist # on first page

    nextControl = @state.div.querySelector('.page-wrapper > .next')
    expect(nextControl).to.exist
    expect(nextControl.href).to.contain(ECO_SECOND_PAGE)
    page = ReactTestUtils.findRenderedComponentWithType(@state.book, Page)

    ReferenceBookPageActions.loaded(ECO_PAGES[ECO_SECOND_PAGE_ID], ECO_SECOND_PAGE_ID)
    # button:0 is a mystery argument needed by ReactRouter, taken from their specs
    commonActions.click(nextControl, {button: 0})

    expect(page.context.router.getCurrentParams().section)
      .to.equal(ECO_SECOND_PAGE)

    expect(page.context.router.getCurrentQuery())
      .to.have.property('ecosystemId').and.equal(ECO_ID)

    expect(@state.div.querySelector('.page-wrapper > .next').href)
      .to.contain("?ecosystemId=#{ECO_ID}")

    expect(@state.div.querySelector('.page-wrapper > .prev').href)
      .to.contain("?ecosystemId=#{ECO_ID}")

  it 'keeps ecosystem id in table of contents links', ->
    tocLinks = _.toArray(@state.div.querySelectorAll('.toc a'))
    linkAddresses = _.pluck(tocLinks, 'href')

    _.each(linkAddresses, (linkAddress) ->
      expect(linkAddress).to.contain("?ecosystemId=#{ECO_ID}")
    )

