|
| 1 | +import {SelectionModel} from './selection'; |
| 2 | + |
| 3 | + |
| 4 | +describe('SelectionModel', () => { |
| 5 | + describe('single selection', () => { |
| 6 | + let model: SelectionModel<any>; |
| 7 | + |
| 8 | + beforeEach(() => model = new SelectionModel()); |
| 9 | + |
| 10 | + it('should be able to select a single value', () => { |
| 11 | + model.select(1); |
| 12 | + |
| 13 | + expect(model.selected.length).toBe(1); |
| 14 | + expect(model.isSelected(1)).toBe(true); |
| 15 | + }); |
| 16 | + |
| 17 | + it('should deselect the previously selected value', () => { |
| 18 | + model.select(1); |
| 19 | + model.select(2); |
| 20 | + |
| 21 | + expect(model.isSelected(1)).toBe(false); |
| 22 | + expect(model.isSelected(2)).toBe(true); |
| 23 | + }); |
| 24 | + |
| 25 | + it('should only preselect one value', () => { |
| 26 | + model = new SelectionModel(false, [1, 2]); |
| 27 | + |
| 28 | + expect(model.selected.length).toBe(1); |
| 29 | + expect(model.isSelected(1)).toBe(true); |
| 30 | + expect(model.isSelected(2)).toBe(false); |
| 31 | + }); |
| 32 | + }); |
| 33 | + |
| 34 | + describe('multiple selection', () => { |
| 35 | + let model: SelectionModel<any>; |
| 36 | + |
| 37 | + beforeEach(() => model = new SelectionModel(true)); |
| 38 | + |
| 39 | + it('should be able to select multiple options at the same time', () => { |
| 40 | + model.select(1); |
| 41 | + model.select(2); |
| 42 | + |
| 43 | + expect(model.selected.length).toBe(2); |
| 44 | + expect(model.isSelected(1)).toBe(true); |
| 45 | + expect(model.isSelected(2)).toBe(true); |
| 46 | + }); |
| 47 | + |
| 48 | + it('should be able to preselect multiple options', () => { |
| 49 | + model = new SelectionModel(true, [1, 2]); |
| 50 | + |
| 51 | + expect(model.selected.length).toBe(2); |
| 52 | + expect(model.isSelected(1)).toBe(true); |
| 53 | + expect(model.isSelected(2)).toBe(true); |
| 54 | + }); |
| 55 | + }); |
| 56 | + |
| 57 | + describe('onChange event', () => { |
| 58 | + it('should return both the added and removed values', () => { |
| 59 | + let model = new SelectionModel(); |
| 60 | + let spy = jasmine.createSpy('SelectionModel change event'); |
| 61 | + |
| 62 | + model.select(1); |
| 63 | + |
| 64 | + model.onChange.subscribe(spy); |
| 65 | + |
| 66 | + model.select(2); |
| 67 | + |
| 68 | + let event = spy.calls.mostRecent().args[0]; |
| 69 | + |
| 70 | + expect(spy).toHaveBeenCalled(); |
| 71 | + expect(event.removed).toEqual([1]); |
| 72 | + expect(event.added).toEqual([2]); |
| 73 | + }); |
| 74 | + |
| 75 | + describe('selection', () => { |
| 76 | + let model: SelectionModel<any>; |
| 77 | + let spy: jasmine.Spy; |
| 78 | + |
| 79 | + beforeEach(() => { |
| 80 | + model = new SelectionModel(true); |
| 81 | + spy = jasmine.createSpy('SelectionModel change event'); |
| 82 | + |
| 83 | + model.onChange.subscribe(spy); |
| 84 | + }); |
| 85 | + |
| 86 | + it('should emit an event when a value is selected', () => { |
| 87 | + model.select(1); |
| 88 | + |
| 89 | + let event = spy.calls.mostRecent().args[0]; |
| 90 | + |
| 91 | + expect(spy).toHaveBeenCalled(); |
| 92 | + expect(event.added).toEqual([1]); |
| 93 | + expect(event.removed).toEqual([]); |
| 94 | + }); |
| 95 | + |
| 96 | + it('should not emit multiple events for the same value', () => { |
| 97 | + model.select(1); |
| 98 | + model.select(1); |
| 99 | + |
| 100 | + expect(spy).toHaveBeenCalledTimes(1); |
| 101 | + }); |
| 102 | + |
| 103 | + it('should not emit an event when preselecting values', () => { |
| 104 | + model = new SelectionModel(false, [1]); |
| 105 | + spy = jasmine.createSpy('SelectionModel initial change event'); |
| 106 | + model.onChange.subscribe(spy); |
| 107 | + |
| 108 | + expect(spy).not.toHaveBeenCalled(); |
| 109 | + }); |
| 110 | + }); |
| 111 | + |
| 112 | + describe('deselection', () => { |
| 113 | + let model: SelectionModel<any>; |
| 114 | + let spy: jasmine.Spy; |
| 115 | + |
| 116 | + beforeEach(() => { |
| 117 | + model = new SelectionModel(true, [1, 2, 3]); |
| 118 | + spy = jasmine.createSpy('SelectionModel change event'); |
| 119 | + |
| 120 | + model.onChange.subscribe(spy); |
| 121 | + }); |
| 122 | + |
| 123 | + it('should emit an event when a value is deselected', () => { |
| 124 | + model.deselect(1); |
| 125 | + |
| 126 | + let event = spy.calls.mostRecent().args[0]; |
| 127 | + |
| 128 | + expect(spy).toHaveBeenCalled(); |
| 129 | + expect(event.removed).toEqual([1]); |
| 130 | + }); |
| 131 | + |
| 132 | + it('should not emit an event when a non-selected value is deselected', () => { |
| 133 | + model.deselect(4); |
| 134 | + expect(spy).not.toHaveBeenCalled(); |
| 135 | + }); |
| 136 | + |
| 137 | + it('should emit a single event when clearing all of the selected options', () => { |
| 138 | + model.clear(); |
| 139 | + |
| 140 | + let event = spy.calls.mostRecent().args[0]; |
| 141 | + |
| 142 | + expect(spy).toHaveBeenCalledTimes(1); |
| 143 | + expect(event.removed).toEqual([1, 2, 3]); |
| 144 | + }); |
| 145 | + |
| 146 | + }); |
| 147 | + }); |
| 148 | + |
| 149 | + it('should be able to determine whether it is empty', () => { |
| 150 | + let model = new SelectionModel(); |
| 151 | + |
| 152 | + expect(model.isEmpty()).toBe(true); |
| 153 | + |
| 154 | + model.select(1); |
| 155 | + |
| 156 | + expect(model.isEmpty()).toBe(false); |
| 157 | + }); |
| 158 | + |
| 159 | + it('should be able to clear the selected options', () => { |
| 160 | + let model = new SelectionModel(true); |
| 161 | + |
| 162 | + model.select(1); |
| 163 | + model.select(2); |
| 164 | + |
| 165 | + expect(model.selected.length).toBe(2); |
| 166 | + |
| 167 | + model.clear(); |
| 168 | + |
| 169 | + expect(model.selected.length).toBe(0); |
| 170 | + expect(model.isEmpty()).toBe(true); |
| 171 | + }); |
| 172 | +}); |
0 commit comments