|
| 1 | +import '@testing-library/jest-dom'; |
| 2 | + |
| 3 | +import organizePlugins from './utils'; |
| 4 | +import { DirectPluginOperations } from './DirectPlugin'; |
| 5 | + |
| 6 | +const mockModifyWidget = (widget) => { |
| 7 | + const newContent = { |
| 8 | + url: '/search', |
| 9 | + label: 'Search', |
| 10 | + }; |
| 11 | + const modifiedWidget = widget; |
| 12 | + modifiedWidget.content = newContent; |
| 13 | + return modifiedWidget; |
| 14 | +}; |
| 15 | + |
| 16 | +function mockWrapWidget({ widget }) { |
| 17 | + const isAdmin = true; |
| 18 | + return isAdmin ? widget : null; |
| 19 | +} |
| 20 | + |
| 21 | +const mockSlotChanges = [ |
| 22 | + { |
| 23 | + op: DirectPluginOperations.Wrap, |
| 24 | + widgetId: 'drafts', |
| 25 | + wrapper: mockWrapWidget, |
| 26 | + }, |
| 27 | + { |
| 28 | + op: DirectPluginOperations.Hide, |
| 29 | + widgetId: 'home', |
| 30 | + }, |
| 31 | + { |
| 32 | + op: DirectPluginOperations.Modify, |
| 33 | + widgetId: 'lookUp', |
| 34 | + fn: mockModifyWidget, |
| 35 | + }, |
| 36 | + { |
| 37 | + op: DirectPluginOperations.Insert, |
| 38 | + widget: { |
| 39 | + id: 'login', |
| 40 | + priority: 50, |
| 41 | + content: { |
| 42 | + url: '/login', label: 'Login', |
| 43 | + }, |
| 44 | + }, |
| 45 | + }, |
| 46 | +]; |
| 47 | + |
| 48 | +const mockDefaultContent = [ |
| 49 | + { |
| 50 | + id: 'home', |
| 51 | + priority: 5, |
| 52 | + content: { url: '/', label: 'Home' }, |
| 53 | + }, |
| 54 | + { |
| 55 | + id: 'lookUp', |
| 56 | + priority: 25, |
| 57 | + content: { url: '/lookup', label: 'Lookup' }, |
| 58 | + }, |
| 59 | + { |
| 60 | + id: 'drafts', |
| 61 | + priority: 35, |
| 62 | + content: { url: '/drafts', label: 'Drafts' }, |
| 63 | + }, |
| 64 | +]; |
| 65 | + |
| 66 | +describe('organizePlugins', () => { |
| 67 | + describe('when there is no defaultContent', () => { |
| 68 | + afterEach(() => { |
| 69 | + jest.clearAllMocks(); |
| 70 | + }); |
| 71 | + |
| 72 | + it('should return an empty array when there are no changes or additions to slot', () => { |
| 73 | + const plugins = organizePlugins([], []); |
| 74 | + expect(plugins.length).toBe(0); |
| 75 | + expect(plugins).toEqual([]); |
| 76 | + }); |
| 77 | + |
| 78 | + it('should return an array of changes for non-default plugins', () => { |
| 79 | + const plugins = organizePlugins([], mockSlotChanges); |
| 80 | + expect(plugins.length).toEqual(1); |
| 81 | + expect(plugins[0].id).toEqual('login'); |
| 82 | + }); |
| 83 | + }); |
| 84 | + |
| 85 | + describe('when there is defaultContent', () => { |
| 86 | + afterEach(() => { |
| 87 | + jest.clearAllMocks(); |
| 88 | + }); |
| 89 | + |
| 90 | + it('should return an array of defaultContent if no changes for plugins in slot', () => { |
| 91 | + const plugins = organizePlugins(mockDefaultContent, []); |
| 92 | + expect(plugins.length).toEqual(3); |
| 93 | + expect(plugins).toEqual(mockDefaultContent); |
| 94 | + }); |
| 95 | + |
| 96 | + it('should remove plugins with DirectOperation.Hide', () => { |
| 97 | + const plugins = organizePlugins(mockDefaultContent, mockSlotChanges); |
| 98 | + const widget = plugins.find((w) => w.id === 'home'); |
| 99 | + expect(plugins.length).toEqual(4); |
| 100 | + expect(widget.hidden).toBe(true); |
| 101 | + }); |
| 102 | + |
| 103 | + it('should modify plugins with DirectOperation.Modify', () => { |
| 104 | + const plugins = organizePlugins(mockDefaultContent, mockSlotChanges); |
| 105 | + const widget = plugins.find((w) => w.id === 'lookUp'); |
| 106 | + |
| 107 | + expect(plugins.length).toEqual(4); |
| 108 | + expect(widget.content.url).toEqual('/search'); |
| 109 | + }); |
| 110 | + |
| 111 | + it('should wrap plugins with DirectOperation.Wrap', () => { |
| 112 | + const plugins = organizePlugins(mockDefaultContent, mockSlotChanges); |
| 113 | + const widget = plugins.find((w) => w.id === 'drafts'); |
| 114 | + expect(plugins.length).toEqual(4); |
| 115 | + expect(widget.wrappers.length).toEqual(1); |
| 116 | + }); |
| 117 | + |
| 118 | + it('should accept several wrappers for a single plugin with DirectOperation.Wrap', () => { |
| 119 | + const newMockWrapComponent = ({ widget }) => { |
| 120 | + const isStudent = false; |
| 121 | + return isStudent ? null : widget; |
| 122 | + }; |
| 123 | + const newPluginChange = { |
| 124 | + op: DirectPluginOperations.Wrap, |
| 125 | + widgetId: 'drafts', |
| 126 | + wrapper: newMockWrapComponent, |
| 127 | + }; |
| 128 | + mockSlotChanges.push(newPluginChange); |
| 129 | + const plugins = organizePlugins(mockDefaultContent, mockSlotChanges); |
| 130 | + const widget = plugins.find((w) => w.id === 'drafts'); |
| 131 | + expect(plugins.length).toEqual(4); |
| 132 | + expect(widget.wrappers.length).toEqual(2); |
| 133 | + expect(widget.wrappers[0]).toEqual(mockWrapWidget); |
| 134 | + expect(widget.wrappers[1]).toEqual(newMockWrapComponent); |
| 135 | + }); |
| 136 | + |
| 137 | + it('should return plugins arranged by priority', () => { |
| 138 | + const newPluginChange = { |
| 139 | + op: DirectPluginOperations.Insert, |
| 140 | + widget: { |
| 141 | + id: 'profile', |
| 142 | + priority: 1, |
| 143 | + content: { |
| 144 | + url: '/profile', label: 'Profile', |
| 145 | + }, |
| 146 | + }, |
| 147 | + }; |
| 148 | + mockSlotChanges.push(newPluginChange); |
| 149 | + const plugins = organizePlugins(mockDefaultContent, mockSlotChanges); |
| 150 | + expect(plugins.length).toEqual(5); |
| 151 | + expect(plugins[0].id).toBe('profile'); |
| 152 | + expect(plugins[1].id).toBe('home'); |
| 153 | + expect(plugins[2].id).toBe('lookUp'); |
| 154 | + expect(plugins[3].id).toBe('drafts'); |
| 155 | + expect(plugins[4].id).toBe('login'); |
| 156 | + }); |
| 157 | + |
| 158 | + it('should raise an error for an operation that does not exist', async () => { |
| 159 | + const badPluginChange = { |
| 160 | + op: DirectPluginOperations.Destroy, |
| 161 | + widgetId: 'drafts', |
| 162 | + }; |
| 163 | + mockSlotChanges.push(badPluginChange); |
| 164 | + |
| 165 | + expect.assertions(1); |
| 166 | + try { |
| 167 | + await organizePlugins(mockDefaultContent, mockSlotChanges); |
| 168 | + } catch (error) { |
| 169 | + expect(error.message).toBe('unknown direct plugin change operation'); |
| 170 | + } |
| 171 | + }); |
| 172 | + }); |
| 173 | +}); |
0 commit comments