mattermost-mobile-upstream/app/routes/(authenticated)/code.test.tsx
Eva Sarafianou 9a40cad748
Route through usePropsFromParams for remaining stragglers (#9949)
Four route files were still consuming useLocalSearchParams directly,
bypassing the safeParseJSON decode that every other route already
uses via usePropsFromParams. Combined with propsToParams unconditionally
JSON.stringify'ing every prop (#9931), that means their string params
arrived with literal quote characters embedded (e.g. rootId "abc123"
-> `"abc123"`).

Impact:
- thread.tsx: rootId doesn't match any post in the DB, rootPost stays
  undefined, ThreadContent never renders. The user sees a blank thread
  screen with subtitle `in "channel name"` (visible quote characters).
- code.tsx: code param is rendered verbatim by the highlighter with
  outer quotes and escaped inner quotes/backslashes. The MM-69330
  workaround (bypassing the hook to preserve string type) is obsolete
  now that propsToParams always JSON-encodes: safeParseJSON('"42"')
  returns the string '42', not the number 42.
- about.tsx / settings_display_timezone_select.tsx: header text and
  timezone identifier arrive with extra quotes.

Migrating all four to usePropsFromParams closes the symmetry: every
param is JSON-encoded on send and JSON-decoded on receive.

Also updates code.test.tsx to feed the route JSON-encoded params, so
the MM-69330 regression coverage still exercises the exact scenario
seen in production.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-07-18 19:16:46 +03:00

93 lines
3.4 KiB
TypeScript

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import React from 'react';
import {renderWithIntlAndTheme} from '@test/intl-test-helper';
import CodeRoute from './code';
// react-syntax-highlighter pulls in ESM-only deps (refractor/hastscript) that
// jest doesn't transform, and it's not the code under test here. Stub it with a
// passthrough that renders the highlighted `code` as plain text. The crash this
// test targets happens in our own Highlighter (getMaximumLineLength -> code.split)
// before this component is ever rendered, so stubbing it does not hide the bug.
jest.mock('react-syntax-highlighter', () => {
const {Text: RNText} = require('react-native');
return {
__esModule: true,
default: ({children}: {children: React.ReactNode}) => <RNText>{children}</RNText>,
};
});
jest.mock('react-syntax-highlighter/dist/cjs/styles/hljs', () => ({
githubGist: {hljs: {}},
monokai: {hljs: {}},
solarizedDark: {hljs: {}},
solarizedLight: {hljs: {}},
}));
// ESM-only submodule imported transitively by the syntax_highlight renderer.
jest.mock('react-syntax-highlighter/create-element', () => ({
createStyleObject: jest.fn(() => ({})),
}));
// Mock expo-router with just the exports the route + screen use, so each test
// can feed the route its params via useLocalSearchParams. Values match what
// propsToParams produces in production: JSON-encoded strings.
const mockUseLocalSearchParams = jest.fn();
jest.mock('expo-router', () => ({
useLocalSearchParams: () => mockUseLocalSearchParams(),
useNavigation: () => ({
setOptions: jest.fn(),
addListener: jest.fn(() => jest.fn()),
}),
useRouter: () => ({
canGoBack: jest.fn(() => true),
}),
}));
describe('CodeRoute (MM-69330)', () => {
beforeEach(() => {
jest.clearAllMocks();
});
// Regression: tapping a code block whose text happens to be valid JSON
// (e.g. "42", "{...}", "true") used to crash the Highlighter because
// `safeParseJSON` coerced the string into a number/object/boolean.
// Now that propsToParams always JSON-encodes on the send side, decoding
// '"42"' with safeParseJSON produces the string '42', preserving type.
it.each([
['a bare number', '42'],
['a JSON object', '{"hello": "world"}'],
['a JSON array', '[1, 2, 3]'],
['a boolean literal', 'true'],
])('renders without crashing when the code block content is %s', (_label, code) => {
mockUseLocalSearchParams.mockReturnValue({
code: JSON.stringify(code),
language: JSON.stringify('json'),
textStyle: JSON.stringify({}),
title: JSON.stringify('Code'),
});
const {getByText} = renderWithIntlAndTheme(<CodeRoute/>);
// The raw code text must reach the viewer verbatim, as a string.
expect(getByText(code)).toBeTruthy();
});
it('renders plain (non-JSON) code without crashing', () => {
const code = 'const greeting = "hello";';
mockUseLocalSearchParams.mockReturnValue({
code: JSON.stringify(code),
language: JSON.stringify('javascript'),
textStyle: JSON.stringify({}),
title: JSON.stringify('Code'),
});
const {getByText} = renderWithIntlAndTheme(<CodeRoute/>);
expect(getByText(code)).toBeTruthy();
});
});