bito-code-review[bot] commented on code in PR #42602:
URL: https://github.com/apache/superset/pull/42602#discussion_r3708313952
##########
superset-frontend/packages/superset-core/src/theme/Theme.test.tsx:
##########
@@ -206,6 +206,47 @@ test('Theme.toggleDarkMode preserves other algorithms when
toggling dark mode',
expect(serialized.algorithm).not.toContain(ThemeAlgorithm.DARK);
});
+test('Theme.toggleDarkMode is a no-op when the requested mode is already
active', () => {
+ // Pages with many live component demos (see docs/src/components/
+ // StorybookWrapper.jsx's ThemeSync) mount one dark-mode-sync bridge per
+ // demo, so a single toggle event can call toggleDarkMode once per demo
+ // with the same isDark value. Only the first of those calls should
+ // actually recompute the theme and fan out to providers.
+ const theme = Theme.fromConfig();
+ const setConfigSpy = jest.spyOn(theme, 'setConfig');
+
+ theme.toggleDarkMode(true);
+ expect(setConfigSpy).toHaveBeenCalledTimes(1);
+
+ // Repeating the same toggle should not recompute the theme again.
+ theme.toggleDarkMode(true);
+ theme.toggleDarkMode(true);
+ expect(setConfigSpy).toHaveBeenCalledTimes(1);
+
+ // Toggling to the other mode should still go through.
+ theme.toggleDarkMode(false);
+ expect(setConfigSpy).toHaveBeenCalledTimes(2);
+
+ setConfigSpy.mockRestore();
+});
+
+test('Theme.toggleDarkMode no-op check accounts for other algorithms in the
array', () => {
+ // Start already in dark mode alongside a non-mode algorithm (compact).
+ const theme = Theme.fromConfig({
+ algorithm: [
+ antdThemeImport.compactAlgorithm,
+ antdThemeImport.darkAlgorithm,
+ ],
+ });
+ const setConfigSpy = jest.spyOn(theme, 'setConfig');
+
+ // Already dark, so this should be a no-op rather than reordering the array.
+ theme.toggleDarkMode(true);
Review Comment:
<div>
<div id="suggestion">
<div id="issue"><b>Test assertion contradicts implementation
behavior</b></div>
<div id="fix">
The test at line 236-240 configures `algorithm: [compactAlgorithm,
darkAlgorithm]`, but `toggleDarkMode(true)` reorders this to `[darkAlgorithm,
compactAlgorithm]`. The identity-based array comparison at Theme.tsx lines
197-201 is order-sensitive, so these arrays are not equal, causing `setConfig`
to be called. The assertion at line 245 (`not.toHaveBeenCalled()`) expects a
no-op, but the implementation will invoke `setConfig` because the algorithm
arrays differ.
</div>
</div>
<small><i>Code Review Run #24736e</i></small>
</div>
---
Should Bito avoid suggestions like this for future reviews? (<a
href=https://alpha.bito.ai/home/ai-agents/review-rules>Manage Rules</a>)
- [ ] Yes, avoid them
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]