<commit_before>/*
 * Copyright (C) 2018 - present Instructure, Inc.
 *
 * This file is part of Canvas.
 *
 * Canvas is free software: you can redistribute it and/or modify it under
 * the terms of the GNU Affero General Public License as published by the Free
 * Software Foundation, version 3 of the License.
 *
 * Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 * A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
 * details.
 *
 * You should have received a copy of the GNU Affero General Public License along
 * with this program. If not, see <http://www.gnu.org/licenses/>.
 */

import React from 'react'
import { mount, shallow } from 'enzyme'
import moment from 'moment'

import { DiscussionsContainer, mapState, discussionTarget } from 'jsx/discussions/components/DiscussionContainer'

const defaultProps = () => ({
  title: "discussions",
  closeForComments: () => {},
  permissions: {create: false, manage_content: false, moderate: false},
  discussions: [{id: 1, filtered: false, permissions: {delete: true}}],
  discussionsPage: 1,
  isLoadingDiscussions: false,
  hasLoadedDiscussions: false,
  getDiscussions: () => {},
  roles: ["student", "user"],
  renderContainerBackground: () => {},
})

QUnit.module('DiscussionsContainer component')

test('renders the component', () => {
  const tree = shallow(<DiscussionsContainer {...defaultProps()} />)
  const node = tree.find('.discussions-container__wrapper')
  ok(node.exists())
})

test('renders passed in component when renderContainerBackground is present', () => {
  const props = defaultProps()
  props.discussions = []
  props.renderContainerBackground = () => (
    <div className="discussions-v2__test-image">
      <p>testing</p>
    </div>
  )
  const tree = shallow(<DiscussionsContainer {...props} />)
  const node = tree.find('.discussions-v2__test-image')
  ok(node.exists())
})

test('renders regular discussion row when user does not have moderate permissions', () => {
  const props = defaultProps()
  const tree = shallow(<DiscussionsContainer {...props} />)
  const node = tree.find('Connect(DiscussionRow)')
  ok(node.exists())
})

test('renders a draggable discussion row when user has moderate permissions', () => {
  const props = defaultProps()
  props.permissions.moderate = true
  const tree = shallow(<DiscussionsContainer {...props} />)
  const node = tree.find('Connect(DropTarget(DragSource(DiscussionRow)))')
  ok(node.exists())
})

test('discussionTarget canDrop returns false if assignment due_at is in the past', () => {
  const assignment = {due_at: '2017-05-13T00:59:59Z'}
  const getItem = function () {
    return {assignment}
  }
  const mockMonitor = {getItem}
  ok(discussionTarget.canDrop({closedState: true}, mockMonitor))
})

test('discussionTarget canDrop returns true if not dragging to closed state', () => {
  const assignment = {due_at: '2018-05-13T00:59:59Z'}
  const getItem = function () {
    return {assignment}
  }
  const mockMonitor = {getItem}
  ok(discussionTarget.canDrop({closedState: false}, mockMonitor))
})

QUnit.skip('discussionTarget canDrop returns true if assignment due_at is in the future', () => {
  // This line is broken, it is not causing moment() to to return this. Working on a fix
  moment.now = function () {
      return +new Date('2015-05-13T00:59:59Z');
  }
  const assignment = {due_at: '2018-05-13T00:59:59Z'}
  const getItem = function () {
    return {assignment}
  }
  const mockMonitor = {getItem}
  ok(!discussionTarget.canDrop({closedState: true}, mockMonitor))
})

moment.now = function () {
    return +new Date();
}

test('connected mapStateToProps filters out filtered discussions', () => {
  const state = {}
  const ownProps = {
    discussions: [
      {id: 1, filtered: true},
      {id: 2, filtered: false}
    ],
  }
  const connectedProps = mapState(state, ownProps)
  deepEqual(connectedProps.discussions, [{id: 2, filtered: false}])
})



test('renders background image no discussions are present', () => {
  const props = defaultProps()
  const renderBackgroundSpy = sinon.spy()
  props.discussions = []
  props.renderContainerBackground = renderBackgroundSpy

  const tree = mount(<DiscussionsContainer {...props} />)
  const node = tree.find('Connect(DiscussionRow)')
  ok(renderBackgroundSpy.calledOnce)
  ok(!node.exists())
})<commit_msg>Fix broken unit test<commit_after>/*
 * Copyright (C) 2018 - present Instructure, Inc.
 *
 * This file is part of Canvas.
 *
 * Canvas is free software: you can redistribute it and/or modify it under
 * the terms of the GNU Affero General Public License as published by the Free
 * Software Foundation, version 3 of the License.
 *
 * Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 * A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
 * details.
 *
 * You should have received a copy of the GNU Affero General Public License along
 * with this program. If not, see <http://www.gnu.org/licenses/>.
 */

import React from 'react'
import { mount, shallow } from 'enzyme'
import moment from 'moment'

import { DiscussionsContainer, mapState, discussionTarget } from 'jsx/discussions/components/DiscussionContainer'

const defaultProps = () => ({
  title: "discussions",
  closeForComments: () => {},
  permissions: {create: false, manage_content: false, moderate: false},
  discussions: [{id: 1, filtered: false, permissions: {delete: true}}],
  discussionsPage: 1,
  isLoadingDiscussions: false,
  hasLoadedDiscussions: false,
  getDiscussions: () => {},
  roles: ["student", "user"],
  renderContainerBackground: () => {},
})

QUnit.module('DiscussionsContainer component')

test('renders the component', () => {
  const tree = shallow(<DiscussionsContainer {...defaultProps()} />)
  const node = tree.find('.discussions-container__wrapper')
  ok(node.exists())
})

test('renders passed in component when renderContainerBackground is present', () => {
  const props = defaultProps()
  props.discussions = []
  props.renderContainerBackground = () => (
    <div className="discussions-v2__test-image">
      <p>testing</p>
    </div>
  )
  const tree = shallow(<DiscussionsContainer {...props} />)
  const node = tree.find('.discussions-v2__test-image')
  ok(node.exists())
})

test('renders regular discussion row when user does not have moderate permissions', () => {
  const props = defaultProps()
  const tree = shallow(<DiscussionsContainer {...props} />)
  const node = tree.find('Connect(DiscussionRow)')
  ok(node.exists())
})

test('renders a draggable discussion row when user has moderate permissions', () => {
  const props = defaultProps()
  props.permissions.moderate = true
  const tree = shallow(<DiscussionsContainer {...props} />)
  const node = tree.find('Connect(DropTarget(DragSource(DiscussionRow)))')
  ok(node.exists())
})

test('discussionTarget canDrop returns false if assignment due_at is in the past', () => {
  const assignment = {due_at: '2017-05-13T00:59:59Z'}
  const getItem = function () {
    return {assignment}
  }
  const mockMonitor = {getItem}
  ok(discussionTarget.canDrop({closedState: true}, mockMonitor))
})

test('discussionTarget canDrop returns true if not dragging to closed state', () => {
  const assignment = {due_at: '2018-05-13T00:59:59Z'}
  const getItem = function () {
    return {assignment}
  }
  const mockMonitor = {getItem}
  ok(discussionTarget.canDrop({closedState: false}, mockMonitor))
})

test('discussionTarget canDrop returns true if assignment due_at is in the future', () => {
  const dueAt = moment().add(7, 'days')
  const assignment = {due_at: dueAt.format()}
  const getItem = function () {
    return {assignment}
  }
  const mockMonitor = {getItem}
  ok(!discussionTarget.canDrop({closedState: true}, mockMonitor))
})

test('connected mapStateToProps filters out filtered discussions', () => {
  const state = {}
  const ownProps = {
    discussions: [
      {id: 1, filtered: true},
      {id: 2, filtered: false}
    ],
  }
  const connectedProps = mapState(state, ownProps)
  deepEqual(connectedProps.discussions, [{id: 2, filtered: false}])
})

test('renders background image no discussions are present', () => {
  const props = defaultProps()
  const renderBackgroundSpy = sinon.spy()
  props.discussions = []
  props.renderContainerBackground = renderBackgroundSpy

  const tree = mount(<DiscussionsContainer {...props} />)
  const node = tree.find('Connect(DiscussionRow)')
  ok(renderBackgroundSpy.calledOnce)
  ok(!node.exists())
})
{'fraction_non_alphanumeric': 0.13157591803844826, 'fraction_numerical': 0.012547484747323588, 'mean_word_length': 4.43, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 51, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '830557', 'n_tokens_mistral': 2620, 'n_tokens_neox': 2508, 'n_words': 770}