-
Notifications
You must be signed in to change notification settings - Fork 19
fix: move getServerRoute to selectors module to avoid TDZ on plugin init #84
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
avasconcelos114
merged 2 commits into
mattermost-community:master
from
roberson-io:fix/initialize-tdz-error
Jul 16, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| /** | ||
| * @jest-environment jsdom | ||
| */ | ||
|
|
||
| /* eslint-disable max-nested-callbacks */ | ||
|
|
||
| jest.mock('@emotion/react', () => ({ | ||
| jsx: (type, props) => ({type, props}), | ||
| })); | ||
|
|
||
| jest.mock('./components/icon.jsx', () => ({__esModule: true, default: () => null})); | ||
| jest.mock('./components/post_type_webex', () => ({__esModule: true, default: () => null})); | ||
|
|
||
| jest.mock('./actions', () => ({ | ||
| startMeeting: jest.fn(() => jest.fn()), | ||
| })); | ||
|
|
||
| jest.mock('./client', () => ({ | ||
| __esModule: true, | ||
| default: { | ||
| setServerRoute: jest.fn(), | ||
| }, | ||
| })); | ||
|
|
||
| jest.mock('mattermost-redux/selectors/entities/general', () => ({ | ||
| getConfig: jest.fn(() => ({SiteURL: 'https://example.com'})), | ||
| })); | ||
|
|
||
| jest.mock('./selectors', () => ({ | ||
| getServerRoute: jest.fn(() => ''), | ||
| })); | ||
|
|
||
| describe('Plugin initialization', () => { | ||
| let mockRegistry; | ||
| let mockStore; | ||
|
|
||
| beforeEach(() => { | ||
| jest.clearAllMocks(); | ||
|
|
||
| mockRegistry = { | ||
| registerChannelHeaderButtonAction: jest.fn(), | ||
| registerAppBarComponent: jest.fn(), | ||
| registerPostTypeComponent: jest.fn(), | ||
| }; | ||
| mockStore = { | ||
| getState: jest.fn(() => ({})), | ||
| dispatch: jest.fn(), | ||
| }; | ||
|
|
||
| // Mirror modern Mattermost: window.registerPlugin invokes initialize() | ||
| // synchronously during registration. If any module-scoped identifier | ||
| // used inside initialize() is still in the temporal dead zone at this | ||
| // point (e.g. a const arrow declared after registerPlugin), requiring | ||
| // ./index will throw a ReferenceError. | ||
| window.registerPlugin = jest.fn((_pluginId, plugin) => { | ||
| plugin.initialize(mockRegistry, mockStore); | ||
| }); | ||
| }); | ||
|
|
||
| test('module evaluation with synchronous initialize() does not throw', () => { | ||
| expect(() => { | ||
| jest.isolateModules(() => { | ||
| require('./index'); // eslint-disable-line global-require | ||
| }); | ||
| }).not.toThrow(); | ||
| }); | ||
|
|
||
| test('registers channel header button, app bar icon, and post type component', () => { | ||
| jest.isolateModules(() => { | ||
| require('./index'); // eslint-disable-line global-require | ||
| }); | ||
|
|
||
| expect(mockRegistry.registerChannelHeaderButtonAction).toHaveBeenCalledTimes(1); | ||
| expect(mockRegistry.registerAppBarComponent).toHaveBeenCalledTimes(1); | ||
| expect(mockRegistry.registerPostTypeComponent).toHaveBeenCalledWith( | ||
| 'custom_webex', | ||
| expect.anything(), | ||
| ); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| import {getConfig} from 'mattermost-redux/selectors/entities/general'; | ||
|
|
||
| export const getServerRoute = (state) => { | ||
| const config = getConfig(state); | ||
|
|
||
| let basePath = ''; | ||
| if (config && config.SiteURL) { | ||
| try { | ||
| basePath = new URL(config.SiteURL).pathname; | ||
|
|
||
| if (basePath && basePath[basePath.length - 1] === '/') { | ||
| basePath = basePath.substr(0, basePath.length - 1); | ||
| } | ||
| } catch (e) { | ||
| basePath = ''; | ||
| } | ||
| } | ||
|
|
||
| return basePath; | ||
| }; | ||
|
Comment on lines
+3
to
+20
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It might be nice to have a test suite for this selector specifically as well
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @avasconcelos114 I added selector tests. |
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| import {getConfig} from 'mattermost-redux/selectors/entities/general'; | ||
|
|
||
| import {getServerRoute} from './selectors'; | ||
|
|
||
| jest.mock('mattermost-redux/selectors/entities/general', () => ({ | ||
| getConfig: jest.fn(), | ||
| })); | ||
|
|
||
| describe('getServerRoute', () => { | ||
| beforeEach(() => { | ||
| jest.clearAllMocks(); | ||
| }); | ||
|
|
||
| test('returns empty string when config is missing', () => { | ||
| getConfig.mockReturnValue(null); | ||
| expect(getServerRoute({})).toBe(''); | ||
| }); | ||
|
|
||
| test('returns empty string when SiteURL is missing', () => { | ||
| getConfig.mockReturnValue({}); | ||
| expect(getServerRoute({})).toBe(''); | ||
| }); | ||
|
|
||
| test('returns empty string when SiteURL has no subpath', () => { | ||
| getConfig.mockReturnValue({SiteURL: 'https://example.com'}); | ||
| expect(getServerRoute({})).toBe(''); | ||
| }); | ||
|
|
||
| test('returns pathname when SiteURL has a subpath', () => { | ||
| getConfig.mockReturnValue({SiteURL: 'https://example.com/mattermost'}); | ||
| expect(getServerRoute({})).toBe('/mattermost'); | ||
| }); | ||
|
|
||
| test('strips trailing slash from subpath', () => { | ||
| getConfig.mockReturnValue({SiteURL: 'https://example.com/mattermost/'}); | ||
| expect(getServerRoute({})).toBe('/mattermost'); | ||
| }); | ||
|
|
||
| test('returns empty string when SiteURL is a bare root URL with trailing slash', () => { | ||
| getConfig.mockReturnValue({SiteURL: 'https://example.com/'}); | ||
| expect(getServerRoute({})).toBe(''); | ||
| }); | ||
|
|
||
| test('returns empty string when SiteURL is malformed', () => { | ||
| getConfig.mockReturnValue({SiteURL: 'not a valid url'}); | ||
| expect(getServerRoute({})).toBe(''); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It might be nice to wrap this in a try-catch block to prevent any possible runtime errors due to a malformed SiteURL
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@avasconcelos114 I wrapped this in a try/catch block.