-
-
Notifications
You must be signed in to change notification settings - Fork 145
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support snapshots of GlobalStyle
components
#324
Comments
Is this a request of jest-styled-components? Maybe we have to push this in other place! |
I made a workaround until we have support to Basically, I just mocked the lib and made the CSS interpolation myself and returned as string from the component. Maybe this could benefit you @marnixhoh #397 and @masakudamatsu masakudamatsu/nextjs-template#17 It is not ideal, but we can guarantee the theme values. interleave function used link. import { render } from '@testing-library/react'
import { GlobalStyle } from '../GlobalStyle'
// GlobalStyle is the: const GlobalStyle = createGlobalStyle`
jest.mock('styled-components', () => {
const originalModule = jest.requireActual('styled-components')
function interleave(strings, interpolations) {
const result = [strings[0]]
for (let i = 0, len = interpolations.length; i < len; i += 1) {
result.push(interpolations[i], strings[i + 1])
}
return result
}
const injectTheme = (css, { theme, fns }) =>
interleave(
css,
fns.map((fn) => fn({ theme })),
)
const createGlobalStyle = (css, ...fns) => {
return ({ theme }) => injectTheme(css, { theme, fns })
}
return {
__esModule: true,
...originalModule,
createGlobalStyle: jest.fn(createGlobalStyle),
}
})
const theme = {
palette: {
common: {
black: '#000',
white: '#fff',
},
},
}
describe('GlobalStyle', () => {
it('should match snapshot', () => {
const { baseElement } = render(<GlobalStyle theme={theme} />)
expect(baseElement).toMatchSnapshot()
})
}) |
Any updates? It's a big problem for those who use Global Style and basically can't test it. |
The style extraction methods in
src/styleSheetSerializer.js
should extract the styles created by "global styled components" such as created by thecreateGlobalStyle
API. Currently, this does not happen.Adapting an example from the styled-components docs...
...the above code should result in the following snapshot:
This has already been cited and left unresolved in the following threads:
#292
#262
#232
The text was updated successfully, but these errors were encountered: