<commit_before>{expect} = require 'chai'

React = require 'react'

{TaskPlanActions, TaskPlanStore} = require '../../src/flux/task-plan'
{TocActions} = require '../../src/flux/toc'
{ReadingPlan} = require '../../src/components/task-plan/reading'

TEST_TOC = [
  {
    id: '2'
    title: 'Chapter title'
    type: 'part'
    children: [
      {id: '1', type: 'page', title: 'Introduction'}
    ]
  }
]

tomorrow = Date.now() + 1000 * 3600 * 24
dayAfter = tomorrow + 1000 * 3600 * 24

VALID_MODEL =
  type: 'reading'
  id: '111'
  title: 'Test Title'
  opens_at: (new Date(tomorrow)).toString()
  due_at:  (new Date(dayAfter)).toString()
  settings:
    page_ids: ['1']


helper = (model, doChangeTitle) ->
  {id} = model
  # Load the plan into the store
  TaskPlanActions.loaded(model, id)
  if doChangeTitle
    TaskPlanActions.updateTitle(id, 'Some other title')
  html = React.renderToString(<ReadingPlan id={id} />)
  div = document.createElement('div')
  div.innerHTML = html
  div


describe 'Reading Plan', ->
  beforeEach ->
    TaskPlanActions.reset()
    TocActions.reset()
    TocActions.loaded(TEST_TOC)

  it 'should not allow save/publish when empty', ->
    model =
      type: 'reading'
      id: '0'

    node = helper(model, false)
    expect(node.querySelector('.-save.disabled')).to.not.be.null
    expect(node.querySelector('.-publish.disabled')).to.not.be.null
    expect(node.querySelector('.-delete')).to.not.be.null

  it 'should have the right buttons when valid but not changed', ->
    node = helper(VALID_MODEL, false)
    expect(node.querySelector('.-save.disabled')).to.not.be.null
    expect(node.querySelector('.-publish.disabled')).to.be.null
    expect(node.querySelector('.-delete')).to.not.be.null

  it 'should allow save when the title is changed', ->
    node = helper(VALID_MODEL, true)
    expect(node.querySelector('.-save.disabled')).to.be.null
    expect(node.querySelector('.-publish.disabled')).to.not.be.null
    expect(node.querySelector('.-delete')).to.not.be.null


  it 'should not allow delete when the plan does not exist in the backend', ->
    id = TaskPlanStore.freshLocalId()
    model =
      type: 'reading'
      id: id

    TaskPlanActions.created(model, id)
    node = helper(model, true)
    expect(node.querySelector('.-delete')).to.be.null


  it 'should not show delete if plan is published', ->
    yesterday = Date.now() - 3600 * 1000 * 24
    model =<commit_msg>updating tests to remove save button from plan footer<commit_after>{expect} = require 'chai'

React = require 'react'

{TaskPlanActions, TaskPlanStore} = require '../../src/flux/task-plan'
{TocActions} = require '../../src/flux/toc'
{ReadingPlan} = require '../../src/components/task-plan/reading'

TEST_TOC = [
  {
    id: '2'
    title: 'Chapter title'
    type: 'part'
    children: [
      {id: '1', type: 'page', title: 'Introduction'}
    ]
  }
]

tomorrow = Date.now() + 1000 * 3600 * 24
dayAfter = tomorrow + 1000 * 3600 * 24

VALID_MODEL =
  type: 'reading'
  id: '111'
  title: 'Test Title'
  opens_at: (new Date(tomorrow)).toString()
  due_at:  (new Date(dayAfter)).toString()
  settings:
    page_ids: ['1']


helper = (model, doChangeTitle) ->
  {id} = model
  # Load the plan into the store
  TaskPlanActions.loaded(model, id)
  if doChangeTitle
    TaskPlanActions.updateTitle(id, 'Some other title')
  html = React.renderToString(<ReadingPlan id={id} />)
  div = document.createElement('div')
  div.innerHTML = html
  div


describe 'Reading Plan', ->
  beforeEach ->
    TaskPlanActions.reset()
    TocActions.reset()
    TocActions.loaded(TEST_TOC)

  it 'should not allow save/publish when empty', ->
    model =
      type: 'reading'
      id: '0'

    node = helper(model, false)
    expect(node.querySelector('.-publish.disabled')).to.not.be.null
    expect(node.querySelector('.-delete')).to.not.be.null

  it 'should have the right buttons when valid but not changed', ->
    node = helper(VALID_MODEL, false)
    expect(node.querySelector('.-publish.disabled')).to.be.null
    expect(node.querySelector('.-delete')).to.not.be.null

  it 'should allow publish/save when the title is changed', ->
    node = helper(VALID_MODEL, true)
    expect(node.querySelector('.-publish.disabled')).to.be.null
    expect(node.querySelector('.-delete')).to.not.be.null


  it 'should not allow delete when the plan does not exist in the backend', ->
    id = TaskPlanStore.freshLocalId()
    model =
      type: 'reading'
      id: id

    TaskPlanActions.created(model, id)
    node = helper(model, true)
    expect(node.querySelector('.-delete')).to.be.null


  it 'should not show delete if plan is published', ->
    yesterday = Date.now() - 3600 * 1000 * 24
    model =
{'fraction_non_alphanumeric': 0.15418875927889714, 'fraction_numerical': 0.01569459172852598, 'mean_word_length': 3.2524797114517585, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4810977', 'n_tokens_mistral': 1603, 'n_tokens_neox': 1539, 'n_words': 373}