2015-05-20 07:06:59 +08:00
|
|
|
_ = require 'underscore'
|
2015-04-09 10:25:00 +08:00
|
|
|
|
2016-01-26 08:36:56 +08:00
|
|
|
{Thread} = require 'nylas-exports'
|
|
|
|
{ListTabular} = require 'nylas-component-kit'
|
|
|
|
|
|
|
|
ListDataSource = ListTabular.DataSource
|
|
|
|
ListSelection = ListTabular.Selection
|
2015-04-09 10:25:00 +08:00
|
|
|
|
2016-01-15 04:26:54 +08:00
|
|
|
describe "ListSelection", ->
|
2015-04-09 10:25:00 +08:00
|
|
|
beforeEach ->
|
|
|
|
@trigger = jasmine.createSpy('trigger')
|
2016-01-12 05:47:34 +08:00
|
|
|
|
2015-04-09 10:25:00 +08:00
|
|
|
@items = []
|
2017-06-22 04:12:49 +08:00
|
|
|
@items.push(new Thread(id: "#{ii}")) for ii in [0..99]
|
2015-04-09 10:25:00 +08:00
|
|
|
|
2016-01-15 04:26:54 +08:00
|
|
|
@view = new ListDataSource()
|
2016-01-12 05:47:34 +08:00
|
|
|
@view.indexOfId = jasmine.createSpy('indexOfId').andCallFake (id) =>
|
|
|
|
_.findIndex(@items, _.matcher({id}))
|
|
|
|
@view.get = jasmine.createSpy('get').andCallFake (idx) =>
|
|
|
|
@items[idx]
|
|
|
|
|
2016-01-15 04:26:54 +08:00
|
|
|
@selection = new ListSelection(@view, @trigger)
|
2015-04-09 10:25:00 +08:00
|
|
|
|
|
|
|
it "should initialize with an empty set", ->
|
|
|
|
expect(@selection.items()).toEqual([])
|
|
|
|
expect(@selection.ids()).toEqual([])
|
|
|
|
|
|
|
|
it "should throw an exception if a view is not provided", ->
|
2016-01-15 04:26:54 +08:00
|
|
|
expect( => new ListSelection(null, @trigger)).toThrow()
|
2015-04-09 10:25:00 +08:00
|
|
|
|
|
|
|
describe "set", ->
|
|
|
|
it "should replace the current selection with the provided models", ->
|
|
|
|
@selection.set([@items[2], @items[4], @items[7]])
|
|
|
|
expect(@selection.ids()).toEqual(['2', '4', '7'])
|
|
|
|
@selection.set([@items[2], @items[5], @items[6]])
|
|
|
|
expect(@selection.ids()).toEqual(['2', '5', '6'])
|
|
|
|
|
|
|
|
it "should throw an exception if the items passed are not models", ->
|
|
|
|
expect( => @selection.set(['hi'])).toThrow()
|
|
|
|
|
|
|
|
it "should trigger", ->
|
|
|
|
@selection.set([@items[2], @items[4], @items[7]])
|
|
|
|
expect(@trigger).toHaveBeenCalled()
|
|
|
|
|
|
|
|
describe "clear", ->
|
|
|
|
beforeEach ->
|
|
|
|
@selection.set([@items[2]])
|
|
|
|
|
|
|
|
it "should empty the selection set", ->
|
|
|
|
@selection.clear()
|
|
|
|
expect(@selection.ids()).toEqual([])
|
|
|
|
|
|
|
|
it "should trigger", ->
|
|
|
|
@selection.clear()
|
|
|
|
expect(@trigger).toHaveBeenCalled()
|
|
|
|
|
2015-06-02 09:29:39 +08:00
|
|
|
describe "remove", ->
|
|
|
|
beforeEach ->
|
2016-01-26 05:33:23 +08:00
|
|
|
@selection.set([@items[2], @items[4], @items[7]])
|
2015-06-02 09:29:39 +08:00
|
|
|
|
|
|
|
it "should do nothing if called without a valid item", ->
|
|
|
|
@selection.remove(null)
|
|
|
|
@selection.remove(undefined)
|
|
|
|
@selection.remove(false)
|
2016-01-26 05:33:23 +08:00
|
|
|
expect(@selection.ids()).toEqual(['2', '4', '7'])
|
2015-06-02 09:29:39 +08:00
|
|
|
|
|
|
|
it "should remove the item from the set", ->
|
|
|
|
@selection.remove(@items[2])
|
2016-01-26 05:33:23 +08:00
|
|
|
expect(@selection.ids()).toEqual(['4', '7'])
|
2015-06-02 09:29:39 +08:00
|
|
|
|
2016-01-26 05:33:23 +08:00
|
|
|
it "should throw an exception if any item passed is not a model", ->
|
2015-06-02 09:29:39 +08:00
|
|
|
expect( => @selection.remove('hi')).toThrow()
|
|
|
|
|
2016-01-26 05:33:23 +08:00
|
|
|
it "should accept an array of models as well as a single item", ->
|
|
|
|
@selection.remove([@items[2], @items[4]])
|
|
|
|
expect(@selection.ids()).toEqual(['7'])
|
|
|
|
|
2015-06-02 09:29:39 +08:00
|
|
|
it "should trigger", ->
|
|
|
|
@selection.remove()
|
|
|
|
expect(@trigger).toHaveBeenCalled()
|
|
|
|
|
2016-01-26 05:33:23 +08:00
|
|
|
describe "_applyChangeRecord", ->
|
2015-04-09 10:25:00 +08:00
|
|
|
it "should replace items in the selection with the matching provided items, if present", ->
|
|
|
|
@selection.set([@items[2], @items[4], @items[7]])
|
|
|
|
expect(@selection.items()[0]).toBe(@items[2])
|
|
|
|
expect(@selection.items()[0].subject).toBe(undefined)
|
2017-06-22 04:12:49 +08:00
|
|
|
newItem2 = new Thread(id: '2', subject:'Hello world!')
|
2016-01-26 05:33:23 +08:00
|
|
|
@selection._applyChangeRecord({objectClass: 'Thread', objects: [newItem2], type: 'persist'})
|
2015-04-09 10:25:00 +08:00
|
|
|
expect(@selection.items()[0].subject).toBe('Hello world!')
|
|
|
|
|
2016-01-26 05:33:23 +08:00
|
|
|
it "should rremove items in the selection if type is unpersist", ->
|
|
|
|
@selection.set([@items[2], @items[4], @items[7]])
|
2017-06-22 04:12:49 +08:00
|
|
|
newItem2 = new Thread(id: '2', subject:'Hello world!')
|
2016-01-26 05:33:23 +08:00
|
|
|
@selection._applyChangeRecord({objectClass: 'Thread', objects: [newItem2], type: 'unpersist'})
|
|
|
|
expect(@selection.ids()).toEqual(['4', '7'])
|
|
|
|
|
2015-04-09 10:25:00 +08:00
|
|
|
describe "toggle", ->
|
|
|
|
beforeEach ->
|
|
|
|
@selection.set([@items[2]])
|
|
|
|
|
2015-06-02 09:29:39 +08:00
|
|
|
it "should do nothing if called without a valid item", ->
|
|
|
|
@selection.toggle(null)
|
|
|
|
@selection.toggle(undefined)
|
|
|
|
@selection.toggle(false)
|
|
|
|
expect(@selection.ids()).toEqual(['2'])
|
|
|
|
|
|
|
|
it "should throw an exception if the item passed is not a model", ->
|
|
|
|
expect( => @selection.toggle('hi')).toThrow()
|
|
|
|
|
2015-04-09 10:25:00 +08:00
|
|
|
it "should select the item if it is not selected", ->
|
|
|
|
@selection.toggle(@items[3])
|
|
|
|
expect(@selection.ids()).toEqual(['2', '3'])
|
|
|
|
|
|
|
|
it "should de-select the item if it is selected", ->
|
|
|
|
@selection.toggle(@items[2])
|
|
|
|
expect(@selection.ids()).toEqual([])
|
|
|
|
|
|
|
|
it "should trigger", ->
|
|
|
|
@selection.toggle(@items[2])
|
|
|
|
expect(@trigger).toHaveBeenCalled()
|
|
|
|
|
|
|
|
describe "expandTo", ->
|
|
|
|
it "should select the item, if no other items are selected", ->
|
|
|
|
@selection.clear()
|
|
|
|
@selection.expandTo(@items[2])
|
|
|
|
expect(@selection.ids()).toEqual(['2'])
|
|
|
|
|
2015-06-02 09:29:39 +08:00
|
|
|
it "should do nothing if called without a valid item", ->
|
|
|
|
@selection.expandTo(null)
|
|
|
|
@selection.expandTo(undefined)
|
|
|
|
@selection.expandTo(false)
|
|
|
|
expect(@selection.ids()).toEqual([])
|
|
|
|
|
|
|
|
it "should throw an exception if the item passed is not a model", ->
|
|
|
|
expect( => @selection.expandTo('hi')).toThrow()
|
|
|
|
|
2016-11-03 02:46:13 +08:00
|
|
|
it "should select all items from the last selected item to the provided item when the provided item is below the current selection", ->
|
2015-04-09 10:25:00 +08:00
|
|
|
@selection.set([@items[2], @items[5]])
|
|
|
|
@selection.expandTo(@items[8])
|
|
|
|
expect(@selection.ids()).toEqual(['2','5','6','7','8'])
|
|
|
|
|
2016-11-03 02:46:13 +08:00
|
|
|
it "should select all items from the last selected item to the provided item when the provided item is above the current selection", ->
|
|
|
|
@selection.set([@items[7], @items[5]])
|
|
|
|
@selection.expandTo(@items[2])
|
|
|
|
expect(@selection.ids()).toEqual(['7', '5', '4', '3', '2'])
|
|
|
|
|
2015-04-09 10:25:00 +08:00
|
|
|
it "should not do anything if the provided item is not in the view set", ->
|
|
|
|
@selection.set([@items[2]])
|
|
|
|
@selection.expandTo(new Thread(id:'not-in-view!'))
|
|
|
|
expect(@selection.ids()).toEqual(['2'])
|
|
|
|
|
|
|
|
it "should re-order items so that the order still reflects the order selection actions were taken", ->
|
|
|
|
@selection.set([@items[10], @items[4], @items[1]])
|
|
|
|
@selection.expandTo(@items[8])
|
|
|
|
expect(@selection.ids()).toEqual(['10','1','2','3','4','5','6','7','8'])
|
|
|
|
|
|
|
|
it "should trigger", ->
|
|
|
|
@selection.set([@items[5], @items[4], @items[1]])
|
|
|
|
@selection.expandTo(@items[8])
|
|
|
|
expect(@trigger).toHaveBeenCalled()
|
|
|
|
|
|
|
|
describe "walk", ->
|
|
|
|
beforeEach ->
|
|
|
|
@selection.set([@items[2]])
|
|
|
|
|
|
|
|
it "should trigger", ->
|
|
|
|
current = @items[4]
|
|
|
|
next = @items[5]
|
|
|
|
@selection.walk({current, next})
|
|
|
|
expect(@trigger).toHaveBeenCalled()
|
|
|
|
|
|
|
|
it "should select both items if neither the start row or the end row are selected", ->
|
|
|
|
current = @items[4]
|
|
|
|
next = @items[5]
|
|
|
|
@selection.walk({current, next})
|
|
|
|
expect(@selection.ids()).toEqual(['2', '4', '5'])
|
|
|
|
|
refactor(*): Thread list fixes, flexible workspace store, multiple root sheets
Summary:
Remember to remove all the event listeners added to email frame
New files tab, queryable filename, not attribute
Rename ThreadSelectionBar to RootSelectionBar to go with RootCenterComponent, make it appear for draft selection and file selection as well
Initial file list and file list store, File Location
Remove unnecessary shouldComponentUpdate
Always track whether new requests have happened since ours to prevent out of order triggers
Always scroll to the current [focused/keyboard-cursor] in lists
So goodbye to the trash tag
Only scroll to current item if focus or keyboard has moved
Show message snippet in notification if no subject line
Make the RootSelectionBar pull items from Component Registry
New Archive button (prettier than the other one)
Refactor event additions to iframe so iframe can be used for file display also
Thread List is no longer the uber root package - drafts and files moved to separate packages
WorkspaceStore now allows packages to register sheets, "view" concept replaced with "root sheet" concept, "mode" may not be observed by all sheets, and is now called "preferred mode"
Don't animate transitions between two root sheets
Mode switch is only visible on root sheets that support multiple modes
Account sidebar now shows "Views" that have registered themselves: drafts and files for now
Model Selection Bar is now a component, just like ModelList. Meant to be in the toolbar above a Model List
Misc supporting changes
New files package which registers it's views and components
Rename files package to `file-list`
Move checkmark column down into model list
Don't throw exception if shift-down arrow and nothing selected
Takes a long time on login to fetch first page of threads, make pages smaller
Displaynames, spec fixes
Test Plan: Run tests
Reviewers: evan
Reviewed By: evan
Differential Revision: https://review.inboxapp.com/D1412
2015-04-11 05:33:05 +08:00
|
|
|
it "should select only one item if either current or next is null or undefined", ->
|
|
|
|
current = null
|
|
|
|
next = @items[5]
|
|
|
|
@selection.walk({current, next})
|
|
|
|
expect(@selection.ids()).toEqual(['2', '5'])
|
|
|
|
|
|
|
|
next = null
|
|
|
|
current = @items[7]
|
|
|
|
@selection.walk({current, next})
|
|
|
|
expect(@selection.ids()).toEqual(['2', '5', '7'])
|
|
|
|
|
2015-04-09 10:25:00 +08:00
|
|
|
describe "when the `next` item is a step backwards in the selection history", ->
|
|
|
|
it "should deselect the current item", ->
|
|
|
|
@selection.set([@items[2], @items[3], @items[4], @items[5]])
|
|
|
|
current = @items[5]
|
|
|
|
next = @items[4]
|
|
|
|
@selection.walk({current, next})
|
|
|
|
expect(@selection.ids()).toEqual(['2', '3', '4'])
|
|
|
|
|
|
|
|
describe "otherwise", ->
|
|
|
|
it "should select the next item", ->
|
|
|
|
@selection.set([@items[2], @items[3], @items[4], @items[5]])
|
|
|
|
current = @items[5]
|
|
|
|
next = @items[6]
|
|
|
|
@selection.walk({current, next})
|
|
|
|
expect(@selection.ids()).toEqual(['2', '3', '4', '5', '6'])
|
|
|
|
|
|
|
|
describe "if the item was already selected", ->
|
|
|
|
it "should re-order the selection array so the selection still represents selection history", ->
|
|
|
|
@selection.set([@items[5], @items[8], @items[7], @items[6]])
|
|
|
|
expect(@selection.ids()).toEqual(['5', '8', '7', '6'])
|
|
|
|
|
|
|
|
current = @items[6]
|
|
|
|
next = @items[5]
|
|
|
|
@selection.walk({current, next})
|
|
|
|
expect(@selection.ids()).toEqual(['8', '7', '6', '5'])
|