Comments (1)
Thanks for this! It was very helpful. I figured I would contribute additional points for how to integrate this with testing.
Perhaps since this issue was made, the repo changed, but when this solution is ran as is with the current code, jest will complain that matchMedia
isn't a function and needs to be mocked.
The solution can actually be stolen from this repo lol. All one needs to do is add the following code to your setup file or test file as needed to mock out everything this library needs mocked:
let localStorageMock: { [key: string]: string } = {}
beforeAll(() => {
// Create a mock of the window.matchMedia function
global.matchMedia = jest.fn(query => ({
matches: false,
media: query,
onchange: null,
addListener: jest.fn(),
removeListener: jest.fn(),
addEventListener: jest.fn(),
removeEventListener: jest.fn(),
dispatchEvent: jest.fn()
}))
// Create mocks of localStorage getItem and setItem functions
global.Storage.prototype.getItem = jest.fn(
(key: string) => localStorageMock[key]
)
global.Storage.prototype.setItem = jest.fn((key: string, value: string) => {
localStorageMock[key] = value
})
})
beforeEach(() => {
// Clear the localStorage-mock
localStorageMock = {}
})
If you set resetMocks: true
in your jest config like I do (ref to the docs for resetMocks), then you will need all of this in a before each since the mocks get wiped before each test
let localStorageMock: { [key: string]: string } = {}
beforeEach(() => {
global.matchMedia = jest.fn(query => ({
matches: false,
media: query,
onchange: null,
addListener: jest.fn(),
removeListener: jest.fn(),
addEventListener: jest.fn(),
removeEventListener: jest.fn(),
dispatchEvent: jest.fn()
}))
global.Storage.prototype.getItem = jest.fn(
(key: string) => localStorageMock[key]
)
global.Storage.prototype.setItem = jest.fn((key: string, value: string) => {
localStorageMock[key] = value
})
localStorageMock = {}
})
Feel free to let me know if I missed anything
from next-themes.
Related Issues (20)
- Warning: Extra attributes from the server: data-theme,style HOT 6
- Transitions from light to dark mode don't work HOT 7
- useTheme Hook is causing re-renders HOT 2
- How to set dark by default HOT 1
- can we use more than one ThemeProviders in one app? HOT 3
- inline styles based on theme HOT 2
- Force page to a theme with Next 13 app dir HOT 6
- window.print() changes theme to light mode
- Example of multiple themes NextJS13 Tailwind 3 HOT 1
- Hydration errors/warnings after multiple themes HOT 2
- Is it really Next.js only? HOT 4
- useTheme returns undefined in next js HOT 2
- i cannot add Providers component at layout.tsx for webpack error
- background color initial issue HOT 1
- Unexpected Theme Switch to Light Mode After User Login HOT 4
- Using forcedTheme with app/ HOT 1
- useTheme is undefined in linux environment HOT 1
- could I change default color when refresh page
- Calling SetTheme doesn't immediately update the background-color
- How do i provide theme value to clerkProvider that wraps ThemeProvider? HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from next-themes.