Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions static/app/components/themeAndStyleProvider.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import {OrganizationFixture} from 'sentry-fixture/organization';
import {UserFixture} from 'sentry-fixture/user';

import {act, render} from 'sentry-test/reactTestingLibrary';

import {ConfigStore} from 'sentry/stores/configStore';
import {OrganizationStore} from 'sentry/stores/organizationStore';
import type {User} from 'sentry/types/user';

import {ThemeAndStyleProvider} from './themeAndStyleProvider';

Expand All @@ -14,6 +17,7 @@ function userWithTheme(theme: 'light' | 'dark' | 'system') {
describe('ThemeAndStyleProvider', () => {
afterEach(() => {
delete document.documentElement.dataset.colorScheme;
delete document.documentElement.dataset.themeDark;
});

it('sets data-color-scheme on <html> to the unresolved user setting', () => {
Expand Down Expand Up @@ -43,4 +47,44 @@ describe('ThemeAndStyleProvider', () => {

expect(document.documentElement).toHaveAttribute('data-color-scheme', 'system');
});

describe('data-theme-dark', () => {
beforeEach(() => {
OrganizationStore.reset();
});

function userWithDarkTheme(themeVariantDark: User['options']['themeVariantDark']) {
const user = UserFixture();
return {...user, options: {...user.options, themeVariantDark}};
}

it('sets the saved dark theme with theme-variants', () => {
OrganizationStore.onUpdate(OrganizationFixture({features: ['theme-variants']}));
ConfigStore.set('user', userWithDarkTheme('midnight'));

render(<ThemeAndStyleProvider>content</ThemeAndStyleProvider>);

expect(document.documentElement).toHaveAttribute('data-theme-dark', 'midnight');
});

it('removes the attribute when the dark theme goes back to default', () => {
OrganizationStore.onUpdate(OrganizationFixture({features: ['theme-variants']}));
ConfigStore.set('user', userWithDarkTheme('ash'));

render(<ThemeAndStyleProvider>content</ThemeAndStyleProvider>);
expect(document.documentElement).toHaveAttribute('data-theme-dark', 'ash');

act(() => ConfigStore.set('user', userWithDarkTheme('default')));
expect(document.documentElement).not.toHaveAttribute('data-theme-dark');
});

it('does not set the attribute without theme-variants', () => {
OrganizationStore.onUpdate(OrganizationFixture({features: []}));
ConfigStore.set('user', userWithDarkTheme('ash'));

render(<ThemeAndStyleProvider>content</ThemeAndStyleProvider>);

expect(document.documentElement).not.toHaveAttribute('data-theme-dark');
});
});
});
23 changes: 23 additions & 0 deletions static/app/components/themeAndStyleProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import '@sentry/scraps/theme.css';
import {printConsoleBanner} from 'sentry/bootstrap/printConsoleBanner';
import {NODE_ENV} from 'sentry/constants';
import {ConfigStore} from 'sentry/stores/configStore';
import {OrganizationStore} from 'sentry/stores/organizationStore';
import {GlobalStyles} from 'sentry/styles/global';

const SentryComponentInspector =
Expand Down Expand Up @@ -49,6 +50,28 @@ export function ThemeAndStyleProvider({children}: Props) {
document.documentElement.dataset.colorScheme = colorScheme;
}, [colorScheme]);

const themeVariantDark = useStore(
ConfigStore,
state => state.user?.options.themeVariantDark
);
const hasThemeVariants = useStore(
OrganizationStore,
state => state.organization?.features.includes('theme-variants') ?? false
);
// theme.css only reads `ash` and `midnight`; without the attribute it uses the default dark theme.
const themeDark =
hasThemeVariants && themeVariantDark && themeVariantDark !== 'default'
? themeVariantDark
: undefined;

useLayoutEffect(() => {
if (themeDark) {
document.documentElement.dataset.themeDark = themeDark;
} else {
delete document.documentElement.dataset.themeDark;
}
}, [themeDark]);

const theme = colorMode === 'dark' ? darkTheme : lightTheme;

const didPrintBanner = useRef(false);
Expand Down
2 changes: 2 additions & 0 deletions static/app/types/user.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ export interface User extends Omit<AvatarUser, 'options'> {
stacktraceOrder: StacktraceOrder;
theme: 'system' | 'light' | 'dark';
timezone: string;
themeVariantDark?: 'default' | 'ash' | 'midnight';
themeVariantLight?: 'default';
};
permissions: Set<string>;
authenticators?: UserEnrolledAuthenticator[];
Expand Down
10 changes: 9 additions & 1 deletion static/app/views/settings/account/accountAppearance.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ describe('AccountAppearance', () => {

expect(screen.getByText('Page Not Found')).toBeInTheDocument();
expect(screen.queryByLabelText('Theme')).not.toBeInTheDocument();
expect(screen.queryByRole('radiogroup')).not.toBeInTheDocument();
});

it('keeps the theme field in Account Details', async () => {
Expand All @@ -67,6 +68,13 @@ describe('AccountAppearance', () => {
);
});

it('shows the Light and Dark theme pickers', () => {
render(<AccountAppearance />, {organization});

expect(screen.getByRole('radiogroup', {name: 'Light'})).toBeInTheDocument();
expect(screen.getByRole('radiogroup', {name: 'Dark'})).toBeInTheDocument();
});

it('saves the theme option', async () => {
const mockUserUpdate = MockApiClient.addMockResponse({
url: '/users/me/',
Expand All @@ -76,7 +84,7 @@ describe('AccountAppearance', () => {
render(<AccountAppearance />, {organization});

expect(screen.getByRole('heading', {name: 'Appearance'})).toBeInTheDocument();
await userEvent.click(screen.getByText('Light'));
await userEvent.click(screen.getByLabelText('Theme'));
await userEvent.click(await screen.findByRole('menuitemradio', {name: 'Dark'}));

await waitFor(() => {
Expand Down
4 changes: 4 additions & 0 deletions static/app/views/settings/account/accountAppearance.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {useUser} from 'sentry/utils/useUser';
import {SettingsPageHeader} from 'sentry/views/settings/components/settingsPageHeader';

import {THEME_OPTIONS, themeSchema} from './accountDetails';
import {ThemeVariantPicker} from './themeVariantPicker';

const appearanceSchema = z.object({theme: themeSchema});

Expand Down Expand Up @@ -67,6 +68,9 @@ function AccountAppearance() {
)}
</AutoSaveForm>
</FieldGroup>
<FieldGroup title={t('Themes')}>
<ThemeVariantPicker />
</FieldGroup>
</Fragment>
);
}
Expand Down
136 changes: 136 additions & 0 deletions static/app/views/settings/account/themeVariantPicker.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
import {UserFixture} from 'sentry-fixture/user';

import {
render,
screen,
userEvent,
waitFor,
within,
} from 'sentry-test/reactTestingLibrary';

import {ConfigStore} from 'sentry/stores/configStore';
import type {User} from 'sentry/types/user';

import {ThemeVariantPicker} from './themeVariantPicker';

function userWithVariants(options: Partial<User['options']> = {}) {
const user = UserFixture();
return {...user, options: {...user.options, ...options}};
}

function getPreview(radio: HTMLElement) {
// The preview sits inside the radio's label, next to the native input.
return within(radio.parentElement!).getByTestId('theme-preview');
}

describe('ThemeVariantPicker', () => {
let mockUserUpdate: jest.Mock;

beforeEach(() => {
MockApiClient.clearMockResponses();
ConfigStore.set('user', userWithVariants());
mockUserUpdate = MockApiClient.addMockResponse({
url: '/users/me/',
method: 'PUT',
body: userWithVariants({themeVariantDark: 'ash'}),
});
});

it('renders a Light and a Dark radio group', () => {
render(<ThemeVariantPicker />);

const light = screen.getByRole('radiogroup', {name: 'Light'});
const dark = screen.getByRole('radiogroup', {name: 'Dark'});

expect(
within(light)
.getAllByRole('radio')
.map(radio => radio.getAttribute('value'))
).toEqual(['default']);
expect(
within(dark)
.getAllByRole('radio')
.map(radio => radio.getAttribute('value'))
).toEqual(['default', 'ash', 'midnight']);
expect(within(light).getByRole('radio', {name: 'Default'})).toBeChecked();
expect(within(dark).getByRole('radio', {name: 'Default'})).toBeChecked();
});

it('selects one dark theme at a time without changing the light theme', async () => {
render(<ThemeVariantPicker />);

const light = screen.getByRole('radiogroup', {name: 'Light'});
const dark = screen.getByRole('radiogroup', {name: 'Dark'});

await userEvent.click(within(dark).getByText('Ash'));
expect(within(dark).getByRole('radio', {name: 'Ash'})).toBeChecked();
expect(within(dark).getByRole('radio', {name: 'Default'})).not.toBeChecked();
expect(within(light).getByRole('radio', {name: 'Default'})).toBeChecked();

await userEvent.keyboard('{ArrowRight}');
expect(within(dark).getByRole('radio', {name: 'Midnight'})).toBeChecked();
expect(within(dark).getByRole('radio', {name: 'Midnight'})).toHaveFocus();
expect(within(dark).getByRole('radio', {name: 'Ash'})).not.toBeChecked();
});

it('scopes each preview to its color scheme and theme', () => {
render(<ThemeVariantPicker />);

const light = screen.getByRole('radiogroup', {name: 'Light'});
const lightPreview = getPreview(within(light).getByRole('radio', {name: 'Default'}));
expect(lightPreview).toHaveAttribute('data-color-scheme', 'light');
expect(lightPreview).not.toHaveAttribute('data-theme-dark');

const dark = screen.getByRole('radiogroup', {name: 'Dark'});
for (const [name, value] of [
['Default', 'default'],
['Ash', 'ash'],
['Midnight', 'midnight'],
]) {
const preview = getPreview(within(dark).getByRole('radio', {name}));
expect(preview).toHaveAttribute('data-color-scheme', 'dark');
expect(preview).toHaveAttribute('data-theme-dark', value);
expect(preview).toHaveAttribute('aria-hidden', 'true');
}
});

it('shows the saved theme for each scheme', () => {
ConfigStore.set('user', userWithVariants({themeVariantDark: 'midnight'}));

render(<ThemeVariantPicker />);

const dark = screen.getByRole('radiogroup', {name: 'Dark'});
expect(within(dark).getByRole('radio', {name: 'Midnight'})).toBeChecked();
const light = screen.getByRole('radiogroup', {name: 'Light'});
expect(within(light).getByRole('radio', {name: 'Default'})).toBeChecked();
});

it('saves the dark theme as a user option', async () => {
render(<ThemeVariantPicker />);

const dark = screen.getByRole('radiogroup', {name: 'Dark'});
await userEvent.click(within(dark).getByText('Ash'));

await waitFor(() =>
expect(ConfigStore.get('user').options.themeVariantDark).toBe('ash')
);
expect(mockUserUpdate).toHaveBeenCalledWith(
'/users/me/',
expect.objectContaining({data: {options: {themeVariantDark: 'ash'}}})
);
});

it('goes back to the saved theme when saving fails', async () => {
MockApiClient.addMockResponse({url: '/users/me/', method: 'PUT', statusCode: 500});

render(<ThemeVariantPicker />);

const dark = screen.getByRole('radiogroup', {name: 'Dark'});
await userEvent.click(within(dark).getByText('Ash'));

await waitFor(() =>
expect(within(dark).getByRole('radio', {name: 'Default'})).toBeChecked()
);
expect(within(dark).getByRole('radio', {name: 'Ash'})).not.toBeChecked();
});
});
Loading
Loading