commandsStates.test.ts167 lines · main
| 1 | import { beforeEach, describe, expect, it } from 'vitest' |
| 2 | |
| 3 | import { section$new } from '../CommandSection' |
| 4 | import { type ICommandSection, type ICommandsState } from '../types' |
| 5 | import { initCommandsState, orderSectionFirst } from './commandsState' |
| 6 | |
| 7 | describe('orderSectionFirst', () => { |
| 8 | it('Orders newly created section first', () => { |
| 9 | const first = section$new('First') |
| 10 | const second = section$new('Second') |
| 11 | const third = section$new('Third') |
| 12 | const sections = [first, second, third] |
| 13 | |
| 14 | const expected = [third, first, second] |
| 15 | const actual = orderSectionFirst(sections, 2) |
| 16 | expect(actual).toEqual(expected) |
| 17 | }) |
| 18 | |
| 19 | it('Orders sole section first', () => { |
| 20 | const only = section$new('Only') |
| 21 | const sections = [only] |
| 22 | |
| 23 | const actual = orderSectionFirst(sections, 0) |
| 24 | expect(actual).toEqual(sections) |
| 25 | }) |
| 26 | |
| 27 | it('Orders existing section first', () => { |
| 28 | const first = section$new('First') |
| 29 | const second = section$new('Second') |
| 30 | const third = section$new('Third') |
| 31 | const sections = [first, second, third] |
| 32 | |
| 33 | const expected = [second, first, third] |
| 34 | const actual = orderSectionFirst(sections, 1) |
| 35 | expect(actual).toEqual(expected) |
| 36 | }) |
| 37 | }) |
| 38 | |
| 39 | describe('commandState', () => { |
| 40 | let commandsState: ICommandsState |
| 41 | |
| 42 | beforeEach(() => { |
| 43 | commandsState = initCommandsState() |
| 44 | }) |
| 45 | |
| 46 | it('Registers commands in new section', () => { |
| 47 | const sectionName = 'Section' |
| 48 | const commands = [ |
| 49 | { id: 'first', name: 'First', action: () => {} }, |
| 50 | { id: 'second', name: 'Second', action: () => {} }, |
| 51 | ] |
| 52 | |
| 53 | const expected = [section$new(sectionName)] |
| 54 | expected[0].commands = commands |
| 55 | |
| 56 | commandsState.registerSection(sectionName, commands) |
| 57 | |
| 58 | expect(commandsState.commandSections).toEqual(expected) |
| 59 | }) |
| 60 | |
| 61 | it('Registers command in pre-existing section', () => { |
| 62 | const sectionA = 'A' |
| 63 | const sectionB = 'B' |
| 64 | |
| 65 | const existingCommands = [ |
| 66 | { id: 'first', name: 'First', action: () => {} }, |
| 67 | { id: 'second', name: 'Second', action: () => {} }, |
| 68 | ] |
| 69 | |
| 70 | const commandsToAdd = [{ id: 'third', name: 'Third', action: () => {} }] |
| 71 | |
| 72 | const sections = [section$new(sectionA), section$new(sectionB)] |
| 73 | sections[0].commands = existingCommands |
| 74 | commandsState.commandSections = sections |
| 75 | |
| 76 | const expected = [section$new(sectionA), section$new(sectionB)] |
| 77 | expected[0].commands = existingCommands |
| 78 | expected[0].commands = expected[0].commands.concat(commandsToAdd) |
| 79 | |
| 80 | commandsState.registerSection('A', commandsToAdd) |
| 81 | expect(commandsState.commandSections).toEqual(expected) |
| 82 | }) |
| 83 | |
| 84 | it('Reorders sections if orderSection is given', () => { |
| 85 | const sectionA = 'A' |
| 86 | const sectionB = 'B' |
| 87 | |
| 88 | const commands = [{ id: 'first', name: 'First', action: () => {} }] |
| 89 | |
| 90 | const sections = [section$new(sectionA), section$new(sectionB)] |
| 91 | commandsState.commandSections = sections |
| 92 | |
| 93 | const expected = [section$new(sectionB), section$new(sectionA)] |
| 94 | expected[0].commands = commands |
| 95 | |
| 96 | commandsState.registerSection('B', commands, { orderSection: orderSectionFirst }) |
| 97 | expect(commandsState.commandSections).toEqual(expected) |
| 98 | }) |
| 99 | |
| 100 | it('Reorders commands if orderCommands is given', () => { |
| 101 | const sectionName = 'Section' |
| 102 | |
| 103 | const existingCommands = [ |
| 104 | { id: 'first', name: 'First', action: () => {} }, |
| 105 | { id: 'second', name: 'Second', action: () => {} }, |
| 106 | ] |
| 107 | |
| 108 | const sections = [section$new(sectionName)] |
| 109 | sections[0].commands = existingCommands |
| 110 | commandsState.commandSections = sections |
| 111 | |
| 112 | const commandsToAdd = [{ id: 'third', name: 'Third', action: () => {} }] |
| 113 | |
| 114 | const expected = [section$new(sectionName)] |
| 115 | expected[0].commands = [existingCommands[0], commandsToAdd[0], existingCommands[1]] |
| 116 | |
| 117 | commandsState.registerSection('Section', commandsToAdd, { |
| 118 | orderCommands: (oldCommands, newCommands) => { |
| 119 | const commands = [...oldCommands] |
| 120 | commands.splice(1, 0, ...newCommands) |
| 121 | return commands |
| 122 | }, |
| 123 | }) |
| 124 | |
| 125 | expect(commandsState.commandSections).toEqual(expected) |
| 126 | }) |
| 127 | |
| 128 | it('Unregisters commands when returned function is called', () => { |
| 129 | const sectionName = 'Section' |
| 130 | const firstCommands = [{ id: 'command', name: 'Command', action: () => {} }] |
| 131 | const secondCommands = [{ id: 'second', name: 'Second', action: () => {} }] |
| 132 | |
| 133 | const expected = [section$new(sectionName)] |
| 134 | expected[0].commands = [...firstCommands, ...secondCommands] |
| 135 | |
| 136 | commandsState.registerSection(sectionName, firstCommands) |
| 137 | const unregister = commandsState.registerSection(sectionName, secondCommands) |
| 138 | |
| 139 | expect(commandsState.commandSections).toEqual(expected) |
| 140 | |
| 141 | const newExpected = [section$new(sectionName)] |
| 142 | newExpected[0].commands = firstCommands |
| 143 | |
| 144 | unregister() |
| 145 | expect(commandsState.commandSections).toHaveLength(1) |
| 146 | expect(commandsState.commandSections[0].commands).toHaveLength(1) |
| 147 | expect(commandsState.commandSections).toEqual(newExpected) |
| 148 | }) |
| 149 | |
| 150 | it('Unregisters entire section if no commands remain', () => { |
| 151 | const sectionName = 'Section' |
| 152 | const commands = [{ id: 'command', name: 'Command', action: () => {} }] |
| 153 | |
| 154 | const expected = [section$new(sectionName)] |
| 155 | expected[0].commands = commands |
| 156 | |
| 157 | const unregister = commandsState.registerSection(sectionName, commands) |
| 158 | |
| 159 | expect(commandsState.commandSections).toEqual(expected) |
| 160 | |
| 161 | const newExpected: ICommandSection[] = [] |
| 162 | |
| 163 | unregister() |
| 164 | expect(commandsState.commandSections).toHaveLength(0) |
| 165 | expect(commandsState.commandSections).toEqual(newExpected) |
| 166 | }) |
| 167 | }) |