Skip to content
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

Open
lunelson opened this issue May 26, 2020 · 3 comments
Open

Support snapshots of GlobalStyle components #324

lunelson opened this issue May 26, 2020 · 3 comments

Comments

@lunelson
Copy link

The style extraction methods in src/styleSheetSerializer.js should extract the styles created by "global styled components" such as created by the createGlobalStyle API. Currently, this does not happen.

Adapting an example from the styled-components docs...

import React from 'react'
import styled, { createGlobalStyle } from 'styled-components'
import renderer from 'react-test-renderer'
import 'jest-styled-components'

const GlobalStyle = createGlobalStyle`
  body {
    color: ${props => (props.whiteColor ? 'white' : 'black')};
  }
`

test('it works', () => {
  const tree = renderer.create(<GlobalStyle whiteColor />).toJSON()
  expect(tree).toMatchSnapshot()
})

...the above code should result in the following snapshot:

exports[`it works 1`] = `
  body {
    color: white;
  }
`;

This has already been cited and left unresolved in the following threads:

#292
#262
#232

@sauldeleon
Copy link

Is this a request of jest-styled-components? Maybe we have to push this in other place!

@hugonasciutti
Copy link

I made a workaround until we have support to toMatchSnapshot or toHaveStyle.

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()
  })
})

@Katreque
Copy link

Any updates? It's a big problem for those who use Global Style and basically can't test it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants