Refactor compose path detection into a reusable utility function and add related tests (#40663)

This commit is contained in:
Dmytro Oliinyk
2026-09-24 17:41:28 +00:00
committed by GitHub
parent 8cf326a423
commit e5fc481ab6
3 changed files with 44 additions and 1 deletions

View File

@@ -10,6 +10,7 @@ import { tagHistory } from '@/mastodon/settings';
import { emojiMartSearch } from '@/mastodon/features/emoji/picker';
import { showAlert, showAlertForError } from './alerts';
import { isStandaloneComposePath } from './compose_path';
import { emojiUse } from './emojis';
import { importFetchedAccounts, importFetchedStatus } from './importer';
import { openModal } from './modal';
@@ -278,7 +279,7 @@ export function submitCompose(successCallback) {
'Idempotency-Key': getState().getIn(['compose', 'idempotencyKey']),
},
}).then(function (response) {
if ((browserHistory.location.pathname === '/publish' || browserHistory.location.pathname === '/statuses/new') && window.history.state) {
if (isStandaloneComposePath(browserHistory.location.pathname) && window.history.state) {
browserHistory.goBack();
}

View File

@@ -0,0 +1,35 @@
import { isStandaloneComposePath } from './compose_path';
describe('isStandaloneComposePath', () => {
test('returns true for /publish', () => {
expect(isStandaloneComposePath('/publish')).toBe(true);
});
test('returns true for /statuses/new', () => {
expect(isStandaloneComposePath('/statuses/new')).toBe(true);
});
test('returns true for /deck/publish', () => {
expect(isStandaloneComposePath('/deck/publish')).toBe(true);
});
test('returns true for /deck/statuses/new', () => {
expect(isStandaloneComposePath('/deck/statuses/new')).toBe(true);
});
test('returns false for /deck/home', () => {
expect(isStandaloneComposePath('/deck/home')).toBe(false);
});
test('returns false for /home', () => {
expect(isStandaloneComposePath('/home')).toBe(false);
});
test('returns false for /deck', () => {
expect(isStandaloneComposePath('/deck')).toBe(false);
});
test('returns false for /deck/publish/extra', () => {
expect(isStandaloneComposePath('/deck/publish/extra')).toBe(false);
});
});

View File

@@ -0,0 +1,7 @@
export function isStandaloneComposePath(pathname: string): boolean {
const normalized = pathname.startsWith('/deck/')
? pathname.slice('/deck'.length)
: pathname;
return normalized === '/publish' || normalized === '/statuses/new';
}