bito-code-review[bot] commented on code in PR #42602:
URL: https://github.com/apache/superset/pull/42602#discussion_r3709798835
##########
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:
<!-- Bito Reply -->
The suggestion is valid. The implementation correctly addresses the issue by
ensuring that the no-op check for `toggleDarkMode` is order-insensitive,
preventing unnecessary recomputations when the algorithm array contains the
same elements in a different order.
**superset-frontend/packages/superset-core/src/theme/Theme.test.tsx**
```
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);
```
--
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]