|
| 1 | +import { describe, expect, it } from 'vitest'; |
| 2 | +import type { WorkspaceStoreState2 } from '@openmrs/esm-extensions'; |
| 3 | +import { workspace2StoreActions } from './workspace2'; |
| 4 | + |
| 5 | +function makeState(overrides: Partial<WorkspaceStoreState2> = {}): WorkspaceStoreState2 { |
| 6 | + return { |
| 7 | + registeredGroupsByName: {}, |
| 8 | + registeredWindowsByName: {}, |
| 9 | + registeredWorkspacesByName: { |
| 10 | + 'parent-workspace': { name: 'parent-workspace', component: 'parent', window: 'test-window', moduleName: 'test' }, |
| 11 | + 'child-workspace': { name: 'child-workspace', component: 'child', window: 'test-window', moduleName: 'test' }, |
| 12 | + 'grandchild-workspace': { |
| 13 | + name: 'grandchild-workspace', |
| 14 | + component: 'grandchild', |
| 15 | + window: 'test-window', |
| 16 | + moduleName: 'test', |
| 17 | + }, |
| 18 | + 'other-window-workspace': { |
| 19 | + name: 'other-window-workspace', |
| 20 | + component: 'other', |
| 21 | + window: 'other-window', |
| 22 | + moduleName: 'test', |
| 23 | + }, |
| 24 | + }, |
| 25 | + openedGroup: null, |
| 26 | + openedWindows: [], |
| 27 | + isMostRecentlyOpenedWindowHidden: false, |
| 28 | + workspaceTitleByWorkspaceName: {}, |
| 29 | + ...overrides, |
| 30 | + }; |
| 31 | +} |
| 32 | + |
| 33 | +function makeOpenedWorkspace(name: string, hasUnsavedChanges = false) { |
| 34 | + return { workspaceName: name, props: {}, hasUnsavedChanges, uuid: `uuid-${name}` }; |
| 35 | +} |
| 36 | + |
| 37 | +describe('openChildWorkspace', () => { |
| 38 | + it('opens a child workspace from the leaf parent', () => { |
| 39 | + const state = makeState({ |
| 40 | + openedWindows: [ |
| 41 | + { |
| 42 | + windowName: 'test-window', |
| 43 | + openedWorkspaces: [makeOpenedWorkspace('parent-workspace')], |
| 44 | + props: null, |
| 45 | + maximized: false, |
| 46 | + }, |
| 47 | + ], |
| 48 | + }); |
| 49 | + |
| 50 | + const result = workspace2StoreActions.openChildWorkspace(state, 'parent-workspace', 'child-workspace', { |
| 51 | + foo: 'bar', |
| 52 | + }); |
| 53 | + |
| 54 | + expect(result.openedWindows[0].openedWorkspaces).toHaveLength(2); |
| 55 | + expect(result.openedWindows[0].openedWorkspaces[0].workspaceName).toBe('parent-workspace'); |
| 56 | + expect(result.openedWindows[0].openedWorkspaces[1].workspaceName).toBe('child-workspace'); |
| 57 | + expect(result.openedWindows[0].openedWorkspaces[1].props).toEqual({ foo: 'bar' }); |
| 58 | + }); |
| 59 | + |
| 60 | + it('trims workspaces above the parent when parent is not the leaf (double-click race)', () => { |
| 61 | + const state = makeState({ |
| 62 | + openedWindows: [ |
| 63 | + { |
| 64 | + windowName: 'test-window', |
| 65 | + openedWorkspaces: [makeOpenedWorkspace('parent-workspace'), makeOpenedWorkspace('child-workspace')], |
| 66 | + props: null, |
| 67 | + maximized: false, |
| 68 | + }, |
| 69 | + ], |
| 70 | + }); |
| 71 | + |
| 72 | + // Simulates the second click in a double-click: parent tries to open the same child again |
| 73 | + const result = workspace2StoreActions.openChildWorkspace(state, 'parent-workspace', 'child-workspace', { |
| 74 | + newProps: true, |
| 75 | + }); |
| 76 | + |
| 77 | + expect(result.openedWindows[0].openedWorkspaces).toHaveLength(2); |
| 78 | + expect(result.openedWindows[0].openedWorkspaces[0].workspaceName).toBe('parent-workspace'); |
| 79 | + expect(result.openedWindows[0].openedWorkspaces[1].workspaceName).toBe('child-workspace'); |
| 80 | + expect(result.openedWindows[0].openedWorkspaces[1].props).toEqual({ newProps: true }); |
| 81 | + }); |
| 82 | + |
| 83 | + it('trims grandchild when parent opens a new child', () => { |
| 84 | + const state = makeState({ |
| 85 | + openedWindows: [ |
| 86 | + { |
| 87 | + windowName: 'test-window', |
| 88 | + openedWorkspaces: [ |
| 89 | + makeOpenedWorkspace('parent-workspace'), |
| 90 | + makeOpenedWorkspace('child-workspace'), |
| 91 | + makeOpenedWorkspace('grandchild-workspace'), |
| 92 | + ], |
| 93 | + props: null, |
| 94 | + maximized: false, |
| 95 | + }, |
| 96 | + ], |
| 97 | + }); |
| 98 | + |
| 99 | + // Parent opens a new child — both child and grandchild should be trimmed |
| 100 | + const result = workspace2StoreActions.openChildWorkspace(state, 'parent-workspace', 'child-workspace', {}); |
| 101 | + |
| 102 | + expect(result.openedWindows[0].openedWorkspaces).toHaveLength(2); |
| 103 | + expect(result.openedWindows[0].openedWorkspaces[0].workspaceName).toBe('parent-workspace'); |
| 104 | + expect(result.openedWindows[0].openedWorkspaces[1].workspaceName).toBe('child-workspace'); |
| 105 | + }); |
| 106 | + |
| 107 | + it('throws when child workspace is not registered', () => { |
| 108 | + const state = makeState({ |
| 109 | + openedWindows: [ |
| 110 | + { |
| 111 | + windowName: 'test-window', |
| 112 | + openedWorkspaces: [makeOpenedWorkspace('parent-workspace')], |
| 113 | + props: null, |
| 114 | + maximized: false, |
| 115 | + }, |
| 116 | + ], |
| 117 | + }); |
| 118 | + |
| 119 | + expect(() => |
| 120 | + workspace2StoreActions.openChildWorkspace(state, 'parent-workspace', 'nonexistent-workspace', {}), |
| 121 | + ).toThrow('No workspace named "nonexistent-workspace" registered'); |
| 122 | + }); |
| 123 | + |
| 124 | + it('throws when parent workspace is not registered', () => { |
| 125 | + const state = makeState({ |
| 126 | + openedWindows: [ |
| 127 | + { |
| 128 | + windowName: 'test-window', |
| 129 | + openedWorkspaces: [makeOpenedWorkspace('parent-workspace')], |
| 130 | + props: null, |
| 131 | + maximized: false, |
| 132 | + }, |
| 133 | + ], |
| 134 | + }); |
| 135 | + |
| 136 | + expect(() => workspace2StoreActions.openChildWorkspace(state, 'nonexistent-parent', 'child-workspace', {})).toThrow( |
| 137 | + 'No workspace named "nonexistent-parent" registered', |
| 138 | + ); |
| 139 | + }); |
| 140 | + |
| 141 | + it('throws when child belongs to a different window than parent', () => { |
| 142 | + const state = makeState({ |
| 143 | + openedWindows: [ |
| 144 | + { |
| 145 | + windowName: 'test-window', |
| 146 | + openedWorkspaces: [makeOpenedWorkspace('parent-workspace')], |
| 147 | + props: null, |
| 148 | + maximized: false, |
| 149 | + }, |
| 150 | + ], |
| 151 | + }); |
| 152 | + |
| 153 | + expect(() => |
| 154 | + workspace2StoreActions.openChildWorkspace(state, 'parent-workspace', 'other-window-workspace', {}), |
| 155 | + ).toThrow('does not belong to the same workspace window'); |
| 156 | + }); |
| 157 | + |
| 158 | + it('throws when the window is not opened', () => { |
| 159 | + const state = makeState({ |
| 160 | + openedWindows: [], |
| 161 | + }); |
| 162 | + |
| 163 | + expect(() => workspace2StoreActions.openChildWorkspace(state, 'parent-workspace', 'child-workspace', {})).toThrow( |
| 164 | + 'window test-window is not opened', |
| 165 | + ); |
| 166 | + }); |
| 167 | + |
| 168 | + it('throws when the parent workspace is not in the opened window', () => { |
| 169 | + const state = makeState({ |
| 170 | + openedWindows: [ |
| 171 | + { |
| 172 | + windowName: 'test-window', |
| 173 | + openedWorkspaces: [makeOpenedWorkspace('child-workspace')], |
| 174 | + props: null, |
| 175 | + maximized: false, |
| 176 | + }, |
| 177 | + ], |
| 178 | + }); |
| 179 | + |
| 180 | + expect(() => |
| 181 | + workspace2StoreActions.openChildWorkspace(state, 'parent-workspace', 'grandchild-workspace', {}), |
| 182 | + ).toThrow('parent is not opened within the workspace window'); |
| 183 | + }); |
| 184 | + |
| 185 | + it('does not mutate the original state', () => { |
| 186 | + const originalWorkspaces = [makeOpenedWorkspace('parent-workspace'), makeOpenedWorkspace('child-workspace')]; |
| 187 | + const state = makeState({ |
| 188 | + openedWindows: [ |
| 189 | + { |
| 190 | + windowName: 'test-window', |
| 191 | + openedWorkspaces: [...originalWorkspaces], |
| 192 | + props: null, |
| 193 | + maximized: false, |
| 194 | + }, |
| 195 | + ], |
| 196 | + }); |
| 197 | + |
| 198 | + workspace2StoreActions.openChildWorkspace(state, 'parent-workspace', 'child-workspace', {}); |
| 199 | + |
| 200 | + // Original state should be untouched |
| 201 | + expect(state.openedWindows[0].openedWorkspaces).toHaveLength(2); |
| 202 | + expect(state.openedWindows[0].openedWorkspaces[0].workspaceName).toBe('parent-workspace'); |
| 203 | + expect(state.openedWindows[0].openedWorkspaces[1].workspaceName).toBe('child-workspace'); |
| 204 | + }); |
| 205 | +}); |
0 commit comments