2015-06-12 09:00:40 +08:00
|
|
|
MultiselectSplitInteractionHandler = require '../../src/components/multiselect-split-interaction-handler'
|
|
|
|
WorkspaceStore = require '../../src/flux/stores/workspace-store'
|
|
|
|
FocusedContentStore = require '../../src/flux/stores/focused-content-store'
|
2016-05-04 07:42:28 +08:00
|
|
|
Thread = require('../../src/flux/models/thread').default
|
2016-10-28 09:48:33 +08:00
|
|
|
Actions = require('../../src/flux/actions').default
|
2015-06-12 09:00:40 +08:00
|
|
|
_ = require 'underscore'
|
|
|
|
|
|
|
|
describe "MultiselectSplitInteractionHandler", ->
|
|
|
|
beforeEach ->
|
|
|
|
@item = new Thread(id:'123')
|
|
|
|
@itemFocus = new Thread({id: 'focus'})
|
|
|
|
@itemKeyboardFocus = new Thread({id: 'keyboard-focus'})
|
|
|
|
@itemAfterFocus = new Thread(id:'after-focus')
|
|
|
|
@itemAfterKeyboardFocus = new Thread(id:'after-keyboard-focus')
|
|
|
|
|
|
|
|
data = [@item, @itemFocus, @itemAfterFocus, @itemKeyboardFocus, @itemAfterKeyboardFocus]
|
2016-01-23 05:47:54 +08:00
|
|
|
|
|
|
|
@onFocusItem = jasmine.createSpy('onFocusItem')
|
|
|
|
@onSetCursorPosition = jasmine.createSpy('onSetCursorPosition')
|
2015-06-12 09:00:40 +08:00
|
|
|
@selection = []
|
2016-01-15 04:26:54 +08:00
|
|
|
@dataSource =
|
2015-06-12 09:00:40 +08:00
|
|
|
selection:
|
|
|
|
toggle: jasmine.createSpy('toggle')
|
|
|
|
expandTo: jasmine.createSpy('expandTo')
|
|
|
|
add: jasmine.createSpy('add')
|
|
|
|
walk: jasmine.createSpy('walk')
|
|
|
|
clear: jasmine.createSpy('clear')
|
|
|
|
count: => @selection.length
|
|
|
|
items: => @selection
|
|
|
|
top: => @selection[-1]
|
|
|
|
|
|
|
|
get: (idx) ->
|
|
|
|
data[idx]
|
|
|
|
getById: (id) ->
|
|
|
|
_.find data, (item) -> item.id is id
|
|
|
|
indexOfId: (id) ->
|
|
|
|
_.findIndex data, (item) -> item.id is id
|
|
|
|
count: -> data.length
|
|
|
|
|
2016-01-23 05:47:54 +08:00
|
|
|
@props =
|
|
|
|
dataSource: @dataSource
|
|
|
|
keyboardCursorId: 'keyboard-focus'
|
|
|
|
focused: @itemFocus
|
|
|
|
focusedId: 'focus'
|
|
|
|
onFocusItem: @onFocusItem
|
|
|
|
onSetCursorPosition: @onSetCursorPosition
|
|
|
|
|
2015-06-12 09:00:40 +08:00
|
|
|
@collection = 'threads'
|
|
|
|
@isRootSheet = true
|
2016-01-23 05:47:54 +08:00
|
|
|
@handler = new MultiselectSplitInteractionHandler(@props)
|
2015-06-12 09:00:40 +08:00
|
|
|
|
|
|
|
spyOn(WorkspaceStore, 'topSheet').andCallFake => {root: @isRootSheet}
|
|
|
|
|
|
|
|
it "should always show focus", ->
|
|
|
|
expect(@handler.shouldShowFocus()).toEqual(true)
|
|
|
|
|
|
|
|
it "should show the keyboard cursor when multiple items are selected", ->
|
|
|
|
@selection = []
|
|
|
|
expect(@handler.shouldShowKeyboardCursor()).toEqual(false)
|
|
|
|
@selection = [@item]
|
|
|
|
expect(@handler.shouldShowKeyboardCursor()).toEqual(false)
|
|
|
|
@selection = [@item, @itemFocus]
|
|
|
|
expect(@handler.shouldShowKeyboardCursor()).toEqual(true)
|
|
|
|
|
|
|
|
describe "onClick", ->
|
2015-12-30 01:15:18 +08:00
|
|
|
it "should focus the list item and indicate it was focused via click", ->
|
2015-06-12 09:00:40 +08:00
|
|
|
@handler.onClick(@item)
|
2016-01-23 05:47:54 +08:00
|
|
|
expect(@onFocusItem).toHaveBeenCalledWith(@item)
|
2015-06-12 09:00:40 +08:00
|
|
|
|
|
|
|
describe "onMetaClick", ->
|
|
|
|
describe "when there is currently a focused item", ->
|
|
|
|
it "should turn the focused item into the first selected item", ->
|
|
|
|
@handler.onMetaClick(@item)
|
2016-01-15 04:26:54 +08:00
|
|
|
expect(@dataSource.selection.add).toHaveBeenCalledWith(@itemFocus)
|
2015-06-12 09:00:40 +08:00
|
|
|
|
|
|
|
it "should clear the focus", ->
|
|
|
|
@handler.onMetaClick(@item)
|
2016-01-23 05:47:54 +08:00
|
|
|
expect(@onFocusItem).toHaveBeenCalledWith(null)
|
2015-06-12 09:00:40 +08:00
|
|
|
|
|
|
|
it "should toggle selection", ->
|
|
|
|
@handler.onMetaClick(@item)
|
2016-01-15 04:26:54 +08:00
|
|
|
expect(@dataSource.selection.toggle).toHaveBeenCalledWith(@item)
|
2015-06-12 09:00:40 +08:00
|
|
|
|
|
|
|
it "should call _checkSelectionAndFocusConsistency", ->
|
|
|
|
spyOn(@handler, '_checkSelectionAndFocusConsistency')
|
|
|
|
@handler.onMetaClick(@item)
|
|
|
|
expect(@handler._checkSelectionAndFocusConsistency).toHaveBeenCalled()
|
|
|
|
|
|
|
|
describe "onShiftClick", ->
|
|
|
|
describe "when there is currently a focused item", ->
|
|
|
|
|
|
|
|
it "should turn the focused item into the first selected item", ->
|
|
|
|
@handler.onMetaClick(@item)
|
2016-01-15 04:26:54 +08:00
|
|
|
expect(@dataSource.selection.add).toHaveBeenCalledWith(@itemFocus)
|
2015-06-12 09:00:40 +08:00
|
|
|
|
|
|
|
it "should clear the focus", ->
|
|
|
|
@handler.onMetaClick(@item)
|
2016-01-23 05:47:54 +08:00
|
|
|
expect(@onFocusItem).toHaveBeenCalledWith(null)
|
2015-06-12 09:00:40 +08:00
|
|
|
|
|
|
|
it "should expand selection", ->
|
|
|
|
@handler.onShiftClick(@item)
|
2016-01-15 04:26:54 +08:00
|
|
|
expect(@dataSource.selection.expandTo).toHaveBeenCalledWith(@item)
|
2015-06-12 09:00:40 +08:00
|
|
|
|
|
|
|
it "should call _checkSelectionAndFocusConsistency", ->
|
|
|
|
spyOn(@handler, '_checkSelectionAndFocusConsistency')
|
|
|
|
@handler.onMetaClick(@item)
|
|
|
|
expect(@handler._checkSelectionAndFocusConsistency).toHaveBeenCalled()
|
|
|
|
|
|
|
|
describe "onEnter", ->
|
|
|
|
|
|
|
|
describe "onSelect (x key on keyboard)", ->
|
|
|
|
it "should call _checkSelectionAndFocusConsistency", ->
|
|
|
|
spyOn(@handler, '_checkSelectionAndFocusConsistency')
|
|
|
|
@handler.onMetaClick(@item)
|
|
|
|
expect(@handler._checkSelectionAndFocusConsistency).toHaveBeenCalled()
|
|
|
|
|
|
|
|
describe "onShift", ->
|
|
|
|
it "should call _checkSelectionAndFocusConsistency", ->
|
|
|
|
spyOn(@handler, '_checkSelectionAndFocusConsistency')
|
|
|
|
@handler.onMetaClick(@item)
|
|
|
|
expect(@handler._checkSelectionAndFocusConsistency).toHaveBeenCalled()
|
|
|
|
|
|
|
|
describe "when the select option is passed", ->
|
|
|
|
it "should turn the existing focused item into a selected item", ->
|
|
|
|
@handler.onShift(1, {select: true})
|
2016-01-15 04:26:54 +08:00
|
|
|
expect(@dataSource.selection.add).toHaveBeenCalledWith(@itemFocus)
|
2015-06-12 09:00:40 +08:00
|
|
|
|
|
|
|
it "should walk the selection to the shift target", ->
|
|
|
|
@handler.onShift(1, {select: true})
|
2016-01-15 04:26:54 +08:00
|
|
|
expect(@dataSource.selection.walk).toHaveBeenCalledWith({current: @itemFocus, next: @itemAfterFocus})
|
2015-06-12 09:00:40 +08:00
|
|
|
|
|
|
|
describe "when one or more items is selected", ->
|
|
|
|
it "should move the keyboard cursor", ->
|
|
|
|
@selection = [@itemFocus, @itemAfterFocus, @itemKeyboardFocus]
|
|
|
|
@handler.onShift(1, {})
|
2016-01-23 05:47:54 +08:00
|
|
|
expect(@onSetCursorPosition).toHaveBeenCalledWith(@itemAfterKeyboardFocus)
|
2015-06-12 09:00:40 +08:00
|
|
|
|
|
|
|
describe "when no items are selected", ->
|
|
|
|
it "should move the focus", ->
|
|
|
|
@handler.onShift(1, {})
|
2016-01-23 05:47:54 +08:00
|
|
|
expect(@onFocusItem).toHaveBeenCalledWith(@itemAfterFocus)
|
2015-06-12 09:00:40 +08:00
|
|
|
|
|
|
|
|
|
|
|
describe "_checkSelectionAndFocusConsistency", ->
|
|
|
|
describe "when only one item is selected", ->
|
|
|
|
beforeEach ->
|
|
|
|
@selection = [@item]
|
2016-01-23 05:47:54 +08:00
|
|
|
@props.focused = null
|
|
|
|
@handler = new MultiselectSplitInteractionHandler(@props)
|
2015-06-12 09:00:40 +08:00
|
|
|
|
|
|
|
it "should clear the selection and make the item focused", ->
|
|
|
|
@handler._checkSelectionAndFocusConsistency()
|
2016-01-15 04:26:54 +08:00
|
|
|
expect(@dataSource.selection.clear).toHaveBeenCalled()
|
2016-01-23 05:47:54 +08:00
|
|
|
expect(@onFocusItem).toHaveBeenCalledWith(@item)
|