<commit_before>Ext = window.Ext4 || window.Ext

Ext.require [
  'Rally.test.apps.roadmapplanningboard.helper.TestDependencyHelper'
  'Rally.apps.roadmapplanningboard.plugin.RoadmapScrollable'
  'Rally.apps.roadmapplanningboard.PlanningBoard',
  'Rally.apps.roadmapplanningboard.AppModelFactory'
]

describe 'Rally.apps.roadmapplanningboard.plugin.RoadmapScrollable', ->

  helpers
    createBacklogColumn: (id) ->
      xtype: 'backlogplanningcolumn', testId: "#{id}"

    createColumn: (id, date = new Date()) ->
      timeframeRecord = Ext.create Rally.apps.roadmapplanningboard.AppModelFactory.getTimeframeModel(),
        id: "#{id}"
        name: "#{id}"
        start: date
        end: date
      planRecord = Ext.create Rally.apps.roadmapplanningboard.AppModelFactory.getPlanModel(),
        id: "#{id}"
        name: "#{id}"

      return {
        xtype: 'timeframeplanningcolumn'
        testId: "#{id}"
        timeframeRecord: timeframeRecord
        planRecord: planRecord
        columnHeaderConfig:
          record: timeframeRecord
          fieldToDisplay: 'name'
          editable: true
        columnConfig: {}
      }

    createCardboard: (config) ->
      config = _.extend
        timeframeColumnCount: 4
        pastColumnCount: 1
        presentColumnCount: 5
      , config

      id = 0
      pastDate = new Date '2012-01-01'
      presentDate = Ext.Date.add(new Date(), Ext.Date.DAY, 1)

      columns = [
        @createBacklogColumn(id++)
      ]

      columns = columns.concat (@createColumn(num, pastDate) for num in [id...id+(config.pastColumnCount or 0)])
      id += config.pastColumnCount
      columns = columns.concat (@createColumn(num, presentDate) for num in [id...id+(config.presentColumnCount or 0)])

      @cardboard = Ext.create 'Rally.apps.roadmapplanningboard.PlanningBoard',
        _.extend
          buildColumnsFromStore: ->
            @columns = columns

          _retrieveLowestLevelPI: (callback) ->
            callback.call(@, {get: -> 'PortfolioItem/Feature'})
          renderTo: 'testDiv'

          plugins: [
            ptype: 'rallytimeframescrollablecardboard', timeframeColumnCount: config.timeframeColumnCount
          ]

          slideDuration: 10
        , config

      @plugin = @cardboard.plugins[0]

      @waitForComponentReady(@cardboard)

    scrollBackwards: ->
      @click(className: 'scroll-backwards')

    scrollForwards: ->
      @click(className: 'scroll-forwards')

    getColumnHeaderCells: ->
      @cardboard.getEl().query('th.card-column')

    getColumnContentCells: ->
      @cardboard.getEl().query('td.card-column')

    clickCollapse: ->
      collapseStub = @stub()
      @cardboard.on 'headersizechanged', collapseStub
      @click(css: '.themeButtonCollapse').then =>
        @once
          condition: ->
            collapseStub.called

    clickExpand: ->
      expandStub = @stub()
      @cardboard.on 'headersizechanged', expandStub
      @click(css: '.themeButtonExpand').then =>
        @once
          condition: ->
            expandStub.called

    getThemeElements: ->
      _.map(@cardboard.getEl().query('.theme_container'), Ext.get)

    assertButtonIsInColumnHeader: (button, column) ->
      expect(column.getColumnHeader().getEl().getById(button.getEl().id)).not.toBeNull()

  beforeEach ->
    Rally.test.apps.roadmapplanningboard.helper.TestDependencyHelper.loadDependencies()
    @ajax.whenQuerying('PortfolioItem/Feature').respondWith([])

  describe 'scrollable board setup', ->

    it 'should get a list of scrollable columns', ->
      @createCardboard(pastColumnCount: 0, presentColumnCount: 6).then =>
        expect(@plugin.getScrollableColumns()).toEqual @cardboard.getColumns().slice(1)

    it 'should get the last visible scrollable column', ->
      @createCardboard(pastColumnCount: 0, presentColumnCount: 6).then =>
        expect(@plugin.getLastVisibleScrollableColumn().testId).toEqual '4'

    it 'should get the first visible scrollable column', ->
      @createCardboard(pastColumnCount: 0, presentColumnCount: 6).then =>
        expect(@plugin.getFirstVisibleScrollableColumn().testId).toEqual '1'

    it 'should restrict the number of columns on the component', ->
      @createCardboard(pastColumnCount: 0, presentColumnCount: 6, timeframeColumnCount: 4).then =>
        expect(@plugin.buildColumnsFromStore().length).toEqual 5 # 4 + 1 backlog

    it 'should not show past timeframes', ->
      @createCardboard(pastColumnCount: 4, presentColumnCount: 4, timeframeColumnCount: 4).then =>
        expect(@plugin.getFirstVisibleScrollableColumn().testId).toEqual '5'

    it 'should show a left scroll arrow for past timeframes', ->
      @createCardboard(pastColumnCount: 1, presentColumnCount: 4, timeframeColumnCount: 4).then =>
        expect(@plugin.backwardsButton.hidden).toBe false

    it 'should show a right scroll arrow for extra future timeframes', ->
      @createCardboard(pastColumnCount: 0, presentColumnCount: 5, timeframeColumnCount: 4).then =>
        expect(@plugin.forwardsButton.hidden).toBe false

    it 'should not show a left scroll arrow if there are no past timeframes', ->
      @createCardboard(pastColumnCount: 0, presentColumnCount: 4, timeframeColumnCount: 4).then =>
        expect(@plugin.backwardsButton.hidden).toBe true

    it 'should not show a right scroll arrow if there are no extra future timeframes', ->
      @createCardboard(pastColumnCount: 1, presentColumnCount: 4, timeframeColumnCount: 4).then =>
        expect(@plugin.forwardsButton.hidden).toBe true

  describe 'when back scroll button is clicked', ->
    it 'should scroll backward', ->
      @createCardboard(pastColumnCount: 1, presentColumnCount: 4, timeframeColumnCount: 4).then =>
        @scrollBackwards().then =>
          expect(@plugin.getFirstVisibleScrollableColumn().timeframeRecord.getId()).toEqual '1'<commit_msg>DE18339 - Fixed RoadmapScrollable.js to handle end dates correctly<commit_after>Ext = window.Ext4 || window.Ext

Ext.require [
  'Rally.test.apps.roadmapplanningboard.helper.TestDependencyHelper'
  'Rally.apps.roadmapplanningboard.plugin.RoadmapScrollable'
  'Rally.apps.roadmapplanningboard.PlanningBoard',
  'Rally.apps.roadmapplanningboard.AppModelFactory'
]

describe 'Rally.apps.roadmapplanningboard.plugin.RoadmapScrollable', ->

  helpers
    createBacklogColumn: (id) ->
      xtype: 'backlogplanningcolumn', testId: "#{id}"

    createColumn: (id, date = new Date(), offset = 0) ->
      timeframeRecord = Ext.create Rally.apps.roadmapplanningboard.AppModelFactory.getTimeframeModel(),
        id: "#{id}"
        name: "#{id}"
        start: Ext.Date.add(date, Ext.Date.MONTH, offset-1)
        end: Ext.Date.add(Ext.Date.add(date, Ext.Date.MONTH, offset), Ext.Date.DAY, -1)
      planRecord = Ext.create Rally.apps.roadmapplanningboard.AppModelFactory.getPlanModel(),
        id: "#{id}"
        name: "#{id}"

      return {
        xtype: 'timeframeplanningcolumn'
        testId: "#{id}"
        timeframeRecord: timeframeRecord
        planRecord: planRecord
        columnHeaderConfig:
          record: timeframeRecord
          fieldToDisplay: 'name'
          editable: true
        columnConfig: {}
      }

    createCardboard: (config) ->
      config = _.extend
        timeframeColumnCount: 4
        pastColumnCount: 1
        presentColumnCount: 5
      , config

      id = 0
      date = config.date || new Date(Ext.Date.format(new Date(), 'Y-m-d'))

      columns = [
        @createBacklogColumn(id++)
      ]

      columns = columns.concat (@createColumn(num, date, num-config.pastColumnCount) for num in [id...id+(config.pastColumnCount or 0)])
      id += config.pastColumnCount
      columns = columns.concat (@createColumn(num, date, num-config.pastColumnCount) for num in [id...id+(config.presentColumnCount or 0)])

      @cardboard = Ext.create 'Rally.apps.roadmapplanningboard.PlanningBoard',
        _.extend
          buildColumnsFromStore: ->
            @columns = columns

          _retrieveLowestLevelPI: (callback) ->
            callback.call(@, {get: -> 'PortfolioItem/Feature'})
          renderTo: 'testDiv'

          plugins: [
            ptype: 'rallytimeframescrollablecardboard', timeframeColumnCount: config.timeframeColumnCount
          ]

          slideDuration: 10
        , config

      @plugin = @cardboard.plugins[0]

      @waitForComponentReady(@cardboard)

    scrollBackwards: ->
      @click(className: 'scroll-backwards')

    scrollForwards: ->
      @click(className: 'scroll-forwards')

    getColumnHeaderCells: ->
      @cardboard.getEl().query('th.card-column')

    getColumnContentCells: ->
      @cardboard.getEl().query('td.card-column')

    clickCollapse: ->
      collapseStub = @stub()
      @cardboard.on 'headersizechanged', collapseStub
      @click(css: '.themeButtonCollapse').then =>
        @once
          condition: ->
            collapseStub.called

    clickExpand: ->
      expandStub = @stub()
      @cardboard.on 'headersizechanged', expandStub
      @click(css: '.themeButtonExpand').then =>
        @once
          condition: ->
            expandStub.called

    getThemeElements: ->
      _.map(@cardboard.getEl().query('.theme_container'), Ext.get)

    assertButtonIsInColumnHeader: (button, column) ->
      expect(column.getColumnHeader().getEl().getById(button.getEl().id)).not.toBeNull()

  beforeEach ->
    Rally.test.apps.roadmapplanningboard.helper.TestDependencyHelper.loadDependencies()
    @ajax.whenQuerying('PortfolioItem/Feature').respondWith([])

  describe 'scrollable board setup', ->

    it 'should get a list of scrollable columns', ->
      @createCardboard(pastColumnCount: 0, presentColumnCount: 6).then =>
        expect(@plugin.getScrollableColumns()).toEqual @cardboard.getColumns().slice(1)

    it 'should get the last visible scrollable column', ->
      @createCardboard(pastColumnCount: 0, presentColumnCount: 6).then =>
        expect(@plugin.getLastVisibleScrollableColumn().testId).toEqual '4'

    it 'should get the first visible scrollable column', ->
      @createCardboard(pastColumnCount: 0, presentColumnCount: 6).then =>
        expect(@plugin.getFirstVisibleScrollableColumn().testId).toEqual '1'

    it 'should restrict the number of columns on the component', ->
      @createCardboard(pastColumnCount: 0, presentColumnCount: 6, timeframeColumnCount: 4).then =>
        expect(@plugin.buildColumnsFromStore().length).toEqual 5 # 4 + 1 backlog

    it 'should not show past timeframes', ->
      @createCardboard(pastColumnCount: 4, presentColumnCount: 4, timeframeColumnCount: 4).then =>
        expect(@plugin.getFirstVisibleScrollableColumn().testId).toEqual '5'

    it 'should show current timeframe if the timeframe ends today', ->
      tomorrow = Ext.Date.add(new Date(), Ext.Date.DAY, 1) # This will actually move a past column to the present columns
      @createCardboard(pastColumnCount: 4, presentColumnCount: 4, date: tomorrow).then =>
        expect(@plugin.getFirstVisibleScrollableColumn().testId).toEqual '4'

    it 'should show a left scroll arrow for past timeframes', ->
      @createCardboard(pastColumnCount: 1, presentColumnCount: 4, timeframeColumnCount: 4).then =>
        expect(@plugin.backwardsButton.hidden).toBe false

    it 'should show a right scroll arrow for extra future timeframes', ->
      @createCardboard(pastColumnCount: 0, presentColumnCount: 5, timeframeColumnCount: 4).then =>
        expect(@plugin.forwardsButton.hidden).toBe false

    it 'should not show a left scroll arrow if there are no past timeframes', ->
      @createCardboard(pastColumnCount: 0, presentColumnCount: 4, timeframeColumnCount: 4).then =>
        expect(@plugin.backwardsButton.hidden).toBe true

    it 'should not show a right scroll arrow if there are no extra future timeframes', ->
      @createCardboard(pastColumnCount: 1, presentColumnCount: 4, timeframeColumnCount: 4).then =>
        expect(@plugin.forwardsButton.hidden).toBe true

  describe 'when back scroll button is clicked', ->
    it 'should scroll backward', ->
      @createCardboard(pastColumnCount: 1, presentColumnCount: 4, timeframeColumnCount: 4).then =>
        @scrollBackwards().then =>
          expect(@plugin.getFirstVisibleScrollableColumn().timeframeRecord.getId()).toEqual '1'
{'fraction_non_alphanumeric': 0.11843493790080363, 'fraction_numerical': 0.009010471629190681, 'mean_word_length': 3.2453480358373534, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 91, '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': '933310', 'n_tokens_mistral': 3633, 'n_tokens_neox': 3482, 'n_words': 753}