|
| 1 | +import * as React from 'react'; |
| 2 | +import { render, screen } from '@testing-library/react'; |
| 3 | +import { Typography, ThemeOptions } from '@mui/material'; |
| 4 | +import expect from 'expect'; |
| 5 | +import { memoryStore } from 'ra-core'; |
| 6 | + |
| 7 | +import { AdminContext } from './AdminContext'; |
| 8 | +import { ThemeTestWrapper } from './layout/ThemeTestWrapper'; |
| 9 | + |
| 10 | +const lightTheme: ThemeOptions = {}; |
| 11 | +const darkTheme: ThemeOptions = { palette: { mode: 'dark' } }; |
| 12 | + |
| 13 | +const LIGHT_MODE_TEXT_COLOR = 'rgb(25, 118, 210)'; // text is dark blue in light mode |
| 14 | +const DARK_MODE_TEXT_COLOR = 'rgb(144, 202, 249)'; // text is light blue in dark mode |
| 15 | + |
| 16 | +describe('AdminContext', () => { |
| 17 | + it('should default to light theme', () => { |
| 18 | + render( |
| 19 | + <AdminContext theme={lightTheme} darkTheme={darkTheme}> |
| 20 | + <Typography color="primary">Test</Typography> |
| 21 | + </AdminContext> |
| 22 | + ); |
| 23 | + const text = screen.getByText('Test'); |
| 24 | + expect(getComputedStyle(text).color).toBe(LIGHT_MODE_TEXT_COLOR); |
| 25 | + }); |
| 26 | + it('should default to dark theme when the browser detects a dark mode preference', () => { |
| 27 | + render( |
| 28 | + <ThemeTestWrapper mode="dark"> |
| 29 | + <AdminContext theme={lightTheme} darkTheme={darkTheme}> |
| 30 | + <Typography color="primary">Test</Typography> |
| 31 | + </AdminContext> |
| 32 | + </ThemeTestWrapper> |
| 33 | + ); |
| 34 | + const text = screen.getByText('Test'); |
| 35 | + expect(getComputedStyle(text).color).toBe(DARK_MODE_TEXT_COLOR); |
| 36 | + }); |
| 37 | + it('should default to light theme when the browser detects a dark mode preference', () => { |
| 38 | + render( |
| 39 | + <ThemeTestWrapper mode="light"> |
| 40 | + <AdminContext theme={lightTheme} darkTheme={darkTheme}> |
| 41 | + <Typography color="primary">Test</Typography> |
| 42 | + </AdminContext> |
| 43 | + </ThemeTestWrapper> |
| 44 | + ); |
| 45 | + const text = screen.getByText('Test'); |
| 46 | + expect(getComputedStyle(text).color).toBe(LIGHT_MODE_TEXT_COLOR); |
| 47 | + }); |
| 48 | + it('should default to dark theme when user preference is dark', () => { |
| 49 | + render( |
| 50 | + <AdminContext |
| 51 | + theme={lightTheme} |
| 52 | + darkTheme={darkTheme} |
| 53 | + store={memoryStore({ theme: 'dark' })} |
| 54 | + > |
| 55 | + <Typography color="primary">Test</Typography> |
| 56 | + </AdminContext> |
| 57 | + ); |
| 58 | + const text = screen.getByText('Test'); |
| 59 | + expect(getComputedStyle(text).color).toBe(DARK_MODE_TEXT_COLOR); |
| 60 | + }); |
| 61 | + it('should default to light theme when user preference is light', () => { |
| 62 | + render( |
| 63 | + <ThemeTestWrapper mode="dark"> |
| 64 | + <AdminContext |
| 65 | + theme={lightTheme} |
| 66 | + darkTheme={darkTheme} |
| 67 | + store={memoryStore({ theme: 'light' })} |
| 68 | + > |
| 69 | + <Typography color="primary">Test</Typography> |
| 70 | + </AdminContext> |
| 71 | + </ThemeTestWrapper> |
| 72 | + ); |
| 73 | + const text = screen.getByText('Test'); |
| 74 | + expect(getComputedStyle(text).color).toBe(LIGHT_MODE_TEXT_COLOR); |
| 75 | + }); |
| 76 | +}); |
0 commit comments